Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ft delete category #6

Merged
merged 22 commits into from
Oct 31, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
language: python
python:
- "3.6.4"
install:
- pip install pytest
- pip install -r requirements.txt
- pip install coveralls
- pip install pytest-cov
services:
- postgresql
# env:
# global:
# - app.config["TESTING"] = True
before_script:
- psql -c 'create database manager;' -U postgres
- psql -c 'create database test_db;' -U postgres
script:
- python -m pytest
- pytest --cov
after_success:
- coveralls
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<!-- [![Build Status](https://travis-ci.org/oma0256/store-manager-api.svg?branch=develop)](https://travis-ci.org/oma0256/store-manager-api)
[![Coverage Status](https://coveralls.io/repos/github/oma0256/store-manager-api/badge.svg?branch=develop)](https://coveralls.io/github/oma0256/store-manager-api)
[![Maintainability](https://api.codeclimate.com/v1/badges/3303ae1c369c0bd693df/maintainability)](https://codeclimate.com/github/oma0256/store-manager-api/maintainability) -->
[![Build Status](https://travis-ci.org/oma0256/store-manager-api.svg?branch=ft-delete-category)](https://travis-ci.org/oma0256/store-manager-api)
[![Coverage Status](https://coveralls.io/repos/github/oma0256/store-manager-api/badge.svg?branch=ft-delete-category)](https://coveralls.io/github/oma0256/store-manager-api)
[![Maintainability](https://api.codeclimate.com/v1/badges/3303ae1c369c0bd693df/maintainability)](https://codeclimate.com/github/oma0256/store-manager-api/maintainability)
# Store Manager Api
Store Manager is a web application that helps store owners manage sales and product inventory records. This application is meant for use in a single store.

Expand Down
2 changes: 1 addition & 1 deletion api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@
from flask_jwt_extended import JWTManager

app = Flask(__name__)
app.secret_key = 'super secret key'


# Setup flask-jwt-extended
app.config['JWT_SECRET_KEY'] = 'sajsvhca'
app.config.from_object("config.DevelopmentConfig")
jwt = JWTManager(app)

from api import views
8 changes: 8 additions & 0 deletions api/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,11 @@ def __init__(self, sale_id, cart_items,
self.cart_items = cart_items
self.attendant = attendannt
self.total = total

class Category:
"""
Define Category structure
"""
def __init__(self, name, description=None):
self.name = name
self.description = description
141 changes: 118 additions & 23 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from flask_jwt_extended import (create_access_token,
jwt_required,
get_jwt_identity)
from api.models import Product, Sale, User
from api.models import Product, Sale, User, Category
from api.__init__ import app
from api.utils.validators import (validate_product,
validate_login_data,
Expand All @@ -34,7 +34,6 @@ def post(self):
Function to perform user login
"""
# Get data sent
db_conn = DB()
data = request.get_json()
# Get attributes of the data sent
email = data.get("email")
Expand Down Expand Up @@ -76,8 +75,6 @@ def post(self):
"""
Function to add a store attendant
"""
db_conn = DB()

# Get logged in user
current_user = get_jwt_identity()
loggedin_user = db_conn.get_user(current_user)
Expand Down Expand Up @@ -130,8 +127,6 @@ def post(self):
"""
Handles creating of a product
"""
db_conn = DB()

# Get logged in user
current_user = get_jwt_identity()
loggedin_user = db_conn.get_user(current_user)
Expand Down Expand Up @@ -170,7 +165,6 @@ def get(self, product_id=None):
"""
Get all products
"""
db_conn = DB()
# Check if an id has been passed
if product_id:
product = db_conn.get_product_by_id(int(product_id))
Expand All @@ -180,21 +174,21 @@ def get(self, product_id=None):
"error": "This product does not exist"
}), 404
return jsonify({
"message": "Product returned successfully"
"message": "Product returned successfully",
"product": product
})
# Get all products
db_conn.get_products()
products = db_conn.get_products()
return jsonify({
"message": "Products returned successfully"
"message": "Products returned successfully",
"products": products
})

@jwt_required
def put(self, product_id):
"""
Funtion to modify a product
"""
db_conn = DB()

# Get logged in user
current_user = get_jwt_identity()
loggedin_user = db_conn.get_user(current_user)
Expand Down Expand Up @@ -232,8 +226,6 @@ def delete(self, product_id):
"""
Funtion to delete a product
"""
db_conn = DB()

# Get logged in user
current_user = get_jwt_identity()
loggedin_user = db_conn.get_user(current_user)
Expand Down Expand Up @@ -267,8 +259,6 @@ def post(self):
"""
Method to create a sale record
"""
db_conn = DB()

# Get logged in user
current_user = get_jwt_identity()
loggedin_user = db_conn.get_user(current_user)
Expand All @@ -284,7 +274,7 @@ def post(self):
total = 0
product_names = ""
for cart_item in cart_items:
product_id = cart_item.get("product")
product_id = cart_item.get("product_id")
quantity = cart_item.get("quantity")
# validate each product
res = validate_cart_item(product_id, quantity)
Expand All @@ -311,7 +301,6 @@ def get(self, sale_id=None):
"""
Perform GET on sale records
"""
db_conn = DB()
# Get current user
current_user = get_jwt_identity()
user = db_conn.get_user(current_user)
Expand All @@ -327,22 +316,123 @@ def get(self, sale_id=None):
# run if it's a store owner
if user["is_admin"]:
return jsonify({
"message": "Sale record returned successfully"
"message": "Sale record returned successfully",
"sale_record": sale_record
})
# run if it's a store attendant
if sale_record["attendant"] == db_conn.get_user(current_user)["id"]:
return jsonify({
"message": "Sale record returned successfully"
"message": "Sale record returned successfully",
"sale_record": sale_record
})
return jsonify({"error": "You didn't make this sale"}), 403
# run if request is for all sale records and if it's a store
# owner
if user["is_admin"]:
sale_records = db_conn.get_sale_records()
return jsonify({
"message": "Sale records returned successfully"
"message": "Sale records returned successfully",
"sale_records": sale_records
})
return jsonify({"error": "Please login as a store owner"}), 403
# run if request is for all sale records and if it's a store
# attendant
if not user["is_admin"]:
sale_records = db_conn.get_sale_records_user(user["id"])
return jsonify({
"message": "Sale records returned successfully",
"sale_records": sale_records
})


class CategoryView(MethodView):

@jwt_required
def post(self):
current_user = get_jwt_identity()
user = db_conn.get_user(current_user)
if not user["is_admin"]:
return jsonify({
"error": "Please login as a store owner"
}), 403
data = request.get_json()
name = data.get("name")
description = data.get("description")
if not name:
return jsonify({
"error": "The category name is required"
}), 400
category = db_conn.get_category_by_name(name)
if category:
return jsonify({
"error": "Category with this name exists"
}), 400
new_category = Category(name, description=description)
db_conn.add_category(new_category)
return jsonify({
"message": "Successfully created product category"
}), 201

@jwt_required
def put(self, category_id):
"""
Function to modify a category
"""
# Get logged in user
current_user = get_jwt_identity()
loggedin_user = db_conn.get_user(current_user)
# # Check if it's not store owner
if not loggedin_user["is_admin"]:
return jsonify({
"error": "Please login as a store owner"
}), 403

# Check if category exists
category = db_conn.get_category_by_id(int(category_id))
if not category:
return jsonify({
"error": "The category you're trying to modify doesn't exist"
}), 404

data = request.get_json()
# Get the fields which were sent
name = data.get("name")
description = data.get("description")
if not name:
return jsonify({
"error": "The category name is required"
}), 400

# Modify category
db_conn.update_category(name, description, category_id)
return jsonify({
"message": "Category updated successfully"
})

@jwt_required
def delete(self, category_id):
"""
Funtion to category a product
"""
# Get logged in user
current_user = get_jwt_identity()
loggedin_user = db_conn.get_user(current_user)
# Check if it's not store owner
if loggedin_user["is_admin"]:
# Check if category exists
category = db_conn.get_category_by_id(int(category_id))
if not category:
return jsonify({
"error": "Category you're trying to delete doesn't exist"
}), 404

# Delete category
db_conn.delete_category(int(category_id))
return jsonify({
"message": "Category has been deleted successfully"
})
return jsonify({
"error": "Please login as a store owner"
}), 403


# Map urls to view classes
Expand All @@ -359,5 +449,10 @@ def get(self, sale_id=None):
app.add_url_rule('/api/v2/sales',
view_func=SaleView.as_view('sale_view'),
methods=["GET","POST"])
app.add_url_rule('/api/v1/sales/<sale_id>',
app.add_url_rule('/api/v2/sales/<sale_id>',
view_func=SaleView.as_view('sale_view1'), methods=["GET"])
app.add_url_rule('/api/v2/categories',
view_func=CategoryView.as_view('category_view'),
methods=["POST"])
app.add_url_rule('/api/v2/categories/<category_id>',
view_func=CategoryView.as_view('category_view1'), methods=["PUT", "DELETE"])
5 changes: 5 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class TestConfig:
TESTING = True

class DevelopmentConfig:
TESTING = False
Loading