def test_amendments_from_work(self): amending = Work.objects.get(id=1) # this work has two docs: # 2 - expression date: 2011-01-01 # 3 - expression date: 2012-02-02 amended = Work.objects.get(id=3) d = date(2011, 12, 10) # this will impact only document id 3 user = User.objects.get(pk=1) a = Amendment(amending_work=amending, amended_work=amended, date=d, created_by_user=user) a.save() doc = Document.objects.get(id=2) # only the pre-existing amendment event assert_equal(len(doc.amendment_events()), 1) doc = Document.objects.get(id=3) events = list(doc.amendment_events()) # one new in addition to the two previous ones assert_equal(len(events), 3) assert_equal(events[1].amending_uri, amending.frbr_uri) assert_equal(events[1].amending_title, amending.title) assert_equal(events[1].date, d)
def test_possible_expression_dates_consolidation(self): self.work.publication_date = datetime.date(2017, 2, 23) consolidation = ArbitraryExpressionDate(work=self.work, date='2020-09-13', created_by_user_id=1) consolidation.save() amendment = Amendment(amended_work=self.work, amending_work_id=2, date='2018-12-05', created_by_user_id=1) amendment.save() dates = self.work.possible_expression_dates() self.assertEqual(dates, [ { 'date': datetime.date(2020, 9, 13), 'amendment': False, 'consolidation': True, 'initial': False }, { 'date': datetime.date(2018, 12, 5), 'amendment': True, 'consolidation': False, 'initial': False }, { 'date': datetime.date(2017, 2, 23), 'initial': True }, ])
def test_amendments_from_work(self): amending = Work.objects.get(id=1) # this work has two docs: # 2 - expression date: 2011-01-01 # 3 - expression date: 2012-02-02 amended = Work.objects.get(id=3) d = date(2011, 12, 10) # this will impact only work 2 user = User.objects.get(pk=1) a = Amendment(amending_work=amending, amended_work=amended, date=d, created_by_user=user) a.save() doc = Document.objects.get(id=2) assert_equal(doc.amendment_events(), []) doc = Document.objects.get(id=3) events = doc.amendment_events() assert_equal(len(events), 1) assert_equal(events[0].amending_uri, amending.frbr_uri) assert_equal(events[0].amending_title, amending.title) assert_equal(events[0].date, d)
def test_possible_expression_dates_commencement(self): self.work.publication_date = None # the commencements fixture commences work 1 on 2016-07-15 amendment = Amendment(amended_work=self.work, amending_work_id=2, date='2020-09-13', created_by_user_id=1) amendment.save() amendment = Amendment(amended_work=self.work, amending_work_id=2, date='2018-12-05', created_by_user_id=1) amendment.save() dates = self.work.possible_expression_dates() self.assertEqual(dates, [ { 'date': datetime.date(2020, 9, 13), 'amendment': True, 'consolidation': False, 'initial': False }, { 'date': datetime.date(2018, 12, 5), 'amendment': True, 'consolidation': False, 'initial': False }, { 'date': datetime.date(2016, 7, 15), 'initial': True }, ])
def link_amendment(self, work, info): # if the work `amends` something, try linking it # (this will only work if there's only one amendment listed) # make a task if this fails amended_work = self.find_work_by_title(info['amends']) if not amended_work: return self.create_task(work, info, task_type='link-amendment') date = info.get('commencement_date') or work.commencement_date if not date: return self.create_task(work, info, task_type='link-amendment') try: Amendment.objects.get( amended_work=amended_work, amending_work=work, date=date ) except Amendment.DoesNotExist: amendment = Amendment() amendment.amended_work = amended_work amendment.amending_work = work amendment.created_by_user = self.user amendment.date = date amendment.save()
def link_amendment(self, info, form): # if the work `amends` something, try linking it # (this will only work if there's only one amendment listed) # make a task if this fails if info.get('amends'): try: amended_work = self.find_work_by_title(info.get('amends')) date = info.get('commencement_date') or info['work'].commencement_date try: Amendment.objects.get( amended_work=amended_work, amending_work=info['work'], date=date ) except Amendment.DoesNotExist: if date: amendment = Amendment() amendment.amended_work = amended_work amendment.amending_work = info['work'] amendment.created_by_user = self.request.user amendment.date = date amendment.save() else: self.create_task(info, form, task_type='amendment') except Work.DoesNotExist: self.create_task(info, form, task_type='amendment')
def test_get_work_timeline(self): view = WorkViewBase() work = Work.objects.get(pk=1) work.assent_date = datetime.date(2014, 3, 20) amendment = Amendment(amended_work_id=1, amending_work_id=2, date='2019-01-01', created_by_user_id=1) amendment.save() consolidation = ArbitraryExpressionDate(work=work, date='2018-01-01', created_by_user_id=1) consolidation.save() timeline = WorkViewBase.get_work_timeline(view, work) # most recent event first self.assertEqual(timeline[0]['date'], datetime.date(2019, 1, 1)) self.assertEqual(timeline[0]['amendments'][0].date, datetime.date(2019, 1, 1)) self.assertEqual(timeline[0]['initial'], False) # consolidation self.assertEqual(timeline[1]['date'], datetime.date(2018, 1, 1)) self.assertEqual(timeline[1]['consolidations'][0].date, datetime.date(2018, 1, 1)) self.assertEqual(timeline[1]['initial'], False) # publication date on fixture self.assertEqual(timeline[2]['date'], datetime.date(2014, 4, 2)) self.assertEqual(timeline[2]['publication_date'], True) self.assertEqual(timeline[2]['initial'], True) # assent date (oldest) self.assertEqual(timeline[-1]['date'], datetime.date(2014, 3, 20)) self.assertEqual(timeline[-1]['assent_date'], True)
def get_form_kwargs(self): kwargs = super(AddWorkAmendmentView, self).get_form_kwargs() kwargs['instance'] = Amendment(amended_work=self.work) kwargs['instance'].created_by_user = self.request.user kwargs['instance'].updated_by_user = self.request.user return kwargs