Esempio n. 1
0
 def delete(self, username):
     with lock.lock:
         user = AccountsModel.find_by_username(username)
         if user:
             user.delete_from_db()
             return {n.MSG: "User deleted successfully"}, 200
         else:
             return {n.MSG: "User not found"}, 404
Esempio n. 2
0
    def post(self):
        parser = reqparse.RequestParser(
        )  # create parameters parser from request
        # define al input parameters need and its type
        parser.add_argument(n.USR, type=str, required=True, help="Username")
        parser.add_argument(n.PSW, type=str, required=True, help="Password")
        data = parser.parse_args()

        with lock.lock:
            user = AccountsModel.find_by_username(data[n.USR])
            if user:
                return {n.MSG: "User already in database"}, 400

            user = AccountsModel(data[n.USR])
            user.hash_password(data[n.PSW])
            user.save_to_db()
            return user.json(), 200
Esempio n. 3
0
    def post(self):
        parser = reqparse.RequestParser(
        )  # create parameters parser from request
        # define al input parameters need and its type
        parser.add_argument(n.USR, type=str, required=True, help="Username")
        parser.add_argument(n.PSW, type=str, required=True, help="Password")
        data = parser.parse_args()

        user = AccountsModel.find_by_username(data[n.USR])
        if user:
            verification = user.check_password(data[n.PSW])

            if verification:
                return {n.TKN: user.generate_auth_token().decode('utf-8')}, 200
            else:
                return {n.MSG: "Invalid password"}, 400
        else:
            return {n.MSG: "User not found"}, 404
Esempio n. 4
0
    def post(self, username):
        user = AccountsModel.find_by_username(username)
        if user:
            parser = reqparse.RequestParser()
            parser.add_argument(n.IDEVNT, type=int, required=True, help="Id of the event")
            parser.add_argument(n.TCK, type=int, required=True, help="Number of tickets the user wants to buy")
            data = parser.parse_args()

            with lock.lock:
                if username == g.user.username:
                    event = EventModel.find_by_id(data[n.IDEVNT])
                    if event:
                        price = event.price*data[n.TCK]
                        if user.available_money >= price:
                            if event.total_available_tickets >= data[n.TCK]:
                                try:
                                    user.pay(price)
                                    event.sell(data[n.TCK])
                                    new_order = OrdersModel(data[n.IDEVNT], data[n.TCK])
                                    user.orders.append(new_order)
                                    new_order.save_to_db()
                                    user.save_to_db()
                                    event.save_to_db()
                                    return new_order.json(), 201
                                except:
                                    return {n.MSG: "Error Description"}, 500
                            else:
                                return {n.MSG: "The event does not have enough available ticket"}, 400
                        else:
                            return {n.MSG: "The user does not have enough money"}, 400
                    else:
                        return {n.MSG: "Event not found"}, 404
                else:
                    return {n.MSG: "Username error"}, 400
        else:
            return {n.MSG: "Username not found"}, 404
Esempio n. 5
0
 def get(self, username):
     account = AccountsModel.find_by_username(username)
     if account:
         return {n.ORDRS: account.orders}, 200
     else:
         return {n.MSG: "Username not found"}, 404
Esempio n. 6
0
 def get(self, username):
     user = AccountsModel.find_by_username(username)
     if user:
         return user.json(), 200
     else:
         return {n.MSG: "User not found"}, 404