Exemplo n.º 1
0
    def test_crud(self):
        with self.app_context():
            user = UserModel('test-user', '12345')

            self.assertIsNone(
                UserModel.find_by_username('test-user'),
                "Found a user with username {}, but expected not to.".format(
                    user.username))
            self.assertIsNone(UserModel.find_by_id(1))
            user.save_to_db()

            self.assertIsNotNone(UserModel.find_by_username('test-user'))
            self.assertIsNotNone(UserModel.find_by_id(1))
Exemplo n.º 2
0
    def put(self):
        id = request.args.get('object_id')
        name = request.args.get('name')
        description = request.args.get('description')
        object_value = request.args.get('object_value')
        must_be_returned = request.args.get('must_be_returned')
        must_be_returned_date = request.args.get('must_be_returned_date')
        shipping_possible = request.args.get('shipping_possible')
        is_borrowable = request.args.get('is_borrowable')

        current_user = get_jwt_identity()
        user = UserModel.find_by_id(current_user)
        if user:
            a = ObjectModel.find_by_id(int(id))
            if a is None:
                return "oggetto non esiste", 402
            if not (a.owner_id == user.id):
                return "non sei possessore di questo oggetto (in teoria non dovresti essere qui allora)", 406
            if a.is_away == True:
                return "you can't modify object while it's away", 403
            a.name = name
            a.description = description
            a.object_value = object_value
            a.must_be_returned = bool(must_be_returned)
            a.must_be_returned_date = must_be_returned_date
            a.shipping_possible = bool(shipping_possible)
            a.is_borrowable = bool(is_borrowable)
            a.save_to_db()

            return "ok", 200
        return "user does not exist", 401
Exemplo n.º 3
0
    def post(self, user_id):
        data = request.get_json()
        blocchi = int(data["blocchi"])
        corsie = int(data["corsie"])
        livelli = int(data["livelli"])
        if blocchi > corsie:
            return "troppi blocchi", 406
        user = UserModel.find_by_id(user_id)
        lobby = LobbyModel.find_by_id(user.lobby_id)
        if lobby.status == 1:
            return "match has already started", 403
        # RANDOM POSITION
        user.give_random_corsia(lobby.corsie)
        lobby.status = 1
        users = UserModel.find_all_by_lobby_id_and_status(lobby.id, 1)
        for u in users:
            u.status = 2
            u.jolly_earthquake = 1
            u.jolly_reveal = 1
            u.save_to_db()

        n_users = UserModel.find_all_by_lobby_id_and_status(lobby.id, 0)
        for u in n_users:
            u.jolly_earthquake = 0
            u.jolly_reveal = 0
            u.pedina_number = 0
            u.save_to_db()

        lobby.update_turn()
        lobby.blocchi = blocchi
        lobby.livelli = livelli
        lobby.corsie = corsie
        lobby.save_to_db()
        lobby.create_blocks()
        return "match started", 200
Exemplo n.º 4
0
def add_claims_to_jwt(identity):  # !!! identity comes from access_token
    user = UserModel.find_by_id(identity)

    if user and user.role == "admin":
        return {'is_admin': True}

    return {'is_admin': False}
Exemplo n.º 5
0
 def delete(self, p_id):
     user_data = UserModel.find_by_id(p_id)
     if user_data:
         user_data.db_to_delete()
         return {'message': 'user data deleted successfully'}, 200
     else:
         return {'message': 'User not found'}, 500
Exemplo n.º 6
0
 def get(self):
     object_id = request.args.get('object_id')
     current_user = get_jwt_identity()
     user = UserModel.find_by_id(current_user)
     if user:
         i = ObjectModel.find_by_id(object_id)
         if i:
             if i.owner_id != user.id:
                 return "non hai accesso a questo elemento", 407
             hashtot = []
             hashtags = HashtagObjects.find_by_object_id(object_id)
             for l in hashtags:
                 k = AllHashtags.find_by_id(l)
                 hashtot.append({
                     "hashtag_id": k.id,
                     "hashtag_name": k.name,
                     "total_times_used": k.total_times_used
                 })
             object = {
                 "name": i.name,
                 "description": i.description,
                 "value": i.object_value,
                 "currency": "TODO",
                 "must_be_returned": i.must_be_returned,
                 "must_be_returned_date": i.must_be_returned_date,
                 "shipping_possible": i.shipping_possible,
                 "is_away": i.is_away,
                 "keeper": "TODO",
                 "id": i.id,
                 "hashtags": hashtot
             }
             return object, 200
         return "object does not exist", 402
     return "user does not exist", 401
Exemplo n.º 7
0
 def get(cls, user_id):
     if get_jwt_identity() != user_id:
         return {'msg': "Unauthorized Content"}, 401
     user = UserModel.find_by_id(user_id)
     if not user:
         return {'msg': "Invalid User!! User Not Exists!!"}, 400
     return user_schema.dump(user), 200
Exemplo n.º 8
0
 def delete(cls):
     user_id = get_jwt_identity()
     user = UserModel.find_by_id(user_id)
     if user:
         user.delete_from_db()             
         return {"message": "User deleted successfully."}, 200           
     return {"message": "User not found."}, 404
Exemplo n.º 9
0
    def post(self):
        username = request.args.get('username')
        telephone_number = request.args.get('telephone_number')
        profile_foto = request.args.get('profile_foto')
        country_id = request.args.get('country_id')
        language_id = request.args.get('language_id')
        address = request.args.get('address')
        city = request.args.get('city')
        zip_code = request.args.get('zip_code')
        privacy_setting = request.args.get('privacy_setting')
        currency_id = request.args.get('currency_id')

        current_user = get_jwt_identity()
        user = UserModel.find_by_id(current_user)

        if user:
            user.telephone_number = telephone_number
            user.username = username
            user.profile_foto = profile_foto
            user.country_id = country_id
            user.language_id = language_id
            user.address = address
            user.city = city
            user.zip_code = zip_code
            user.privacy_setting = bool(privacy_setting)
            user.currency_id = currency_id
            user.save_to_db()
            return "changes applied correctly"
        return "user does not exist", 401
Exemplo n.º 10
0
def createPlace():
    if not request.json or \
       not 'name' in request.json or \
       not 'gid' in request.json or \
       not 'address' in request.json or \
       not 'yelpurl' in request.json or \
       not 'userID' in request.json:
        abort(400)

    user = UserModel.find_by_id(request.json['userID'])
    if user is None:
        return "User not found", 400

    place = {
        'name': request.json['name'],
        'gid': request.json['gid'],
        'address': request.json['address'],
        'yelpurl': request.json['yelpurl'],
        'delete_tag': 0,
        'created_date': datetime.datetime.now()
    }
    place = StoreModel(place)
    place.users.append(user)
    place.save_to_db()
    return jsonify({'place': place.name}), 201
Exemplo n.º 11
0
 def post(self, user_id):
     user = UserModel.find_by_id(user_id)
     turn = TurnModel.find_by_lobby_id(int(user.lobby_id))
     if turn.find_current() != user.id:
         return "mmmmh", 400
     turn.update()
     return "updated", 200
Exemplo n.º 12
0
 def get(self):
     user_id = get_jwt_identity()
     if user_id:
         user = UserModel.find_by_id(user_id)
         return user.describe(complete=False), 200
     return {
         'users': [user.describe() for user in UserModel.find_all()]
     }, 200
Exemplo n.º 13
0
 def post(self, user_id):
     user = UserModel.find_by_id(user_id)
     user.status = 0
     user.pedina_number = -1
     user.save_to_db()
     lobby = LobbyModel.find_by_id(user.lobby_id)
     lobby.update_turn()
     return "user out", 200
Exemplo n.º 14
0
def password_reset(user_id, password):
    user = UserModel.find_by_id(user_id)
    if not user:
        return {'msg': "Invalid User!!"}, 404
    new_password = Hash_Password(password).hash_pwd()
    UserModel.changePwd(user_id, new_password)
    print(user.username)
    return render_template('pwd_changed.html', username=user.username)
Exemplo n.º 15
0
 def __call__(self):
     return {
         'id': self.id,
         'task_id': self.task_id,
         'title': self.title,
         'posted_by': UserModel.find_by_id(self.posted_by).id,
         'url': self.url,
     }
Exemplo n.º 16
0
 def delete(cls, user_id):
     user = UserModel.find_by_id(user_id)
     if not user:
         return {'message': "User ({}) not found".format(user_id)}, 404
     user.delete_from_db()
     return {
         'message': "User ({}) sucsessfuly deleted".format(user_id)
     }, 200
Exemplo n.º 17
0
def mysongs_list(user_id):
    if not UserModel.find_by_id(user_id):
        raise Exception('User Not found!!')
    songs = [song() for song in SongModel.find_by_user(user_id)]
    if not songs:
        return response_builder(status_code=404, body="Empty!!")
    print(type(songs))
    return response_builder(status_code=200, body=songs)
Exemplo n.º 18
0
 def get(self):
     current_user = get_jwt_identity()
     user = UserModel.find_by_id(current_user)
     if user:
         classe = find_by_id(user.classe_id)
         if classe:
             return {"username": classe.nome}
         return {"message": "user has no class"}, 200
     return {"message": "user does not exist"}, 402
Exemplo n.º 19
0
 def get(self):
     try:
         user_id = get_jwt_identity()
         user = UserModel.find_by_id(user_id)
         if user is not None:
             return {"success": True, "user": user.json()}, 200
     except ValidationError as err:
         return err.messages, 400
     return {"success": False, "message": RETRIEVAL_ERROR}
Exemplo n.º 20
0
def identity(payload):
    """
    Function that gets called when user has already authenticated, and Flask-JWT,
    verified that their authorization header is correct
    :param payload:a dictionary with 'identity' key, which is user_id
    :return: A UserModel O
    """
    user_id = payload['identity']
    return UserModel.find_by_id(user_id)
Exemplo n.º 21
0
 def get(self, user_id):
     user = UserModel.find_by_id(user_id)
     if user.jolly_earthquake < 1:
         return "ah ah", 400
     lobby = LobbyModel.find_by_id(user.lobby_id)
     lobby.create_blocks()
     user.jolly_earthquake = 0
     user.save_to_db()
     return "ok", 200
Exemplo n.º 22
0
 def post(self):
     current_user = get_jwt_identity()
     a = get_raw_jwt()
     user = UserModel.find_by_id(current_user)
     #return str(a["iat"])+" "+str(int(time.time()))
     #return str(user.password_change)+" "+str(a["iat"])
     if a["iat"] >= user.password_change_date:
         expires = datetime.timedelta(days=365)
         new_token = create_access_token(identity=current_user, fresh=False)
         return {"access_token": new_token}
     return "password was changed"
Exemplo n.º 23
0
 def post(cls):
     data = user_schema.load(request.get_json())
     current_user_id = get_jwt_identity()
     current_user = UserModel.find_by_id(current_user_id)
     if not Hash_Password.check_pwd(data["password"],
                                    current_user.password):
         return {'msg': "Wrong Credentials!"}, 401
     new_token = create_access_token(identity=current_user_id,
                                     fresh=False,
                                     expires_delta=False)
     return {'access_token': new_token}, 200
Exemplo n.º 24
0
    def post(self, user_id):
        user = UserModel.find_by_id(user_id)
        lobby = LobbyModel.find_by_id(user.lobby_id)
        users = UserModel.find_all_by_lobby_id(lobby.id)
        for u in users:
            u.status = 0
            u.save_to_db()
        lobby.status = 0
        lobby.save_to_db()

        return "ok", 200
Exemplo n.º 25
0
 def get(self, user_id):
     user = UserModel.find_by_id(user_id)
     lobby = LobbyModel.find_by_id(user.lobby_id)
     if not lobby:
         return "ah ah", 401
     j_result = {
         "blocchi": lobby.blocchi,
         "corsie": lobby.corsie,
         "livelli": lobby.livelli
     }
     return j_result
Exemplo n.º 26
0
def getUserPlaces(id):
    app.logger.info('Entering getUserPlaces method')
    user = UserModel.find_by_id(id)
    if user is None:
        return ""
    places = user.get_all_places()
    ret = []
    for place in places:
        if place is not None:
            ret.append(place.json())
    return json.dumps(ret)
Exemplo n.º 27
0
    def get(cls):
        data = _user_parser.parse_args()

        # -1 => targets self
        if data['target'] == -1:
            user_id = get_jwt_identity()
        else:
            user_id = data['target']
        user = UserModel.find_by_id(user_id)
        if user:
            return user.json(), 200
        return {"message": "User not found."}, 404
Exemplo n.º 28
0
    def delete(cls):
        data = _user_parser.parse_args()

        # -1 => targets self
        if data['target'] == -1:
            user_id = get_jwt_identity()
        else:
            user_id = data['target']
        user = UserModel.find_by_id(user_id)
        if user:
            user.delete_from_db()
            return {"message": "User deleted successfully."}, 200
        return {"message": "User not found."}, 404
Exemplo n.º 29
0
 def post(self):
     current_user = get_jwt_identity()
     user = UserModel.find_by_id(current_user)
     className = request.args.get("classe")
     if user:
         if user.admin == True:
             classe = find_by_id(user.classe_id)
             if classe:
                 classe.nome = className
                 classe.save_to_db()
                 return {"message": classe.nome}, 200
             return "banana"
         return {"message": "user is not admin or is not in a class"}, 405
     return {"message": "user does not exist"}, 402
Exemplo n.º 30
0
 def get(self, user_id):
     user = UserModel.find_by_id(user_id)
     lobby = LobbyModel.find_by_id(user.lobby_id)
     blocchi_array = []
     blocchi = BloccoModel.find_all_by_lobby_id(lobby.id)
     for b in blocchi:
         j_bloc = {"id": b.id, "corsia": b.corsia, "livello": b.livello}
         blocchi_array.append(j_bloc)
     game_status = lobby.status
     users_array = []
     winners_array = []
     users = UserModel.find_all_by_lobby_id_and_status(
         lobby.id, game_status + 1)
     users.sort(key=lambda x: x.id)
     for u in users:
         c = None
         u_id = u.id
         if u.id == user.id:
             c = u.corsia
         is_playing = False
         if lobby.status == 1 and lobby.find_player_playing().id == u.id:
             is_playing = True
         if u.livello >= lobby.livelli:
             winners_array.append({
                 "id": u.id,
                 "pedina_number": u.pedina_number
             })
         else:
             j_user = {
                 "id": u_id,
                 "livello": u.livello,
                 "pedina_number": u.pedina_number,
                 "corsia": c,
                 "is_playing": is_playing,
                 "jolly_reveal": u.jolly_reveal,
                 "jolly_earthquake": u.jolly_earthquake
             }
             users_array.append(j_user)
     j_griglia = {
         "corsie": lobby.corsie,
         "livelli": lobby.livelli,
         "status": game_status
     }
     j_result = {
         "griglia": j_griglia,
         "blocchi": blocchi_array,
         "users": users_array,
         "winners": winners_array
     }
     return j_result