def addProduct(): if request.method == 'POST': result = request.form newProduct = product.Product() newProduct.Product(result['name']) myresult = product.addProduct(newProduct) return redirect('/add/product/success')
def list_products(): p = repo.Product() data = p.all() arr = [] for i in data: arr.append(repo.to_json(i)) result = {"data": arr} return make_response(jsonify(result)), 200
def post(self): data = ProductResource.parser.parse_args() if ProductResource.checkAuth(data["auth"]): database = session["outlet"][2] userProd = product.Product(None, data["name"], data["type"], data["color"]) params = userProd.getParams() res = product.Product.insert_into_database(database, params) if res: return {"message": "Accepted"}, 202 abort(406, message="Insufficient values") abort(401, message="Unauthorized access")
def put(self, id): data = ProductResource.parser.parse_args() if ProductResource.checkAuth(data["auth"]): database = session["outlet"][2] prod = product.Product.find_product_by_id(database, {"id": id}) userProd = product.Product(id, data["name"], data["type"], data["color"], data["status"], data["updated_at"]) params = {**prod.getParams(), **userProd.getParams()} key = {"id": id} res = product.Product.update_from_database(database, key, params) if res: return {"message": "Accepted"}, 202 abort(406, message="Insufficient values") abort(401, message="Unauthorized access")
def create_dummy_data(): fake = Faker() dummy = [] mongoConnect = mongodb.MongoDb() db = mongoConnect.getClient().unga for i in range(0, 5): advert = ad.Advert(fake.text()) db.adverts.insert(advert.serialize()) dummy.append(advert.serialize()) user = u.User(fake.name(), data.dummyPasswords[i], fake.email(), generate_password_hash(data.dummyPasswords[i])) db.users.insert(user.serialize()) dummy.append(user.serialize()) product = prod.Product(data.dummyRentalTypes[i], fake.text()) db.products.insert(product.serialize()) dummy.append(product.serialize()) mongoConnect.getClient().close() return jsonify(dummy)
def search(product_name, attempt=0): URL = BASE_URL + SEARCH_PRE_PARAMS + product_name # + SEARCH_POST_PARAMS products = [] print(URL) try: if (attempt > 0): #URL = URL +'_'+str(attempt) print(str(attempt)) print(URL) response = requests.get(URL) # If the response was successful, no Exception will be raised response.raise_for_status() results = BeautifulSoup(response.content, 'html.parser') product_elements = results.find_all(has_attr_data_id) for each_product in product_elements: product = productsmodel.Product() #print("-------------------") #print(each_product) all_links = each_product.find_all( "a", attrs={"rel": "noopener noreferrer"}) #foundContent = False for eachlink in all_links: #print("===========") #print(eachlink) #print("--------") #if(eachlink.has_attr('title') or not foundContent): # next if (eachlink.has_attr('title')): #foundContent = True product.name = eachlink["title"] product.website = str(BASE_URL + eachlink["href"]) #print(product.name) #print(product.website) if ("₹" in eachlink.text): prices_div = eachlink.find_all('div')[0] if (len(prices_div) > 0): prices_inner_div = prices_div.find_all('div') if (len(prices_inner_div) > 0): price = prices_div.find_all('div')[0] product.price = price.text if (len(prices_inner_div) > 1): original_price = prices_div.find_all('div')[1] product.orginal_price = original_price.text #print(eachlink.innerHTML) #print(product.price) #print("===========") product.no_of_users_rated = str(0) product.rating = str(0) rating = each_product.find_all("div") i = 0 for r in rating: #print(str(i) + ":" + str(r.text)) i += 1 if ("(" in r.text and not "₹" in r.text): rating_text = r.text #print(rating_text) ratings_list = r.text.split('(') if (len(ratings_list) > 0): product.rating = ratings_list[0] if (len(ratings_list) > 1): product.no_of_users_rated = ratings_list[1].split( ')')[0] break if logging and product.name != "": print("Name:" + product.name + " Current Price:" + product.price + " Original Price:" + product.orginal_price + " No of user rated:" + product.no_of_users_rated + " Rating:" + product.rating + " Website:" + product.website) if (product.name != ""): products.append(product) return products except HTTPError as http_err: print(f'HTTP error occurred: {http_err}') # Python 3.6 time.sleep(10) if (attempt <= 5): return search(product_name, attempt + 1) else: print('Maximum retries reached') return [] except Exception as err: print(f'Other error occurred: {err}') # Python 3.6 exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] print(exc_type, fname, exc_tb.tb_lineno) time.sleep(10) if (attempt <= 5): return search(product_name, attempt + 1) else: print('Maximum retries reached') return [] else: print('Success') return products
def list_product(ID): p = repo.Product() p.get(ID) response = p.to_json() return make_response(jsonify(response)), 200
def search(product_name, attempt=0): URL = BASE_URL + SEARCH_PRE_PARAMS + product_name + SEARCH_POST_PARAMS products = [] print(URL) try: if (attempt > 0): URL = URL + '_' + str(attempt) print(str(attempt)) print(URL) response = requests.get(URL) # If the response was successful, no Exception will be raised response.raise_for_status() results = BeautifulSoup(response.content, 'html.parser') product_elements = results.find_all('div', class_='s-result-item') for each_product in product_elements: product = productsmodel.Product() all_links = each_product.find_all('a', class_='a-link-normal') for eachlink in all_links: if (eachlink.find('span', class_='a-size-medium')): title_elem = eachlink product.name = title_elem.text product.website = BASE_URL + eachlink.get('href') if (len(eachlink.find_all('span', class_="a-price")) > 0): all_prices = eachlink.find_all('span', class_="a-price") if (len(all_prices) > 0): product.price = all_prices[0].find_all('span')[0].text if (len(all_prices) > 1): product.orginal_price = all_prices[1].find_all( 'span')[0].text if (eachlink.find('span', class_='a-size-base')): # print(eachlink) product.url = eachlink.get('href') product.no_of_users_rated = eachlink.text if (each_product.find('i', class_='a-icon-star-small')): starelement = each_product.find('i', class_='a-icon-star-small') product.rating = starelement.text.split(' ')[0] # if(eachlink.find('span',class_='a-price')): # print(eachlink) if logging and product.name != "": print("Name:" + product.name + " Current Price:" + product.price + " Original Price:" + product.orginal_price + " No of user rated:" + product.no_of_users_rated + " Rating:" + product.rating + " Website:" + product.website) if (product.name != ""): products.append(product) # return products except HTTPError as http_err: print(f'HTTP error occurred: {http_err}') # Python 3.6 time.sleep(10) if (attempt <= 5): return search(product_name, attempt + 1) else: print('Maximum retries reached') return [] except Exception as err: print(f'Other error occurred: {err}') # Python 3.6 exc_type, exc_obj, exc_tb = sys.exc_info() fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1] print(exc_type, fname, exc_tb.tb_lineno) time.sleep(10) if (attempt <= 5): return search(product_name, attempt + 1) else: print('Maximum retries reached') return [] else: print('Success') return products