diff options
author | Étienne Loks <etienne.loks@proxience.com> | 2015-05-04 01:10:40 +0200 |
---|---|---|
committer | Étienne Loks <etienne.loks@proxience.com> | 2015-05-04 01:10:40 +0200 |
commit | bdc9cdab0d5e314b5d1a33a2d87fca22138d649d (patch) | |
tree | dcfa2689395822cf1475deb1a6107bebb6006701 /ishtar_common/models.py | |
parent | 274b8d44ccf1f099f2e22b5a6a70f9743b746c1d (diff) | |
download | Ishtar-bdc9cdab0d5e314b5d1a33a2d87fca22138d649d.tar.bz2 Ishtar-bdc9cdab0d5e314b5d1a33a2d87fca22138d649d.zip |
Imports: manage unmatched item links
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r-- | ishtar_common/models.py | 42 |
1 files changed, 28 insertions, 14 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py index b43d3a50c..24b64bec0 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -973,7 +973,7 @@ def get_model_fields(model): """ fields = {} options = model._meta - for field in sorted(options.fields): + for field in sorted(options.fields + options.many_to_many): fields[field.name] = field return fields @@ -1041,6 +1041,17 @@ class ImporterType(models.Model): newclass = type(name, (Importer,), args) return newclass +def get_associated_model(parent_model, keys): + field = None + OBJECT_CLS = import_class(parent_model) + for idx, item in enumerate(keys): + if not idx: + field = get_model_fields(OBJECT_CLS)[item] + else: + raise NotImplemented() + if hasattr(field, 'rel') and hasattr(field.rel, 'to'): + return field.rel.to + class ImporterDefault(models.Model): """ Targets of default values in an import @@ -1057,15 +1068,7 @@ class ImporterDefault(models.Model): @property def associated_model(self): - field = None - OBJECT_CLS = import_class(self.importer_type.associated_models) - for idx, item in enumerate(self.keys): - if not idx: - field = get_model_fields(OBJECT_CLS)[item] - else: - raise NotImplemented() - if hasattr(field, 'rel') and hasattr(field.rel, 'to'): - return field.rel.to + return get_associated_model(self.importer_type.associated_models, self.keys) @property def values(self): @@ -1157,7 +1160,17 @@ class ImportTarget(models.Model): verbose_name_plural = _(u"Importer - Targets") def __unicode__(self): - return u" - ".join([unicode(self.column), self.target[:50]]) + return self.target[:50] + + @property + def associated_model(self): + return get_associated_model(self.column.importer_type.associated_models, + self.target.split('__')) + + def get_choices(self): + if not self.associated_model or not hasattr(self.associated_model, 'get_types'): + return [] + return self.associated_model.get_types() class TargetKey(models.Model): """ @@ -1298,6 +1311,10 @@ class Import(models.Model): return u"%s - %s" % (unicode(self.importer_type), unicode(self.user)) + def need_matching(self): + return bool(ImporterType.objects.filter(pk=self.importer_type.pk, + columns__targets__keys__is_set=False).count()) + def get_actions(self): """ Get available action relevant with the current status @@ -1306,9 +1323,6 @@ class Import(models.Model): if self.state == 'C': actions.append(('A', _(u"Analyse"))) if self.state == 'A': - if ImporterType.objects.filter(pk=self.importer_type.pk, - columns__targets__keys__is_set=False).count(): - actions.append(('L', _(u"Link unmatched items"))) actions.append(('A', _(u"Re-analyse"))) actions.append(('I', _(u"Launch import"))) actions.append(('D', _(u"Delete"))) |