fix occasional bug

pull/1/head
eric 2015-08-22 14:29:35 -04:00
parent 8d80be5248
commit 1833318c2d
2 changed files with 21 additions and 2 deletions

View File

@ -1261,8 +1261,13 @@ class Work(models.Model):
self.save()
return self.selected_edition
except IndexError:
#should only happen if there are no editions for the work
return None
#should only happen if there are no editions for the work,
#which can happen when works are being merged
try:
return WasWork.objects.get(was=self.id).work.preferred_edition
except WasWork.DoesNotExist:
#should not happen
return None
def last_campaign_status(self):
campaign = self.last_campaign()

View File

@ -788,6 +788,20 @@ class SafeGetWorkTest(TestCase):
work = safe_get_work(w2_id)
self.assertEqual(work, w1)
self.assertRaises(Http404, safe_get_work, 3)
class WorkTests(TestCase):
def test_preferred_edition(self):
w1 = models.Work.objects.create()
w2 = models.Work.objects.create()
ww = models.WasWork.objects.create(work=w1, was= w2.id)
e1 = models.Edition.objects.create(work=w1)
self.assertEqual(e1, w1.preferred_edition)
e2 = models.Edition.objects.create(work=w1)
w1.selected_edition=e2
w1.save()
self.assertEqual(e2, w1.preferred_edition)
self.assertEqual(e2, w2.preferred_edition)
class DownloadPageTest(TestCase):
def test_download_page(self):