diff options
-rw-r--r-- | archaeological_files/models.py | 19 | ||||
-rw-r--r-- | archaeological_operations/models.py | 41 | ||||
-rw-r--r-- | archaeological_operations/tests.py | 25 | ||||
-rw-r--r-- | archaeological_operations/views.py | 7 |
4 files changed, 64 insertions, 28 deletions
diff --git a/archaeological_files/models.py b/archaeological_files/models.py index 9709824df..1cfbe35d3 100644 --- a/archaeological_files/models.py +++ b/archaeological_files/models.py @@ -33,7 +33,8 @@ from ishtar_common.models import GeneralType, BaseHistorizedItem, \ Dashboard, DashboardFormItem, IshtarUser, ValueGetter, ShortMenuItem, \ OperationType, get_external_id -from archaeological_operations.models import get_values_town_related +from archaeological_operations.models import get_values_town_related, \ + ClosedItem class FileType(GeneralType): @@ -68,8 +69,8 @@ if settings.COUNTRY == 'fr': ordering = ('label',) -class File(BaseHistorizedItem, OwnPerms, ValueGetter, ShortMenuItem, - DashboardFormItem): +class File(ClosedItem, BaseHistorizedItem, OwnPerms, ValueGetter, + ShortMenuItem, DashboardFormItem): TABLE_COLS = ['numeric_reference', 'year', 'internal_reference', 'file_type', 'saisine_type', 'towns', ] year = models.IntegerField(_(u"Year"), @@ -396,18 +397,6 @@ class File(BaseHistorizedItem, OwnPerms, ValueGetter, ShortMenuItem, def town_list(self): return u", ".join([unicode(tw) for tw in self.towns.all()]) - def closing(self): - if self.is_active(): - return - q = self.history - if not q.count(): - return {'date': None, 'user': None} - for item in q.all(): - if not item.end_date: - break - return {'date': item.history_date, - 'user': IshtarUser.objects.get(pk=item.history_modifier_id)} - def total_surface_ha(self): if self.total_surface: return self.total_surface / 10000.0 diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index b82854f20..2db101104 100644 --- a/archaeological_operations/models.py +++ b/archaeological_operations/models.py @@ -137,8 +137,36 @@ QUALITY = (('ND', _(u"Not documented")), ('R', _(u"Reliable")),) -class Operation(BaseHistorizedItem, OwnPerms, ValueGetter, ShortMenuItem, - DashboardFormItem): +class ClosedItem(object): + def closing(self): + if self.is_active(): + return + in_history = False + date = self.end_date + # last action is closing? + for idx, item in enumerate( + self.history.order_by('-history_date').all()): + if not idx: + # last action + continue + if not item.end_date or item.end_date != self.end_date: + break + in_history = True + user = None + if in_history: + if item.history_modifier_id: + q = IshtarUser.objects.filter(pk=item.history_modifier_id) + if q.count(): + user = q.all()[0] + elif self.history_modifier_id: + q = IshtarUser.objects.filter(pk=self.history_modifier_id) + if q.count(): + user = q.all()[0] + return {'date': date, 'user': user} + + +class Operation(ClosedItem, BaseHistorizedItem, OwnPerms, ValueGetter, + ShortMenuItem, DashboardFormItem): QUALITY_DICT = dict(QUALITY) SHOW_URL = 'show-operation' TABLE_COLS = ['year_index', 'operation_type', 'remains', 'towns', @@ -423,15 +451,6 @@ class Operation(BaseHistorizedItem, OwnPerms, ValueGetter, ShortMenuItem, def is_active(self): return not bool(self.end_date) - def closing(self): - if self.is_active(): - return - for item in self.history.all(): - if not item.end_date: - break - return {'date': item.history_date, - 'user': IshtarUser.objects.get(pk=item.history_modifier_id)} - def save(self, *args, **kwargs): # put a default year if start_date is defined if self.start_date and not self.year: diff --git a/archaeological_operations/tests.py b/archaeological_operations/tests.py index 2bf3a6691..a8d627dd5 100644 --- a/archaeological_operations/tests.py +++ b/archaeological_operations/tests.py @@ -692,6 +692,31 @@ class OperationWizardCreationTest(WizardTest, OperationInitTest, TestCase): self.operation_number + 1) +class OperationWizardClosingTest(OperationWizardCreationTest): + fixtures = OperationWizardCreationTest.fixtures + url_name = 'operation_closing' + wizard_name = 'operation_closing_wizard' + steps = views.operation_closing_steps + form_datas = [[ + { + 'selec-operation_closing': {'pk': None}, + 'date-operation_closing': {'end_date': '2016-01-01'}, + }, []]] + + def pre_wizard(self): + self.ope = self.get_default_operation() + self.form_datas[0][0]['selec-operation_closing']['pk'] = self.ope.pk + self.assertTrue(self.ope.is_active()) + super(OperationWizardClosingTest, self).pre_wizard() + + def post_wizard(self): + ope = models.Operation.objects.get(pk=self.ope.pk) + self.assertFalse(ope.is_active()) + self.assertEqual( + ope.closing()['date'].strftime('%Y-%d-%m'), + self.form_datas[0][0]['date-operation_closing']['end_date']) + + class OperationAdminActWizardCreationTest(WizardTest, OperationInitTest, TestCase): fixtures = [settings.ROOT_PATH + diff --git a/archaeological_operations/views.py b/archaeological_operations/views.py index 607f06a81..55e2b2692 100644 --- a/archaeological_operations/views.py +++ b/archaeological_operations/views.py @@ -345,10 +345,13 @@ def operation_add(request, file_id): return redirect(reverse('operation_creation', kwargs={'step': 'general-operation_creation'})) -operation_closing_wizard = OperationClosingWizard.as_view([ +operation_closing_steps = [ ('selec-operation_closing', OperationFormSelection), ('date-operation_closing', ClosingDateFormSelection), - ('final-operation_closing', FinalOperationClosingForm)], + ('final-operation_closing', FinalOperationClosingForm)] + +operation_closing_wizard = OperationClosingWizard.as_view( + operation_closing_steps, label=_(u"Operation closing"), url_name='operation_closing',) |