From eb6c8f6a65bf6ff765241b689576e0b5ded96970 Mon Sep 17 00:00:00 2001 From: gfawcett Date: Sun, 5 Apr 2009 22:27:58 +0000 Subject: [PATCH] better unique-physical-object detection; friendlier error message on collision. git-svn-id: svn://svn.open-ils.org/ILS-Contrib/servres/trunk@282 6d9bc8c9-1ec2-4278-b937-99fde70a366f --- conifer/TODO | 6 +----- conifer/syrup/models.py | 21 +++++++++++---------- conifer/syrup/views.py | 13 ++++++++----- 3 files changed, 20 insertions(+), 20 deletions(-) diff --git a/conifer/TODO b/conifer/TODO index e00212e..1af056f 100644 --- a/conifer/TODO +++ b/conifer/TODO @@ -3,16 +3,12 @@ CRITICAL: IMPORTANT: * finish the physical-item received workflow. - * how to model 'received' in the database? + * nicer error-message if smallint already taken. * if someone has item checked out, show due date/time on item-about page. * does 'move to new heading' show up in the right places? Should be like 'edit'. -* a short-number for physical items. Sort of a barcode, but intended - for easier communicatinon between patrons and staff. - (Update views.search() when this is in place) - * Import of reserves data from Leddy voyager. Laurentian, others? * People should be able to register themselves into open courses. diff --git a/conifer/syrup/models.py b/conifer/syrup/models.py index a5ba061..70353b7 100644 --- a/conifer/syrup/models.py +++ b/conifer/syrup/models.py @@ -596,19 +596,20 @@ class PhysicalObject(m.Model): # an optional small-integer used as a human-shareable barcode by some institutions. smallint = m.IntegerField(blank=True, null=True) + def __unicode__(self): + return '%s (%s) %s' % (self.barcode, self.smallint, self.departed and 'gone' or 'live') def save(self, force_insert=False, force_update=False): # Must ensure that barcode is unique for non-departed items. Same with smallint - try: - unique_thing = 'barcode' - already = PhysicalObject.objects.exclude(pk=self.id).get(departed=None) - unique_thing = 'smallint' - if self.smallint: - already = PhysicalObject.objects.exclude(pk=self.id).get(smallint=self.smallint) - except PhysicalObject.DoesNotExist: - super(PhysicalObject, self).save(force_insert, force_update) - else: - raise AssertionError, '%s is not unique in active PhysicalObject collection.' % unique_thing + live_objs = PhysicalObject.objects.exclude(pk=self.id).filter(departed=None) + same_barcode = live_objs.filter(barcode=self.barcode) + assert not same_barcode, \ + 'Barcode is not unique in active PhysicalObject collection.' + if self.smallint: + same_smallint = live_objs.filter(smallint=self.smallint) + assert not same_smallint, \ + 'Small Number is not unique in active PhysicalObject collection.' + super(PhysicalObject, self).save(force_insert, force_update) @classmethod def by_smallint(cls, smallint): diff --git a/conifer/syrup/views.py b/conifer/syrup/views.py index a5d80b1..bd3eabc 100644 --- a/conifer/syrup/views.py +++ b/conifer/syrup/views.py @@ -1359,16 +1359,19 @@ def phys_mark_arrived_match(request): barcode = request.POST.get('barcode', '').strip() assert barcode smallint = request.POST.get('smallint', '').strip() or None - phys = models.PhysicalObject(barcode=barcode, - receiver = request.user, - smallint = smallint) - phys.save() + try: + phys = models.PhysicalObject(barcode=barcode, + receiver = request.user, + smallint = smallint) + phys.save() + except Exception, e: + return simple_message(_('Error'), repr(e), go_back=True) for c in choices: item = models.Item.objects.get(pk=c) if not item.barcode(): item.metadata_set.create(name='syrup:barcode', value=barcode) item.save() - return simple_message(_('Matches saved.'), '', go_back=False) + return g.render('phys/mark_arrived_outcome.xhtml') -- 2.11.0