diff options
| author | Étienne Loks <etienne.loks@proxience.com> | 2015-02-26 20:00:21 +0100 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@proxience.com> | 2015-05-06 16:04:02 +0200 | 
| commit | 53be3b87eb1da93534f8d173183b55bb92e4717c (patch) | |
| tree | 3982f2a204b906384ea665d7d71a1c8e1c38cab6 | |
| parent | 802a19e209cd5786fb44fdcb95b47d6c51bbbb6f (diff) | |
| download | Ishtar-53be3b87eb1da93534f8d173183b55bb92e4717c.tar.bz2 Ishtar-53be3b87eb1da93534f8d173183b55bb92e4717c.zip | |
Automatically update raw_general_contractor and raw_town_planning_service on save - improve pdl import of raw_names and of dates
| -rw-r--r-- | archaeological_files/data_importer.py | 39 | ||||
| -rw-r--r-- | archaeological_files/models.py | 47 | ||||
| -rw-r--r-- | ishtar_common/data_importer.py | 7 | ||||
| -rw-r--r-- | ishtar_common/models.py | 9 | 
4 files changed, 75 insertions, 27 deletions
| diff --git a/archaeological_files/data_importer.py b/archaeological_files/data_importer.py index 4d2962d98..2b715cb42 100644 --- a/archaeological_files/data_importer.py +++ b/archaeological_files/data_importer.py @@ -41,6 +41,20 @@ class ImportClosingFormater(ImportFormater):          obj.end_date = open_date + datetime.timedelta(30)          obj.save() +class ImportMayorFormater(ImportFormater): +    def post_process(self, obj, context, value, owner=None): +        value = self.formater.format(value) +        if type(self.field_name) in (list, tuple): +            return #not managed +        associated_obj = get_object_from_path(obj, self.field_name) +        if not value or not obj.main_town or not associated_obj: +            return +        if slugify(value).endswith('le-maire'): +            value += u" de " + obj.main_town.name +            value = value[:300] +        setattr(associated_obj, self.field_name.split('__')[-1], value) +        associated_obj.save() +  class FilePostProcessing(object):      # erroneaous date to be easily identified on post-processing      DEFAULT_YEAR = 1756 @@ -85,14 +99,14 @@ class FileImporterSraPdL(FilePostProcessing, Importer):                      regexp=RE_ADD_CD_POSTAL_TOWN,                      regexp_formater_args=[[0], [1], [2, 1]], required=False,                      comment=u"Dossier - adresse"), -        ImportFormater('general_contractor__raw_name', # C, 3 TODO - extraire nom_prenom_titre +        ImportMayorFormater('general_contractor__raw_name', # C, 3 TODO - extraire nom_prenom_titre                         UnicodeFormater(200),                         comment=u"Aménageur - nom brut", -                       duplicate_fields=['general_contractor__attached_to__name'], +                       post_processing=True,                         required=False), -        ImportFormater(['general_contractor__attached_to__address', # D, 4 -                        'general_contractor__attached_to__postal_code', -                        'general_contractor__attached_to__town'], +        ImportFormater(['general_contractor__address', # D, 4 +                        'general_contractor__postal_code', +                        'general_contractor__town'],                      [UnicodeFormater(500, clean=True),                       UnicodeFormater(5, re_filter=RE_CD_POSTAL_FILTER),                       TownFormater(town_full_dct=tf._town_full_dct, @@ -132,9 +146,9 @@ class FileImporterSraPdL(FilePostProcessing, Importer):          None, # Q, 17          ImportFormater([                      'responsible_town_planning_service__raw_name', # R, 18 service instructeur -                    'responsible_town_planning_service__attached_to__address', -                    'responsible_town_planning_service__attached_to__postal_code', -                    'responsible_town_planning_service__attached_to__town',], +                    'responsible_town_planning_service__address', +                    'responsible_town_planning_service__postal_code', +                    'responsible_town_planning_service__town',],                      [UnicodeFormater(300, clean=True),                       UnicodeFormater(300, clean=True),                       UnicodeFormater(5, re_filter=RE_CD_POSTAL_FILTER), @@ -168,7 +182,7 @@ class FileImporterSraPdL(FilePostProcessing, Importer):          None, # AG, 33          None, # AH, 34          ImportFormater('creation_date', # AI, 35 -                       DateFormater(['%d/%m/%Y', '%d/%m/%Y']), +                       DateFormater(['%d/%m/%Y', '%d/%m/%y']),                         force_value=True,                         comment=u"Date de création",                         required=False,), @@ -227,12 +241,7 @@ class FileImporterSraPdL(FilePostProcessing, Importer):          None, # BQ, 69          None, # BR, 70          None, # BS, 71 -        ImportFormater( -                    'responsible_town_planning_service__attached_to__name', # BT, 72 service instructeur -                    UnicodeFormater(300, clean=True), -                    regexp=RE_ORGA, -                    comment=u"Service instructeur - nom", -                    required=False), +        None, # BT, 72          None, # BU, 73          ImportClosingFormater('', StrToBoolean(cli=True),                                post_processing=True, required=False, diff --git a/archaeological_files/models.py b/archaeological_files/models.py index f2e86d725..dd7773e40 100644 --- a/archaeological_files/models.py +++ b/archaeological_files/models.py @@ -326,22 +326,45 @@ class File(BaseHistorizedItem, OwnPerms, ValueGetter, ShortMenuItem,                  acts.append(act)          return acts +    def update_raw_town_planning_service(self): +        if (self.raw_town_planning_service and not \ +           self.responsible_town_planning_service) or \ +           not self.responsible_town_planning_service: +            return False +        current_lbl = "" +        if self.raw_town_planning_service: +            current_lbl = self.raw_town_planning_service[:] +        lbl = unicode(self.responsible_town_planning_service) +        if not lbl: +            return False +        self.raw_town_planning_service = lbl[:200] +        print('planning_service', lbl) +        return current_lbl != self.raw_town_planning_service + +    def update_raw_general_contractor(self): +        if (self.raw_general_contractor and not \ +           self.general_contractor) or \ +           not self.general_contractor: +            return False +        current_lbl = "" +        if self.raw_general_contractor: +            current_lbl = self.raw_general_contractor[:] +        lbl = unicode(self.general_contractor) +        print('contractor', lbl) +        if not lbl: +            return False +        self.raw_general_contractor = lbl[:200] +        return current_lbl != self.raw_general_contractor + +      def save(self, *args, **kwargs):          returned = super(File, self).save(*args, **kwargs)          if self.main_town and not self.main_town in list(self.towns.all()):              self.towns.add(self.main_town) -        if self.responsible_town_planning_service \ -           and not self.raw_town_planning_service: -            lbl = unicode(self.responsible_town_planning_service) -            if lbl: -                self.raw_town_planning_service = lbl[:200] -                self.save -        if self.general_contractor \ -           and not self.raw_general_contractor: -            lbl = unicode(self.general_contractor)[:200] -            if lbl: -                self.raw_general_contractor = lbl -                self.save() +        updated = self.update_raw_town_planning_service() +        updated += self.update_raw_general_contractor() +        if updated: +            self.save()          self.update_delay_date()          self.update_short_menu_class()          return returned diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py index 6b021e6f3..09c511a25 100644 --- a/ishtar_common/data_importer.py +++ b/ishtar_common/data_importer.py @@ -449,6 +449,13 @@ class StrToBoolean(Formater, ChoiceChecker):  logger = logging.getLogger(__name__) +def get_object_from_path(obj, path): +    for k in path.split('__')[:-1]: +        if not hasattr(obj, k): +            return +        obj = getattr(obj, k) +    return obj +  class Importer(object):      DESC = ""      LINE_FORMAT = [] diff --git a/ishtar_common/models.py b/ishtar_common/models.py index d5e0e5fc5..a99470c2b 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -1630,6 +1630,15 @@ class Person(Address, Merge, OwnPerms, ValueGetter) :                                 if getattr(self, attr)]          return slugify(u"-".join(values)) +    def save(self, *args, **kwargs): +        super(Person, self).save(*args, **kwargs) +        if hasattr(self, 'responsible_town_planning_service'): +            for fle in self.responsible_town_planning_service.all(): +                fle.save() # force update of raw_town_planning_service +        if hasattr(self, 'general_contractor'): +            for fle in self.general_contractor.all(): +                fle.save() # force update of raw_general_contractor +  class IshtarUser(User):      person = models.ForeignKey(Person, verbose_name=_(u"Person"), unique=True,                                 related_name='ishtaruser') | 
