コード例 #1
0
def create_person(sender, instance, created, **kwargs):
    """
    This signal is meant to create a person object
    when a new django user is created
    """

    if created:
        default_person = {
            "id": instance.id,
            "username": instance.username,
            "first_name": instance.first_name,
            "last_name": instance.last_name,
            "email": {
                "value": instance.email
            }
        }
        json_obj = JSON(collection='opensocial_people.person',
                        json_string=json.dumps(default_person))
        json_obj.save()
        time_obj = TimeD()
        time_obj.save()

        Person(user=instance,
               json_data=json_obj,
               time=time_obj).save()

        #create relationship for oneself
        Relationship(initial_user=instance,
                     group='@self',
                     target_user=instance).save()
コード例 #2
0
    def create(self, feature, *args, **kwargs):
        """
        This function creates a feature which is a
        python dict representation of a geojson object.

        The feature properties like create_time or
        expire time should be given in the feature object
        e.g.
        {
        "type": "Feature",
        "geometry": ...,
        "crs": ...,
        "properties": ...,
        "id": ...,
        "private": ...,
        "time": ...,
        "user": ...,
        "group": ...
        }
        """
        #TODO do not use GDAL
#        self.geometry = OGRGeometry(json.dumps(feature['geometry'])).geos
#        self.geometry = GEOSGeometry(json.dumps(feature['geometry']))
        # There is a problem with GDAL from_json routine
        # so we use shapely library to circumvent the problem.
        temp_geom = asShape(feature['geometry'])
        # Extract the Coordinate referense system of the feature
        if 'crs' in feature.keys():
            try:
                feat_srid = int(feature['crs']['properties']['name'].split(':')[1])
            except (IndexError, KeyError):
                feat_srid = None
        else:
            feat_srid = None
        self.geometry = GEOSGeometry(temp_geom.to_wkt(), feat_srid)
        self.private = feature.get('private', True)
        timed = TimeD()
        timed.save()
        self.time = timed
        prop = Property(user = self.user,
                        group = self.group)
        prop.create(feature['properties'])
        super(Feature, self).save(*args, **kwargs)
        self.properties.add(prop)

        # kind of a cache for json
        self.json_str = json.dumps(self.to_json())
        super(Feature, self).save(*args, **kwargs)
コード例 #3
0
    def create(self, properties, *args, **kwargs):
        """
        This function saves properties of the feature,
        """
        js = JSON(json_string = json.dumps(properties),
                  collection = 'properties')
        js.save()
        self.json_data = js
        timed = TimeD()
        timed.save()
        self.time = timed
        super(Property, self).save(*args, **kwargs)

        # kind of a cache for json
        self.json_str = json.dumps(self.to_json())
        super(Property, self).save(*args, **kwargs)
コード例 #4
0
    def update(self, json_string):
        """
        Updates this persons information.

        If the new information is the same as the saved one
        then nothing is done, otherwise the old person is
        expired and a new person is created.
        """

        json_dict = self.json()
        old_dict = deepcopy(json_dict)
        # this one throws an error if not valid json -->
        # you get a 500
        json_dict.update(json.loads(json_string))

        if old_dict != json_dict:
            #set old feature as expired
            self.time.expire()
            self.save()

            #check the values that should be updated in the user model

            if json_dict.has_key('first_name'):
                self.user.first_name = json_dict['first_name']
            if json_dict.has_key('last_name'):
                self.user.last_name = json_dict['last_name']

            #email handling
            person_email = json_dict.get('email', {})

            try:
                temp_person_email = person_email.get('value', '')
                person_email = temp_person_email
            except AttributeError:
                pass

            if person_email == {}:
                self.user.email = ''
            else:
                self.user.email = person_email

            self.json_data.remove_values(['first_name',
                                          'last_name',
                                          'email'])

            #save the user
            self.user.save()

            #save the new property
            new_json = JSON(collection='opensocial_people.person',
                            json_string=json.dumps(json_dict))
            new_json.save()

            new_time = TimeD()
            new_time.save()
            new_person = Person(user = self.user,
                                json_data = new_json,
                                time = new_time)
            new_person.save()

            return new_person

        else:
            return self
コード例 #5
0
ファイル: data_migrate.py プロジェクト: MikkoJo/releases
from django.contrib.gis.gdal import OGRGeometry
from django.contrib.auth.models import User
from geonition_utils.models import JSON
from geonition_utils.models import TimeD

for key, value in data_dict.items():
    print key
    print value

    #create feature
    feature = value['feature']
    geometry = OGRGeometry(feature['geometry']).geos
    private = feature.get('private', True)
    user = User.objects.get(id = feature['user'])
    group = 'PP-perhela-perhelan-kortteli' #they are all put into the same group Jarvenpaa
    time = TimeD(create_time = feature['create_time'],
                 expire_time = feature['expire_time'])
    time.save()
    new_feature = Feature(geometry = geometry,
                          user = user,
                          group = group,
                          private = private,
                          time = time)
    new_feature.save()

    #create property
    timed = TimeD()
    timed.save()
    time = timed
    prop = Property()
    #self.properties.add(prop)