def test_replace_location_by_different_source(place, place2, other_data_source, event):
    assert event.location == place
    place2.name = place.name
    place2.save()
    replace_location(replace=place, by_source=other_data_source.id)
    updated_event = Event.objects.get(id=event.id)
    assert updated_event.location == place2
示例#2
0
 def delete_and_replace(self, obj):
     obj.deleted = True
     obj.save(update_fields=['deleted'])
     # we won't stand idly by and watch tprek delete needed units willy-nilly without raising a ruckus!
     if obj.events.count() > 0:
         # try to replace by tprek and, failing that, matko
         replaced = replace_location(replace=obj, by_source='tprek')
         if not replaced:
             # matko location may indeed be deleted by an earlier iteration
             replaced = replace_location(replace=obj,
                                         by_source='matko',
                                         include_deleted=True)
         if not replaced:
             # matko location may never have been imported in the first place, do it now!
             call_command('event_import',
                          'matko',
                          places=True,
                          single=obj.name)
             replaced = replace_location(replace=obj, by_source='matko')
         if not replaced:
             self.logger.warning(
                 "Tprek deleted location %s (%s) with events."
                 "No unambiguous replacement was found. "
                 "Please look for a replacement location and save it in the replaced_by field. "
                 "Until then, events will stay mapped to the deleted location."
                 % (obj.id, str(obj)))
     return True
示例#3
0
 def delete_and_replace(self, obj):
     obj.deleted = True
     obj.save(update_fields=['deleted'])
     if obj.events.count() > 0:
         replaced = replace_location(replace=obj, by_source='tpr')
         if not replaced:
             replaced = replace_location(replace=obj, by_source='matko', include_deleted=True)
         if not replaced:
             call_command('event_import', 'matko', places=True, single=obj.name)
             replaced = replace_location(replace=obj, by_source='matko')
         if not replaced:
             logger.warning("Tpr deleted location %s (%s) with events."
                            "No unambiguous replacement was found. "
                            "Please look for a replacement location and save it in the replaced_by field. "
                            "Until then, events will stay mapped to the deleted location." %
                            (obj.id, str(obj)))
     return True
示例#4
0
 def delete_and_replace(self, obj):
     obj.deleted = True
     obj.save(update_fields=['deleted'])
     # we won't stand idly by and watch tprek delete needed units willy-nilly without raising a ruckus!
     if obj.events.count() > 0:
         # try to replace by tprek and, failing that, matko
         replaced = replace_location(replace=obj, by_source='tprek')
         if not replaced:
             # matko location may indeed be deleted by an earlier iteration
             replaced = replace_location(replace=obj, by_source='matko', include_deleted=True)
         if not replaced:
             # matko location may never have been imported in the first place, do it now!
             call_command('event_import', 'matko', places=True, single=obj.name)
             replaced = replace_location(replace=obj, by_source='matko')
         if not replaced:
             logger.warning("Tprek deleted location %s (%s) with events."
                            "No unambiguous replacement was found. "
                            "Please look for a replacement location and save it in the replaced_by field. "
                            "Until then, events will stay mapped to the deleted location." %
                            (obj.id, str(obj)))
     return True
示例#5
0
    def _import_unit(self, syncher, info):
        n = 0
        e = 0
        isPositionOk = True
        try:
            n = info['location']['coordinates'][0]  # latitude
            e = info['location']['coordinates'][1]  # longitude
        except:
            isPositionOk == False
            POSITIONERROR.append(info['id'])

        if isPositionOk:
            obj = syncher.get(str(info['id']))
            obj_id = 'tpr:%s' % str(info['id'])

            if not obj:
                obj = Place(data_source=self.data_source, origin_id=info['id'])
                obj._changed = True
                obj._created = True
                obj.id = obj_id
            else:
                assert obj.id == obj_id
                obj._created = False
            obj._changed_fields = []

            try:
                self._save_translated_field_multilevel(obj, 'name', info, 'name')
                self._save_translated_field_multilevel(obj, 'description', info, 'description')
                self._save_translated_field_multilevel(obj, 'street_address', info, 'street_address')
            except:
                NONEVALUES.append('unit ID: ' + str(info['id']) + ' some multilanguage field is none')
                pass

            try:
                self._save_field(obj, 'info_url', info, 'www', max_length=1000)
            except:
                NONEVALUES.append('unit ID: ' + str(info['id']) + ' info_url field is empty!')
                pass

            try:
                self._save_field(obj, 'address_locality', info, 'municipality')
            except:
                NONEVALUES.append('unit ID: ' + str(info['id']) + ' address_locality field is empty!')
                pass

            try:
                self._save_field(obj, 'telephone', info, 'phone')
            except:
                NONEVALUES.append('unit ID: ' + str(info['id']) + ' telephone field is empty!')
                pass

            field_map = {
                'address_zip': 'postal_code',
                'address_postal_full': None,
                'email': 'email',
            }
            for src_field, obj_field in field_map.items():
                if not obj_field:
                    continue
                val = info.get(src_field, None)
                if getattr(obj, obj_field) != val:
                    setattr(obj, obj_field, val)
                    obj._changed_fields.append(obj_field)
                    obj._changed = True

            position = None
            if n and e:
                if os.name == 'nt':
                    p = Point(e, n, srid=4326)  # GPS coordinate system (WGS 84)
                else:
                    p = Point(n, e, srid=4326)  # GPS coordinate system (WGS 84)

                if p.within(self.bounding_box):
                    if self.target_srid != 4326:
                        p.transform(self.gps_to_target_ct)
                    position = p
                else:
                    logger.warning("Invalid coordinates (%f, %f) for %s" % (n, e, obj))

            try:
                picture_url = info.get('picture_url', '').strip()
            except:
                NONEVALUES.append('unit ID: ' + str(info['id']) + ' picture_url field is empty!')
                pass

            if position and obj.position:
                # If the distance is less than 10cm, assume the location
                # hasn't changed.
                assert obj.position.srid == settings.PROJECTION_SRID
                if position.distance(obj.position) < 0.10:
                    position = obj.position

            if position != obj.position:
                obj._changed = True
                obj._changed_fields.append('position')
                obj.position = position

            if obj.publisher_id != self.organization.id:
                obj.publisher = self.organization
                obj._changed_fields.append('publisher')
                obj._changed = True

            if obj.deleted:
                obj.deleted = False
                replace_location(from_source='matko', by=obj)
                obj._changed_fields.append('deleted')
                obj._changed = True

            if obj._changed:
                if obj._created:
                    verb = "created"
                else:
                    verb = "changed (fields: %s)" % ', '.join(obj._changed_fields)
                logger.info("%s %s" % (obj, verb))
                obj.save()

            syncher.mark(obj)
示例#6
0
    def _import_unit(self, syncher, info):
        obj = syncher.get(str(info['id']))
        obj_id = 'tprek:%s' % str(info['id'])
        if not obj:
            obj = Place(data_source=self.data_source, origin_id=info['id'])
            obj._changed = True
            obj._created = True
            obj.id = obj_id
        else:
            assert obj.id == obj_id
            obj._created = False
        obj._changed_fields = []

        self._save_translated_field(obj, 'name', info, 'name')
        self._save_translated_field(obj, 'description', info, 'desc')
        self._save_translated_field(obj, 'street_address', info, 'street_address')
        self._save_translated_field(obj, 'address_locality', info, 'address_city')

        self._save_translated_field(obj, 'info_url', info, 'www', max_length=1000)

        self._save_field(obj, 'telephone', info, 'phone')

        field_map = {
            'address_zip': 'postal_code',
            'address_postal_full': None,
            'email': 'email',
        }
        for src_field, obj_field in field_map.items():
            if not obj_field:
                continue
            val = info.get(src_field, None)
            if getattr(obj, obj_field) != val:
                setattr(obj, obj_field, val)
                obj._changed_fields.append(obj_field)
                obj._changed = True

        n = info.get('latitude', 0)
        e = info.get('longitude', 0)
        position = None
        if n and e:
            p = Point(e, n, srid=4326)  # GPS coordinate system
            if p.within(self.bounding_box):
                if self.target_srid != 4326:
                    p.transform(self.gps_to_target_ct)
                position = p
            else:
                logger.warning("Invalid coordinates (%f, %f) for %s" % (n, e, obj))

        picture_url = info.get('picture_url', '').strip()
        if picture_url:
            self.set_image(obj, {'url': picture_url})

        if position and obj.position:
            # If the distance is less than 10cm, assume the location
            # hasn't changed.
            assert obj.position.srid == settings.PROJECTION_SRID
            if position.distance(obj.position) < 0.10:
                position = obj.position
        if position != obj.position:
            obj._changed = True
            obj._changed_fields.append('position')
            obj.position = position

        if obj.publisher_id != self.organization.id:
            obj.publisher = self.organization
            obj._changed_fields.append('publisher')
            obj._changed = True

        if obj.deleted:
            obj.deleted = False
            # location has been reinstated in tprek, hip hip hooray!
            replace_location(from_source='matko', by=obj)
            obj._changed_fields.append('deleted')
            obj._changed = True

        if obj._changed:
            if obj._created:
                verb = "created"
            else:
                verb = "changed (fields: %s)" % ', '.join(obj._changed_fields)
            logger.info("%s %s" % (obj, verb))
            obj.save()

        syncher.mark(obj)
示例#7
0
    def _import_unit(self, syncher, info):
        obj = syncher.get(str(info['id']))
        obj_id = 'tprek:%s' % str(info['id'])
        if not obj:
            obj = Place(data_source=self.data_source, origin_id=info['id'])
            obj._changed = True
            obj._created = True
            obj.id = obj_id
        else:
            assert obj.id == obj_id
            obj._created = False
        obj._changed_fields = []

        self._save_translated_field(obj, 'name', info, 'name')
        self._save_translated_field(obj, 'description', info, 'desc')
        self._save_translated_field(obj, 'street_address', info, 'street_address')
        self._save_translated_field(obj, 'address_locality', info, 'address_city')

        self._save_translated_field(obj, 'info_url', info, 'www', max_length=1000)

        self._save_field(obj, 'telephone', info, 'phone')

        field_map = {
            'address_zip': 'postal_code',
            'address_postal_full': None,
            'email': 'email',
        }
        for src_field, obj_field in field_map.items():
            if not obj_field:
                continue
            val = info.get(src_field, None)
            if getattr(obj, obj_field) != val:
                setattr(obj, obj_field, val)
                obj._changed_fields.append(obj_field)
                obj._changed = True

        n = info.get('latitude', 0)
        e = info.get('longitude', 0)
        position = None
        if n and e:
            p = Point(e, n, srid=4326)  # GPS coordinate system
            if p.within(self.bounding_box):
                if self.target_srid != 4326:
                    p.transform(self.gps_to_target_ct)
                position = p
            else:
                logger.warning("Invalid coordinates (%f, %f) for %s" % (n, e, obj))

        picture_url = info.get('picture_url', '').strip()
        if picture_url:
            self.set_image(obj, {'url': picture_url})

        if position and obj.position:
            # If the distance is less than 10cm, assume the location
            # hasn't changed.
            assert obj.position.srid == settings.PROJECTION_SRID
            if position.distance(obj.position) < 0.10:
                position = obj.position
        if position != obj.position:
            obj._changed = True
            obj._changed_fields.append('position')
            obj.position = position

        if obj.publisher_id != self.organization.id:
            obj.publisher = self.organization
            obj._changed_fields.append('publisher')
            obj._changed = True

        if obj.deleted:
            obj.deleted = False
            # location has been reinstated in tprek, hip hip hooray!
            replace_location(from_source='matko', by=obj)
            obj._changed_fields.append('undeleted')
            obj._changed = True

        if obj._changed:
            if obj._created:
                verb = "created"
            else:
                verb = "changed (fields: %s)" % ', '.join(obj._changed_fields)
            logger.info("%s %s" % (obj, verb))
            obj.save()

        syncher.mark(obj)