Skip to content

Commit

Permalink
Merge pull request #22 from hogum/ft-models-implemetation-161413448
Browse files Browse the repository at this point in the history
Ft models implemetation 161413448
  • Loading branch information
mugoh committed Dec 25, 2018
2 parents 8658100 + 2474423 commit a8159ad
Show file tree
Hide file tree
Showing 25 changed files with 1,745 additions and 29 deletions.
1 change: 1 addition & 0 deletions .coverage
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!coverage.py: This is a private format, don't read it directly!{"lines":{"/root/Documents/save_me/storeManager/app/__init__.py":[1],"/root/Documents/save_me/storeManager/app/app.py":[2,3,4,5,7,8,9,10,14,15,18,19,20,23,24,25,26,27,28,30,31,35,36,39,40,41,44,45,46,47,48,49,51,52,59,60,61,62,63,64,69,70,71,72,73,74,75,81,82,84,85,86,91,92,93,94,95,96,101,102,103,104,105,111,112,113,116,117,125,127,128,129,131,132,134,135,137,138,140,141,146,150,151,155,163,171,179,187,188,190,245,250,286,287,290,301,309,326,340,341,342,368,374,394,395,396,416,426,442,455,456,457,460,461,462,463,343,344,345,346,349,350,351,354,355,357,358,361,362,363,366,370,371,397,398,399,402,403,406,407,410,411,414,417,419,423,165,166,167,157,158,376,379,380,381,382,383,384,387,390,391,420,444,446,448,451,428,430,432,434,435,439,431,173,174,196,197,198,199,200,203,204,205,207,208,209,211,212,213,217,218,219,220,222,223,224,226,227,228,230,231,232,234,236,237,239,240,241,243,247,291,292,293,294,295,296,297,299,302,304,307,251,254,255,256,257,258,259,260,261,262,263,264,265,266,270,272,273,274,279,282,283,181,182,183,305,327,328,331,333,336,310,311,314,317,320,321,324,315],"/root/Documents/save_me/storeManager/app/api/app.py":[],"/root/Documents/save_me/storeManager/app/api/__init__.py":[]}}
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
.DS_Store

__pycache__/
*.py[cod]
*$py.class

# virtualenv
.pyvirtual/
pyvirtual/
env/

14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: python
python:
- "3.6"
cache: pip

install:
- pip install -r requirements.txt

script:
- coverage run -m pytest
- py.test --cov=app

after_success:
- coveralls
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: gunicorn app.app:my_app --preload --workers 4
13 changes: 13 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,16 @@
# storeManager


[![Coverage Status](https://coveralls.io/repos/github/hogum/storeManager/badge.svg)](https://coveralls.io/github/hogum/storeManager)

[![Code Climate](https://codeclimate.com/github/codeclimate/codeclimate/badges/gpa.svg)](https://codeclimate.com/github/hogum/storeManager)

[![GitHub issues](https://img.shields.io/github/issues/hogum/storeManager.svg?style=for-the-badge)](https://github.com/hogum/storeManager/issues)

A web application to help store managers maintain their inventories and manage sale records.


Try on Heroku: https://store-man90.herokuapp.com/stman/api/v1.0/products


[![License: GPL v3](https://img.shields.io/badge/License-GPL%20v3-blue.svg)](https://www.gnu.org/licenses/gpl-3.0)
1 change: 1 addition & 0 deletions app/.coverage
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
!coverage.py: This is a private format, don't read it directly!{"lines":{"/root/Documents/save_me/storeManager/app/api/app.py":[],"/root/Documents/save_me/storeManager/app/api/__init__.py":[]}}
Empty file added app/__init__.py
Empty file.
4 changes: 4 additions & 0 deletions app/api/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .app import create_app

def app_instance(app_setting):
return create_app(app_setting)
29 changes: 29 additions & 0 deletions app/api/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from flask import Flask, Blueprint
from flask_restful import Api
from .views import product_views, sale_views, user_views
from app.instance.config import app_config

store_blueprint = Blueprint("store-man", __name__)


def create_app(config_setting):
my_app = Flask(__name__)
my_app.config.from_object(app_config[config_setting])
api = Api(store_blueprint)

api.add_resource(sale_views.SalesAPI,
'/stman/api/v1.0/sales', endpoint='sales')
api.add_resource(product_views.ProductsAPI,
'/stman/api/v1.0/products', endpoint='products')
api.add_resource(sale_views.SaleAPI,
'/stman/api/v1.0/sales/<int:sales_record>',
endpoint='sale')
api.add_resource(product_views.ProductAPI,
'/stman/api/v1.0/products/<int:id>',
endpoint='product')
api.add_resource(user_views.UsersAPI,
'/stman/api/v1.0/users/',
endpoint='users')

my_app.register_blueprint(store_blueprint)
return my_app
Empty file added app/api/models/__init__.py
Empty file.
22 changes: 22 additions & 0 deletions app/api/models/products.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from datetime import datetime


products = []

product_record_example = {
'title': 'Innocent Coconut Water',
'category': 'Women sure can Sleep',
'price': 94534,
'in stock': True,
'date received': datetime(
2018, 5, 30, 22, 12, 38, 649),
'id': 2
}


class Products(object):
@staticmethod
def productsList():
products.append(product_record_example)

return products
34 changes: 34 additions & 0 deletions app/api/models/sales.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
from datetime import datetime

sales = []

# example of a sale record

sale_example = {
'sales_record': 1,
'attendant': u'Attendant One',

# Customer contacts
'name': u'Customer One',
'address': u'45 bright street',
'contact': [u'+00012345', u'customer_c@example.co'],

# Transaction Info
'product': u'Spam 2.0',
'quantity': 1,
'date': datetime(2018, 6, 6, 5, 28, 56, 243),
'description': u'Hot with extra extra spam',
'transaction_type': u'Cash on Delivery',
'complete': False,

'gifts': 100, # Anything to reduce sale e.g discounts
'price': 276,
}


class Sales():
@staticmethod
def salesList():
sales.append(sale_example)

return sales
27 changes: 27 additions & 0 deletions app/api/models/users.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from werkzeug.security import generate_password_hash

users = []

cool_user_sample = {
'name': 'Evil Cow',
'username': 'e-cow',
'email': 'ecow@isus.mammals',
'password': generate_password_hash('wah!-things-we-do!'),
'user id': 1
}


class Users(object):
"""docstring for Users"""
def __init__(self, name, username, email, password):
super(Users, self).__init__()
self.name = name
self.username = username
self.email = email
self.password = password

@staticmethod
def usersList():
users.append(cool_user_sample)

return users
Empty file added app/api/views/__init__.py
Empty file.
131 changes: 131 additions & 0 deletions app/api/views/product_views.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
from flask_restful import Resource, reqparse, marshal, fields
from app.api.models import products
from datetime import datetime
from flask import abort

products = products.Products.productsList()


class ProductsAPI(Resource):
def __init__(self):
self.parse = reqparse.RequestParser()
self.parse.add_argument('title', type=str, required=True,
help="Please add a title",
location='json'
)

self.parse.add_argument('category', type=str,
default='None',
location='json'
)

self.parse.add_argument('price', type=int,
required=True,
help="You are not \
allowed to give out stuff for free",
location='json'
)

self.parse.add_argument('in stock', type=bool,
default=True,
location='json'
)

super(ProductsAPI, self).__init__()

def get(self):
return {
'product': [marshal(product, product_fields)
for product in products]
}

def post(self):
elements = self.parse.parse_args()

product = {
'title': elements['title'],
'category': elements['category'],
'price': elements['price'],
'in stock': True,
'date received': datetime.now(),
'id': products[-1]['id'] + 1
}

products.append(product)

return {
'product': marshal(product, product_fields)
}, 201


class ProductAPI(Resource):
"""docstring for ProductAPI"""
def __init__(self):
self.parse = reqparse.RequestParser()
self.parse.add_argument('title', type=str,
location='json'
)

self.parse.add_argument('category', type=str,
location='json'
)

self.parse.add_argument('price', type=int,
location='json'
)

self.parse.add_argument('in stock', type=bool,
location='json'
)

super(ProductAPI, self).__init__()

def get(self, id):
product = [product for product in
products if product['id'] is id]

if not product:
abort(404)

return {
'product': marshal(product[0], product_fields)
}

def put(self, id):
product = [product for product in
products if product['id'] is id]

if not product:
abort(404)
elements = self.parse.parse_args()

for key, value in list(elements.items()):
if value:
product[0][key] = value

return {
'product': marshal(product, product_fields)
}

def delete(self, id):
product = [product for product
in products if product['id'] is id]

if not product:
abort(404)
products.remove(product[0])

return {
'Status': True
}


product_fields = {
'title': fields.String,
'category': fields.String,
'price': fields.Integer,
'in stock': fields.Boolean,
'date received': fields.DateTime,
'url': fields.Url('product') # Ensure user doen't
# need to know how to generate url
}

0 comments on commit a8159ad

Please sign in to comment.