diff options
Diffstat (limited to 'archaeological_operations/models.py')
| -rw-r--r-- | archaeological_operations/models.py | 437 | 
1 files changed, 212 insertions, 225 deletions
| diff --git a/archaeological_operations/models.py b/archaeological_operations/models.py index 448adcd68..5bbe357c0 100644 --- a/archaeological_operations/models.py +++ b/archaeological_operations/models.py @@ -35,8 +35,6 @@ from ishtar_common.models import GeneralType, BaseHistorizedItem, \      Person, Organization, Town, Dashboard, IshtarUser, ValueGetter, \      DocumentTemplate, ShortMenuItem, DashboardFormItem, GeneralRelationType,\      GeneralRecordRelations, post_delete_record_relation, OperationType -FILES_AVAILABLE = 'archaeological_files' in settings.INSTALLED_APPS -FINDS_AVAILABLE = 'archaeological_finds' in settings.INSTALLED_APPS  class RemainType(GeneralType): @@ -137,8 +135,7 @@ class Operation(BaseHistorizedItem, OwnPerms, ValueGetter, ShortMenuItem,      SHOW_URL = 'show-operation'      TABLE_COLS = ['year_index', 'operation_type', 'remains', 'towns',                    'start_date', 'excavation_end_date'] -    if FILES_AVAILABLE: -        TABLE_COLS.insert(4, 'associated_file_short_label') +    TABLE_COLS.insert(4, 'associated_file_short_label')      creation_date = models.DateField(_(u"Creation date"),                                       default=datetime.date.today)      end_date = models.DateField(_(u"Closing date"), null=True, blank=True) @@ -161,11 +158,10 @@ class Operation(BaseHistorizedItem, OwnPerms, ValueGetter, ShortMenuItem,      year = models.IntegerField(_(u"Year"), null=True, blank=True)      operation_code = models.IntegerField(_(u"Operation code"), null=True,                                           blank=True) -    if FILES_AVAILABLE: -        associated_file = models.ForeignKey( -            'archaeological_files.File', -            related_name='operations', verbose_name=_(u"File"), -            blank=True, null=True) +    associated_file = models.ForeignKey( +        'archaeological_files.File', +        related_name='operations', verbose_name=_(u"File"), +        blank=True, null=True)      operation_type = models.ForeignKey(OperationType, related_name='+',                                         verbose_name=_(u"Operation type"))      surface = models.IntegerField(_(u"Surface (m2)"), blank=True, null=True) @@ -286,15 +282,12 @@ class Operation(BaseHistorizedItem, OwnPerms, ValueGetter, ShortMenuItem,          return reverse('show-operation', args=[self.pk, ''])      def has_finds(self): -        if not FINDS_AVAILABLE: -            return          from archaeological_finds.models import BaseFind          return BaseFind.objects.filter(context_record__operation=self).count()      def finds(self): -        if FINDS_AVAILABLE: -            from archaeological_finds.models import BaseFind -            return BaseFind.objects.filter(context_record__operation=self) +        from archaeological_finds.models import BaseFind +        return BaseFind.objects.filter(context_record__operation=self)      def get_reference(self, full=False):          ref = "" @@ -360,7 +353,7 @@ class Operation(BaseHistorizedItem, OwnPerms, ValueGetter, ShortMenuItem,      @property      def associated_file_short_label(self): -        if not FILES_AVAILABLE or not self.associated_file: +        if not self.associated_file:              return ""          return self.associated_file.short_label @@ -453,10 +446,9 @@ def operation_post_save(sender, **kwargs):          operation.fnap_financing = fnap_percent          operation.save()      cached_label_changed(sender, **kwargs) -    if FILES_AVAILABLE and operation.associated_file: +    if operation.associated_file:          operation.associated_file.update_short_menu_class() -    # manage parcel association -    if FILES_AVAILABLE and operation.associated_file: +        # manage parcel association          for parcel in operation.parcels.all():              parcel.copy_to_file()  post_save.connect(operation_post_save, sender=Operation) @@ -548,216 +540,214 @@ class ActType(GeneralType):          verbose_name_plural = _(u"Act types")          ordering = ('label',) -AdministrativeAct = None -if FILES_AVAILABLE: -    class AdministrativeAct(BaseHistorizedItem, OwnPerms, ValueGetter): -        TABLE_COLS = ['full_ref', 'year', 'index', 'act_type', 'act_object', -                      'signature_date', 'associated_file', 'operation', -                      'towns_label'] -        TABLE_COLS_FILE = [ -            'full_ref', 'year', 'index', 'act_type', -            'act_object', 'associated_file', 'towns_label', -        ] -        TABLE_COLS_OPE = ['full_ref', 'year', 'index', 'act_type', 'operation', -                          'act_object', 'towns_label'] -        if settings.COUNTRY == 'fr': -            TABLE_COLS.append('departments_label') -            TABLE_COLS_FILE.append('departments_label') -            TABLE_COLS_OPE.append('departments_label') -        act_type = models.ForeignKey(ActType, verbose_name=_(u"Act type")) -        in_charge = models.ForeignKey( -            Person, blank=True, null=True, -            related_name='adminact_operation_in_charge', -            verbose_name=_(u"Person in charge of the operation"), -            on_delete=models.SET_NULL,) -        index = models.IntegerField(verbose_name=_(u"Index"), blank=True, -                                    null=True) -        operator = models.ForeignKey( -            Organization, blank=True, null=True, -            verbose_name=_(u"Archaeological preventive operator"), -            related_name='adminact_operator', on_delete=models.SET_NULL) -        scientist = models.ForeignKey( -            Person, blank=True, null=True, -            related_name='adminact_scientist', on_delete=models.SET_NULL, -            verbose_name=_(u"Scientist in charge")) -        signatory = models.ForeignKey( -            Person, blank=True, null=True, related_name='signatory', -            verbose_name=_(u"Signatory"), on_delete=models.SET_NULL,) -        operation = models.ForeignKey( -            Operation, blank=True, null=True, -            related_name='administrative_act', verbose_name=_(u"Operation")) -        if FILES_AVAILABLE: -            associated_file = models.ForeignKey( -                'archaeological_files.File', -                blank=True, null=True, -                related_name='administrative_act', -                verbose_name=_(u"Archaelogical file")) -        signature_date = models.DateField(_(u"Signature date"), blank=True, -                                          null=True) -        year = models.IntegerField(_(u"Year"), blank=True, null=True) -        act_object = models.TextField(_(u"Object"), max_length=300, blank=True, + +class AdministrativeAct(BaseHistorizedItem, OwnPerms, ValueGetter): +    TABLE_COLS = ['full_ref', 'year', 'index', 'act_type', 'act_object', +                  'signature_date', 'associated_file', 'operation', +                  'towns_label'] +    TABLE_COLS_FILE = [ +        'full_ref', 'year', 'index', 'act_type', +        'act_object', 'associated_file', 'towns_label', +    ] +    TABLE_COLS_OPE = ['full_ref', 'year', 'index', 'act_type', 'operation', +                      'act_object', 'towns_label'] +    if settings.COUNTRY == 'fr': +        TABLE_COLS.append('departments_label') +        TABLE_COLS_FILE.append('departments_label') +        TABLE_COLS_OPE.append('departments_label') +    act_type = models.ForeignKey(ActType, verbose_name=_(u"Act type")) +    in_charge = models.ForeignKey( +        Person, blank=True, null=True, +        related_name='adminact_operation_in_charge', +        verbose_name=_(u"Person in charge of the operation"), +        on_delete=models.SET_NULL,) +    index = models.IntegerField(verbose_name=_(u"Index"), blank=True, +                                null=True) +    operator = models.ForeignKey( +        Organization, blank=True, null=True, +        verbose_name=_(u"Archaeological preventive operator"), +        related_name='adminact_operator', on_delete=models.SET_NULL) +    scientist = models.ForeignKey( +        Person, blank=True, null=True, +        related_name='adminact_scientist', on_delete=models.SET_NULL, +        verbose_name=_(u"Scientist in charge")) +    signatory = models.ForeignKey( +        Person, blank=True, null=True, related_name='signatory', +        verbose_name=_(u"Signatory"), on_delete=models.SET_NULL,) +    operation = models.ForeignKey( +        Operation, blank=True, null=True, +        related_name='administrative_act', verbose_name=_(u"Operation")) +    associated_file = models.ForeignKey( +        'archaeological_files.File', +        blank=True, null=True, +        related_name='administrative_act', +        verbose_name=_(u"Archaelogical file")) +    signature_date = models.DateField(_(u"Signature date"), blank=True,                                        null=True) -        if settings.COUNTRY == 'fr': -            ref_sra = models.CharField(u"Référence SRA", max_length=15, -                                       blank=True, null=True) -            departments_label = models.TextField( -                _(u"Departments"), blank=True, null=True, -                help_text=_(u"Cached values get from associated departments")) -        towns_label = models.TextField( -            _(u"Towns"), blank=True, null=True, -            help_text=_(u"Cached values get from associated towns")) -        history = HistoricalRecords() -        _prefix = 'adminact_' - -        class Meta: -            ordering = ('year', 'signature_date', 'index', 'act_type') -            verbose_name = _(u"Administrative act") -            verbose_name_plural = _(u"Administrative acts") -            permissions = ( -                ("view_administrativeact", -                 ugettext(u"Can view all Administrative act")), -                ("view_own_administrativeact", -                 ugettext(u"Can view own Administrative act")), -                ("add_own_administrativeact", -                 ugettext(u"Can add own Administrative act")), -                ("change_own_administrativeact", -                 ugettext(u"Can change own Administrative act")), -                ("delete_own_administrativeact", -                 ugettext(u"Can delete own Administrative act")), -            ) +    year = models.IntegerField(_(u"Year"), blank=True, null=True) +    act_object = models.TextField(_(u"Object"), max_length=300, blank=True, +                                  null=True) +    if settings.COUNTRY == 'fr': +        ref_sra = models.CharField(u"Référence SRA", max_length=15, +                                   blank=True, null=True) +        departments_label = models.TextField( +            _(u"Departments"), blank=True, null=True, +            help_text=_(u"Cached values get from associated departments")) +    towns_label = models.TextField( +        _(u"Towns"), blank=True, null=True, +        help_text=_(u"Cached values get from associated towns")) +    history = HistoricalRecords() +    _prefix = 'adminact_' + +    class Meta: +        ordering = ('year', 'signature_date', 'index', 'act_type') +        verbose_name = _(u"Administrative act") +        verbose_name_plural = _(u"Administrative acts") +        permissions = ( +            ("view_administrativeact", +             ugettext(u"Can view all Administrative act")), +            ("view_own_administrativeact", +             ugettext(u"Can view own Administrative act")), +            ("add_own_administrativeact", +             ugettext(u"Can add own Administrative act")), +            ("change_own_administrativeact", +             ugettext(u"Can change own Administrative act")), +            ("delete_own_administrativeact", +             ugettext(u"Can delete own Administrative act")), +        ) + +    def __unicode__(self): +        return settings.JOINT.join( +            [unicode(item) for item in [ +                self.operation, self.associated_file, self.act_object] +             if item]) -        def __unicode__(self): -            return settings.JOINT.join( -                [unicode(item) for item in [ -                    self.operation, self.associated_file, self.act_object] -                 if item]) - -        full_ref_lbl = _(u"Ref.") - -        @property -        def full_ref(self): -            lbl = [] -            if self.year: -                lbl.append(unicode(self.year)) -            if self.index: -                lbl.append(u"n°%d" % self.index) -            if settings.COUNTRY == 'fr' and self.ref_sra: -                lbl.append(u"[%s]" % self.ref_sra) -            return u" ".join(lbl) - -        @property -        def towns(self): -            if self.associated_file: -                return self.associated_file.towns.all() -            elif self.operation: -                return self.operation.towns.all() -            return [] - -        @property -        def departments(self): -            if settings.COUNTRY != 'fr': -                return '' -            q = None -            if self.associated_file: -                q = self.associated_file.towns.all() -            elif self.operation: -                q = self.operation.towns.all() -            if not q: -                return '' -            dpts = [] -            for town in q: -                dpt = town.numero_insee[:2] -                if dpt not in dpts: -                    dpts.append(dpt) -            return ', '.join(list(sorted(dpts))) - -        @property -        def related_item(self): -            return self.operation if self.operation else self.associated_file - -        def get_filename(self): -            filename = self.related_item.associated_filename -            filename = u"-".join(filename.split('-')[:-1])  # remove date -            if self.act_type.code: -                filename += u"-" + self.act_type.code -            if self.signature_date and self.index: -                filename += u"-%d-%d" % (self.signature_date.year, -                                         self.index) -            if self.signature_date: -                filename += u"-" + self.signature_date.strftime('%Y%m%d') -            return filename - -        def publish(self, template_pk=None): -            if not self.act_type.associated_template.count(): +    full_ref_lbl = _(u"Ref.") + +    @property +    def full_ref(self): +        lbl = [] +        if self.year: +            lbl.append(unicode(self.year)) +        if self.index: +            lbl.append(u"n°%d" % self.index) +        if settings.COUNTRY == 'fr' and self.ref_sra: +            lbl.append(u"[%s]" % self.ref_sra) +        return u" ".join(lbl) + +    @property +    def towns(self): +        if self.associated_file: +            return self.associated_file.towns.all() +        elif self.operation: +            return self.operation.towns.all() +        return [] + +    @property +    def departments(self): +        if settings.COUNTRY != 'fr': +            return '' +        q = None +        if self.associated_file: +            q = self.associated_file.towns.all() +        elif self.operation: +            q = self.operation.towns.all() +        if not q: +            return '' +        dpts = [] +        for town in q: +            dpt = town.numero_insee[:2] +            if dpt not in dpts: +                dpts.append(dpt) +        return ', '.join(list(sorted(dpts))) + +    @property +    def related_item(self): +        return self.operation if self.operation else self.associated_file + +    def get_filename(self): +        filename = self.related_item.associated_filename +        filename = u"-".join(filename.split('-')[:-1])  # remove date +        if self.act_type.code: +            filename += u"-" + self.act_type.code +        if self.signature_date and self.index: +            filename += u"-%d-%d" % (self.signature_date.year, +                                     self.index) +        if self.signature_date: +            filename += u"-" + self.signature_date.strftime('%Y%m%d') +        return filename + +    def publish(self, template_pk=None): +        if not self.act_type.associated_template.count(): +            return +        if not template_pk: +            template = self.act_type.associated_template.all()[0] +        else: +            q = self.act_type.associated_template.filter(pk=template_pk) +            if not q.count():                  return -            if not template_pk: -                template = self.act_type.associated_template.all()[0] -            else: -                q = self.act_type.associated_template.filter(pk=template_pk) -                if not q.count(): -                    return -                template = q.all()[0] -            return template.publish(self) - -        def _get_index(self): -            if not self.index: -                c_index = 1 -                q = AdministrativeAct.objects.filter( -                    act_type__indexed=True, signature_date__year=self.year, -                    index__isnull=False).order_by("-index") -                if q.count(): -                    c_index = q.all()[0].index + 1 -                self.index = c_index -            conflict = AdministrativeAct.objects.filter( +            template = q.all()[0] +        return template.publish(self) + +    def _get_index(self): +        if not self.index: +            c_index = 1 +            q = AdministrativeAct.objects.filter(                  act_type__indexed=True, signature_date__year=self.year, -                index=self.index) +                index__isnull=False).order_by("-index") +            if q.count(): +                c_index = q.all()[0].index + 1 +            self.index = c_index +        conflict = AdministrativeAct.objects.filter( +            act_type__indexed=True, signature_date__year=self.year, +            index=self.index) +        if self.pk: +            conflict = conflict.exclude(pk=self.pk) +        if conflict.count():              if self.pk: -                conflict = conflict.exclude(pk=self.pk) -            if conflict.count(): -                if self.pk: -                    raise ValidationError(_(u"This index already exists for " -                                            u"this year")) -                else: -                    self._get_index() - -        def clean(self, *args, **kwargs): -            if not self.signature_date: -                return super(AdministrativeAct, self).clean(*args, **kwargs) -            self.year = self.signature_date.year -            if not self.act_type.indexed: -                return super(AdministrativeAct, self).clean(*args, **kwargs) -            self._get_index() -            super(AdministrativeAct, self).clean(*args, **kwargs) +                raise ValidationError(_(u"This index already exists for " +                                        u"this year")) +            else: +                self._get_index() + +    def clean(self, *args, **kwargs): +        if not self.signature_date: +            return super(AdministrativeAct, self).clean(*args, **kwargs) +        self.year = self.signature_date.year +        if not self.act_type.indexed: +            return super(AdministrativeAct, self).clean(*args, **kwargs) +        self._get_index() +        super(AdministrativeAct, self).clean(*args, **kwargs) -        def save(self, *args, **kwargs): -            if settings.COUNTRY == 'fr': -                self.departments_label = self.departments -            self.towns_label = u", ".join( -                list(sorted([unicode(town) for town in self.towns]))) +    def save(self, *args, **kwargs): +        if settings.COUNTRY == 'fr': +            self.departments_label = self.departments +        self.towns_label = u", ".join( +            list(sorted([unicode(town) for town in self.towns]))) -            force = False -            if 'force' in kwargs: -                force = kwargs.pop('force') +        force = False +        if 'force' in kwargs: +            force = kwargs.pop('force') -            if not self.signature_date: -                return super(AdministrativeAct, self).save(*args, **kwargs) -            self.year = self.signature_date.year +        if not self.signature_date: +            return super(AdministrativeAct, self).save(*args, **kwargs) +        self.year = self.signature_date.year -            if not self.act_type.indexed: -                return super(AdministrativeAct, self).save(*args, **kwargs) +        if not self.act_type.indexed: +            return super(AdministrativeAct, self).save(*args, **kwargs) -            if not force: +        if not force: +            self._get_index() +        else: +            try:                  self._get_index() -            else: -                try: -                    self._get_index() -                except: -                    pass +            except: +                pass -            super(AdministrativeAct, self).save(*args, **kwargs) -            if hasattr(self, 'associated_file') and self.associated_file: -                self.associated_file.update_has_admin_act() -                self.associated_file.update_short_menu_class() +        super(AdministrativeAct, self).save(*args, **kwargs) +        if hasattr(self, 'associated_file') and self.associated_file: +            self.associated_file.update_has_admin_act() +            self.associated_file.update_short_menu_class()  def strip_zero(value): @@ -768,11 +758,10 @@ def strip_zero(value):  class Parcel(LightHistorizedItem): -    if FILES_AVAILABLE: -        associated_file = models.ForeignKey( -            'archaeological_files.File', -            related_name='parcels', verbose_name=_(u"File"), -            blank=True, null=True) +    associated_file = models.ForeignKey( +        'archaeological_files.File', +        related_name='parcels', verbose_name=_(u"File"), +        blank=True, null=True)      operation = models.ForeignKey(          Operation, related_name='parcels', blank=True, null=True,          verbose_name=_(u"Operation")) @@ -867,7 +856,7 @@ class Parcel(LightHistorizedItem):      def long_label(self):          items = [unicode(self.operation) or -                 (FILES_AVAILABLE and unicode(self.associated_file)) or ""] +                 unicode(self.associated_file) or ""]          items += [unicode(item) for item in [self.section, self.parcel_number]                    if item]          return settings.JOINT.join(items) @@ -931,12 +920,10 @@ def parcel_post_save(sender, **kwargs):      if parcel.operation and parcel.operation.pk and \         parcel.town not in list(parcel.operation.towns.all()):          parcel.operation.towns.add(parcel.town) -    if FILES_AVAILABLE and parcel.associated_file and \ +    if parcel.associated_file and \         parcel.associated_file.pk and \         parcel.town not in list(parcel.associated_file.towns.all()):          parcel.associated_file.towns.add(parcel.town) -    if not FILES_AVAILABLE: -        return      if parcel.operation and parcel.associated_file:          # parcels are copied between files and operations          parcel.copy_to_operation() | 
