├── __init__.py ├── config.py ├── run.py ├── README.md ├── requirements.txt ├── utils.py ├── models.py └── routes.py /__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /config.py: -------------------------------------------------------------------------------- 1 | from run import app 2 | app.secret_key = "jaffa" 3 | app.config['SQLALCHEMY_DATABASE_URI'] = 'mysql://jaffa:jaffa@localhost/JaffaData' -------------------------------------------------------------------------------- /run.py: -------------------------------------------------------------------------------- 1 | from flask import Flask 2 | from flask_sqlalchemy import SQLAlchemy 3 | app = Flask(__name__) 4 | db = SQLAlchemy(app) 5 | from routes import * 6 | from models import * 7 | from config import * 8 | db.create_all() 9 | if __name__ == '__main__': 10 | app.run("0.0.0.0", 8080) -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Weavedin 2 | 3 | Steps to run 4 | 5 | pip install -r requirements.txt 6 | 7 | Go to mysql console and execute below commands 8 | 9 | 10 | CREATE DATABASE JaffaData; 11 | 12 | 13 | CREATE USER 'jaffa'@'localhost' IDENTIFIED BY 'jaffa'; 14 | 15 | 16 | GRANT ALL PRIVILEGES ON *.* TO 'jaffa'@'localhost'; 17 | 18 | 19 | 20 | Now run 21 | 22 | python run.py 23 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | Flask==1.0.2 2 | Flask-HTTPAuth==3.2.4 3 | Flask-Login==0.4.1 4 | Flask-MySQL==1.4.0 5 | Flask-SQLAlchemy==2.3.2 6 | Jinja2==2.10 7 | MarkupSafe==1.0 8 | PyMySQL==0.9.2 9 | SQLAlchemy==1.2.10 10 | Werkzeug==0.14.1 11 | asn1crypto==0.24.0 12 | cffi==1.11.5 13 | click==6.7 14 | cryptography==2.3 15 | idna==2.7 16 | itsdangerous==0.24 17 | mysqlclient==1.3.13 18 | pycparser==2.18 19 | six==1.11.0 20 | -------------------------------------------------------------------------------- /utils.py: -------------------------------------------------------------------------------- 1 | import datetime, json 2 | from models import * 3 | from enum import IntEnum 4 | class OPER(IntEnum): 5 | ADD = 1 6 | MODIFY = 2 7 | DELETE = 3 8 | 9 | class OBJ(IntEnum): 10 | ITEM = 1 11 | VARIANT = 2 12 | 13 | def add_update(op, data, user, objtype): 14 | now_time = datetime.datetime.now() 15 | old_time = datetime.datetime.now() - datetime.timedelta(minutes=5) 16 | updates = Data_Update.query.filter_by(updater=user).filter(Data_Update.update_time>old_time, Data_Update.update_time' % self.email 40 | def get_id(self): 41 | return str(self.id) 42 | 43 | class Data_Update(db.Model): 44 | id = db.Column('update_id', db.Integer, primary_key = True) 45 | update_time = db.Column(db.DateTime(timezone=True), nullable=False) 46 | update = db.Column(db.Text) 47 | user_id = db.Column(db.Integer, db.ForeignKey('user.user_id'), nullable=True) 48 | def __init__(self, update): 49 | self.update_time = datetime.datetime.now() 50 | self.update = update -------------------------------------------------------------------------------- /routes.py: -------------------------------------------------------------------------------- 1 | from flask_login import LoginManager, login_user, login_required, logout_user, current_user 2 | from flask import request, Response 3 | import json 4 | from models import * 5 | import datetime 6 | from utils import * 7 | login_manager = LoginManager() 8 | login_manager.init_app(app) 9 | 10 | @app.route('/add_item', methods = ['POST']) 11 | @login_required 12 | def add_item(): 13 | try: 14 | name = request.form['name'] 15 | brand = request.form['brand'] 16 | category = request.form['category'] 17 | product_code = request.form['product_code'] 18 | item = Item(name, brand, category, product_code) 19 | db.session.add(item) 20 | db.session.commit() 21 | return Response('{"message":"Added item"}', status=200, mimetype='application/json') 22 | add_update(OPER.ADD, name, current_user, OBJ.ITEM) 23 | except: 24 | return Response('{"message":"Invalid input"}', status=400, mimetype='application/json') 25 | 26 | @app.route('/update_item', methods = ['POST']) 27 | @login_required 28 | def update_item(): 29 | try: 30 | item_id = request.form['id'] 31 | item = Item.query.get_or_404(item_id) 32 | update_info = {} 33 | change = False 34 | if('name' in request.form and request.form['name'] != item.name): 35 | name = request.form['name'] 36 | update_info["name"] = name 37 | item.name = name 38 | change = True 39 | if('brand' in request.form and request.form['brand'] != item.brand): 40 | brand = request.form['brand'] 41 | update_info["brand"] = brand 42 | item.brand = brand 43 | change = True 44 | if('category' in request.form and request.form['category'] != item.category): 45 | category = request.form['category'] 46 | update_info["category"] = request.form['category'] 47 | item.category = category 48 | change = True 49 | if('product_code' in request.form and request.form['product_code'] != item.product_code): 50 | product_code = request.form['product_code'] 51 | update_info["product_code"] = request.form['product_code'] 52 | item.product_code = product_code 53 | change = True 54 | if(not change): 55 | return Response('{"message":"No changes"}', status=200, mimetype='application/json') 56 | add_update(OPER.MODIFY, update_info, current_user, OBJ.ITEM) 57 | db.session.commit() 58 | return Response('{"message":"Updated item"}', status=200, mimetype='application/json') 59 | except: 60 | return Response('{"message":"Invalid input"}', status=400, mimetype='application/json') 61 | 62 | @app.route('/delete_item', methods = ['POST']) 63 | @login_required 64 | def delete_item(): 65 | try: 66 | item_id = request.form['id'] 67 | name = request.form['name'] 68 | item = Item.query.get_or_404(item_id) 69 | db.session.delete(item) 70 | db.session.commit() 71 | add_update(OPER.DELETE, name, current_user, OBJ.ITEM) 72 | return Response('{"message":"Deleted item"}', status=200, mimetype='application/json') 73 | except: 74 | return Response('{"message":"Invalid input"}', status=400, mimetype='application/json') 75 | 76 | @app.route('/add_variant', methods = ['POST']) 77 | @login_required 78 | def add_variant(): 79 | try: 80 | name = request.form['name'] 81 | selling_price = request.form['selling_price'] 82 | cost_price = request.form['cost_price'] 83 | properties = request.form['properties'] 84 | quantity = request.form['quantity'] 85 | item_id = request.form['id'] 86 | var_item = Item.query.get_or_404(item_id) 87 | email = request.form['email'] 88 | variant = Variant(name=name, selling_price=selling_price, cost_price=cost_price, properties=properties, quantity=quantity, item=var_item) 89 | db.session.add(variant) 90 | db.session.commit() 91 | add_update(OPER.ADD, name, current_user, OBJ.VARIANT) 92 | return Response('{"message":"Added variant"}', status=200, mimetype='application/json') 93 | except: 94 | return Response('{"message":"Invalid input"}', status=400, mimetype='application/json') 95 | 96 | @app.route('/update_variant', methods = ['POST']) 97 | @login_required 98 | def update_variant(): 99 | try: 100 | var_id = request.form['id'] 101 | variant = Variant.query.get_or_404(var_id) 102 | update_info = {} 103 | change = False 104 | if('cost_price' in request.form and int(request.form['cost_price']) != variant.cost_price): 105 | cost_price = request.form['cost_price'] 106 | update_info["cost_price"] = cost_price 107 | variant.cost_price = cost_price 108 | change = True 109 | if('selling_price' in request.form and int(request.form['selling_price']) != variant.selling_price): 110 | selling_price = request.form['selling_price'] 111 | update_info["selling_price"] = selling_price 112 | variant.selling_price = selling_price 113 | change = True 114 | if('name' in request.form and request.form['name'] != variant.name): 115 | name = request.form['name'] 116 | update_info["name"] = request.form['name'] 117 | variant.name = name 118 | change = True 119 | if('quantity' in request.form and int(request.form['quantity']) != variant.quantity): 120 | quantity = request.form['quantity'] 121 | update_info["quantity"] = request.form['quantity'] 122 | variant.quantity = quantity 123 | change = True 124 | if('properties' in request.form and request.form['properties'] != variant.properties): 125 | properties = json.loads(request.form['properties']) 126 | current_properties = json.loads(variant.properties) 127 | for key in properties: 128 | if(key not in current_properties or current_properties[key] != properties[key]): 129 | update_info[key] = properties[key] 130 | current_properties[key] = properties[key] 131 | change = True 132 | variant.properties = json.dumps(current_properties) 133 | if(not change): 134 | return Response('{"message":"No changes"}', status=200, mimetype='application/json') 135 | add_update(OPER.MODIFY, update_info, current_user, OBJ.VARIANT) 136 | db.session.commit() 137 | return Response('{"message":"Updated item"}', status=200, mimetype='application/json') 138 | except: 139 | return Response('{"message":"Invalid input"}', status=400, mimetype='application/json') 140 | 141 | @app.route('/delete_variant', methods = ['POST']) 142 | @login_required 143 | def delete_variant(): 144 | try: 145 | var_id = request.form['id'] 146 | name = request.form['name'] 147 | variant = Variant.query.get_or_404(var_id) 148 | db.session.delete(variant) 149 | db.session.commit() 150 | add_update(OPER.DELETE, name, current_user, OBJ.VARIANT) 151 | return Response('{"message":"Deleted variant"}', status=200, mimetype='application/json') 152 | except: 153 | return Response('{"message":"Invalid input"}', status=400, mimetype='application/json') 154 | 155 | @app.route('/remove_property', methods = ['POST']) 156 | @login_required 157 | def remove_property(): 158 | try: 159 | var_id = request.form['id'] 160 | name = request.form['name'] 161 | variant = Variant.query.get_or_404(var_id) 162 | curr_properties = json.loads(variant.properties) 163 | if(name not in curr_properties): 164 | return Response('{"message":"Invalid input"}', status=400, mimetype='application/json') 165 | curr_properties.pop(name) 166 | variant.properties = json.dumps(curr_properties) 167 | db.session.commit() 168 | add_update(OPER.MODIFY, name, current_user, OBJ.VARIANT) 169 | return Response('{"message":"Removed property"}', status=200, mimetype='application/json') 170 | except: 171 | return Response('{"message":"Invalid input"}', status=400, mimetype='application/json') 172 | 173 | @app.route('/get_items', methods = ['GET']) 174 | @login_required 175 | def get_items(): 176 | itList = [] 177 | for it in Item.query.all(): 178 | itDict = { 179 | 'id': it.id, 180 | 'name': it.name, 181 | 'brand': it.brand, 182 | 'category': it.category, 183 | 'product_code': it.product_code} 184 | itList.append(itDict) 185 | return Response(json.dumps(itList), status=200, mimetype='application/json') 186 | 187 | @app.route('/get_variants', methods = ['GET']) 188 | @login_required 189 | def get_variants(): 190 | varList = [] 191 | for var in Variant.query.all(): 192 | varDict = { 193 | 'id': var.id, 194 | 'name': var.name, 195 | 'selling_price': var.selling_price, 196 | 'cost_price': var.cost_price, 197 | 'properties' : var.properties, 198 | 'quantity': var.quantity} 199 | varList.append(varDict) 200 | return Response(json.dumps(varList), status=200, mimetype='application/json') 201 | 202 | @app.route('/get_updates', methods = ['GET']) 203 | @login_required 204 | def get_updates(): 205 | updateList = [] 206 | user_id = request.args.get('user_id') 207 | if(user_id == None): 208 | data_update = Data_Update.query.all() 209 | else: 210 | data_update = Data_Update.query.filter_by(user_id=user_id) 211 | for update in data_update: 212 | updateDict = { 213 | 'update': update.update, 214 | 'user' : update.updater.name} 215 | updateList.append(updateDict) 216 | return Response(json.dumps(updateList), status=200, mimetype='application/json') 217 | 218 | @app.route('/register' , methods = ['POST']) 219 | def register(): 220 | try: 221 | user = User(request.form['email'] , request.form['password'], request.form['name']) 222 | db.session.add(user) 223 | db.session.commit() 224 | login_user(user) 225 | return Response('{"message":"User successfully registered"}', status=200, mimetype='application/json') 226 | except: 227 | return Response('{"message":"Invalid input"}', status=400, mimetype='application/json') 228 | 229 | @app.route('/login',methods=['POST']) 230 | def login(): 231 | try: 232 | email = request.form['email'] 233 | password = request.form['password'] 234 | registered_user = User.query.filter_by(email=email,password=password).first() 235 | if registered_user is None: 236 | return Response('{"message":"Invalid user"}', status=401, mimetype='application/json') 237 | login_user(registered_user) 238 | return Response('{"message":"Logged in successfully"}', status=200, mimetype='application/json') 239 | except: 240 | return Response('{"message":"Invalid input"}', status=400, mimetype='application/json') 241 | 242 | @app.route('/logout') 243 | @login_required 244 | def logout(): 245 | logout_user() 246 | return Response('{"message":"Logged out successfully"}', status=200, mimetype='application/json') 247 | 248 | @login_manager.user_loader 249 | def load_user(id): 250 | return User.query.filter_by(id=id).first() --------------------------------------------------------------------------------