diff options
Diffstat (limited to 'ishtar_common/models.py')
| -rw-r--r-- | ishtar_common/models.py | 138 | 
1 files changed, 131 insertions, 7 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 21b96d85b..87cad0d72 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -326,13 +326,34 @@ class GeneralType(models.Model, Cached):      @classmethod      def get_types(cls, dct={}, instances=False, exclude=[], empty_first=True,                    default=None): +        # cache +        cache_key = None +        if not instances: +            keys = [u"{}".format(ex) for ex in exclude] + [ +                empty_first and 'empty_first' or ''] + [u"{}".format(default)] +            cache_key, value = get_cache(cls, keys) +            if value: +                return value          base_dct = dct.copy()          if hasattr(cls, 'parent'): -            return cls._get_parent_types( -                base_dct, instances, exclude=exclude, empty_first=empty_first, -                default=default) -        return cls._get_types(base_dct, instances, exclude=exclude, -                              empty_first=empty_first, default=default) +            if not cache_key: +                return cls._get_parent_types( +                    base_dct, instances, exclude=exclude, +                    empty_first=empty_first, default=default) +            vals = [v for v in cls._get_parent_types( +                    base_dct, instances, exclude=exclude, +                    empty_first=empty_first, default=default)] +            cache.set(cache_key, vals, settings.CACHE_TIMEOUT) +            return vals + +        if not cache_key: +            return cls._get_types(base_dct, instances, exclude=exclude, +                                  empty_first=empty_first, default=default) +        vals = [v for v in cls._get_types( +            base_dct, instances, exclude=exclude, empty_first=empty_first, +            default=default)] +        cache.set(cache_key, vals, settings.CACHE_TIMEOUT) +        return vals      @classmethod      def _get_types(cls, dct={}, instances=False, exclude=[], empty_first=True, @@ -564,7 +585,11 @@ class ImageModel(models.Model):                                  save=False)                  if old_path != self.image.path: -                    os.remove(old_path) +                    try: +                        os.remove(old_path) +                    except OSError: +                        # already clean +                        pass                  # save the thumbnail                  self.thumbnail.save( @@ -661,6 +686,13 @@ class BaseHistorizedItem(Imported):          except IndexError:              return +    @property +    def history_creation_date(self): +        try: +            return self.history.order_by('history_date').all()[0].history_date +        except IndexError: +            return +      def rollback(self, date):          """          Rollback to a previous state @@ -811,6 +843,38 @@ class LightHistorizedItem(BaseHistorizedItem):          super(LightHistorizedItem, self).save(*args, **kwargs)          return True +PARSE_FORMULA = re.compile("{([^}]*)}") + + +def get_external_id(key, item): +    profile = get_current_profile() +    if not hasattr(profile, key): +        return +    formula = getattr(profile, key) +    dct = {} +    for fkey in PARSE_FORMULA.findall(formula): +        if fkey.startswith('settings__'): +            dct[fkey] = getattr(settings, fkey[len('settings__'):]) or '' +            continue +        obj = item +        for k in fkey.split('__'): +            try: +                obj = getattr(obj, k) +            except ObjectDoesNotExist: +                obj = None +            if callable(obj): +                obj = obj() +            if obj is None: +                break +        if obj is None: +            dct[fkey] = '' +        else: +            dct[fkey] = obj +    return formula.format(**dct) + +CURRENCY = ((u"€", _(u"Euro")), +            (u"$", _(u"US dollar"))) +  class IshtarSiteProfile(models.Model, Cached):      slug_field = 'slug' @@ -829,7 +893,45 @@ class IshtarSiteProfile(models.Model, Cached):          _(u"Home page"), null=True, blank=True,          help_text=_(u"Homepage of Ishtar - if not defined a default homepage "                      u"will appear. Use the markdown syntax.")) +    file_external_id = models.TextField( +        _(u"File external id"), +        default="{settings__ISHTAR_LOCAL_PREFIX}{year}-{numeric_reference}", +        help_text=_(u"Formula to manage file external ID. " +                    u"Change this with care. With incorrect formula, the " +                    u"application might be unusable and import of external " +                    u"data can be destructive.")) +    parcel_external_id = models.TextField( +        _(u"Parcel external id"), +        default="{associated_file__external_id}{operation__code_patriarche}-" +                "{town__numero_insee}-{section}{parcel_number}", +        help_text=_(u"Formula to manage parcel external ID. " +                    u"Change this with care. With incorrect formula, the " +                    u"application might be unusable and import of external " +                    u"data can be destructive.")) +    context_record_external_id = models.TextField( +        _(u"Context record external id"), +        default="{parcel__external_id}-{label}", +        help_text=_(u"Formula to manage context record external ID. " +                    u"Change this with care. With incorrect formula, the " +                    u"application might be unusable and import of external " +                    u"data can be destructive.")) +    base_find_external_id = models.TextField( +        _(u"Base find external id"), +        default="{context_record__external_id}-{label}", +        help_text=_(u"Formula to manage base find external ID. " +                    u"Change this with care. With incorrect formula, the " +                    u"application might be unusable and import of external " +                    u"data can be destructive.")) +    find_external_id = models.TextField( +        _(u"Find external id"), +        default="{get_first_base_find__context_record__external_id}-{label}", +        help_text=_(u"Formula to manage find external ID. " +                    u"Change this with care. With incorrect formula, the " +                    u"application might be unusable and import of external " +                    u"data can be destructive."))      active = models.BooleanField(_(u"Current active"), default=False) +    currency = models.CharField(_(u"Currency"), default=u"€", +                                choices=CURRENCY, max_length='5')      class Meta:          verbose_name = _(u"Ishtar site profile") @@ -2239,6 +2341,21 @@ class Person(Address, Merge, OwnPerms, ValueGetter):                    if getattr(self, attr)]          return slugify(u"-".join(values)) +    def operation_docs_q(self): +        from archaeological_operations.models import OperationSource +        return OperationSource.objects.filter( +            authors__person=self) + +    def contextrecord_docs_q(self): +        from archaeological_context_records.models import ContextRecordSource +        return ContextRecordSource.objects.filter( +            authors__person=self) + +    def find_docs_q(self): +        from archaeological_finds.models import FindSource +        return FindSource.objects.filter( +            authors__person=self) +      def save(self, *args, **kwargs):          super(Person, self).save(*args, **kwargs)          if hasattr(self, 'responsible_town_planning_service'): @@ -2250,6 +2367,9 @@ class Person(Address, Merge, OwnPerms, ValueGetter):  class IshtarUser(User): +    TABLE_COLS = ('username', 'person__name', 'person__surname', +                  'person__email', 'person__person_types_list', +                  'person__attached_to')      person = models.ForeignKey(Person, verbose_name=_(u"Person"), unique=True,                                 related_name='ishtaruser') @@ -2296,6 +2416,9 @@ class IshtarUser(User):          cache.set(cache_key, res, settings.CACHE_SMALLTIMEOUT)          return res +IshtarUser._meta.get_field('password').help_text = _( +    u"To modify the password use the form in Auth > User") +  class AuthorType(GeneralType):      class Meta: @@ -2374,7 +2497,8 @@ class Source(models.Model):      additional_information = models.TextField(_(u"Additional information"),                                                blank=True, null=True)      duplicate = models.BooleanField(_(u"Has a duplicate"), default=False) -    TABLE_COLS = ['title', 'source_type', 'authors', ] +    TABLE_COLS = ['title', 'source_type', 'authors', 'associated_url'] +    COL_LINK = ['associated_url']      class Meta:          abstract = True  | 
