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, 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)
    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