Merge pull request #515 from Gluejar/recover-edition

fix occasional bug
pull/1/head
Raymond Yee 2015-08-24 20:50:14 -07:00
commit 40cdd1b141
2 changed files with 21 additions and 2 deletions

View File

@ -1262,7 +1262,12 @@ class Work(models.Model):
self.save()
return self.selected_edition
except IndexError:
#should only happen if there are no editions for the work
#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):

View File

@ -789,6 +789,20 @@ class SafeGetWorkTest(TestCase):
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):
w = models.Work()