def get_product_byID(mongo, id): try: response = Response() extracted= mongo.db.product.find_one_or_404({'_id': ObjectId(id)}) processed = { 'id': str(extracted['_id']), 'groupID': extracted['groupID'], 'groupName': extracted['groupName'], 'category': extracted['category'], 'name': extracted['name'], 'link': extracted['link'], 'brand': extracted['brand'], 'imageURL': extracted['imageURL'], 'price': extracted['price'], 'description': extracted['description'], 'quantity': extracted['quantity'], 'star': extracted['star'], 'soldNumber': extracted['soldNumber'] } response.create(Response.SUCCESS) response.data = processed output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) except: response.create(Response.ERROR) response.data = "Datebase connection is false." output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) return output
def get_product_by_category(mongo, category): try: response = Response() processed = [] extracted = list(mongo.db.product.find({'category': category})) for product in extracted: processed.append({ 'id': str(product['_id']), 'groupID': product['groupID'], 'groupName': product['groupName'], 'category': product['category'], 'name': product['name'], 'link': product['link'], 'brand': product['brand'], 'imageURL': product['imageURL'], 'price': product['price'], 'description': product['description'], 'quantity': product['quantity'], 'star': product['star'], 'soldNumber': product['soldNumber'] }) response.create(Response.SUCCESS) response.data = processed output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) except: response.create(Response.ERROR) response.data = "Datebase connection is false." output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) return output
def get_all_categories_non_group(mongo): try: response = Response() processed = [] extracted = list(mongo.db.productCategory.find({})) for categoryGroup in extracted: for category in categoryGroup['children']: processed.append({ 'id': category['id'], 'name': category['name'], 'quantity': category['quantity'], 'url': category['url'], 'icon': category['icon'], 'brands': category['brands'] }) response.create(Response.SUCCESS) response.data = processed output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) except: print(response.data) response.create(Response.ERROR) response.data = "Datebase connection is false." output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) return output
def add_product(mongo): try: response = Response() params = json.loads(request.data) mongo.db.product.insert(params) response.create(Response.SUCCESS) response.data = 'Product was created.' output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) except: response.create(Response.ERROR) response.data = "Datebase connection is false." output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) return output
def get_customer(mongo): try: response = Response() processed = [] extracted = list(mongo.db.customer.find({})) for customer in extracted: paidNumber = 0 unPaidNumber = 0 for bill in customer['bill']: bill['id'] = str(bill['_id']) bill.pop('_id') if bill['status'] == True: paidNumber = paidNumber + 1 else: unPaidNumber = unPaidNumber + 1 processed.append({ 'uid': customer['uid'], 'email': customer['email'], 'name': customer['bill'][0]['infor']['name'], 'bill': customer['bill'], 'paidNumber': paidNumber, 'unPaidNumber': unPaidNumber }) response.create(Response.SUCCESS) response.data = processed output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) except Exception as e: print(e) response.create(Response.ERROR) output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) return output
def delete_category(mongo): try: response = Response() params = json.loads(request.data) mongo.db.productCategory.remove({'id': params['id']}) response.create(Response.SUCCESS) response.data = 'category was deleted.' output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) except: response.create(Response.ERROR) response.data = 'Datebase connection is false.' output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) return output
def delete_product(mongo): try: response = Response() params = json.loads(request.data) mongo.db.product.remove({'_id': ObjectId(params['id'])}) response.create(Response.SUCCESS) response.data = 'Product was deleted.' output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) except Exception as e: print(e) response.create(Response.ERROR) response.data = 'Datebase connection is false.' output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) return output
def get_map(mongo): try: response = Response() processed = [] extracted = mongo.db.hanoiDistricts.find({}) for district in extracted: processed.append({ 'id': str(district['id']), 'name': district['name'], 'location': district['location'], 'type': district['type'], 'province_id': district['province_id'], 'ward': district['ward'] }) response.create(Response.SUCCESS) response.data = processed output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) except Exception as e: print(e) response.create(Response.ERROR) output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) return output
def update_star(mongo): try: response = Response() params = json.loads(request.data) params['_id'] = ObjectId(params['id']) params.pop('id') # print(params, params['_id']) mongo.db.product.update({'_id': params['_id']}, {'$set': {'star':params['star']}}) response.create(Response.SUCCESS) response.data = 'category was updated.' output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) except: response.create(Response.ERROR) response.data = 'Datebase connection is false.' output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) return output
def get_product_by_category_page(mongo, category, page, numOfElement): try: page = int(page) numOfElement = int(numOfElement) response = Response() extracted = list(mongo.db.product.find({'category': category})) processed = split_product_by_page(extracted, page, numOfElement) response.create(Response.SUCCESS) response.data = processed output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) except Exception as e: print(e) response.create(Response.ERROR) response.data = "Datebase connection is false." output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) return output
def update_product(mongo): try: response = Response() params = json.loads(request.data) # Modify 'id' field to '_id' field params['_id'] = ObjectId(params['id']) params.pop('id') mongo.db.product.update({'_id': params['_id']}, {'$set': params}) response.create(Response.SUCCESS) response.data = 'Product was updated.' output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) except Exception as e: print(e) response.create(Response.ERROR) response.data = 'Datebase connection is false.' output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) return output
def get_product_by_filter(mongo, searchedString, page, numOfElement): try: page = int(page) numOfElement = int(numOfElement) response = Response() data_all = list(mongo.db.product.find({})) extracted = list(filter(lambda y: (no_accent_vietnamese(y['brand']).count(no_accent_vietnamese(searchedString))) or (no_accent_vietnamese(y['name']).count(no_accent_vietnamese(searchedString))) or (no_accent_vietnamese(y['groupName']).count(no_accent_vietnamese(searchedString))),data_all)) processed = split_product_by_page(extracted, page, numOfElement) response.create(Response.SUCCESS) response.data = processed output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False).encode('utf-8')) except: response.create(Response.ERROR) response.data = "Datebase connection is false." output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False).encode('utf-8')) return output
def get_bill_by_all_year(mongo): try: response = Response() processed = [] extracted = list(mongo.db.bill.find({"status": True})) for paidBill in extracted: processed.append({ 'cart': paidBill['cart'], 'date': paidBill['date'], 'totalMoney': paidBill['totalMoney'], }) response.create(Response.SUCCESS) response.data = processed output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) except: response.create(Response.ERROR) response.data = "Datebase connection is false." output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) return output
def get_all_categories(mongo): try: response = Response() processed = [] extracted = list(mongo.db.productCategory.find({})) for category in extracted: processed.append({ 'groupId': category['groupId'], 'name': category['name'], 'children': category['children'] }) response.create(Response.SUCCESS) response.data = processed output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) except: response.create(Response.ERROR) response.data = "Datebase connection is false." output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) return output
def get_number_of_customers(mongo): try: response = Response() extracted = list(mongo.db.customer.find({})) response.create(Response.SUCCESS) response.data = len(extracted) output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) except Exception as e: print(e) response.create(Response.ERROR) output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) return output
def get_number_of_unpaid_bills(mongo): try: response = Response() extracted = list(mongo.db.bill.find({"status": False})) length = len(extracted) response.create(Response.SUCCESS) response.data = length output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) except: response.create(Response.ERROR) output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) return output
def get_all_type_of_payment(mongo): try: response = Response() processed = [] extracted = list(mongo.db.typeOfPayment.find({})) for type in extracted: processed.append({ 'id': type['_id'], 'value': type['value'], 'name': type['name'] }) response.create(Response.SUCCESS) response.data = processed output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) except: response.create(Response.ERROR) output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) return output
def getUnPaidBill(mongo, typeOfPayment): try: response = Response() processed = [] if typeOfPayment == "cash": extracted = list( mongo.db.bill.find({ "status": False, "typeOfPayment": "cash" })) elif typeOfPayment == "card": extracted = list( mongo.db.bill.find({ "status": False, "typeOfPayment": "card", "onlinePaymentChecking": True })) for bill in extracted: processed.append({ 'id': str(bill['_id']), 'uid': bill['uid'], 'email': bill['email'], 'cart': bill['cart'], 'infor': bill['infor'], 'date': bill['date'], 'totalMoney': bill['totalMoney'], 'status': bill['status'] }) response.create(Response.SUCCESS) response.data = processed output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) except: response.create(Response.ERROR) output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False)) return output