I could handle it better, but by catching the exception I get a list of all of the reservations that are 'in the way'
In the view code: try: new_reservation = server.add_reservation(trainer=trainer,start=start,end=end) except AlreadyReserved, e: error = 'Please choose another server, %s' % e Then I use the normail error display to tell the user its already reserved. I have to run, but I can post a full example if you need it Thanks, -Aaron JirkaJ wrote: > Hi Aaron, > i have one question-how do u handle AlreadyReserved exception? I woould > like to make some checks before saving some things in admin interface > but i dont know how may i tell the user that there is some error... > > > Aaron wrote: > > I just wanted to drop a note about a new app that I deployed. > > > > I recently launched a prototype and users are happy. > > > > Basically it uses the gamespy API to query gamespy for a list of active > > game servers, then contacts each server in turn to obtain critical > > stats (map being player, number of players, scores....) > > > > Players can come to the website and reserve a particular server for a > > number of hours. > > > > The players then get a unique URL that they can pass around. Going to > > the URL will start the game and connect you to the reserved server. > > > > All that happens in 121 python statements outside of django (according > > to pylint). > > > > All of the heavy lifting (gamespy, activeX, apache/http) was done in C, > > but all of the business logic was handled in Python. > > > > After this little project I'm starting to get the model API. > > > > I thought this bit was tricky, so I'll share: > > > > English: > > If you try to make a reservation but that server is already reserved > > for any part of the time period then that reservation should fail, > > unless you are the person that made the original reservation. > > > > Django: > > class Reservation(meta.Model): > > .... > > def _pre_save(self): > > '''A server is already reserved if there is another reservation > > with a start of end date in the > > same range as the new reservation''' > > from django.models.regservers import AlreadyReserved, > > reservations, servers > > from django.core.meta import Q > > server = servers.get_object(id__exact=self.server_id) > > blockers = server.get_reservation_list(complex=( > > Q(start__range=(self.start,self.end)) | > > > > Q(end__range=(self.start,self.end)) ) ) > > # custom logic to determine if I need to stop the reservation > > blockers = [b for b in blockers if b.user <> self.user] > > if len(blockers)>0: > > raise AlreadyReserved(blockers) > > > > > > It needs a little finishing, like transactional support and error > > handling - but so far the system works really well and is agile in > > every sense of the word. > > > > Thanks again, > > -Aaron Held --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Django users" group. To post to this group, send email to django-users@googlegroups.com To unsubscribe from this group, send email to [EMAIL PROTECTED] For more options, visit this group at http://groups.google.com/group/django-users -~----------~----~----~----~------~----~------~--~---