def put_buyer_firebase_token_details(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 BuyerFireBaseToken.validateBuyerFireBaseTokenData( buyer): return customResponse(400, error_code=5, error_details="Invalid data sent in request") try: buyerID = parameters["buyersArr"][0] except Exception as e: buyerID = None try: buyerFireBaseTokenPtr, created = BuyerFireBaseToken.objects.get_or_create( instance_id=buyer["instance_id"]) buyerFireBaseTokenPtr.buyer_id = buyerID buyerFireBaseTokenPtr.token = buyer["token"] buyerFireBaseTokenPtr.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code=1) else: return customResponse(200, {"token": "token successfully created"})
def post_new_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 validateCategoryData(category, Category(), 1): return customResponse(400, error_code=5, error_details="Invalid data for category sent") category["slug"] = slugify(category["name"]) try: newCategory = Category() populateCategoryData(newCategory, category) newCategory.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code=1) else: closeDBConnection() return customResponse( 200, {"categories": serialize_categories(newCategory)})
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 post_new_buyer_panel_tracking(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 validate_integer(buyer["buyerID"]): # return customResponse("4XX", {"error": "Id for buyer not sent"}) buyer["buyerID"] = parameters["buyersArr"][0] buyerPtr = Buyer.objects.filter(id=int(buyer["buyerID"]), delete_status=False) if not buyerPtr.exists(): return customResponse(400, error_code=6, error_details= "Invalid id for buyer sent") if not "page_closed" in buyer or buyer["page_closed"] ==None: return customResponse(400, error_code=6, error_details= "Page number not sent") try: newBuyerPanelInstructionsTracking = BuyerPanelInstructionsTracking(buyer_id=int(buyer["buyerID"]), page_closed=buyer["page_closed"]) newBuyerPanelInstructionsTracking.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code = 1) else: closeDBConnection() return customResponse(200, {"buyer_product_tracking":"successfully added"})
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 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 update_buyer_store_url(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) buyer["buyerID"] = parameters["buyersArr"][0] buyerPtr = Buyer.objects.filter(id=int(buyer["buyerID"]), delete_status=False) if not buyerPtr.exists(): return customResponse(400, error_code=6, error_details= "Invalid id for buyer sent") buyerPtr = buyerPtr[0] if not buyerPtr.validateBuyerStoreUrlData(buyer): return customResponse(400, error_code=5, error_details= "Invalid data sent") if Buyer.objects.exclude(id=int(buyer["buyerID"])).filter(store_url = buyer["store_url"]).exists(): return customResponse(400, error_code=6, error_details= "Store url already exists") try: buyerPtr.store_url = buyer["store_url"] buyerPtr.store_active = int(buyer["store_active"]) buyerPtr.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code = 1) else: closeDBConnection() return customResponse(200, {"buyer_store_url":"successfully updated"})
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 post_new_buyer_contacts(request, parameters): try: requestbody = request.body.decode("utf-8") buyer_contacts = convert_keys_to_string(json.loads(requestbody)) except Exception as e: return customResponse(400, error_code=4) if not len(buyer_contacts) or not "contacts" in buyer_contacts or not isinstance(buyer_contacts["contacts"], list) or not len(buyer_contacts["contacts"]) > 0: return customResponse(400, error_code=5, error_details= "Invalid data sent in request") buyerPtr = Buyer.objects.filter(id=parameters["buyersArr"][0], delete_status=False) if not buyerPtr.exists(): return customResponse(400, error_code=6, error_details= "Invalid id for buyer sent") if not validateBuyerContactsData(buyer_contacts["contacts"]): return customResponse(400, error_code=5, error_details= "Contact Id not sent") if not "firebase_token" in buyer_contacts or buyer_contacts["firebase_token"] == None: buyer_contacts["firebase_token"] = str(uuid.uuid1()) try: for buyer_contact in buyer_contacts["contacts"]: buyerContactsPtr, created = BuyerContacts.objects.get_or_create(buyer_id = parameters["buyersArr"][0], client_contact_id = buyer_contact["contactID"], firebase_token = buyer_contacts["firebase_token"]) buyerContactsPtr.emails = ','.join([x.encode('utf-8') for x in buyer_contact["mailArr"]]) buyerContactsPtr.numbers = ','.join([x.encode('utf-8') for x in buyer_contact["numbersArr"]]) buyerContactsPtr.contact_name = buyer_contact["name"] buyerContactsPtr.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code = 1) else: closeDBConnection() return customResponse(200, {"success" : "contacts saved"})
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 post_new_buyer_registration(request, parameters): try: requestbody = request.body.decode("utf-8") buyer_registration = convert_keys_to_string(json.loads(requestbody)) except Exception as e: return customResponse(400, error_code=4) if not len(buyer_registration) or not validateBuyerRegistrationData( buyer_registration): return customResponse( 400, error_code=5, error_details="Invalid data for buyer registration sent") if buyer_registration["email"] != "" and buyerEmailExists( buyer_registration["email"]): return customResponse(400, error_code=6, error_details="buyer email already exists") if buyerMobileNumberExists(buyer_registration["mobile_number"]): return customResponse( 400, error_code=6, error_details="buyer phone number already exists") ## Invalidate all registrations with same email or mobile number if buyer_registration["email"] != "": BuyerRegistration.objects.filter( email=buyer_registration["email"]).update( is_active=False, updated_at=timezone.now()) BuyerRegistration.objects.filter( mobile_number=buyer_registration["mobile_number"]).update( is_active=False, updated_at=timezone.now()) try: newBuyerRegistration = BuyerRegistration() newBuyerRegistration.populateBuyerRegistration(buyer_registration) newBuyerRegistration.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code=1) else: ## Send mobile number verification SMS newBuyerRegistration.sendVerificationSMS() closeDBConnection() return customResponse( 200, { "buyer_registration": serialize_buyer_registration(newBuyerRegistration, parameters) })
def post_buyer_change_password(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 validateBuyerPasswordChangeData(buyer): return customResponse(400, error_code=5, error_details="Invalid data sent in request") refresh_token = getAccessToken(request, "refresh_token") if refresh_token == "": return customResponse(400, error_code=5, error_details="Refresh token not sent") tokenPayload = validateBuyerRefreshToken(refresh_token) if tokenPayload == {}: return customResponse(403, error_code=7, error_details="Token invalid") buyerPtr = Buyer.objects.filter(id=parameters["buyersArr"][0], delete_status=False, blocked=False) if len(buyerPtr) == 0: return customResponse(400, error_code=6, error_details="Invalid buyer id sent") buyerPtr = buyerPtr[0] if not buyerPtr.password == buyer["password"]: return customResponse(400, error_code=9, error_details="Incorrect password sent") try: buyerPtr.password = buyer["new_password"] buyerPtr.save() BuyerRefreshToken.objects.filter(buyer_id=buyerPtr.id).update( is_active=False, updated_at=timezone.now()) except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code=1) else: closeDBConnection() return customResponse(200, {"success": "successfully changed password"})
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 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 post_buyer_forgot_password(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 validateBuyerForgotPasswordData(buyer): return customResponse(400, error_code=5, error_details="Invalid data sent in request") buyerPtr = Buyer.objects.filter(mobile_number=buyer["mobile_number"], blocked=False, delete_status=False) if len(buyerPtr) == 0: return customResponse(400, error_code=6, error_details="Invalid mobile number sent") buyerPtr = buyerPtr[0] ## Invalidate all registrations with same email or mobile number BuyerForgotPasswordToken.objects.filter( mobile_number=buyer["mobile_number"]).update(is_active=False, updated_at=timezone.now()) try: newBuyerForgotPasswordToken = BuyerForgotPasswordToken() newBuyerForgotPasswordToken.populateBuyerBuyerForgotPasswordToken( buyer) newBuyerForgotPasswordToken.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code=1) else: ## Send mobile number verification SMS newBuyerForgotPasswordToken.sendVerificationSMS() closeDBConnection() return customResponse( 200, { "buyer_registration": serialize_buyer_forgot_password_token( newBuyerForgotPasswordToken, 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 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 internaluser_login(request, version = "0"): version = getApiVersion(request) response = {} if request.method == 'POST': email = request.POST.get('email', '') password = request.POST.get('password', '') if not email or not password: try: requestbody = request.body.decode("utf-8") requestbody = convert_keys_to_string(json.loads(requestbody)) except Exception as e: return customResponse(400, error_code=4) if "email" not in requestbody or "password" not in requestbody: return customResponse(400, error_code=5, error_details="Either email or password was empty") email = requestbody["email"] password = requestbody["password"] if not email or not password: return customResponse(400, error_code=5, error_details= "Either email or password was empty") # if check_token(request) try: internaluser = InternalUser.objects.get(email=email) except InternalUser.DoesNotExist: return customResponse(400, error_code=9, error_details="Invalid user credentials") if password == internaluser.password: response = { "token": getInternalUserToken(internaluser), "internaluser": serialize_internaluser(internaluser) } return customResponse(200, response) else: return customResponse(401, error_code=9, error_details="Invalid user credentials") return customResponse(404, error_code = 7)
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)})
def update_buyer_product_whatsapp(request, parameters): 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 buyer_product[ "buyerproductID"] == None: return customResponse(400, error_code=5, error_details="Id for buyer product not sent") buyerProductIDs = getArrFromString(str(buyer_product["buyerproductID"])) buyerProductPtr = BuyerProducts.objects.filter(id__in=buyerProductIDs) if parameters["isBuyer"] == 1: buyerProductPtr.filter(buyer_id=parameters["buyersArr"][0]) if len(buyerProductPtr) != len(buyerProductIDs): return customResponse( 400, error_code=6, error_details="Invalid ids for for buyer products sent") try: buyerProductPtr.update(shared_on_whatsapp=True, updated_at=timezone.now()) except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code=3) else: closeDBConnection() return customResponse(200, {"success": "updated"})
def post_buyer_registration_resend_sms(request, parameters): try: requestbody = request.body.decode("utf-8") buyer_registration = convert_keys_to_string(json.loads(requestbody)) except Exception as e: return customResponse(400, error_code=4) if not len( buyer_registration) or not validateBuyerRegistrationResendSMSData( buyer_registration): return customResponse( 400, error_code=5, error_details="Invalid data for buyer registration sent") tokenDetails = getBuyerRegistrationFromToken( buyer_registration["registration_token"]) if tokenDetails[0] == False: return tokenDetails[1] buyerRegistrationPtr = tokenDetails[1] if buyerRegistrationPtr.messages_sent > 3: return customResponse(400, error_code=11, error_details="SMS count exceeded") try: buyerRegistrationPtr.sendVerificationSMS() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code=1) else: closeDBConnection() return customResponse(200, {"success": "sms sent"})
def delete_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"]), delete_status=False) if len(categoryPtr) == 0: return customResponse(400, error_code=6, error_details="Invalid id for category sent") categoryPtr = categoryPtr[0] try: categoryPtr.delete_status = True Product.objects.filter(category_id=categoryPtr.id).update( delete_status=True, updated_at=timezone.now()) categoryPtr.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code=3) else: closeDBConnection() return customResponse(200, {"success": "category deleted"})
def update_product(request, parameters={}): 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"])).select_related('productdetails') if len(productPtr) == 0: return customResponse(400, error_code=6, error_details="Invalid id for product sent") productPtr = productPtr[0] detailsPresent = 1 detailsSent = 0 productlotSent = 0 if not validateProductData(product, productPtr, 0): return customResponse(400, error_code=5, error_details="Invalid data for product sent") product["slug"] = slugify(product["name"]) try: populateProductData(productPtr, product) if "details" in product and product["details"]: detailsSent = 1 productdetails = product["details"] productPtr.display_name = product["details"][ "brand"] + " " + product["name"] + " " + product["details"][ "seller_catalog_number"] if hasattr(productPtr, "productdetails"): validateProductDetailsData(productdetails, productPtr.productdetails) populateProductDetailsData(productPtr.productdetails, productdetails) else: detailsPresent = 0 validateProductDetailsData(productdetails, ProductDetails()) newProductDetails = ProductDetails(product=productPtr) populateProductDetailsData(newProductDetails, productdetails) if "product_lot" in product and product["product_lot"]: productlotSent = 1 if not validateProductLotData(product["product_lot"]): return customResponse( 400, error_code=5, error_details="Product lots for product not properly sent") productLots = product["product_lot"] ProductLot.objects.filter( product_id=int(product["productID"])).delete() productPtr.min_price_per_unit = Decimal( parseMinPricePerUnit(productLots)) for productLot in productLots: newProductLot = ProductLot(product=productPtr) populateProductLotData(newProductLot, productLot) newProductLot.save() productPtr.save() if detailsSent == 1 and detailsPresent == 1: productPtr.productdetails.save() if detailsPresent == 0: newProductDetails.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code=3) else: closeDBConnection() return customResponse( 200, {"product": serialize_product(productPtr, parameters)})
def post_new_product(request, parameters={}): 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 validateProductData(product, Product(), 1): return customResponse(400, error_code=5, error_details="Invalid data for product sent") if not "sellerID" in product or not validate_integer(product["sellerID"]): return customResponse(400, error_code=5, error_details="Seller id for product not sent") sellerPtr = Seller.objects.filter(id=int(product["sellerID"]), delete_status=False) if not sellerPtr.exists(): return customResponse(400, error_code=6, error_details="Invalid id for seller sent") if not "categoryID" in product or not validate_integer( product["categoryID"]): return customResponse(400, error_code=5, error_details="Category id for product not sent") categoryPtr = Category.objects.filter(id=int(product["categoryID"])) if not categoryPtr.exists(): return customResponse(400, error_code=6, error_details="Invalid id for category sent") sellerCategoryPtr = SellerCategory.objects.filter( category_id=int(product["categoryID"]), seller_id=int(product["sellerID"])) if not sellerCategoryPtr.exists(): newSellerCategory = SellerCategory(seller_id=int(product["sellerID"]), category_id=int( product["categoryID"])) try: newSellerCategory.save() except Exception as e: pass if not "product_lot" in product or not product[ "product_lot"] or not validateProductLotData( product["product_lot"]): return customResponse( 400, error_code=5, error_details="Product lots for product not properly sent") if not "details" in product or not product["details"]: product["details"] = {} validateProductDetailsData(product["details"], ProductDetails()) product["slug"] = slugify(product["name"]) product["min_price_per_unit"] = parseMinPricePerUnit( product["product_lot"]) product["display_name"] = product["details"]["brand"] + " " + product[ "name"] + " " + product["details"]["seller_catalog_number"] try: newProduct = Product(category_id=int(product["categoryID"]), seller_id=int(product["sellerID"])) populateProductData(newProduct, product) newProduct.save() productLots = product["product_lot"] for productLot in productLots: newProductLot = ProductLot(product=newProduct) populateProductLotData(newProductLot, productLot) newProductLot.save() productdetails = product["details"] newProductDetails = ProductDetails(product=newProduct) populateProductDetailsData(newProductDetails, productdetails) newProductDetails.save() except Exception as e: log.critical(e) closeDBConnection() return customResponse(500, error_code=1) else: closeDBConnection() return customResponse( 200, {"product": serialize_product(newProduct, parameters)})
def post_new_buyer(request): 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 validateBuyerData(buyer, Buyer(), 1): return customResponse(400, error_code=5, error_details="Invalid data for buyer sent") if buyer["email"] != None and buyerEmailExists(buyer["email"]): return customResponse(400, error_code=6, error_details="buyer email already exists") if buyerMobileNumberExists(buyer["mobile_number"]): return customResponse( 400, error_code=6, error_details="buyer phone number already exists") if not "address" in buyer or buyer["address"] == None: buyer["address"] = {} validateBuyerAddressData(buyer["address"], BuyerAddress()) if not "details" in buyer or buyer["details"] == None: buyer["details"] = {} validateBuyerDetailsData(buyer["details"], BuyerDetails(), 1) try: newBuyer = Buyer() populateBuyer(newBuyer, buyer) newBuyer.password = buyer["mobile_number"] newBuyer.save() buyeraddress = buyer["address"] newAddress = BuyerAddress(buyer=newBuyer) populateBuyerAddress(newAddress, buyeraddress) newAddress.priority = 0 if newAddress.alias == "": newAddress.alias = "Store" buyerdetails = buyer["details"] newBuyerDetails = BuyerDetails(buyer=newBuyer) if "buyertypeID" in buyer["details"] and validate_integer( buyer["details"]["buyertypeID"]): businessTypePtr = BusinessType.objects.filter( id=int(buyer["details"]["buyertypeID"])) if len(businessTypePtr) > 0: businessTypePtr = businessTypePtr[0] newBuyerDetails.buyer_type = businessTypePtr populateBuyerDetails(newBuyerDetails, buyerdetails) newBuyerDetails.save() 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, {"buyer": serialize_buyer(newBuyer)})