Exemplo n.º 1
0
 def delete(self, username):
     body = Friend.parser.parse_args()
     friend = FriendModel.find_by_contact(username, body['contact_name'])
     if friend:
         friend.delete_from_db()
         return {'message': 'Friend removed'}
     else:
         return {'message': 'Friend not found'}, 404
Exemplo n.º 2
0
 def get(self, username):
     friends = FriendModel.find_by_username(username)
     if friends:
         you = [{
             "contact_name": UserModel.find_by_username(username).email
         }]
         return {'friends': you + [friend.json() for friend in friends]}
     else:
         return {'message': 'User not found'}, 404
Exemplo n.º 3
0
 def delete(self, id):
     friend = FriendModel.find_by_id(id)
     if friend:
         if friend.user_id == get_jwt_identity():
             try:
                 friend.delete_from_db()
             except:
                 return {'message': 'This friend cannot be deleted.'}, 403
             return {'message': 'Friend deleted successfully.'}, 204 # No Content
         return {'message': 'This user cannot delete this friend.'}, 403 # Forbidden
     return {'message': 'There is no frind with the given id.'}, 400 # Bab Request
Exemplo n.º 4
0
 def put(self, username):
     body = Friend.parser.parse_args()
     friends = FriendModel.find_by_username(username)
     newNames = body['contact_name'].split(',')
     print(newNames)
     if friends:
         for i, friend in enumerate(friends):
             newName = newNames[i]
             # print(newName)
             if newName != '' and newName != None:
                 friend.contact_name = newName
                 friend.save_to_db()
                 print(newName)
         return {'message': 'Contacts edited'}
     return {'message': 'User not found'}, 404
Exemplo n.º 5
0
    def post(self):
        data = self.parser.parse_args()
        data['user_id'] = get_jwt_identity()
        friend = FriendModel.find_by_name_and_user_id(**data)
        if friend:
            return {'message': f'This user already has a friend called {data["name"]}.'}
        friend = FriendModel(**data)

        try:
            friend.save_to_db()
        except:
            return {'message': 'The application could not save this friend.'}, 500
        return friend.describe(), 201
Exemplo n.º 6
0
 def post(self, username):
     body = Friend.parser.parse_args()
     if FriendModel.find_by_contact(username, body['contact_name']):
         return {
             'message':
             "A friend with contact '{}' already exists.".format(
                 body['contact_name'])
         }, 400
     friend = FriendModel(username, body['contact_name'])
     try:
         friend.save_to_db()
     except:
         return {'message': 'Exception raised when inserting item'}, 500
     return friend.json(), 201  #201 is CREATED
Exemplo n.º 7
0
    def post(self):
        data = self.parser.parse_args()
        data['user_id'] = get_jwt_identity()
        if FriendModel.find_by_id(data['friend_id']) is None:
            return {
                'message': f'There is no friend with id {data["friend_id"]}'
            }

        if ItemModel.find_by_id(data['item_id']) is None:
            return {'message': f'There is no item with id {data["item_id"]}'}

        data['loan_type'] = 0 if data['loan_type'] == 'friend_to_user' else 1
        loan = LoanModel(**data)

        try:
            loan.save_to_db()
        except:
            return {
                'message': 'The application could not save this loan.'
            }, 500

        return loan.describe()
Exemplo n.º 8
0
 def get(self):
     user_id = get_jwt_identity()
     return {'friends': [friend.describe() for friend in FriendModel.find_all(user_id=user_id)]}