summaryrefslogtreecommitdiff
path: root/ishtar_common/models.py
diff options
context:
space:
mode:
Diffstat (limited to 'ishtar_common/models.py')
-rw-r--r--ishtar_common/models.py78
1 files changed, 44 insertions, 34 deletions
diff --git a/ishtar_common/models.py b/ishtar_common/models.py
index 15c8fe5da..c9903525d 100644
--- a/ishtar_common/models.py
+++ b/ishtar_common/models.py
@@ -35,7 +35,8 @@ import zipfile
from django.conf import settings
from django.core.cache import cache
-from django.core.exceptions import ObjectDoesNotExist, ValidationError
+from django.core.exceptions import ObjectDoesNotExist, ValidationError, \
+ SuspiciousOperation
from django.core.files import File
from django.core.files.uploadedfile import SimpleUploadedFile
from django.core.validators import validate_slug
@@ -665,8 +666,9 @@ class GeneralType(Cached, models.Model):
def get_keys(self):
keys = [self.txt_idx]
content_type = ContentType.objects.get_for_model(self.__class__)
- for ik in ItemKey.objects.filter(content_type=content_type,
- object_id=self.pk).all():
+ for ik in ItemKey.objects.filter(
+ content_type=content_type, object_id=self.pk).exclude(
+ key=self.txt_idx).all():
keys.append(ik.key)
return keys
@@ -1707,28 +1709,6 @@ IMPORTER_CLASSES.update({
'archaeological_files.data_importer.FileImporterSraPdL'})
-def get_importer_models():
- MODELS = [
- ('ishtar_common.models.Person', _(u"Person")),
- ('ishtar_common.models.Organization', _(u"Organization")),
- ('archaeological_operations.models.Operation', _(u"Operation")),
- ('archaeological_operations.models.ArchaeologicalSite',
- _(u"Archaeological site")),
- ('archaeological_operations.models.Parcel', _(u"Parcels")),
- ('archaeological_operations.models.OperationSource',
- _(u"Operation source")),
- ]
- MODELS = [('archaeological_files.models.File',
- _(u"Archaeological files"))] + MODELS
- MODELS = [('archaeological_context_records.models.ContextRecord',
- _(u"Context records")),
- ('archaeological_context_records.models.RecordRelations',
- _(u"Context record relations"))] + MODELS
- MODELS = [('archaeological_finds.models.BaseFind',
- _(u"Base finds")), ] + MODELS
- return MODELS
-
-
def get_model_fields(model):
"""
Return a dict of fields from model
@@ -1744,13 +1724,33 @@ def get_model_fields(model):
def import_class(full_path_classname):
+ """
+ Return the model class from the full path
+ TODO: add a white list for more security
+ """
mods = full_path_classname.split('.')
if len(mods) == 1:
mods = ['ishtar_common', 'models', mods[0]]
+ elif 'models' not in mods:
+ raise SuspiciousOperation(
+ u"Try to import a non model from a string")
module = import_module('.'.join(mods[:-1]))
return getattr(module, mods[-1])
+class ImporterModel(models.Model):
+ name = models.CharField(_(u"Name"), max_length=200)
+ klass = models.CharField(_(u"Class name"), max_length=200)
+
+ class Meta:
+ verbose_name = _(u"Importer - Model")
+ verbose_name_plural = _(u"Importer - Models")
+ ordering = ('name',)
+
+ def __unicode__(self):
+ return self.name
+
+
class ImporterType(models.Model):
"""
Description of a table to be mapped with ishtar database
@@ -1763,9 +1763,13 @@ class ImporterType(models.Model):
max_length=500)
users = models.ManyToManyField('IshtarUser', verbose_name=_(u"Users"),
blank=True, null=True)
- associated_models = models.CharField(_(u"Associated model"),
- max_length=200,
- choices=get_importer_models())
+ associated_models = models.ForeignKey(
+ ImporterModel, verbose_name=_(u"Associated model"),
+ related_name='+', blank=True, null=True)
+ created_models = models.ManyToManyField(
+ ImporterModel, verbose_name=_(u"Models that can accept new items"),
+ blank=True, null=True, help_text=_(u"Leave blank for no restrictions"),
+ related_name='+')
is_template = models.BooleanField(_(u"Is template"), default=False)
unicity_keys = models.CharField(_(u"Unicity keys (separator \";\")"),
blank=True, null=True, max_length=500)
@@ -1781,7 +1785,7 @@ class ImporterType(models.Model):
if self.slug and self.slug in IMPORTER_CLASSES:
cls = import_class(IMPORTER_CLASSES[self.slug])
return cls
- OBJECT_CLS = import_class(self.associated_models)
+ OBJECT_CLS = import_class(self.associated_models.klass)
DEFAULTS = dict([(default.keys, default.values)
for default in self.defaults.all()])
LINE_FORMAT = []
@@ -1824,9 +1828,13 @@ class ImporterType(models.Model):
UNICITY_KEYS = []
if self.unicity_keys:
UNICITY_KEYS = [un.strip() for un in self.unicity_keys.split(';')]
+ MODEL_CREATION_LIMIT = []
+ for modls in self.created_models.all():
+ MODEL_CREATION_LIMIT.append(import_class(modls.klass))
args = {'OBJECT_CLS': OBJECT_CLS, 'DESC': self.description,
'DEFAULTS': DEFAULTS, 'LINE_FORMAT': LINE_FORMAT,
- 'UNICITY_KEYS': UNICITY_KEYS}
+ 'UNICITY_KEYS': UNICITY_KEYS,
+ 'MODEL_CREATION_LIMIT': MODEL_CREATION_LIMIT}
name = str(''.join(
x for x in slugify(self.name).replace('-', ' ').title()
if not x.isspace()))
@@ -1836,7 +1844,6 @@ class ImporterType(models.Model):
def get_associated_model(parent_model, keys):
model = None
- OBJECT_CLS = None
if isinstance(parent_model, unicode) or \
isinstance(parent_model, str):
OBJECT_CLS = import_class(parent_model)
@@ -1874,7 +1881,7 @@ class ImporterDefault(models.Model):
@property
def associated_model(self):
- return get_associated_model(self.importer_type.associated_models,
+ return get_associated_model(self.importer_type.associated_models.klass,
self.keys)
@property
@@ -2014,7 +2021,7 @@ class ImportTarget(models.Model):
def associated_model(self):
try:
return get_associated_model(
- self.column.importer_type.associated_models,
+ self.column.importer_type.associated_models.klass,
self.target.split('__'))
except KeyError:
return
@@ -2221,7 +2228,10 @@ class FormaterType(models.Model):
pass
return UnicodeFormater(**kwargs)
elif self.formater_type == 'DateFormater':
- return DateFormater(self.options, **kwargs)
+ date_formats = self.options
+ if self.many_split:
+ date_formats = self.options.split(kwargs.pop('many_split'))
+ return DateFormater(date_formats, **kwargs)
elif self.formater_type == 'StrToBoolean':
return StrToBoolean(**kwargs)
elif self.formater_type == 'UnknowType':