Package bosco :: Module runner
[hide private]
[frames] | no frames]

Source Code for Module bosco.runner

  1  #/usr/bin/env python 
  2  #    runner.py - Runners, Teams, SI-Cards, ... 
  3  # 
  4  #    Copyright (C) 2008  Gaudenz Steinlin <gaudenz@soziologie.ch> 
  5  # 
  6  #    This program is free software: you can redistribute it and/or modify 
  7  #    it under the terms of the GNU General Public License as published by 
  8  #    the Free Software Foundation, either version 3 of the License, or 
  9  #    (at your option) any later version. 
 10  # 
 11  #    This program is distributed in the hope that it will be useful, 
 12  #    but WITHOUT ANY WARRANTY; without even the implied warranty of 
 13  #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 14  #    GNU General Public License for more details. 
 15  # 
 16  #    You should have received a copy of the GNU General Public License 
 17  #    along with this program.  If not, see <http://www.gnu.org/licenses/>. 
 18   
 19  from storm.locals import * 
 20  from copy import copy 
 21   
 22  from base import MyStorm 
 23  from ranking import Rankable, RankableItem 
 24   
25 -class AbstractRunner(object, RankableItem):
26 """Base class for all runner like classes (runners, teams). This 27 class defines the interface for all objects that work with any kind 28 of runners or teams.""" 29 30 name = None 31 number = None 32 official = None 33 sicards = None
34
35 -class Runner(AbstractRunner, Storm):
36 __storm_table__ = 'runner' 37 38 id = Int(primary=True) 39 number = Unicode() 40 given_name = Unicode() 41 surname = Unicode() 42 dateofbirth = Date() 43 sex = Enum(map = {'male':u'male', 'female':u'female'}) 44 _nation_id = Int(name='nation') 45 nation = Reference(_nation_id, 'Country.id') 46 solvnr = Unicode() 47 startblock = Int() 48 starttime = Date() 49 _category_id = Int(name='category') 50 category = Reference(_category_id, 'Category.id') 51 _club_id = Int(name='club') 52 club = Reference(_club_id, 'Club.id') 53 address1 = Unicode() 54 address2 = Unicode() 55 zipcode = Unicode() 56 city = Unicode() 57 _address_country_id = Int(name='address_country') 58 address_country = Reference(_address_country_id, 'Country.id') 59 email = Unicode() 60 startfee = Int() 61 paid = Bool() 62 preferred_category = Unicode() 63 doping_declaration = Bool() 64 comment = Unicode() 65 _team_id = Int(name='team') 66 team = Reference(_team_id, 'Team.id') 67 sicards = ReferenceSet(id, 'SICard._runner_id') 68
69 - def __init__(self, surname=u'', given_name=u'', sicard = None, category = None, number = None, 70 dateofbirth=None, sex=None, nation=None, solvnr=None, startblock=None, 71 starttime=None, club=None, address1=None, address2=None, zipcode=None, 72 city=None, address_country=None, email=None, startfee=None, paid=None, 73 preferred_category=None, doping_declaration=None, comment=None, 74 ):
75 self.surname = surname 76 self.given_name = given_name 77 if sicard is not None: 78 self.sicards.add(sicard) 79 self.category = category 80 self.number = number 81 self.dateofbirth = dateofbirth 82 self.sex = sex 83 self.nation = nation 84 self.solvnr = solvnr 85 self.startblock = startblock 86 self.starttime = starttime 87 self.club = club 88 self.address1 = address1 89 self.address2 = address2 90 self.zipcode = zipcode 91 self.city = city 92 self.address_country = address_country 93 self.email = email 94 self.startfee = startfee 95 self.paid = paid 96 self.preferred_category = preferred_category 97 self.doping_declaration = doping_declaration 98 self.comment = comment
99
100 - def __str__(self):
101 return unicode(self).encode('utf-8')
102
103 - def __unicode__(self):
104 return (u'%s %s' % (self.given_name, self.surname))
105
106 - def _get_run(self):
107 runs = [] 108 for si in self.sicards: 109 for r in si.runs: 110 runs.append(r) 111 112 if len(runs) == 1: 113 return runs[0] 114 elif len(runs) > 1: 115 # search for complete runs 116 complete_runs = [ r for r in runs if r.complete == True ] 117 if len(complete_runs) == 1: 118 # if there is only one complete run, return this run 119 return complete_runs[0] 120 else: 121 raise RunnerException(u'%s runs for runner %s (%s)' % (len(runs), self, self.number)) 122 else: 123 raise RunnerException(u'No run found for runner %s (%s)' % (self, self.number))
124 run = property(_get_run) 125
126 -class Team(AbstractRunner, Storm):
127 __storm_table__ = 'team' 128 129 id = Int(primary=True) 130 number = Unicode() 131 name = Unicode() 132 official = Bool() 133 override = Int() 134 _responsible_id = Int(name='responsible') 135 responsible = Reference(_responsible_id, 'Runner.id') 136 _category_id = Int(name='category') 137 category = Reference(_category_id, 'Category.id') 138 members = ReferenceSet(id, 'Runner._team_id') 139
140 - def __init__(self, number, name, category, responsible = None, official = True):
141 self.number = number 142 self.name = name 143 self.category = category 144 self.official = official 145 self.responsible = responsible
146
147 - def __str__(self):
148 return unicode(self).encode('utf-8')
149
150 - def __unicode__(self):
151 return self.name
152
153 - def _get_runs(self):
154 runs = [] 155 # import this here to avoid a circular import 156 from run import Run 157 for m in self.members: 158 for si in m.sicards: 159 runs.extend(list(si.runs)) 160 return runs
161 runs = property(_get_runs)
162 163
164 -def sicard_runner_validator(sicard, attribute, value):
165 """This validator avoids that SI-cards are reassigned from one 166 runner to another. If you really want to reassign an SI-card, first 167 remove it from the first runner and then assign it to the other.""" 168 169 if sicard._runner_id is None or sicard._runner_id == value or value is None: 170 return value 171 172 raise RunnerException("SI-Card %s is already assigned to runner %s %s (%s)" % 173 (sicard.id, sicard.runner.given_name, sicard.runner.surname, 174 sicard.runner.number) 175 )
176
177 -class SICard(Storm):
178 __storm_table__ = 'sicard' 179 180 id = Int(primary=True) 181 _runner_id = Int(name='runner', 182 validator = sicard_runner_validator) 183 runner = Reference(_runner_id, 'Runner.id') 184 runs = ReferenceSet(id, 'Run._sicard_id') 185
186 - def __init__(self, nr):
187 self.id = nr
188
189 -class Category(Storm, Rankable):
190 __storm_table__ = 'category' 191 192 id = Int(primary=True) 193 name = Unicode() 194 runners = ReferenceSet(id, 'Runner._category_id') 195 teams = ReferenceSet(id, 'Team._category_id') 196
197 - def __init__(self, name):
198 self.name = name
199
200 - def __str__(self):
201 return unicode(self).encode('utf-8')
202
203 - def __unicode__(self):
204 return self.name
205
206 - def _get_members(self):
207 l = list(self.runners) 208 l.extend(list(self.teams)) 209 return l
210 members = property(_get_members)
211
212 -class Country(Storm):
213 __storm_table__ = 'country' 214 215 id = Int(primary=True) 216 name = Unicode() 217 code2 = Unicode() 218 code3 = Unicode() 219 runners = ReferenceSet(id, 'Runner._nation_id') 220
221 - def __init__(self, code3, code2, name=None):
222 self.name = name 223 self.code3 = code3 224 self.code2 = code2
225
226 - def __str__(self):
227 return unicode(self).encode('utf-8')
228
229 - def __unicode__(self):
230 return self.code3
231 232
233 -class Club(Storm):
234 __storm_table__ = 'club' 235 236 id = Int(primary=True) 237 name = Unicode() 238 runners = ReferenceSet(id, 'Runner._club_id') 239
240 - def __init__(self, name):
241 self.name = name
242
243 - def __str__(self):
244 return unicode(self).encode('utf-8')
245
246 - def __unicode__(self):
247 return self.name
248 249
250 -class RunnerException(Exception):
251 pass
252