diff options
| author | Étienne Loks <etienne.loks@proxience.com> | 2015-10-12 16:05:30 +0200 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@proxience.com> | 2015-10-12 16:05:30 +0200 | 
| commit | 8e0983972824483739f79af4c02fe52c2efc981a (patch) | |
| tree | 3356ec349fa6bc05ab13fd151979b3a6357a51c5 | |
| parent | 5f3d6e4ea652a8187a3497a3fc65b1886495d812 (diff) | |
| download | Ishtar-8e0983972824483739f79af4c02fe52c2efc981a.tar.bz2 Ishtar-8e0983972824483739f79af4c02fe52c2efc981a.zip | |
Fix import - workon file import
| -rw-r--r-- | ishtar_common/data_importer.py | 31 | ||||
| -rw-r--r-- | ishtar_common/models.py | 8 | 
2 files changed, 34 insertions, 5 deletions
| diff --git a/ishtar_common/data_importer.py b/ishtar_common/data_importer.py index 98569ed84..e19c2dcc6 100644 --- a/ishtar_common/data_importer.py +++ b/ishtar_common/data_importer.py @@ -24,6 +24,7 @@ import io  import logging  import re  import sys +import zipfile  from django.contrib.auth.models import User  from django.db import IntegrityError, transaction @@ -401,6 +402,22 @@ class DateFormater(Formater):              'value': value}) +class FileFormater(Formater): +    need_archive = True + +    def format(self, value, archive): +        value = value.strip() +        if not value: +            return +        zp = zipfile.ZipFile(archive) +        try: +            with zp.open(value.strip().replace(u'\\', u'/')) as z: +                return z.read() +        except KeyError: +            raise ValueError(_(u"\"%(value)s\" is not a valid path for the " +                               u"given archive") % {'value': value}) + +  class StrToBoolean(Formater, ChoiceChecker):      def __init__(self, choices={}, cli=False, strict=False, db_target=None):          self.dct = copy.copy(choices) @@ -531,6 +548,9 @@ class Importer(object):          self.check_col_num = check_col_num          self.line_format = copy.copy(self.LINE_FORMAT)          self.import_instance = import_instance +        self.archive = None +        if import_instance and import_instance.imported_images: +            self.archive = import_instance.imported_images          self._defaults = self.DEFAULTS.copy()          # EXTRA_DEFAULTS are for multiple inheritance          if self.EXTRA_DEFAULTS: @@ -825,7 +845,10 @@ class Importer(object):                              args.append(val_group[idx])                          value = func.format(*args)                      else: -                        value = func.format(v) +                        if getattr(func, 'need_archive', False): +                            value = func.format(v, archive=self.archive) +                        else: +                            value = func.format(v)                  except ValueError, e:                      if formater.required:                          self.c_errors = True @@ -924,6 +947,8 @@ class Importer(object):                      field_names = model._meta.get_all_field_names()                      for v in vals:                          if 'history_modifier' in field_names: +                            if 'defaults' not in v: +                                v['defaults'] = {}                              v['defaults']['history_modifier'] = \                                  self.history_modifier                          m2m_m2ms = [] @@ -937,7 +962,6 @@ class Importer(object):                              v = model.objects.create(**v)                          else:                              v['defaults'] = v.get('defaults', {}) -                            print(v)                              v, created = model.objects.get_or_create(                                  **v)                          for att, objs in m2m_m2ms: @@ -972,7 +996,8 @@ class Importer(object):                      continue                  if not data[attribute]:                      continue -                self.get_field(cls, attribute, data, m2ms, c_path) +                if attribute != '__force_new': +                    self.get_field(cls, attribute, data, m2ms, c_path)              # default values              path = tuple(path)              if path in self._defaults: diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 39a79161e..1621f05db 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -41,7 +41,9 @@ from django.db.utils import DatabaseError  from django.db.models import Q, Max, Count  from django.db.models.base import ModelBase  from django.db.models.signals import post_save, pre_delete -from django.utils.translation import ugettext_lazy as _, ugettext +from django.utils.translation import ugettext_lazy as _, ugettext, \ +    pgettext_lazy +  from django.utils.safestring import SafeUnicode, mark_safe  from django.template.defaultfilters import slugify @@ -57,7 +59,7 @@ from ishtar_common.model_merging import merge_model_objects  from ishtar_common.utils import get_cache  from ishtar_common.data_importer import Importer, ImportFormater, \      IntegerFormater, FloatFormater, UnicodeFormater, DateFormater, \ -    TypeFormater, YearFormater, StrToBoolean +    TypeFormater, YearFormater, StrToBoolean, FileFormater  def post_save_user(sender, **kwargs): @@ -1444,6 +1446,7 @@ IMPORTER_TYPES = (      ('TypeFormater', _(u"Type")),      ('YearFormater', _(u"Year")),      ('StrToBoolean', _(u"String to boolean")), +    ('FileFormater', pgettext_lazy("filesystem", u"File")),  )  IMPORTER_TYPES_DCT = { @@ -1454,6 +1457,7 @@ IMPORTER_TYPES_DCT = {      'TypeFormater': TypeFormater,      'YearFormater': YearFormater,      'StrToBoolean': StrToBoolean, +    'FileFormater': FileFormater  }  DATE_FORMATS = ( | 
