def handle_update_profile(self, data):

        if "profile" not in data:
            data = {'profile': data}
        if "profileid" in data["profile"]:
            data["profile"]["id"] = data["profile"]["profileid"]
            del data["profile"]["profileid"]
        if "nick" in data["profile"]:
            data["profile"]["nick"] = data["profile"]["nick"]
            del data["profile"]["nick"]

        profile_model = Profile.get((Profile.id == data['profile']['id']))
        namespaceid = 0
        if "namespaceid" in data['profile']:
            namespaceid = data['profile']['namespaceid']
        if "nick" in data['profile']:
            if not Profile.is_nick_valid(data['profile']['nick'], namespaceid):
                raise OS_Profile_NickInvalid()
        if "uniquenick" in data['profile']:
            if not Profile.is_nick_valid(data['profile']['uniquenick'],
                                         namespaceid):
                raise OS_Profile_UniquenickInvalid()
            nick_available = self.check_uniquenick_available(
                data['profile']['uniquenick'], namespaceid)
            if nick_available["exists"]:
                raise OS_Profile_UniquenickInUse(nick_available["profile"])
        for key in data['profile']:
            if key != "id":
                setattr(profile_model, key, data['profile'][key])
        profile_model.save()
        return {"success": True}
 def handle_create_profile(self, data):
     profile_data = data["profile"]
     user_data = None
     if "user" in data:
         user_data = data["user"]
     if "namespaceid" in profile_data:
         namespaceid = profile_data["namespaceid"]
     else:
         namespaceid = 0
     if "uniquenick" in profile_data:
         if not Profile.is_nick_valid(profile_data["uniquenick"],
                                      namespaceid):
             raise OS_Profile_UniquenickInvalid()
         nick_available = self.check_uniquenick_available(
             profile_data["uniquenick"], namespaceid)
         if nick_available["exists"]:
             raise OS_Profile_UniquenickInUse(nick_available["profile"])
     user = User.get((User.id == user_data["id"]))
     if "nick" in profile_data:
         if not Profile.is_nick_valid(profile_data["nick"], namespaceid):
             raise OS_Profile_NickInvalid()
     profile_data["user"] = user
     profile_pk = Profile.insert(**profile_data).execute()
     profile = Profile.get((Profile.id == profile_pk))
     profile = model_to_dict(profile)
     del profile["user"]
     user = model_to_dict(user)
     return {"user": user, "profile": profile, "success": True}