def validateBuyerStoreLeadData(self, buyer_store_lead, is_new): flag = 0 if not "name" in buyer_store_lead or buyer_store_lead["name"] == None: flag = 1 buyer_store_lead["name"] = self.name if not "mobile_number" in buyer_store_lead or not validate_mobile_number( buyer_store_lead["mobile_number"]): flag = 1 buyer_store_lead["mobile_number"] = self.mobile_number if not "email" in buyer_store_lead or not validate_email( buyer_store_lead["email"]): buyer_store_lead["email"] = self.email if not "status" in buyer_store_lead or not validate_integer( buyer_store_lead["status"]): buyer_store_lead["status"] = self.status if not "sizes" in buyer_store_lead or buyer_store_lead["sizes"] == None: buyer_store_lead["sizes"] = self.sizes if not "quantity" in buyer_store_lead or not validate_integer( buyer_store_lead["quantity"]): buyer_store_lead["quantity"] = self.quantity if is_new == 1 and flag == 1: return False return True
def update_buyer_address(request, parameters): try: requestbody = request.body.decode("utf-8") buyer_address = convert_keys_to_string(json.loads(requestbody)) except Exception as e: return customResponse(400, error_code=4) if parameters["isBuyer"] == 1: buyer_address["buyerID"] = parameters["buyersArr"][0] elif not "buyerID" in buyer_address or not validate_integer( buyer_address["buyerID"]): return customResponse(400, error_code=5, error_details="Id for buyer not sent") if not len(buyer_address): return customResponse( 400, error_code=5, error_details="Invalid data for buyer address sent") if not "addressID" in buyer_address or not validate_integer( buyer_address["addressID"]): return customResponse(400, error_code=5, error_details="Address Id for buyer not sent") buyerAddressPtr = BuyerAddress.objects.filter( buyer_id=int(buyer_address["buyerID"]), id=int(buyer_address["addressID"])) if len(buyerAddressPtr) == 0: return customResponse( 400, error_code=6, error_details="Invalid Address Id for buyer sent") buyerAddressPtr = buyerAddressPtr[0] validateBuyerAddressData(buyer_address, BuyerAddress(), 0) try: populateBuyerAddress(buyerAddressPtr, buyer_address) buyerAddressPtr.save() newBuyerAddressHistory = BuyerAddressHistory() newBuyerAddressHistory.populateFromBuyerAddress(buyerAddressPtr) newBuyerAddressHistory.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code=1) else: closeDBConnection() return customResponse( 200, {"address": serialize_buyer_address(buyerAddressPtr)})
def validateBuyerDetailsData(buyerdetails, oldbuyerdetails, is_new): if not "vat_tin" in buyerdetails or buyerdetails["vat_tin"]==None: buyerdetails["vat_tin"] = oldbuyerdetails.vat_tin if not "cst" in buyerdetails or buyerdetails["cst"]==None: buyerdetails["cst"] = oldbuyerdetails.cst if not "customer_type" in buyerdetails or buyerdetails["customer_type"]==None or not validate_buyer_customer_type(buyerdetails["customer_type"]): buyerdetails["customer_type"] = oldbuyerdetails.customer_type if not "buying_capacity" in buyerdetails or not validate_integer(buyerdetails["buying_capacity"]): buyerdetails["buying_capacity"] = oldbuyerdetails.buying_capacity if not "purchase_duration" in buyerdetails or not validate_integer(buyerdetails["purchase_duration"]): buyerdetails["purchase_duration"] = oldbuyerdetails.purchase_duration
def post_new_buyer_store_lead(request, parameters): try: requestbody = request.body.decode("utf-8") buyer_store_lead = convert_keys_to_string(json.loads(requestbody)) except Exception as e: return customResponse(400, error_code=4) if not len(buyer_store_lead): return customResponse(400, error_code=5, error_details= "Id for buyer not sent") if parameters["isBuyer"] == 1 or parameters["isBuyerStore"] == 1: buyer_store_lead["buyerID"] = parameters["buyersArr"][0] elif not "buyerID" in buyer_store_lead or not validate_integer(buyer_store_lead["buyerID"]): return customResponse(400, error_code=5, error_details= "Id for buyer not sent") buyerPtr = Buyer.objects.filter(id=int(buyer_store_lead["buyerID"]), delete_status=False) if not buyerPtr.exists(): return customResponse(400, error_code=6, error_details= "Invalid id for buyer sent") if not "productID" in buyer_store_lead or not validate_integer(buyer_store_lead["productID"]): return customResponse(400, error_code=5, error_details= "Id for product not sent") productParameters = {} productParameters["productsArr"] = [int(buyer_store_lead["productID"])] productParameters["product_verification"] = True productParameters["product_show_online"] = True productPtr = filterProducts(productParameters) if not productPtr.exists(): return customResponse(400, error_code=6, error_details= "Invalid id for product sent") newBuyerStoreLead = BuyerStoreLead(buyer_id=int(buyer_store_lead["buyerID"]), product_id = int(buyer_store_lead["productID"])) if not newBuyerStoreLead.validateBuyerStoreLeadData(buyer_store_lead, 1): return customResponse(400, error_code=5, error_details= "Invalid data for buyer store lead sent") try: newBuyerStoreLead.populateBuyerStoreLead(buyer_store_lead) newBuyerStoreLead.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code = 1) else: newBuyerStoreLead.sendRetailerMail(parameters) newBuyerStoreLead.sendCustomerMail(parameters) closeDBConnection() return customResponse(200, {"buyer_store_lead":serialize_buyer_store_lead(newBuyerStoreLead)})
def validateProductLotData(product_lots): flag = 1 for i in range(len(product_lots)): product_lot = product_lots[i] if not "lot_size_from" in product_lot or not validate_integer( product_lot["lot_size_from"]): flag = 0 if not "lot_size_to" in product_lot or not validate_integer( product_lot["lot_size_to"]): flag = 0 if not "price_per_unit" in product_lot or not validate_number( product_lot["price_per_unit"]): flag = 0 return flag
def validateCheckoutData(self, checkout): status = int(checkout["status"]) if self.status == 0 and status == 1: if "addressID" in checkout and validate_integer( checkout["addressID"]): return True elif self.status == 1 and status == 2: return True elif self.status == 2 and status == 3: if "payment_method" in checkout and validate_integer( checkout["payment_method"]) and int( checkout["payment_method"]) in [0, 1]: return True return False
def delete_article(request): try: requestbody = request.body.decode("utf-8") article = convert_keys_to_string(json.loads(requestbody)) except Exception as e: return customResponse(400, error_code=4) if not len(article) or not "articleID" in article or not validate_integer( article["articleID"]): return customResponse(400, error_code=5, error_details="Id for article not sent") articlePtr = Article.objects.filter(id=int(article["articleID"])) if len(articlePtr) == 0: return customResponse(400, error_code=6, error_details="Invalid ID for article sent") articlePtr = articlePtr[0] try: articlePtr.delete_status = True articlePtr.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code=3) else: closeDBConnection() return customResponse(200, {"body": "article deleted"})
def populateSellerIDParameters(request, parameters={}, version="0"): accessToken = getAccessToken(request) sellerID = request.GET.get("sellerID", "") tokenPayload = get_token_payload(accessToken, "sellerID") parameters["isSeller"] = 0 if "sellerID" in tokenPayload and validate_integer( tokenPayload["sellerID"]) and "password" in tokenPayload: try: sellerPtr = Seller.objects.get(id=int(tokenPayload["sellerID"]), password=tokenPayload["password"], delete_status=False) except: pass else: parameters["sellersArr"] = [sellerPtr.id] parameters["isSeller"] = 1 elif sellerID != "": parameters["sellersArr"] = getArrFromString(sellerID) sellerShowOnline = request.GET.get("seller_show_online", "") if sellerShowOnline != "" and validate_bool(sellerShowOnline): parameters["seller_show_online"] = int(sellerShowOnline) return parameters
def delete_product(request): try: requestbody = request.body.decode("utf-8") product = convert_keys_to_string(json.loads(requestbody)) except Exception as e: return customResponse(400, error_code=4) if not len(product) or not "productID" in product or not validate_integer( product["productID"]): return customResponse(400, error_code=5, error_details="Id for product not sent") productPtr = Product.objects.filter(id=int(product["productID"]), delete_status=False) if len(productPtr) == 0: return customResponse(400, error_code=6, error_details="Invalid id for product sent") productPtr = productPtr[0] try: productPtr.delete_status = True productPtr.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code=3) else: closeDBConnection() return customResponse(200, {"product": "product deleted"})
def validateSellerPaymentItemsData(orderItems, subOrderID): if len(orderItems) == 0: return False for orderItem in orderItems: if not "orderitemID" in orderItem or not validate_integer( orderItem["orderitemID"]): return False orderItemPtr = OrderItem.objects.filter( id=int(orderItem["orderitemID"])) if len(orderItemPtr) == 0: return False orderItemPtr = orderItemPtr[0] if orderItemPtr.current_status == 4: return False if orderItemPtr.seller_payment != None: return False if orderItemPtr.suborder_id != subOrderID: return False return True
def delete_buyer_buys_from(request, parameters): try: requestbody = request.body.decode("utf-8") buyer_buys_from = convert_keys_to_string(json.loads(requestbody)) except Exception as e: return customResponse(400, error_code=4) if not len( buyer_buys_from ) or not "buyerbuysfromID" in buyer_buys_from or not validate_integer( buyer_buys_from["buyerbuysfromID"]): return customResponse(400, error_code=5, error_details="Id for buyer buys_from not sent") buyerBuysFromPtr = BuyerBuysFrom.objects.filter( id=int(buyer_buys_from["buyerbuysfromID"])) if len(buyerBuysFromPtr) == 0: return customResponse( 400, error_code=6, error_details="Invalid id for buyer buys_from sent") try: buyerBuysFromPtr.update(delete_status=True, updated_at=timezone.now()) except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code=3) else: closeDBConnection() return customResponse(200, {"buyer": "buyer buys_from deleted"})
def delete_buyer(request, parameters): try: requestbody = request.body.decode("utf-8") buyer = convert_keys_to_string(json.loads(requestbody)) except Exception as e: return customResponse(400, error_code=4) if not len(buyer) or not "buyerID" in buyer or not buyer[ "buyerID"] or not validate_integer(buyer["buyerID"]): return customResponse(400, error_code=5, error_details="Id for buyer not sent") buyerPtr = Buyer.objects.filter(id=int(buyer["buyerID"]), delete_status=False) if len(buyerPtr) == 0: return customResponse(400, error_code=6, error_details="Invalid id for buyer sent") buyerPtr = buyerPtr[0] try: buyerPtr.delete_status = True buyerPtr.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code=3) else: closeDBConnection() return customResponse(200, {"buyer": "buyer deleted"})
def post_new_buyer_buys_from(request, parameters): try: requestbody = request.body.decode("utf-8") buyer_buys_from = convert_keys_to_string(json.loads(requestbody)) except Exception as e: return customResponse(400, error_code=4) if not len(buyer_buys_from) or not "buyerID" in buyer_buys_from or not validate_integer(buyer_buys_from["buyerID"]): return customResponse(400, error_code=5, error_details= "Id for buyer not sent") buyerPtr = Buyer.objects.filter(id=int(buyer_buys_from["buyerID"]), delete_status=False) if not buyerPtr.exists(): return customResponse(400, error_code=6, error_details= "Invalid id for buyer sent") if not "businesstypeID" in buyer_buys_from or not validate_integer(buyer_buys_from["businesstypeID"]): return customResponse(400, error_code=5, error_details= "Id for state not sent") businessTypePtr = BusinessType.objects.filter(id=int(buyer_buys_from["businesstypeID"]), can_buyer_buy_from=True) if not businessTypePtr.exists(): return customResponse(400, error_code=6, error_details= "Invalid id for business type sent") BuyerBuysFromPtr = BuyerBuysFrom.objects.filter(buyer_id=int(buyer_buys_from["buyerID"]),business_type_id=int(buyer_buys_from["businesstypeID"])) if len(BuyerBuysFromPtr)>0: BuyerBuysFromPtr = BuyerBuysFromPtr[0] if BuyerBuysFromPtr.delete_status == True: BuyerBuysFromPtr.delete_status = False BuyerBuysFromPtr.save() closeDBConnection() return customResponse(200, {"buyer_buys_from" : serialize_buyer_buys_from(BuyerBuysFromPtr)}) else: return customResponse(400, error_code=6, error_details= "Buyer buys_from already exists") try: newBuyerBuysFrom = BuyerBuysFrom(buyer_id=int(buyer_buys_from["buyerID"]),business_type_id=int(buyer_buys_from["businesstypeID"])) newBuyerBuysFrom.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code = 1) else: closeDBConnection() return customResponse(200, {"buyer_buys_from" : serialize_buyer_buys_from(newBuyerBuysFrom)})
def update_buyer_store_lead(request, parameters): try: requestbody = request.body.decode("utf-8") buyer_store_lead = convert_keys_to_string(json.loads(requestbody)) except Exception as e: return customResponse(400, error_code=4) if not len(buyer_store_lead): return customResponse(400, error_code=5, error_details= "Invalid data sent in request") if parameters["isBuyer"] == 1: buyer_store_lead["buyerID"] = parameters["buyersArr"][0] elif not "buyerID" in buyer_store_lead or not validate_integer(buyer_store_lead["buyerID"]): return customResponse(400, error_code=5, error_details= "Id for buyer not sent") buyerPtr = Buyer.objects.filter(id=int(buyer_store_lead["buyerID"]), delete_status=False) if not buyerPtr.exists(): return customResponse(400, error_code=6, error_details= "Invalid id for buyer sent") if not "buyerstoreleadID" in buyer_store_lead or not validate_integer(buyer_store_lead["buyerstoreleadID"]): return customResponse(400, error_code=5, error_details= "Id for buyer store lead not sent") buyerStoreLeadPtr = BuyerStoreLead.objects.filter(id=int(buyer_store_lead["buyerstoreleadID"]), buyer_id=int(buyer_store_lead["buyerID"])) if len(buyerStoreLeadPtr) == 0: return customResponse(400, error_code=6, error_details= "Invalid id for buyer store lead sent") buyerStoreLeadPtr = buyerStoreLeadPtr[0] if not buyerStoreLeadPtr.validateBuyerStoreLeadData(buyer_store_lead, 0): return customResponse(400, error_code=5, error_details= "Invalid data for buyer store lead sent") try: buyerStoreLeadPtr.populateBuyerStoreLead(buyer_store_lead) buyerStoreLeadPtr.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code = 3) else: closeDBConnection() return customResponse(200, {"buyer_store_lead":serialize_buyer_store_lead(buyerStoreLeadPtr)})
def validateOrderProductsData(orderProducts, productsHash, productIDarr): for orderProduct in orderProducts: if not "productID" in orderProduct or not validate_integer(orderProduct["productID"]): return False productID = int(orderProduct["productID"]) if not "pieces" in orderProduct or not validate_integer(orderProduct["pieces"]): return False if not "edited_price_per_piece" in orderProduct or not validate_number(orderProduct["edited_price_per_piece"]): return False if not "remarks" in orderProduct or orderProduct["remarks"]==None: orderProduct["remarks"] = "" productsHash[productID] = len(productsHash) productIDarr.append(productID) return True
def validateCartItemData(cartProducts, productsHash, productIDarr): for cartitem in cartProducts: if not "productID" in cartitem or not validate_integer(cartitem["productID"]): return False productID = int(cartitem["productID"]) if not "lots" in cartitem or not validate_integer(cartitem["lots"]): return False if not "added_from" in cartitem or not validate_integer(cartitem["added_from"]): cartitem["added_from"] = 0 if not "remarks" in cartitem or cartitem["remarks"] == None: cartitem["remarks"] = "" productsHash[productID] = len(productsHash) productIDarr.append(productID) return True
def validateProductData(product, oldproduct, is_new): flag = 0 if not "name" in product or product["name"] == None: flag = 1 product["name"] = oldproduct.name if not "price_per_unit" in product or not validate_number( product["price_per_unit"]): flag = 1 product["price_per_unit"] = oldproduct.price_per_unit if not "unit" in product or product["unit"] == None: product["unit"] = oldproduct.unit if not "tax" in product or not validate_number(product["tax"]): product["tax"] = oldproduct.tax if not "min_price_per_unit" in product or not validate_number( product["min_price_per_unit"]): product["min_price_per_unit"] = oldproduct.min_price_per_unit if not "lot_size" in product or not validate_integer(product["lot_size"]): flag = 1 product["lot_size"] = oldproduct.lot_size if not "price_per_lot" in product or not validate_number( product["price_per_lot"]): flag = 1 product["price_per_lot"] = oldproduct.price_per_lot if not "verification" in product or not validate_bool( product["verification"]): product["verification"] = oldproduct.verification if not "show_online" in product or not validate_bool( product["show_online"]): product["show_online"] = oldproduct.show_online if not "slug" in product or product["slug"] == None: product["slug"] = oldproduct.slug if not "display_name" in product or product["display_name"] == None: product["display_name"] = oldproduct.display_name if not "is_catalog" in product or not validate_bool(product["is_catalog"]): product["is_catalog"] = oldproduct.is_catalog if not "delete_status" in product or not validate_bool( product["delete_status"]): product["delete_status"] = oldproduct.delete_status if not float(product["min_price_per_unit"]) > 0 or not float( product["price_per_lot"]) > 0 or not float( product["price_per_unit"]) > 0: return False if not float(product["price_per_unit"]) >= float( product["min_price_per_unit"]) or not float( product["price_per_lot"]) >= float(product["price_per_unit"]): return False if is_new == 1 and flag == 1: return False return True
def validateOrderShipmentItemsData(orderItems, sentOrderItems): for orderItem in orderItems: if not "orderitemID" in orderItem or not validate_integer( orderItem["orderitemID"]): return False sentOrderItems.append(int(orderItem["orderitemID"])) return True
def post_new_buyer_address(request, parameters): try: requestbody = request.body.decode("utf-8") buyer_address = convert_keys_to_string(json.loads(requestbody)) except Exception as e: return customResponse(400, error_code=4) if parameters["isBuyer"] == 1: buyer_address["buyerID"] = parameters["buyersArr"][0] elif not "buyerID" in buyer_address or not validate_integer( buyer_address["buyerID"]): return customResponse(400, error_code=5, error_details="Id for buyer not sent") if not len(buyer_address) or not validateBuyerAddressData( buyer_address, BuyerAddress(), 1): return customResponse( 400, error_code=5, error_details="Invalid data for buyer address sent") try: addressPresent = False if "client_id" in buyer_address and str( buyer_address["client_id"]) != "" and len( str(buyer_address["client_id"])) > 6: newAddress = BuyerAddress.objects.filter( buyer_id=int(buyer_address["buyerID"]), client_id=str(buyer_address["client_id"])) if len(newAddress) != 0: addressPresent = True newAddress = newAddress[0] if not addressPresent: newAddress = BuyerAddress(buyer_id=int(buyer_address["buyerID"])) if not BuyerAddress.objects.filter( buyer_id=int(buyer_address["buyerID"])).exists(): newAddress.priority = 0 populateBuyerAddress(newAddress, buyer_address) newAddress.save() newBuyerAddressHistory = BuyerAddressHistory() newBuyerAddressHistory.populateFromBuyerAddress(newAddress) newBuyerAddressHistory.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code=1) else: closeDBConnection() return customResponse(200, {"address": serialize_buyer_address(newAddress)})
def validateBuyerContactsData(buyer_contacts): for buyer_contact in buyer_contacts: if not "contactID" in buyer_contact or not validate_integer( buyer_contact["contactID"]): buyer_contact["contactID"] = random.randint(100000, 10000000) if not "mailArr" in buyer_contact or buyer_contact["mailArr"] == None: buyer_contact["mailArr"] = "" if not "numbersArr" in buyer_contact or buyer_contact[ "numbersArr"] == None: buyer_contact["numbersArr"] = "" if not "name" in buyer_contact or buyer_contact["name"] == None: buyer_contact["name"] = "" return True
def populateBuyerParameters(request, parameters = {}, version = "0"): parameters = populateBuyerIDParameters(request, parameters, version) parameters = populateInternalUserIDParameters(request, parameters, version) buyerInterestID = request.GET.get("buyerinterestID", "") whatsappSharingActive = request.GET.get("whatsapp_sharing_active", None) if validate_bool(whatsappSharingActive): parameters["whatsapp_sharing_active"] = int(whatsappSharingActive) testBuyer = request.GET.get("test_buyer", None) if validate_bool(testBuyer): parameters["test_buyer"] = int(testBuyer) if buyerInterestID != "": parameters["buyerInterestArr"] = getArrFromString(buyerInterestID) buyersharedproductID = request.GET.get("buyersharedproductID", "") if buyersharedproductID != "" and validate_integer(buyersharedproductID): parameters["buyersharedproductID"] = int(buyersharedproductID) buyerMinID = request.GET.get("buyer_min_ID", "") if buyerMinID != "" and validate_integer(buyerMinID): parameters["buyer_min_ID"] = int(buyerMinID) buyerMaxID = request.GET.get("buyer_max_ID", "") if buyerMaxID != "" and validate_integer(buyerMaxID): parameters["buyer_max_ID"] = int(buyerMaxID) buyerPurchasingStateID = request.GET.get("buyerpurchasingstateID", "") if buyerPurchasingStateID != "": parameters["buyerPurchasingStateArr"] = getArrFromString(buyerPurchasingStateID) parameters = populateBuyerDetailsParameters(request, parameters, version) return parameters
def update_category(request): try: requestbody = request.body.decode("utf-8") category = convert_keys_to_string(json.loads(requestbody)) except Exception as e: return customResponse(400, error_code=4) if not len( category) or not "categoryID" in category or not validate_integer( category["categoryID"]): return customResponse(400, error_code=5, error_details="Id for category not sent") categoryPtr = Category.objects.filter(id=int(category["categoryID"])) if len(categoryPtr) == 0: return customResponse(400, error_code=6, error_details="Invalid id for category sent") categoryPtr = categoryPtr[0] if not validateCategoryData(category, categoryPtr, 0): return customResponse(400, error_code=5, error_details="Invalid data for category sent") category["slug"] = slugify(category["name"]) try: if categoryPtr.show_online != int(category["show_online"]): Product.objects.filter(category_id=categoryPtr.id).update( show_online=int(category["show_online"]), updated_at=timezone.now()) populateCategoryData(categoryPtr, category) categoryPtr.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code=3) else: closeDBConnection() return customResponse( 200, {"categories": serialize_categories(categoryPtr)})
def upload_article_file(request): try: requestbody = json.dumps(request.POST).encode("utf-8") article = convert_keys_to_string(json.loads(requestbody)) print article except Exception as e: return customResponse(400, error_code=4) if not len(article) or not "articleID" in article or not validate_integer( article["articleID"]): return customResponse(400, error_code=5, error_details="Id for article not sent") articlePtr = Article.objects.filter(id=int(article["articleID"])) if len(articlePtr) == 0: return customResponse(400, error_code=6, error_details="Invalid ID for article sent") articlePtr = articlePtr[0] try: if "file" in request.FILES: outputLink = "media/uploadedfiles/blogcoverphoto/{}/".format( articlePtr.author.id) outputDirectory = settings.STATIC_ROOT + outputLink outputFileName = "{}-{}.jpg".format(articlePtr.slug, articlePtr.id) articlePtr.cover_photo = outputLink + outputFileName save_file_from_request(request, outputDirectory, outputFileName) articlePtr.save() else: return customResponse(400, error_code=5, error_details="No files sent in request") except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code=2, error_details="Could not save file") else: closeDBConnection() return customResponse(200, {"success": "file uploaded"})
def post_new_buyer_purchasing_state(request): try: requestbody = request.body.decode("utf-8") buyer_purchasing_state = convert_keys_to_string(json.loads(requestbody)) except Exception as e: return customResponse(400, error_code=4) if not len(buyer_purchasing_state) or not "buyerID" in buyer_purchasing_state or not validate_integer(buyer_purchasing_state["buyerID"]): return customResponse(400, error_code=5, error_details= "Id for buyer not sent") buyerPtr = Buyer.objects.filter(id=int(buyer_purchasing_state["buyerID"]), delete_status=False) if not buyerPtr.exists(): return customResponse(400, error_code=6, error_details= "Invalid id for buyer sent") if not "stateID" in buyer_purchasing_state or not validate_integer(buyer_purchasing_state["stateID"]): return customResponse(400, error_code=5, error_details= "Id for state not sent") statePtr = State.objects.filter(id=int(buyer_purchasing_state["stateID"])) if not statePtr.exists(): return customResponse(400, error_code=6, error_details= "Invalid id for state sent") BuyerPurchasingStatePtr = BuyerPurchasingState.objects.filter(buyer_id=int(buyer_purchasing_state["buyerID"]),state_id=int(buyer_purchasing_state["stateID"])) if len(BuyerPurchasingStatePtr)>0: BuyerPurchasingStatePtr = BuyerPurchasingStatePtr[0] if BuyerPurchasingStatePtr.delete_status == True: BuyerPurchasingStatePtr.delete_status = False BuyerPurchasingStatePtr.save() closeDBConnection() return customResponse(200, {"buyer_purchasing_state" : serialize_buyer_purchasing_state(BuyerPurchasingStatePtr)}) else: return customResponse(400, error_code=6, error_details= "Buyer purchasing_state already exists") try: newBuyerPurchasingState = BuyerPurchasingState(buyer_id=int(buyer_purchasing_state["buyerID"]),state_id=int(buyer_purchasing_state["stateID"])) newBuyerPurchasingState.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code = 1) else: closeDBConnection() return customResponse(200, {"buyer_purchasing_state" : serialize_buyer_purchasing_state(newBuyerPurchasingState)})
def populateInternalUserIDParameters(request, parameters = {}, version = "0"): accessToken = getAccessToken(request) tokenPayload = get_token_payload(accessToken, "internaluserID") internalUserID = request.GET.get("internaluserID", "") parameters["isInternalUser"] = 0 if "internaluserID" in tokenPayload and validate_integer(tokenPayload["internaluserID"]) and "password" in tokenPayload: try: internalUserPtr = InternalUser.objects.get(id=int(tokenPayload["internaluserID"]), password=tokenPayload["password"]) except: pass else: parameters["internalusersArr"] = [internalUserPtr.id] parameters["isInternalUser"] = 1 elif internalUserID != "": parameters["internalusersArr"] = getArrFromString(internalUserID) return parameters
def post_delivery_report(request): try: delivery_report = request.POST except Exception as e: return customResponse(400, error_code=4) if not len(delivery_report ) or not "customID" in delivery_report or not validate_integer( delivery_report["customID"]): return customResponse( 400, error_code=5, error_details="Invalid data for delivery report sent") smsSentPtr = SMSSent.objects.filter(id=int(delivery_report["customID"])) if len(smsSentPtr) == 0: return customResponse( 400, error_code=6, error_details="Invalid id for delivery report sent") smsSentPtr = smsSentPtr[0] if not validateSmsSentData(delivery_report): return customResponse(400, error_code=5) try: smsSentPtr.delivery_status = delivery_report["status"] if delivery_report["status"] == "D": smsSentPtr.delivered = 1 smsSentPtr.delivered_time = delivery_report["datetime"] smsSentPtr.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code=3) else: closeDBConnection() return customResponse(200, {"success": "updated successfully"})
def validateSellerPaymentData(sellerPayment): if not "payment_method" in sellerPayment or not validate_integer( sellerPayment["payment_method"]): return False if not "reference_number" in sellerPayment or sellerPayment[ "reference_number"] == None: return False if not "details" in sellerPayment or sellerPayment["details"] == None: sellerPayment["details"] = "" if not "payment_time" in sellerPayment or not validate_date_time( sellerPayment["payment_time"]): return False if not "payment_value" in sellerPayment or not validate_number( sellerPayment["payment_value"]): return False if not "fully_paid" in sellerPayment or not validate_bool( sellerPayment["fully_paid"]): return False return True
def post_new_article(request): try: requestbody = request.body.decode("utf-8") article = convert_keys_to_string(json.loads(requestbody)) except Exception as e: return customResponse(400, error_code=4) if not len(article) or not validateArticleData(article, Article(), 1): return customResponse(400, error_code=5, error_details="Invalid data for article sent") if not "internaluserID" in article or not validate_integer( article["internaluserID"]): return customResponse(400, error_code=5, error_details="ID for author not sent") internalUserPtr = InternalUser.objects.filter( id=int(article["internaluserID"])) if not internalUserPtr.exists(): return customResponse(400, error_code=6, error_details="Invalid ID for author sent") article["slug"] = slugify(article["title"]) try: newArticle = Article(author_id=int(article["internaluserID"])) populateArticleData(newArticle, article) newArticle.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code=1) else: closeDBConnection() return customResponse(200, {"article": serializeArticle(newArticle)})
def post_buyer_product_landing(request): try: requestbody = request.body.decode("utf-8") buyer_product = convert_keys_to_string(json.loads(requestbody)) except Exception as e: return customResponse(400, error_code=4) if not len( buyer_product ) or not "buyerproductID" in buyer_product or not validate_integer( buyer_product["buyerproductID"]): return customResponse(400, error_code=5, error_details="Id for buyer product not sent") buyerProductPtr = BuyerProducts.objects.filter( id=int(buyer_product["buyerproductID"])) if len(buyerProductPtr) == 0: return customResponse( 400, error_code=6, error_details="Invalid id for for buyer product sent") buyerProductPtr = buyerProductPtr[0] try: newBuyerProductLanding = BuyerProductLanding( buyer_id=buyerProductPtr.buyer_id, product_id=buyerProductPtr.product_id, buyer_product_id=buyerProductPtr.id) newBuyerProductLanding.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code=1) else: closeDBConnection() return customResponse(200, {"success": "created"})
def update_article(request): try: requestbody = request.body.decode("utf-8") article = convert_keys_to_string(json.loads(requestbody)) except Exception as e: return customResponse(400, error_code=4) if not len(article) or not "articleID" in article or not validate_integer( article["articleID"]): return customResponse(400, error_code=5, error_details="Id for article not sent") articlePtr = Article.objects.filter(id=int(article["articleID"])) if len(articlePtr) == 0: return customResponse(400, error_code=6, error_details="Invalid ID for article sent") articlePtr = articlePtr[0] if not validateArticleData(article, articlePtr, 0): return customResponse(400, error_code=5, error_details="Invalid data for article sent") article["slug"] = slugify(article["title"]) try: populateArticleData(articlePtr, article) articlePtr.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code=3) else: closeDBConnection() return customResponse(200, {"article": serializeArticle(articlePtr)})