def put(self): parser = reqparse.RequestParser() parser.add_argument('id', type=int, required=True) parser.add_argument('name', type=unicode, required=True) parser.add_argument('stock', type=int, required=True) parser.add_argument('description', type=unicode, required=True) parser.add_argument('price', type=float, required=True) parser.add_argument('is_available', type=bool, required=True) parser.add_argument('categories', type=int, required=True, action='append') args = parser.parse_args() store = get_default_store() p = store.find(Product, Product.id == args['id']).one() if p is None: return "Fail", 404 p.name = args['name'] p.stock = args['stock'] p.description = args['description'] p.price = Decimal(args['price']) p.is_available = args['is_available'] p.categories.clear() cats = store.find(Category, In(Category.id, args['categories'])) for c in cats: p.categories.add(c) store.flush() return "Success", 201
def post(self): parser = reqparse.RequestParser() parser.add_argument('name', type=unicode, required=True) parser.add_argument('stock', type=int, required=True) parser.add_argument('description', type=unicode, required=True) parser.add_argument('price', type=float, required=True) parser.add_argument('is_available', type=bool, required=True) parser.add_argument('categories', type=int, required=True, action='append') args = parser.parse_args() store = get_default_store() p = Product() p.name = args['name'] p.stock = args['stock'] p.description = args['description'] p.price = Decimal(args['price']) p.is_available = args['is_available'] cats = store.find(Category, In(Category.id, args['categories'])) for c in cats: p.categories.add(c) store.add(p) store.commit() return "Success", 201
def login_check(): user_id = session.pop('user', None) store = get_default_store() user = store.find(User, id=user_id).one() if user: return user.json() abort(403)
def put(self): parser = reqparse.RequestParser() parser.add_argument('id', type=int, required=True) parser.add_argument('name', type=unicode, required=True) parser.add_argument('stock', type=int, required=True) parser.add_argument('description', type=unicode, required=True) parser.add_argument('price', type=float, required=True) parser.add_argument('is_available', type=bool, required=True) parser.add_argument('categories', type=int, required=True, action='append') args = parser.parse_args() store = get_default_store() p = store.find(Product, Product.id == args['id']).one() if p is None: return "Fail",404 p.name = args['name'] p.stock = args['stock'] p.description = args['description'] p.price = Decimal(args['price']) p.is_available = args['is_available'] p.categories.clear() cats = store.find(Category, In(Category.id, args['categories'])) for c in cats: p.categories.add(c) store.flush() return "Success",201
def create_user_cart(): store = get_default_store() user = store.find(User, User.id == session['user']).one() # create new cart cart = Cart(user) store.add(cart) store.commit() return cart
def login(): """Log the user in.""" store = get_default_store() user = User.authenticate(store, request.form['email'],request.form['password']) if user: session['user'] = user.id return user.json() abort(403)
def login(): """Log the user in.""" store = get_default_store() user = User.authenticate(store, request.form['email'], request.form['password']) if user: session['user'] = user.id return user.json() abort(403)
def post(self): parser = reqparse.RequestParser() parser.add_argument('name', type=unicode, required=True) args = parser.parse_args() c = Category(args['name']) store = get_default_store() store.add(c) store.commit() return "Success", 201
def update_status(): """ Update order Status """ store = get_default_store() order = store.find(Order, Order.id == int(request.form['id'])).one() if order is None: return "Fail" order.status = unicode(request.form['status']) store.flush() return "Success"
def get_order_products(order): store = get_default_store() prods = store.find(OrderProduct, OrderProduct.order_id == order.id) order_products = [] for p in prods: pdict = p.product.dict() op = {'order_product_id': p.id,'product': pdict, 'quantity': p.quantity} order_products.append(op) return order_products
def post(self): parser = reqparse.RequestParser() parser.add_argument('name', type=unicode, required=True) args = parser.parse_args() c = Category(args['name']) store = get_default_store() store.add(c) store.commit() return "Success",201
def wrapper(*args, **kwargs): user_id = session.get('user', None) if user_id is None: abort(403) store = get_default_store() admin = store.find(Admin, Admin.id == user_id).one() if admin is None: abort(403) return function(*args, **kwargs)
def remove_product(): """ Remove product from cart """ store = get_default_store() op = store.find(OrderProduct, OrderProduct.id == int(request.form['id'])).one() if op is None: return "Fail" store.remove(op) store.commit() return "Success"
def delete(self): parser = reqparse.RequestParser() parser.add_argument('id', type=int, required=True) args = parser.parse_args() store = get_default_store() p = store.find(Product, Product.id == args['id']).one() if p is None: return "Fail",404 store.remove(p) store.commit() return "Success",204
def delete(self): parser = reqparse.RequestParser() parser.add_argument('id', type=int, required=True) args = parser.parse_args() store = get_default_store() p = store.find(Product, Product.id == args['id']).one() if p is None: return "Fail", 404 store.remove(p) store.commit() return "Success", 204
def put(self): parser = reqparse.RequestParser() parser.add_argument('id', type=int, required=True) parser.add_argument('name', type=unicode, required=True) args = parser.parse_args() store = get_default_store() c = store.find(Category, Category.id == args['id']).one() if c is None: return "Fail",404 c.name = args['name'] store.flush() return "Success",201
def close_order(): """ Close the cart order """ store = get_default_store() cart = store.find(Cart, Cart.user_id == session['user']).one() if cart is None: return "Fail" # can't close an empty order if cart.order.products.count() == 0: return "Fail" # can't close an empty order cart.order.status = u"Awaiting Payment" store.remove(cart) return "Success"
def register(): """Register a new user.""" if not is_email_address_valid(request.form['email']): abort(403) store = get_default_store() # Default user is customer, an admin must change the user to staff member (admin or vendor) found = store.find(User, User.email == request.form['email']).one() if found is not None: abort(403) new_customer = Customer() user = User() user.name = request.form['name'] user.email = request.form['email'] user.set_password(request.form['password']) store = get_default_store() new_customer.user = user store.add(new_customer) store.commit() session['user'] = user.id return user.json()
def put(self): parser = reqparse.RequestParser() parser.add_argument('id', type=int, required=True) parser.add_argument('name', type=unicode, required=True) args = parser.parse_args() store = get_default_store() c = store.find(Category, Category.id == args['id']).one() if c is None: return "Fail", 404 c.name = args['name'] store.flush() return "Success", 201
def set_admin(user_id): """ Turn an user into a Admin """ store = get_default_store() user = store.find(User, User.id == user_id).one() if user is None: abort(404) user = store.find(AdminView, AdminView.id == user_id).one() if user is not None: return "Already Admin!" admin = Admin() admin.user = user store.add(admin) return "True"
def set_vendor(user_id): """ Turn an user into a Vendor """ store = get_default_store() user = store.find(User, User.id == user_id).one() if user is None: abort(404) user = store.find(VendorView, VendorView.id == user_id).one() if user is not None: return "Already Vendor!" vendor = Vendor() vendor.user = user store.add(vendor) return "True"
def cart(): """ Get customer cart """ store = get_default_store() cart = store.find(Cart, Cart.user_id == session['user']).one() if cart is None: cart = create_user_cart() orderd = cart.order.dict() prods = get_order_products(cart.order) orderd['products'] = prods json_str = json.dumps(orderd) return Response(json_str, mimetype='application/json')
def add_product(): """ Add product to cart """ store = get_default_store() cart = store.find(Cart, Cart.user_id == session['user']).one() if cart is None: cart = create_user_cart() order = cart.order product = store.find(Product, Product.id == int(request.form['id'])).one() quantity = int(request.form['quantity']) op = OrderProduct(order,product,quantity) store.add(op) store.commit() return "Success"
def me(): """ Get data of the logged user """ store = get_default_store() user_id = session['user'] user = store.find(User, User.id == user_id).one() if user is None: abort(404) userd = {'id': user.id, 'name': user.name, 'email': user.email, 'birth_date': int(mktime(user.birth_date.timetuple()) * 1000), 'register_date': int(mktime(user.register_date.timetuple()) * 1000)} json_str = json.dumps(userd) return Response(json_str, mimetype='application/json')
def me(): """ Get data of the logged user """ store = get_default_store() user_id = session['user'] user = store.find(User, User.id == user_id).one() if user is None: abort(404) userd = { 'id': user.id, 'name': user.name, 'email': user.email, 'birth_date': int(mktime(user.birth_date.timetuple()) * 1000), 'register_date': int(mktime(user.register_date.timetuple()) * 1000) } json_str = json.dumps(userd) return Response(json_str, mimetype='application/json')
def post(self): parser = reqparse.RequestParser() parser.add_argument('name', type=unicode, required=True) parser.add_argument('stock', type=int, required=True) parser.add_argument('description', type=unicode, required=True) parser.add_argument('price', type=float, required=True) parser.add_argument('is_available', type=bool, required=True) parser.add_argument('categories', type=int, required=True, action='append') args = parser.parse_args() store = get_default_store() p = Product() p.name = args['name'] p.stock = args['stock'] p.description = args['description'] p.price = Decimal(args['price']) p.is_available = args['is_available'] cats = store.find(Category, In(Category.id, args['categories'])) for c in cats: p.categories.add(c) store.add(p) store.commit() return "Success",201
def get(self, id=None): """Serves data via the GET method""" store = get_default_store() if id is not None: id = int(id) data = self.table.find(store, id) return data data = store.find(self.table) filters = self.request_filters() if filters: data = data.find(filters) if self.order_by: data = data.order_by(self.order_by) if self.has_limit(): data = data.config(limit=self.limit) if self.has_offset(): data = data.config(offset=self.offset) if self.is_count(): return data.count() return data
def is_admin(self): store = get_default_store() return store.find(Admin, id=self.id).one() is not None