Example #1
0
    def put(self):

        data = Booking.parser.parse_args()

        userid = data['userid']
        bikeid = data['bikeid']

        user = AccountsModel.find_by_id(userid)
        bike = MotosModel.find_by_id(bikeid)

        if user is None:
            return "User not found", 404
        if bike is None:
            return "Bike not found", 404

        try:
            admin_user = AccountsModel.find_by_username('admin')

            if admin_user:
                book = BookingModel.finalize_book(userid, bikeid)
                if book is None:
                    return "No renting found", 404
                MotosModel.change_status(bikeid)

                admin_user.availableMoney += book.price
                user.availableMoney -= book.price

                return {"finalized_rent": book.json()}, 201
            return "Admin user not found", 404
        except:
            return "Something went wrong", 500
Example #2
0
 def delete(self, username):
     with lock.lock:
         acc = AccountsModel.find_by_username(username)
         if acc:
             acc.delete_from_db()
             return { 'message' : 'success'}, 201
         else:
             return {'message': "Not in db"}, 404
Example #3
0
 def get(self, username):
     user = AccountsModel.find_by_username(username)
     if user:
         return user.json(), 200
     else:
         return {
             'message':
             'There is no client with username [{}] .'.format(username)
         }, 404
Example #4
0
 def post(self):
     data = self.parser()
     with lock.lock:
         acc = AccountsModel.find_by_username(data['username'])
         if not acc:
             return {'message': 'username not in db'}, 404
         else:
             token = acc.generate_auth_token()
             if acc.verify_password(data["password"]):
                 return {'token': token.decode('ascii')}, 200
             else:
                 return {'message': "password invalid"}, 400
Example #5
0
    def post(self, username):
        data = self.parser()
        with lock.lock:
            acc = AccountsModel.find_by_username(username)
            if acc:
                return {'message': "username  with id [{}] already exists".format(id)}, 404
            else:
                available_money = data['available_money'] if data["available_money"] else AVAILABLE_MONEY
                is_admin = data['is_admin'] if data["is_admin"]  else IS_ADMIN

                acc = AccountsModel(username, available_money=available_money, is_admin=is_admin)
                acc.hash_password(data['password'])
                acc.save_to_db()

                return {'message' : acc.json()}, 201

            return {'account': acc.json()}, 201
Example #6
0
    def post(self):

        parser = reqparse.RequestParser()
        parser.add_argument('username',
                            type=str,
                            required=True,
                            help="This field cannot be left blank")
        parser.add_argument('password',
                            type=str,
                            required=True,
                            help="This field cannot be left blank")

        data = parser.parse_args()
        user = AccountsModel.find_by_username(data['username'])

        if user:
            if user.verify_password(data['password']):
                token = user.generate_auth_token()
                return {'token': token.decode('ascii')}, 200
            else:
                return {"message": "Password not correct"}, 400
        else:
            return {"message": "User not found"}, 404
Example #7
0
 def post(self, username):
     data = self.parser()
     with lock.lock:
         if username == g.user.username:
             a = AccountsModel.find_by_username(username)
             e = EventModel.find_by_id(data['event_id'])
             if e.total_available_tickets >= data["tickets_bought"]:
                 if a.available_money >= e.price * data["tickets_bought"]:
                     e.total_available_tickets = EventModel.total_available_tickets - data[
                         "tickets_bought"]
                     a.available_money -= data["tickets_bought"] * e.price
                     o = OrdersModel(e.id, data["tickets_bought"])
                     a.orders.append(o)
                     db.session.commit()
                     return {"order": o.json()}, 201
                 else:
                     return {'message': 'Not enough money'}, 501
             else:
                 return {'message': 'No enough tickets'}, 502
         else:
             return {
                 'message':
                 'Endpoint username and g.user.username not equal'
             }, 400
Example #8
0
 def get(self, username):
     with lock.lock:
         orders = AccountsModel.find_by_username(username).orders
         return {
             'orders': list(map(lambda x: x.json(), orders))
         }, 200 if orders else 404
Example #9
0
    def post(self):
        parser = reqparse.RequestParser()
        parser.add_argument('firstname',
                            type=str,
                            required=True,
                            help="This field cannot be left blank")
        parser.add_argument('surname',
                            type=str,
                            required=True,
                            help="This field cannot be left blank")
        parser.add_argument('email',
                            type=str,
                            required=True,
                            help="This field cannot be left blank")
        parser.add_argument('username',
                            type=str,
                            required=True,
                            help="This field cannot be left blank")
        parser.add_argument('password',
                            type=str,
                            required=True,
                            help="This field cannot be left blank")
        parser.add_argument('dni',
                            type=str,
                            required=True,
                            help="This field cannot be left blank")
        parser.add_argument('dataEndDrivePermission',
                            type=str,
                            required=True,
                            help="This field cannot be left blank")
        #parser.add_argument('status', type=str, required=True, help="This field cannot be left blank")
        parser.add_argument('creditCard',
                            type=str,
                            required=True,
                            help="This field cannot be left blank")
        #######parser.add_argument('availableMoney', type=int, required=True, help="This field cannot be left blank")
        parser.add_argument('type',
                            type=int,
                            required=True,
                            help="This field cannot be left blank")
        parser.add_argument('latitude',
                            type=float,
                            required=True,
                            help="This field cannot be left blank")
        parser.add_argument('longitude',
                            type=float,
                            required=True,
                            help="This field cannot be left blank")
        data = parser.parse_args()

        user = AccountsModel.find_by_username(data['username'])
        if user:
            return {"message": "User already exists"}, 400
        else:
            new_user = AccountsModel(data['firstname'], data['surname'],
                                     data['email'], data['username'],
                                     data['dni'],
                                     data['dataEndDrivePermission'],
                                     data['creditCard'], data['type'],
                                     data['latitude'], data['longitude'])
            new_user.hash_password(data['password'])
            try:
                new_user.save_to_db()
                return new_user.json(), 200
            except Exception as e:
                return {"message": "Database error"}, 500
Example #10
0
 def delete(self, username):
     user = AccountsModel.find_by_username(username)
     if not user:
         return {"message": "User not found"}, 404
     user.delete_from_db()
     return {'message': "User deleted"}, 200
Example #11
0
 def find_by_username(cls, username):
     return AccountsModel.find_by_username(username)
Example #12
0
 def get(self, username):
     with lock.lock:
         account = AccountsModel.find_by_username(username)
         return {'account': account.json()}, 200 if account else 404