Esempio n. 1
0
 def post(self, id):
     """
     Sell product
     """
     json_data = request.get_json(force=True)
     sales_validator(json_data)
     email = get_jwt_identity()
     user = Db.get_user(email=email)
     store_id = user.store_id
     number = json_data['number']
     product = Db.get_p_by_id(id)
     if product:
         price = product.price
         amount = number * price
         if product.inventory < number:
             d = product.inventory
             msg = 'There are only {} {} available'.format(d, product.name)
             return abort(400, msg)
         new_sale = Sale(store_id, product.name, number, amount)
         Db.sales.append(new_sale)
         res1 = new_sale.json_dump()
         new_inv = product.inventory - number
         product.inventory = new_inv
         return {"status": "Success!", "data": res1}, 201
     msg = 'Product does not exist'
     return {"message": msg}, 404
Esempio n. 2
0
    def get(self, id):
        """
        Get a specific product
        """

        email = get_jwt_identity()
        user = Db.get_user(email=email)
        store_id = user.store_id
        p = Db.get_p_by_id(id)
        if p.store_id != store_id:
            msg = 'Product does not exist'
            abort(404, msg)
        return {"status": "Success", "data": p.json_dump()}, 200
Esempio n. 3
0
 def delete(self, id):
     """
     Delete a product
     """
     p = Db.get_p_by_id(id)
     email = get_jwt_identity()
     user = Db.get_user(email=email)
     store_id = user.store_id
     if p.store_id != store_id:
         msg = 'Product does not exist'
         abort(404, msg)
     Db.products.remove(p)
     return {"status": "Deleted!", "data": p.json_dump()}, 200
Esempio n. 4
0
 def delete(self, id):
     """
     Delete a sale
     """
     sale = Db.get_s_by_id(id)
     email = get_jwt_identity()
     user = Db.get_user(email=email)
     store_id = user.store_id
     if sale.store_id != store_id:
         msg = 'That record does not exist'
         return abort(404, msg)
     sk = sale.json_dump()
     Db.sales.remove(sale)
     return {"status": "Deleted!", "data": sk}, 200
Esempio n. 5
0
    def get(self, id):
        """
        Get a specicific sale record
        """

        email = get_jwt_identity()
        user = Db.get_user(email=email)
        store_id = user.store_id
        sale = Db.get_s_by_id(id)
        if sale.store_id != store_id:
            msg = 'That record does not exist'
            return abort(404, msg)
        sk = sale.json_dump()
        return {"status": "Success!", "data": sk}, 200
Esempio n. 6
0
 def put(self, id):
     """
     Update a sale
     """
     s = Db.get_s_by_id(id)
     email = get_jwt_identity()
     user = Db.get_user(email=email)
     store_id = user.store_id
     if s.store_id != store_id:
         msg = 'Sale does not exist'
         abort(404, msg)
     json_data = request.get_json(force=True)
     sales_validator(json_data)
     number = json_data['number']
     s.number = number
     return {"status": "Success!", "data": s.json_dump()}, 200
Esempio n. 7
0
 def decorator(*args, **kwargs):
     current_user = Db.get_user(email=get_jwt_identity())
     r = current_user.role
     if r == 2 or r == 'Attendant':
         msg = "Only administrators can access these resource"
         abort(406, msg)
     return f(*args, **kwargs)
Esempio n. 8
0
 def decorator(*args, **kwargs):
     current_user = Db.get_user(email=get_jwt_identity())
     r = current_user.role
     if r != 'SuperAdmin':
         msg = "Only Super Admin can access these resource"
         abort(406, msg)
     return f(*args, **kwargs)
Esempio n. 9
0
 def post(self):
     """
     Add a product to the manager
     """
     json_data = request.get_json(force=True)
     product_validator(json_data)
     p = Db.get_product(json_data['name'])
     if p:
         msg = 'Product already exists.Update product inventory instead'
         abort(406, msg)
     email = get_jwt_identity()
     user = Db.get_user(email=email)
     store_id = user.store_id
     new_product = Product(store_id, json_data['name'],
                           json_data['inventory'], json_data['price'])
     Db.products.append(new_product)
     res = new_product.json_dump()
     return {"status": "Success!", "data": res}, 201
Esempio n. 10
0
 def test_sale_delete(self):
     """Test for the delete sale by id endpoint."""
     login = self.autheniticate()
     token = json.loads(login.data.decode()).get('token')
     self.app.post(p_url,
                   data=json.dumps(self.p_data),
                   headers=dict(Authorization="Bearer " + token),
                   content_type='application/json')
     p = Db.get_product('monster')
     self.app.post("/api/v1/products/{}".format(p.id),
                   data=json.dumps(self.s_data),
                   headers=dict(Authorization="Bearer " + token),
                   content_type='application/json')
     s = Db.get_s_by_product('monster')
     res = self.app.delete("/api/v1/sales/{}".format(s.id),
                           headers=dict(Authorization="Bearer " + token))
     res1 = json.loads(res.data.decode())
     self.assertEqual(res1['status'], 'Deleted!')
     self.assertEqual(res.status_code, 200)
Esempio n. 11
0
 def get(self):
     """
     Get all products
     """
     products = Db.products
     email = get_jwt_identity()
     user = Db.get_user(email=email)
     store_id = user.store_id
     res = [p.json_dump() for p in products if p.store_id == store_id]
     if len(products) < 1:
         res = {"message": 'There are no products at this time'}, 404
     return res
Esempio n. 12
0
 def post(self):
     """
     Add Admin
     """
     json_data = request.get_json(force=True)
     login_validator(json_data)
     email = get_jwt_identity()
     newad = Db.get_user(json_data['email'])
     if newad and newad.role <= 1:
         msg = "User is Admin already"
         abort(406, msg)
     user = Db.get_user(email=email)
     store_id = user.store_id
     role = 1
     user_reg = User(store_id, role, json_data['email'],
                     json_data['password'])
     for i, item in enumerate(Db.users):
         if item == newad:
             Db.users[i] = user_reg
     Db.users.append(user_reg)
     return {"status": "Success!", "data": user_reg.json_dump()}, 201
Esempio n. 13
0
 def post(self):
     """
     Create store
     """
     json_data = request.get_json(force=True)
     new_store_validator(json_data)
     check = Db.get_store(json_data['name'])
     if check:
         msg = 'Store name is already taken'
         abort(406, msg)
     new_store = Store(json_data['name'], json_data['category'])
     Db.stores.append(new_store)
     store = Db.get_store(json_data['name'])
     store_id = store.id
     role = 0
     new_sadmin = User(store_id, role, json_data['email'],
                       json_data['password'])
     Db.users.append(new_sadmin)
     res1 = new_store.json_dump()
     res2 = new_sadmin.json_dump()
     return {"status": "Success!", "store": res1, "owner": res2}, 201
Esempio n. 14
0
 def get(self):
     """
     Get all sales
     """
     sales = Db.sales
     if len(sales) < 1:
         res = {"message": 'There are no sale records'}, 404
         return res
     email = get_jwt_identity()
     user = Db.get_user(email=email)
     store_id = user.store_id
     s_list = [s.json_dump() for s in sales if s.store_id == store_id]
     return {"status": "Success!", "data": s_list}, 200
Esempio n. 15
0
 def post(self):
     """
     Add Attendant
     """
     json_data = request.get_json(force=True)
     login_validator(json_data)
     newatt = Db.get_user(json_data['email'])
     if newatt and newatt.role == 2:
         msg = "User is Attendant already"
         abort(406, msg)
     email = get_jwt_identity()
     user = Db.get_user(email=email)
     store_id = user.store_id
     role = 2
     user_reg = User(store_id, role, json_data['email'],
                     json_data['password'])
     newatt = Db.get_user(json_data['email'])
     for k, j in enumerate(Db.users):
         if j == newatt:
             Db.users[k] = user_reg
     Db.users.append(user_reg)
     return {"status": "Success!", "data": user_reg.json_dump()}, 201
Esempio n. 16
0
 def post(self):
     """
     Login
     """
     json_data = request.get_json(force=True)
     login_validator(json_data)
     u = Db.get_user(json_data['email'])
     epass = json_data['password']
     if u is None or not check_password_hash(u.password, epass):
         msg = 'Invalid credentials'
         abort(400, msg)
     access_token = create_access_token(identity=json_data['email'])
     return {"status": "Success!", "token": access_token}, 200
Esempio n. 17
0
 def put(self, id):
     """
     Edit a product
     """
     p = Db.get_p_by_id(id)
     email = get_jwt_identity()
     user = Db.get_user(email=email)
     store_id = user.store_id
     if p.store_id != store_id:
         msg = 'Product does not exist'
         abort(404, msg)
     json_data = request.get_json(force=True)
     product_update_validator(json_data)
     name = json_data['name']
     inventory = json_data['inventory']
     price = json_data['price']
     if name:
         p.name = name
     if inventory:
         p.inventory = inventory
     if price:
         p.price = price
     return {"status": "Success!", "data": p.json_dump()}, 200
Esempio n. 18
0
 def test_make_sale_with_no_num(self):
     """Test for the make sale endpoint."""
     login = self.autheniticate()
     token = json.loads(login.data.decode()).get('token')
     self.app.post(p_url,
                   data=json.dumps(self.p_data),
                   headers=dict(Authorization="Bearer " + token),
                   content_type='application/json')
     p = Db.get_product('monster')
     res = self.app.post("/api/v1/products/{}".format(p.id),
                         data=json.dumps(self.no_data),
                         headers=dict(Authorization="Bearer " + token),
                         content_type='application/json')
     res1 = json.loads(res.data.decode())
     self.assertEqual(res1['message'], 'Please provide the number field')
     self.assertEqual(res.status_code, 406)
Esempio n. 19
0
 def test_product_update_with_empty_name(self):
     """Test for the add product endpoint."""
     login = self.autheniticate()
     token = json.loads(login.data.decode()).get('token')
     self.app.post(p_url,
                   data=json.dumps(self.data),
                   headers=dict(Authorization="Bearer " + token),
                   content_type='application/json')
     p = Db.get_product('monster')
     url = "/api/v1/products/{}".format(p.id)
     res = self.app.put(url,
                        data=json.dumps(self.e_name),
                        headers=dict(Authorization="Bearer " + token),
                        content_type='application/json')
     res1 = json.loads(res.data.decode())
     self.assertEqual(res1['message'], 'The name can not be empty')
     self.assertEqual(res.status_code, 406)
Esempio n. 20
0
 def test_make_sale_with_morenum_than_available(self):
     """Test for the make sale endpoint."""
     login = self.autheniticate()
     token = json.loads(login.data.decode()).get('token')
     self.app.post(p_url,
                   data=json.dumps(self.p_data),
                   headers=dict(Authorization="Bearer " + token),
                   content_type='application/json')
     p = Db.get_product('monster')
     res = self.app.post("/api/v1/products/{}".format(p.id),
                         data=json.dumps(self.m_data),
                         headers=dict(Authorization="Bearer " + token),
                         content_type='application/json')
     res1 = json.loads(res.data.decode())
     self.assertEqual(res1['message'],
                      'There are only 24 monster available')
     self.assertEqual(res.status_code, 400)
Esempio n. 21
0
    def test_product_update(self):
        """Test for the product update endpoint."""
        login = self.autheniticate()
        token = json.loads(login.data.decode()).get('token')

        self.app.post(p_url,
                      data=json.dumps(self.data),
                      headers=dict(Authorization="Bearer " + token),
                      content_type='application/json')
        p = Db.get_product('monster')
        url = "/api/v1/products/{}".format(p.id)
        res = self.app.put(url,
                           data=json.dumps(self.data),
                           headers=dict(Authorization="Bearer " + token),
                           content_type='application/json')
        res1 = json.loads(res.data.decode())
        self.assertEqual(res1['status'], 'Success!')
        self.assertEqual(res.status_code, 200)
Esempio n. 22
0
 def test_product_update_with_str_inventory(self):
     """Test for the product update endpoint."""
     login = self.autheniticate()
     token = json.loads(login.data.decode()).get('token')
     self.app.post(p_url,
                   data=json.dumps(self.data),
                   headers=dict(Authorization="Bearer " + token),
                   content_type='application/json')
     p = Db.get_product('monster')
     url = "/api/v1/products/{}".format(p.id)
     res = self.app.put(url,
                        data=json.dumps(self.str_data),
                        headers=dict(Authorization="Bearer " + token),
                        content_type='application/json')
     res1 = json.loads(res.data.decode())
     self.assertEqual(res1['message'],
                      'Please make sure the inventory is a number')
     self.assertEqual(res.status_code, 406)