Exemplo n.º 1
0
def add_users(fixtures, created_models):
    user_models = {}

    for fixture in fixtures:
        user = User(fixture.get('private_id'))
        user.db_id = fixture.get('db_id', None)
        user.id = fixture.get('id', None)
        user.authentication_type = fixture.get('authentication_type', None)
        user.person_id = fixture.get('person_id', None)
        user.created_on = fixture.get('created_on', None)
        user.auth_token = fixture.get('auth_token', None)
        user.auth_timestamp = fixture.get('auth_timestamp', None)
        user.credentials = [created_models["credential_models"][credential_id] for credential_id in fixture.get('credential_ids')]
        user.roles = [created_models["role_models"][role_name] for role_name in fixture.get('role_names')]

        profile = Profile()
        profile.national_id_number = fixture.get('profile').get('national_id_number', None)
        profile.active_directory_id = fixture.get('profile').get('active_directory_id', None)
        profile.first_name = fixture.get('profile').get('first_name', None)
        profile.last_name = fixture.get('profile').get('last_name', None)
        profile.email = fixture.get('profile').get('email', None)
        profile.phone = fixture.get('profile').get('phone', None)

        user.profile = profile

        user_models[user.db_id] = user
        app.db_session.add(user)

    created_models["user_models"] = user_models
    return created_models
 def create_or_update(self, request_json):
     private_id = request_json["private_id"]
     authentication_type = request_json["authentication_type"]
     user = None
     try:
         # update
         user = self.find_by_private_id(private_id)
         user.renew_auth()
     except:
         # create
         user = User(private_id, authentication_type=authentication_type)
         profile = Profile()
         if user.authentication_type == AuthenticationTypeEnum.ID_PORTEN:
             profile.national_id_number = private_id
         if user.authentication_type == AuthenticationTypeEnum.ACTIVE_DIRECTORY:
             profile.active_directory_id = private_id
         user.profile = profile
     current_app.db_session.add(user)
     current_app.db_session.commit()
     current_app.db_session.refresh(user)
     return user
    def update(self, **kwargs):
        queried_user_id = kwargs['queried_user_id']
        json_request = kwargs['json_request']
        profile = self.find_by_user_id(queried_user_id)
        if not profile:
            profile = Profile()

        if json_request.get("first_name"):
            profile.first_name = json_request.get("first_name")
        if json_request.get("last_name"):
            profile.last_name = json_request.get("last_name")
        if json_request.get("national_id_number"):
            profile.national_id_number = json_request.get("national_id_number")
        if json_request.get("email"):
            profile.email = json_request.get("email")
        if json_request.get("phone"):
            profile.phone = json_request.get("phone")

        user = current_app.db_session.query(User).filter(User.id == queried_user_id).one()
        user.profile = profile
        current_app.db_session.add(user)
        current_app.db_session.commit()
        current_app.db_session.refresh(user)
        return user.profile