def __init__(self, progress_cb=None, user=None, encoding='utf8'): self.warnings = {} self.line = 0 self.nb_success = 0 self.nb_created = 0 self.nb_updated = 0 self.nb_unmodified = 0 self.progress_cb = progress_cb self.user = user self.structure = user and user.profile.structure or default_structure() self.encoding = encoding try: mto = translator.get_options_for_model(self.model) except NotRegistered: self.translated_fields = [] else: self.translated_fields = mto.fields.keys() if self.fields is None: self.fields = { f.name: force_str(f.verbose_name) for f in self.model._meta.fields if not isinstance(f, TranslationField) } self.m2m_fields = { f.name: force_str(f.verbose_name) for f in self.model._meta.many_to_many }
def get_good_data(self): return { 'name': 'test', 'structure': default_structure().pk, 'stake': '', 'type': '', 'domain': '', 'begin_year': '2010', 'end_year': '2012', 'constraints': '', 'cost': '12', 'comments': '', 'contractors': ContractorFactory.create().pk, 'project_owner': OrganismFactory.create().pk, 'project_manager': OrganismFactory.create().pk, 'funding_set-TOTAL_FORMS': '2', 'funding_set-INITIAL_FORMS': '0', 'funding_set-MAX_NUM_FORMS': '', 'funding_set-0-amount': '468.0', 'funding_set-0-organism': OrganismFactory.create().pk, 'funding_set-0-project': '', 'funding_set-0-id': '', 'funding_set-0-DELETE': '', 'funding_set-1-amount': '789', 'funding_set-1-organism': OrganismFactory.create().pk, 'funding_set-1-project': '', 'funding_set-1-id': '', 'funding_set-1-DELETE': '' }
def get_good_data(self): return { 'name': '', 'structure': default_structure().pk, 'stake': '', 'comfort': ComfortFactory.create().pk, 'trail': '', 'comments': '', 'departure': '', 'arrival': '', 'datasource': '', 'valid': 'on', 'geom': '{"geom": "LINESTRING (99.0 89.0, 100.0 88.0)", "snap": [null, null]}', }
def get_good_data(self): return { 'name_fr': u'test', 'category': TouristicContentCategoryFactory.create().pk, 'structure': default_structure().pk, 'geom': '{"type": "Point", "coordinates":[0, 0]}', }
def __init__(self, *args, **kwargs): super(CommonForm, self).__init__(*args, **kwargs) # Check if structure is present, if so, use hidden input if 'structure' in self.fields: self.fields['structure'].widget = forms.HiddenInput() # On entity creation, use user's structure if not self.instance or not self.instance.pk: structure = default_structure() if self.user: structure = self.user.profile.structure self.fields['structure'].initial = structure for name, field in self.fields.items(): if isinstance(field, django_forms.models.ModelChoiceField): try: modelfield = self.instance._meta.get_field(name) except FieldDoesNotExist: # be careful but custom form fields, not in model modelfield = None if isinstance(modelfield, (ForeignKey, ManyToManyField)): model = modelfield.related.parent_model # Filter structured choice fields according to user's structure if issubclass(model, StructureRelated): field.queryset = StructureRelatedQuerySet.queryset_for_user(field.queryset, self.user) if issubclass(model, NoDeleteMixin): field.queryset = field.queryset.filter(deleted=False)
def get_good_data(self): return { 'name': u'Test', 'structure': default_structure().pk, 'practices': [SportPracticeFactory.create().pk], 'geom': '{"type": "Polygon", "coordinates":[[[0, 0], [0, 1], [1, 0], [0, 0]]]}', }
def __init__(self, progress_cb=None, user=None, encoding='utf8'): self.warnings = {} self.line = 0 self.nb_success = 0 self.nb_created = 0 self.nb_updated = 0 self.nb_unmodified = 0 self.progress_cb = progress_cb self.user = user self.structure = user and user.profile.structure or default_structure() self.encoding = encoding try: mto = translator.get_options_for_model(self.model) except NotRegistered: self.translated_fields = [] else: self.translated_fields = mto.fields.keys() if self.fields is None: self.fields = { f.name: force_text(f.verbose_name) for f in self.model._meta.fields if not isinstance(f, TranslationField) } self.m2m_fields = { f.name: force_text(f.verbose_name) for f in self.model._meta.many_to_many }
def __init__(self, *args, **kwargs): super(CommonForm, self).__init__(*args, **kwargs) # Check if structure is present, if so, use hidden input if 'structure' in self.fields: self.fields['structure'].widget = forms.HiddenInput() # On entity creation, use user's structure if not self.instance or not self.instance.pk: structure = default_structure() if self.user: structure = self.user.profile.structure self.fields['structure'].initial = structure for name, field in self.fields.items(): if isinstance(field, django_forms.models.ModelChoiceField): try: modelfield = self.instance._meta.get_field(name) except FieldDoesNotExist: # be careful but custom form fields, not in model modelfield = None if isinstance(modelfield, (ForeignKey, ManyToManyField)): model = modelfield.related.parent_model # Filter structured choice fields according to user's structure if issubclass(model, StructureRelated): field.queryset = StructureRelatedQuerySet.queryset_for_user( field.queryset, self.user) if issubclass(model, NoDeleteMixin): field.queryset = field.queryset.filter(deleted=False)
def __init__(self, *args, **kwargs): super(CommonForm, self).__init__(*args, **kwargs) # Check if structure is present, if so, use hidden input if 'structure' in self.fields: self.fields['structure'].widget = forms.HiddenInput() # On entity creation, use user's structure if not self.instance or not self.instance.pk: structure = default_structure() if self.user: structure = self.user.profile.structure self.fields['structure'].initial = structure for name, field in self.fields.items(): self.filter_related_field(name, field) # allow to modify layout per instance self.helper.fieldlayout = deepcopy(self.fieldslayout) model = self._meta.model codeperm = '%s.publish_%s' % (model._meta.app_label, model._meta.object_name.lower()) if 'published' in self.fields and self.user and not self.user.has_perm( codeperm): self.deep_remove(self.helper.fieldslayout, 'published') if 'review' in self.fields and self.instance and self.instance.any_published: self.deep_remove(self.helper.fieldslayout, 'review')
def get_good_data(self): return { 'name': 'test', 'structure': default_structure().pk, 'stake': '', 'type': '', 'domain': '', 'begin_year': '2010', 'end_year': '2012', 'constraints': '', 'global_cost': '12', 'comments': '', 'contractors': ContractorFactory.create().pk, 'project_owner': OrganismFactory.create().pk, 'project_manager': OrganismFactory.create().pk, 'funding_set-TOTAL_FORMS': '2', 'funding_set-INITIAL_FORMS': '0', 'funding_set-MAX_NUM_FORMS': '', 'funding_set-0-amount': '468.0', 'funding_set-0-organism': OrganismFactory.create().pk, 'funding_set-0-project': '', 'funding_set-0-id': '', 'funding_set-0-DELETE': '', 'funding_set-1-amount': '789', 'funding_set-1-organism': OrganismFactory.create().pk, 'funding_set-1-project': '', 'funding_set-1-id': '', 'funding_set-1-DELETE': '' }
def get_good_data(self): InterventionStatusFactory.create() # in case not any in db path = PathFactory.create() return { "name": "test", "date": "2012-08-23", "structure": default_structure().pk, "disorders": InterventionDisorderFactory.create().pk, "comments": "", "slope": 0, "area": 0, "subcontract_cost": 0.0, "stake": StakeFactory.create().pk, "height": 0.0, "project": "", "width": 0.0, "length": 0.0, "status": InterventionStatus.objects.all()[0].pk, "heliport_cost": 0.0, "material_cost": 0.0, "topology": '{"paths": [%s]}' % path.pk, "manday_set-TOTAL_FORMS": "2", "manday_set-INITIAL_FORMS": "0", "manday_set-MAX_NUM_FORMS": "", "manday_set-0-nb_days": "48.75", "manday_set-0-job": InterventionJobFactory.create().pk, "manday_set-0-id": "", "manday_set-0-DELETE": "", "manday_set-1-nb_days": "12", "manday_set-1-job": InterventionJobFactory.create().pk, "manday_set-1-id": "", "manday_set-1-DELETE": "", }
def get_good_data(self): return { "name": "test", "structure": default_structure().pk, "stake": "", "type": "", "domain": "", "begin_year": "2010", "end_year": "2012", "constraints": "", "cost": "12", "comments": "", "contractors": ContractorFactory.create().pk, "project_owner": OrganismFactory.create().pk, "project_manager": OrganismFactory.create().pk, "funding_set-TOTAL_FORMS": "2", "funding_set-INITIAL_FORMS": "0", "funding_set-MAX_NUM_FORMS": "", "funding_set-0-amount": "468.0", "funding_set-0-organism": OrganismFactory.create().pk, "funding_set-0-project": "", "funding_set-0-id": "", "funding_set-0-DELETE": "", "funding_set-1-amount": "789", "funding_set-1-organism": OrganismFactory.create().pk, "funding_set-1-project": "", "funding_set-1-id": "", "funding_set-1-DELETE": "", }
def get_good_data(self): return { 'name_fr': u'test', 'category': TouristicContentCategoryFactory.create().pk, 'structure': default_structure().pk, 'geom': '{"type": "Point", "coordinates":[0, 0]}', }
def get_good_data(self): InterventionStatusFactory.create() # in case not any in db path = PathFactory.create() return { 'name': 'test', 'date': '2012-08-23', 'structure': default_structure().pk, 'disorders': InterventionDisorderFactory.create().pk, 'comments': '', 'slope': 0, 'area': 0, 'subcontract_cost': 0.0, 'stake': StakeFactory.create().pk, 'height': 0.0, 'project': '', 'width': 0.0, 'length': 0.0, 'status': InterventionStatus.objects.all()[0].pk, 'heliport_cost': 0.0, 'material_cost': 0.0, 'topology': '{"paths": [%s]}' % path.pk, 'manday_set-TOTAL_FORMS': '2', 'manday_set-INITIAL_FORMS': '0', 'manday_set-MAX_NUM_FORMS': '', 'manday_set-0-nb_days': '48.75', 'manday_set-0-job': InterventionJobFactory.create().pk, 'manday_set-0-id': '', 'manday_set-0-DELETE': '', 'manday_set-1-nb_days': '12', 'manday_set-1-job': InterventionJobFactory.create().pk, 'manday_set-1-id': '', 'manday_set-1-DELETE': '', }
def get_good_data(self): path = PathFactory.create() return { "name": "test", "description": "oh", "structure": default_structure().pk, "type": InfrastructureTypeFactory.create(type=INFRASTRUCTURE_TYPES.BUILDING).pk, "topology": '{"paths": [%s]}' % path.pk, }
def get_good_data(self): PathFactory.create() return { "name": "test", "description": "oh", "structure": default_structure().pk, "type": InfrastructureTypeFactory.create(type=INFRASTRUCTURE_TYPES.BUILDING).pk, "topology": '{"lat": 0.42, "lng": 0.666}', }
def get_good_data(self): path = PathFactory.create() return { 'name': 'test', 'description': 'oh', 'structure': default_structure().pk, 'type': InfrastructureTypeFactory.create(type=INFRASTRUCTURE_TYPES.BUILDING).pk, 'topology': '{"paths": [%s]}' % path.pk, }
def get_good_data(self): PathFactory.create() return { 'name': 'test', 'description': 'oh', 'structure': default_structure().pk, 'type': InfrastructureTypeFactory.create(type=INFRASTRUCTURE_TYPES.BUILDING).pk, 'topology': '{"lat": 0.42, "lng": 0.666}' }
def get_good_data(self): path = PathFactory.create() return { 'name': 'test', 'description': 'oh', 'structure': default_structure().pk, 'type': InfrastructureTypeFactory.create(type=INFRASTRUCTURE_TYPES.BUILDING).pk, 'topology': '{"paths": [%s]}' % path.pk, }
def get_good_data(self): PathFactory.create() return { 'name': 'test', 'description': 'oh', 'structure': default_structure().pk, 'type': InfrastructureTypeFactory.create(type=INFRASTRUCTURE_TYPES.BUILDING).pk, 'topology': '{"lat": 0.42, "lng": 0.666}' }
def forwards(self, orm): # Adding field 'Organism.structure' db.add_column('m_b_organisme', 'structure', self.gf('django.db.models.fields.related.ForeignKey')( to=orm['authent.Structure'], db_column='structure', default=default_structure().pk), keep_default=False)
def start(self): super(AttachmentParserMixin, self).start() if settings.PAPERCLIP_ENABLE_LINK is False and self.download_attachments is False: raise Exception(u'You need to enable PAPERCLIP_ENABLE_LINK to use this function') try: self.filetype = FileType.objects.get(type=self.filetype_name, structure=default_structure()) except FileType.DoesNotExist: raise GlobalImportError(_(u"FileType '{name}' does not exists in Geotrek-Admin. Please add it").format(name=self.filetype_name)) self.creator, created = get_user_model().objects.get_or_create(username='******', defaults={'is_active': False})
def get_good_data(self): path = PathFactory.create() return { 'name': 't', 'departure': 'Below', 'arrival': 'Above', 'comments': 'No comment', 'structure': default_structure().pk, 'topology': '{"paths": [%s]}' % path.pk, }
def get_good_data(self): path = PathFactory.create() return { 'name': 't', 'departure': 'Below', 'arrival': 'Above', 'comments': 'No comment', 'structure': default_structure().pk, 'topology': '{"paths": [%s]}' % path.pk, }
def start(self): super(AttachmentParserMixin, self).start() try: self.filetype = FileType.objects.get(type=self.filetype_name, structure=default_structure()) except FileType.DoesNotExist: raise GlobalImportError( _(u"FileType '{name}' does not exists in Geotrek-Admin. Please add it" ).format(name=self.filetype_name)) self.creator, created = get_user_model().objects.get_or_create( username='******', defaults={'is_active': False})
def save(self, commit=True): """Set structure field before saving if need be""" if self.update: # Structure is already set on object. pass elif not hasattr(self.instance, 'structure'): pass elif 'structure' in self.fields: pass # The form contains the structure field. Let django use its value. elif self.user: self.instance.structure = self.user.profile.structure else: self.instance.structure = default_structure() return super(CommonForm, self).save(commit)
def save(self, commit=True): """Set structure field before saving if need be""" if self.update: # Structure is already set on object. pass elif not hasattr(self.instance, 'structure'): pass elif 'structure' in self.fields: pass # The form contains the structure field. Let django use its value. elif self.user: self.instance.structure = self.user.profile.structure else: self.instance.structure = default_structure() return super(CommonForm, self).save(commit)
def get_good_data(self): return { 'name': '', 'structure': default_structure().pk, 'stake': '', 'comfort': ComfortFactory.create().pk, 'trail': '', 'comments': '', 'departure': '', 'arrival': '', 'datasource': '', 'valid': 'on', 'geom': '{"geom": "LINESTRING (99.0 89.0, 100.0 88.0)", "snap": [null, null]}', }
def __init__(self, *args, **kwargs): super(CommonForm, self).__init__(*args, **kwargs) # Check if structure is present, if so, use hidden input if 'structure' in self.fields: self.fields['structure'].widget = forms.HiddenInput() # On entity creation, use user's structure if not self.instance or not self.instance.pk: structure = default_structure() if self.user: structure = self.user.profile.structure self.fields['structure'].initial = structure for name, field in self.fields.items(): self.filter_related_field(name, field)
def __init__(self, *args, **kwargs): super(CommonForm, self).__init__(*args, **kwargs) # Check if structure is present, if so, use hidden input if 'structure' in self.fields: self.fields['structure'].widget = forms.HiddenInput() # On entity creation, use user's structure if not self.instance or not self.instance.pk: structure = default_structure() if self.user: structure = self.user.profile.structure self.fields['structure'].initial = structure for name, field in self.fields.items(): self.filter_related_field(name, field)
def forwards(self, orm): exists = 'fl_b_fichier' in connection.introspection.table_names() if exists: # Table exists, just add column. db.add_column( 'fl_b_fichier', 'structure', self.gf('django.db.models.fields.related.ForeignKey')( to=orm['authent.Structure'], db_column='structure', default=default_structure().pk), keep_default=False) else: db.create_table('fl_b_fichier', ( ('id', self.gf('django.db.models.fields.AutoField')( primary_key=True)), ('type', self.gf('django.db.models.fields.CharField')(max_length=128)), ('structure', self.gf('django.db.models.fields.related.ForeignKey')( to=orm['authent.Structure'], db_column='structure')), )) db.send_create_signal('common', ['FileType'])
def __init__(self, *args, **kwargs): super(CommonForm, self).__init__(*args, **kwargs) # Check if structure is present, if so, use hidden input if 'structure' in self.fields: self.fields['structure'].widget = forms.HiddenInput() # On entity creation, use user's structure if not self.instance or not self.instance.pk: structure = default_structure() if self.user: structure = self.user.profile.structure self.fields['structure'].initial = structure for name, field in self.fields.items(): self.filter_related_field(name, field) # allow to modify layout per instance self.helper.fieldlayout = deepcopy(self.fieldslayout) model = self._meta.model codeperm = '%s.publish_%s' % (model._meta.app_label, model._meta.object_name.lower()) if 'published' in self.fields and self.user and not self.user.has_perm(codeperm): self.deep_remove(self.helper.fieldslayout, 'published') if 'review' in self.fields and self.instance and self.instance.any_published: self.deep_remove(self.helper.fieldslayout, 'review')
def get_good_data(self): InterventionStatusFactory.create() # in case not any in db path = PathFactory.create() return { 'name': 'test', 'date': '2012-08-23', 'structure': default_structure().pk, 'disorders': InterventionDisorderFactory.create().pk, 'comments': '', 'slope': 0, 'area': 0, 'subcontract_cost': 0.0, 'stake': StakeFactory.create().pk, 'height': 0.0, 'project': '', 'width': 0.0, 'length': 0.0, 'status': InterventionStatus.objects.all()[0].pk, 'heliport_cost': 0.0, 'material_cost': 0.0, 'topology': '{"paths": [%s]}' % path.pk, 'manday_set-TOTAL_FORMS': '2', 'manday_set-INITIAL_FORMS': '0', 'manday_set-MAX_NUM_FORMS': '', 'manday_set-0-nb_days': '48.75', 'manday_set-0-job': InterventionJobFactory.create().pk, 'manday_set-0-id': '', 'manday_set-0-DELETE': '', 'manday_set-1-nb_days': '12', 'manday_set-1-job': InterventionJobFactory.create().pk, 'manday_set-1-id': '', 'manday_set-1-DELETE': '', }
def handle(self, *args, **options): verbosity = options.get('verbosity') try: from osgeo import gdal, ogr, osr # NOQA except ImportError: raise CommandError('GDAL Python bindings are not available. Can not proceed.') filename = options['point_layer'] if not os.path.exists(filename): raise CommandError('File does not exists at: %s' % filename) data_source = DataSource(filename, encoding=options.get('encoding')) field_name = options.get('name_field') field_infrastructure_type = options.get('type_field') field_condition_type = options.get('condition_field') field_structure_type = options.get('structure_field') field_description = options.get('description_field') field_implantation_year = options.get('year_field') field_eid = options.get('eid_field') sid = transaction.savepoint() structure_default = options.get('structure_default') try: for layer in data_source: if verbosity >= 2: self.stdout.write("- Layer '{}' with {} objects found".format(layer.name, layer.num_feat)) available_fields = layer.fields if (field_infrastructure_type and field_infrastructure_type not in available_fields)\ or (not field_infrastructure_type and not options.get('type_default')): self.stdout.write(self.style.ERROR( "Field '{}' not found in data source.".format(field_infrastructure_type))) self.stdout.write(self.style.ERROR( u"Set it with --type-field, or set a default value with --type-default")) break if (field_name and field_name not in available_fields)\ or (not field_name and not options.get('name_default')): self.stdout.write(self.style.ERROR( "Field '{}' not found in data source.".format(field_name))) self.stdout.write(self.style.ERROR( u"Set it with --name-field, or set a default value with --name-default")) break if field_condition_type and field_condition_type not in available_fields: self.stdout.write(self.style.ERROR( "Field '{}' not found in data source.".format(field_condition_type))) self.stdout.write(self.style.ERROR( u"Change your --condition-field option")) break if field_structure_type and field_structure_type not in available_fields: self.stdout.write(self.style.ERROR( "Field '{}' not found in data source.".format(field_structure_type))) self.stdout.write(self.style.ERROR( u"Change your --structure-field option")) break elif not field_structure_type and not structure_default: structure = default_structure() else: try: structure = Structure.objects.get(name=structure_default) if verbosity > 0: self.stdout.write(u"Signages will be linked to {}".format(structure)) except Structure.DoesNotExist: self.stdout.write(u"Structure {} set in options doesn't exist".format(structure_default)) break if field_description and field_description not in available_fields: self.stdout.write(self.style.ERROR( "Field '{}' not found in data source.".format(field_description))) self.stdout.write(self.style.ERROR( u"Change your --description-field option")) break if field_implantation_year and field_implantation_year not in available_fields: self.stdout.write( self.style.ERROR("Field '{}' not found in data source.".format(field_implantation_year))) self.stdout.write(self.style.ERROR( u"Change your --year-field option")) break if field_eid and field_eid not in available_fields: self.stdout.write( self.style.ERROR("Field '{}' not found in data source.".format(field_eid))) self.stdout.write(self.style.ERROR( u"Change your --eid-field option")) break for feature in layer: feature_geom = feature.geom.transform(settings.API_SRID, clone=True) feature_geom.coord_dim = 2 name = feature.get(field_name) if field_name in available_fields else options.get('name_default') type = feature.get( field_infrastructure_type) if field_infrastructure_type in available_fields else options.get( 'type_default') if field_condition_type in available_fields: condition = feature.get(field_condition_type) else: condition = options.get('condition_default') structure = Structure.objects.get(name=feature.get(field_structure_type)) \ if field_structure_type in available_fields else structure description = feature.get( field_description) if field_description in available_fields else options.get( 'description_default') year = int(feature.get( field_implantation_year)) if field_implantation_year in available_fields and feature.get( field_implantation_year).isdigit() else options.get('year_default') eid = feature.get(field_eid) if field_eid in available_fields else None self.create_infrastructure(feature_geom, name, type, condition, structure, description, year, verbosity, eid) transaction.savepoint_commit(sid) if verbosity >= 2: self.stdout.write(self.style.NOTICE(u"{} objects created.".format(self.counter))) except Exception: self.stdout.write(self.style.ERROR(u"An error occured, rolling back operations.")) transaction.savepoint_rollback(sid) raise
def get_good_data(self): return { 'species': SpeciesFactory.create().pk, 'structure': default_structure().pk, 'geom': '{"type": "Polygon", "coordinates":[[[0, 0], [0, 1], [1, 0], [0, 0]]]}', }
def forwards(self, orm): # Adding field 'PhysicalType.structure' db.add_column('f_b_nature', 'structure', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['authent.Structure'], db_column='structure', default=default_structure().pk), keep_default=False)
def forwards(self, orm): exists = 'fl_b_fichier' in connection.introspection.table_names() if exists: # Table exists, just add column. db.add_column('fl_b_fichier', 'structure', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['authent.Structure'], db_column='structure', default=default_structure().pk), keep_default=False) else: db.create_table('fl_b_fichier', ( ('id', self.gf('django.db.models.fields.AutoField')(primary_key=True)), ('type', self.gf('django.db.models.fields.CharField')(max_length=128)), ('structure', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['authent.Structure'], db_column='structure')), )) db.send_create_signal('common', ['FileType'])
def get_good_data(self): return { 'name_fr': u'test', 'structure': default_structure().pk, 'geom': '{"type": "Point", "coordinates":[0, 0]}', }
def handle(self, *args, **options): verbosity = options.get('verbosity') try: from osgeo import gdal, ogr, osr # NOQA except ImportError: raise CommandError( 'GDAL Python bindings are not available. Can not proceed.') filename = options['point_layer'] if not os.path.exists(filename): raise CommandError('File does not exists at: %s' % filename) data_source = DataSource(filename, encoding=options.get('encoding')) field_name = options.get('name_field') field_infrastructure_type = options.get('type_field') field_condition_type = options.get('condition_field') field_structure_type = options.get('structure_field') field_description = options.get('description_field') field_implantation_year = options.get('year_field') field_eid = options.get('eid_field') sid = transaction.savepoint() structure_default = options.get('structure_default') try: for layer in data_source: if verbosity >= 2: self.stdout.write( "- Layer '{}' with {} objects found".format( layer.name, layer.num_feat)) available_fields = layer.fields if (field_infrastructure_type and field_infrastructure_type not in available_fields)\ or (not field_infrastructure_type and not options.get('type_default')): self.stdout.write( self.style.ERROR( "Field '{}' not found in data source.".format( field_infrastructure_type))) self.stdout.write( self.style.ERROR( u"Set it with --type-field, or set a default value with --type-default" )) break if (field_name and field_name not in available_fields)\ or (not field_name and not options.get('name_default')): self.stdout.write( self.style.ERROR( "Field '{}' not found in data source.".format( field_name))) self.stdout.write( self.style.ERROR( u"Set it with --name-field, or set a default value with --name-default" )) break if field_condition_type and field_condition_type not in available_fields: self.stdout.write( self.style.ERROR( "Field '{}' not found in data source.".format( field_condition_type))) self.stdout.write( self.style.ERROR( u"Change your --condition-field option")) break if field_structure_type and field_structure_type not in available_fields: self.stdout.write( self.style.ERROR( "Field '{}' not found in data source.".format( field_structure_type))) self.stdout.write( self.style.ERROR( u"Change your --structure-field option")) break elif not field_structure_type and not structure_default: structure = default_structure() else: try: structure = Structure.objects.get( name=structure_default) if verbosity > 0: self.stdout.write( u"Infrastructures will be linked to {}".format( structure)) except Structure.DoesNotExist: self.stdout.write( u"Structure {} set in options doesn't exist". format(structure_default)) break if field_description and field_description not in available_fields: self.stdout.write( self.style.ERROR( "Field '{}' not found in data source.".format( field_description))) self.stdout.write( self.style.ERROR( u"Change your --description-field option")) break if field_implantation_year and field_implantation_year not in available_fields: self.stdout.write( self.style.ERROR( "Field '{}' not found in data source.".format( field_implantation_year))) self.stdout.write( self.style.ERROR("Change your --year-field option")) break if field_eid and field_eid not in available_fields: self.stdout.write( self.style.ERROR( "Field '{}' not found in data source.".format( field_eid))) self.stdout.write( self.style.ERROR(u"Change your --eid-field option")) break for feature in layer: feature_geom = feature.geom.transform(settings.API_SRID, clone=True) feature_geom.coord_dim = 2 name = feature.get( field_name ) if field_name in available_fields else options.get( 'name_default') if feature_geom.geom_type == 'MultiPoint': self.stdout.write( self.style.NOTICE( u"This object is a MultiPoint : %s" % name)) if len(feature_geom) < 2: feature_geom = feature_geom[0].geos else: raise CommandError( "One of your geometry is a MultiPoint object with multiple points" ) type = feature.get( field_infrastructure_type ) if field_infrastructure_type in available_fields else options.get( 'type_default') if field_condition_type in available_fields: condition = feature.get(field_condition_type) else: condition = options.get('condition_default') structure = Structure.objects.get(name=feature.get(field_structure_type)) \ if field_structure_type in available_fields else structure description = feature.get( field_description ) if field_description in available_fields else options.get( 'description_default') year = int( feature.get(field_implantation_year) ) if field_implantation_year in available_fields and feature.get( field_implantation_year).isdigit() else options.get( 'year_default') eid = feature.get( field_eid) if field_eid in available_fields else None self.create_infrastructure(feature_geom, name, type, condition, structure, description, year, verbosity, eid) transaction.savepoint_commit(sid) if verbosity >= 2: self.stdout.write( self.style.NOTICE(u"{} objects created.".format( self.counter))) except Exception: self.stdout.write( self.style.ERROR( u"An error occured, rolling back operations.")) transaction.savepoint_rollback(sid) raise
def get_good_data(self): return { 'name_fr': u'test', 'structure': default_structure().pk, 'geom': '{"type": "Point", "coordinates":[0, 0]}', }