Exemple #1
0
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    
Exemple #2
0
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
Exemple #3
0
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
Exemple #4
0
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    
Exemple #5
0
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
Exemple #6
0
def add_bill(mongo):
    try:
        response = Response()
        params = json.loads(request.data)
        order = json.loads(request.data)
        customerDB = list(mongo.db.customer.find({}))
        if (len(customerDB)==0):
            output = add_customer(mongo, params, response)
        else:
            extracted = mongo.db.customer.find_one({'uid': params['uid']})            
            if (extracted): 
                bill = {}
                bill = params['bill']
                bill['uid'] = params['uid']
                bill['email'] = params['email']

                mongo.db.bill.insert_one(bill)  # Save to bill Colection 
                extracted['bill'].append(params['bill'])    # Add new bill to old bill list   
                # Save to database         
                mongo.db.customer.update({'uid': params['uid']}, {'$set': extracted})   # customer Colection 

                response.create(Response.SUCCESS)
                output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False))
            else:            
                output = add_customer(mongo, params, response)       
        ## Update quantity for product in DB
        output = update_product_quantity(mongo, order['bill']['cart'], response)
    except Exception as e:
        print(e)
        response.create(Response.ERROR)
        output = jsonify(json_util.dumps(response.__dict__, ensure_ascii=False))

    return output
Exemple #7
0
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
Exemple #8
0
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
Exemple #9
0
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
Exemple #10
0
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
Exemple #11
0
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  
Exemple #12
0
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
Exemple #13
0
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
Exemple #14
0
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
Exemple #15
0
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
Exemple #16
0
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
Exemple #17
0
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_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
Exemple #19
0
def submit_online_payment(mongo):
    try:
        response = Response()
        params = json.loads(request.data)
        mongo.db.bill.update({'onlinePaymentID': params['onlinePaymentID']}, {'$set':{'onlinePaymentChecking': True}})
        customer = mongo.db.customer.find_one_or_404({'uid': params['uid']})
        for bill in customer['bill']:
            if(bill['onlinePaymentID']==params['onlinePaymentID']):
                bill['onlinePaymentChecking'] = True
        mongo.db.customer.update({'uid': params['uid']}, {'$set':customer})
        response.create(Response.SUCCESS)
        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
Exemple #20
0
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
Exemple #21
0
def submit_delivery(mongo):
    try:
        response = Response()
        params = json.loads(request.data)  # params:   - uid: id of customer
        #           - id: id of bill
        billID = ObjectId(params['billID'])
        mongo.db.bill.update({'_id': billID}, {'$set': {'status': True}})
        customer = mongo.db.customer.find_one_or_404({'uid': params['uid']})
        for bill in customer['bill']:
            if (bill['_id'] == billID):
                bill['status'] = True
        mongo.db.customer.update({'uid': params['uid']}, {'$set': customer})
        response.create(Response.SUCCESS)
        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