diff options
Diffstat (limited to 'chimere/models.py')
-rw-r--r-- | chimere/models.py | 78 |
1 files changed, 52 insertions, 26 deletions
diff --git a/chimere/models.py b/chimere/models.py index 6661a30..e6d1636 100644 --- a/chimere/models.py +++ b/chimere/models.py @@ -42,7 +42,7 @@ from chimere.widgets import PointField, RouteField, SelectMultipleField, \ TextareaWidget, DatePickerWidget from chimere.managers import BaseGeoManager from chimere.utils import KMLManager, OSMManager, ShapefileManager, \ - GeoRSSManager + GeoRSSManager, CSVManager class News(models.Model): """News of the site @@ -206,13 +206,15 @@ class SubCategory(models.Model): IMPORTERS = {'KML':KMLManager, 'OSM':OSMManager, 'SHP':ShapefileManager, - 'RSS':GeoRSSManager + 'RSS':GeoRSSManager, + 'CSV':CSVManager } IMPORTER_CHOICES = (('KML', 'KML'), ('OSM', 'OSM'), ('SHP', 'Shapefile'), - ('RSS', 'GeoRSS') + ('RSS', 'GeoRSS'), + ('CSV', 'CSV') ) IMPORTER_CHOICES_DICT = dict(IMPORTER_CHOICES) @@ -226,22 +228,22 @@ class Importer(models.Model): # URL of a KML file or a XAPI service for OSM source = models.CharField(_(u"Source"), max_length=200, blank=True, null=True) + source_file = models.FileField(_(u"Source file"), + upload_to='import_files', blank=True, null=True) filtr = models.CharField(_(u"Filter"), max_length=200, blank=True, null=True) default_name = models.CharField(_(u"Name by default"), max_length=200, blank=True, null=True) - categories = SelectMultipleField(SubCategory, - verbose_name=_(u"Associated subcategories")) - state = models.CharField(_(u"State"), max_length=200, - blank=True, null=True) srid = models.IntegerField(_(u"SRID"), blank=True, null=True) zipped = models.BooleanField(_(u"Zipped file"), default=False) - source_file = models.FileField(_(u"Source file"), - upload_to='import_files', blank=True, null=True) origin = models.CharField(_(u"Origin"), max_length=100, blank=True, null=True) license = models.CharField(_(u"License"), max_length=100, blank=True, null=True) + categories = SelectMultipleField(SubCategory, + verbose_name=_(u"Associated subcategories")) + state = models.CharField(_(u"State"), max_length=200, + blank=True, null=True) class Meta: verbose_name = _(u"Importer") @@ -331,6 +333,16 @@ class GeographicItem(models.Model): def properties(cls): return [pm for pm in PropertyModel.objects.filter(available=True)] +def property_setter(cls, propertymodel): + def setter(self, value): + marker = self + if cls == Route: + if not self.associated_marker.objects.count(): + return + marker = self.associated_marker.objects.all()[0] + marker.setProperty(propertymodel, value) + return setter + class Marker(GeographicItem): '''Marker for a POI ''' @@ -359,6 +371,9 @@ class Marker(GeographicItem): if property: val = property.python_value setattr(self, attr_name, val) + if not hasattr(self, attr_name + '_set'): + setattr(self, attr_name + '_set', + property_setter(self.__class__, pm)) def get_init_multi(self): multis = [forms.model_to_dict(multi) @@ -432,31 +447,37 @@ class Marker(GeographicItem): properties.append(property) return properties + def setProperty(self, pm, value): + u""" + Set a property + """ + properties = Property.objects.filter(marker=self, + propertymodel=pm).all() + # in case of multiple edition as the same time delete arbitrary + # the others + if len(properties) > 1: + for property in properties[1:]: + property.delete() + # new property + if not properties: + new_property = Property.objects.create(marker=self, + propertymodel=pm, + value=value) + new_property.save() + else: + property = properties[0] + property.value = value + property.save() + def saveProperties(self, values): """ Save properties """ for propertymodel in PropertyModel.objects.filter(available=True): - properties = Property.objects.filter(marker=self, - propertymodel=propertymodel).all() - # in case of multiple edition as the same time delete arbitrary - # the others - if len(properties) > 1: - for property in properties[1:]: - property.delete() val = u"" if unicode(propertymodel.id) in values: val = values[unicode(propertymodel.id)] - # new property - if not properties: - new_property = Property.objects.create(marker=self, - propertymodel=propertymodel, - value=val) - new_property.save() - else: - property = properties[0] - property.value = val - property.save() + self.setProperty(propertymodel, val) def getGeoJSON(self, categories_id=[]): '''Return a GeoJSON string @@ -824,8 +845,10 @@ class Route(GeographicItem): def __init__(self, *args, **kwargs): super(Route, self).__init__(*args, **kwargs) + self.description = '' try: associated_marker = Marker.objects.get(route=self) + self.description = associated_marker.description except: associated_marker = None # add read attributes for properties @@ -838,6 +861,9 @@ class Route(GeographicItem): if property: val = property.python_value setattr(self, attr_name, val) + if not hasattr(self, attr_name + '_set'): + setattr(self, attr_name + '_set', + property_setter(self.__class__, pm)) @property def geometry(self): |