def test_editing_an_item(self): self.assertEqual(self.bucketlist.id, 1) self.assertEqual(self.bucketlist.name, "Cook") item = Item("Cook lunch", 1, "Coooking Ugali omena") db.session.add(item) db.session.commit() item = Item.query.filter_by(name="Cook lunch", bucketlist_id=1).first() self.assertEqual(item.name, "Cook lunch") self.assertEqual(item.description, "Coooking Ugali omena") self.assertEqual(item.done, False) item.description = "Coooking Ugali fish" item.done = True db.session.add(item) db.session.commit() edit_item = Item.query.filter_by(name="Cook lunch", bucketlist_id=1).first() self.assertEqual(edit_item.description, "Coooking Ugali fish") self.assertEqual(edit_item.done, True)
def scrape_item(item_elem) -> Item: # Check if entry starts with text indicating it's a topping or addon target = item_elem.find('span', {'class': 'tooltip-target-wrapper'}) prefix = target.find(text=True, recursive=False).strip() is_addon = (prefix in ('w/', '&')) link = item_elem.find('a', {'class': 'recipelink'})['href'] # Chop URL to extract ID string item_id = link.replace('http://menu.dining.ucla.edu/Recipes/', '') item = Item(id=item_id) # First try to fetch detail page. page = requests.get(link + '/Boxed').text soup = BeautifulSoup(page, 'html.parser').find('div', {'class': 'recipecontainer'}) if soup is not None: item.name = soup.find('h2').text print('Parsing full report for ' + item.name) info = soup.find('div', {'class': 'productinfo'}) description_elem = info.find('div', {'class': 'description'}) if description_elem is not None: item.description = description_elem.text traits = info.find_all('div', {'class': 'prodwebcode'}) for trait in traits: setattr(item, trait_format(trait.text), True) nutrition_elem = soup.find('div', {'class': 'nfbox'}) item.nutrition = scrape_nutrition(nutrition_elem) ingredients = soup.find('div', {'class': 'ingred_allergen'}).find('p').find(text=True, recursive=False) if ingredients: ingredients = html.unescape(ingredients.strip()) item.ingredients = ingredients image = item_elem.find('img', {'class': 'recipeimage'}) if image is not None: item.image_url = 'http://menu.dining.ucla.edu' + image['src'] else: item.name = link.text.strip() print('Parsing limited report for ' + item.name) description_elem = item_elem.find('div', {'class': 'item-description'}) description = description_elem.find(text=True, recursive=False) if description: description = description.strip() item.description = description traits = description_elem.find_all('div', {'class': 'tt-prodwebcode'}) for trait in traits: setattr(item, trait_format(trait.text), True) return is_addon, item
def new_item(category_name, name, description): category = Category.query\ .filter(Category.name == category_name).first() item = Item() item.name = name item.description = description item.category = category item.user_id = login_session['user_id'] db.session.add(item) db.session.commit() return Item.query\ .filter(Item.category == category, Item.name == name).first()