diff options
Diffstat (limited to 'ishtar_common/models.py')
| -rw-r--r-- | ishtar_common/models.py | 141 | 
1 files changed, 63 insertions, 78 deletions
| diff --git a/ishtar_common/models.py b/ishtar_common/models.py index bdc621528..ef4ae63e2 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -20,54 +20,50 @@  """  Models description  """ -from cStringIO import StringIO  import copy  import datetime  import inspect -from PIL import Image  import logging  import os  import re -from secretary import Renderer as SecretaryRenderer  import shutil -from subprocess import Popen, PIPE  import tempfile  import time -from unidecode import unidecode +from cStringIO import StringIO +from subprocess import Popen, PIPE +from PIL import Image  from django.conf import settings +from django.contrib.auth.models import User, Group +from django.contrib.contenttypes.fields import GenericForeignKey +from django.contrib.contenttypes.models import ContentType +from django.contrib.gis.db import models  from django.contrib.postgres.fields import JSONField  from django.contrib.postgres.search import SearchVectorField, SearchVector  from django.core.cache import cache  from django.core.exceptions import ObjectDoesNotExist, ValidationError  from django.core.files.uploadedfile import SimpleUploadedFile -from django.core.validators import validate_slug  from django.core.urlresolvers import reverse, NoReverseMatch -from django.db.utils import DatabaseError +from django.core.validators import validate_slug  from django.db.models import Q, Max, Count  from django.db.models.signals import post_save, post_delete, m2m_changed +from django.db.utils import DatabaseError +from django.template.defaultfilters import slugify  from django.utils.functional import lazy -from django.utils.translation import ugettext_lazy as _ -  from django.utils.safestring import SafeUnicode, mark_safe -from django.template.defaultfilters import slugify - -from django.contrib.auth.models import User, Group -from django.contrib.contenttypes.models import ContentType -from django.contrib.contenttypes.fields import GenericForeignKey -from django.contrib.gis.db import models - +from django.utils.translation import ugettext_lazy as _ +from secretary import Renderer as SecretaryRenderer  from simple_history.models import HistoricalRecords as BaseHistoricalRecords +from unidecode import unidecode  from ishtar_common.model_merging import merge_model_objects -from ishtar_common.utils import get_cache, disable_for_loaddata, create_slug,\ -    get_all_field_names, merge_tsvectors, cached_label_changed, \ -    generate_relation_graph -  from ishtar_common.models_imports import ImporterModel, ImporterType, \      ImporterDefault, ImporterDefaultValues, ImporterColumn, \      ImporterDuplicateField, Regexp, ImportTarget, TargetKey, FormaterType, \      Import, TargetKeyGroup +from ishtar_common.utils import get_cache, disable_for_loaddata, create_slug, \ +    get_all_field_names, merge_tsvectors, cached_label_changed, \ +    generate_relation_graph  __all__ = [      'ImporterModel', 'ImporterType', 'ImporterDefault', 'ImporterDefaultValues', @@ -3038,7 +3034,7 @@ class Author(FullSearch):              list(self.contextrecordsource_related.all()) -class SourceType(GeneralType): +class SourceType(HierarchicalType):      class Meta:          verbose_name = _(u"Source type")          verbose_name_plural = _(u"Source types") @@ -3070,11 +3066,31 @@ post_save.connect(post_save_cache, sender=Format)  post_delete.connect(post_save_cache, sender=Format) -class Source(OwnPerms, ImageModel, FullSearch): -    title = models.CharField(_(u"Title"), max_length=300) -    external_id = models.TextField(_(u"External ID"), max_length=300, null=True, -                                   blank=True) -    source_type = models.ForeignKey(SourceType, verbose_name=_(u"Type")) +class LicenseType(GeneralType): +    url = models.URLField(_(u"URL"), blank=True, null=True) + +    class Meta: +        verbose_name = _(u"License type") +        verbose_name_plural = _(u"License types") +        ordering = ('label',) + + +post_save.connect(post_save_cache, sender=LicenseType) +post_delete.connect(post_save_cache, sender=LicenseType) + + +class BaseSource(OwnPerms, ImageModel, FullSearch): +    title = models.TextField(_(u"Title"), blank=True, default='') +    index = models.IntegerField(verbose_name=_(u"Index"), blank=True, +                                null=True) +    external_id = models.TextField(_(u"External ID"), null=True, blank=True) +    reference = models.TextField(_(u"Ref."), null=True, blank=True) +    internal_reference = models.TextField(_(u"Internal ref."), null=True, +                                          blank=True) +    source_type = models.ForeignKey(SourceType, verbose_name=_(u"Type"), +                                    null=True, blank=True) +    licenses = models.ManyToManyField(LicenseType, verbose_name=_(u"License"), +                                      blank=True)      support_type = models.ForeignKey(SupportType, verbose_name=_(u"Support"),                                       blank=True, null=True,)      format_type = models.ForeignKey(Format, verbose_name=_(u"Format"), @@ -3083,6 +3099,8 @@ class Source(OwnPerms, ImageModel, FullSearch):                               blank=True)      authors = models.ManyToManyField(Author, verbose_name=_(u"Authors"),                                       related_name="%(class)s_related") +    authors_raw = models.CharField(verbose_name=_(u"Authors (raw)"), +                                   blank=True, null=True, max_length=250)      associated_url = models.URLField(          blank=True, null=True, max_length=1000,          verbose_name=_(u"Numerical ressource (web address)")) @@ -3094,15 +3112,13 @@ class Source(OwnPerms, ImageModel, FullSearch):          blank=True, null=True,          verbose_name=_(u"Receipt date in documentation"))      item_number = models.IntegerField(_(u"Item number"), default=1) -    reference = models.CharField(_(u"Ref."), max_length=100, null=True, -                                 blank=True) -    internal_reference = models.CharField( -        _(u"Internal ref."), max_length=100, null=True, blank=True)      description = models.TextField(_(u"Description"), blank=True, null=True)      comment = models.TextField(_(u"Comment"), blank=True, null=True)      additional_information = models.TextField(_(u"Additional information"),                                                blank=True, null=True)      duplicate = models.BooleanField(_(u"Has a duplicate"), default=False) +    associated_links = models.TextField(_(u"Symbolic links"), blank=True, +                                        null=True)      TABLE_COLS = ['title', 'source_type', 'authors', 'associated_url']      COL_LINK = ['associated_url'] @@ -3132,22 +3148,11 @@ class Source(OwnPerms, ImageModel, FullSearch):          return slugify(u"-".join(values)) -class LicenseType(GeneralType): -    url = models.URLField(_(u"URL"), blank=True, null=True) -    class Meta: -        verbose_name = _(u"License type") -        verbose_name_plural = _(u"License types") -        ordering = ('label',) - - -class ImageType(GeneralType): -    class Meta: -        verbose_name = _(u"Image type") -        verbose_name_plural = _(u"Image types") -        ordering = ('label',) +class Document(BaseSource): +    pass -class IshtarImage(ImageModel): +class IshtarImage(BaseSource):      # order is important: put the image in the first match found      # other will be symbolic links      RELATED_MODELS = [ @@ -3156,30 +3161,10 @@ class IshtarImage(ImageModel):      ]      LINK_SPLIT = u"<||>" -    name = models.CharField(_(u"Name"), max_length=250, blank=True, null=True) -    description = models.TextField(_(u"Description"), blank=True, null=True) -    associated_links = models.TextField(_(u"Symbolic links"), blank=True, -                                        null=True) -    licenses = models.ManyToManyField(LicenseType, verbose_name=_(u"License"), -                                      blank=True) -    authors = models.ManyToManyField(Author, verbose_name=_(u"Authors"), -                                     blank=True) -    authors_raw = models.CharField(verbose_name=_(u"Authors (raw)"), -                                   blank=True, null=True, max_length=250) - -    image_type = models.ForeignKey(ImageType, verbose_name=_(u"Type"), -                                   blank=True, null=True) -    creation_date = models.DateField(blank=True, null=True, -                                     verbose_name=_(u"Creation date")) -    reference = models.CharField(_(u"Ref."), max_length=250, null=True, -                                 blank=True) -    internal_reference = models.CharField( -        _(u"Internal ref."), max_length=250, null=True, blank=True) -      class Meta:          verbose_name = _(u"Image")          verbose_name_plural = _(u"Images") -        ordering = ('name',) +        ordering = ('title',)      def _get_base_image_paths(self):          for related_model in self.RELATED_MODELS: @@ -3314,22 +3299,22 @@ class ThroughImage(models.Model):          self.image.save() -if settings.COUNTRY == 'fr': -    class Arrondissement(models.Model): -        name = models.CharField(u"Nom", max_length=30) -        department = models.ForeignKey(Department, verbose_name=u"Département") +class Arrondissement(models.Model): +    name = models.CharField(u"Nom", max_length=30) +    department = models.ForeignKey(Department, verbose_name=u"Département") -        def __unicode__(self): -            return settings.JOINT.join((self.name, unicode(self.department))) +    def __unicode__(self): +        return settings.JOINT.join((self.name, unicode(self.department))) -    class Canton(models.Model): -        name = models.CharField(u"Nom", max_length=30) -        arrondissement = models.ForeignKey(Arrondissement, -                                           verbose_name=u"Arrondissement") -        def __unicode__(self): -            return settings.JOINT.join( -                (self.name, unicode(self.arrondissement))) +class Canton(models.Model): +    name = models.CharField(u"Nom", max_length=30) +    arrondissement = models.ForeignKey(Arrondissement, +                                       verbose_name=u"Arrondissement") + +    def __unicode__(self): +        return settings.JOINT.join( +            (self.name, unicode(self.arrondissement)))  class TownManager(models.GeoManager): | 
