def cat_from_snippet(snippet, numeric_snippet=True): """ This function is intended to replace the "get_category_by_name" and the "get_category" function with a single function. If numeric_snippet == True Then it searches by categoryID, otherwise by category name. """ if numeric_snippet == True: s = select([ categories_table.c.categoryID, categories_table.c.category_name ]).where(categories_table.c.categoryID == snippet) elif numeric_snippet == False: s = select([ categories_table.c.categoryID, categories_table.c.category_name ]).where( categories_table.c.category_name.ilike("%{0}%".format(snippet))) else: print('Category not found.') return try: rp = connection.execute(s).fetchone() new_category = Category.from_sqlalchemy(categoryID=rp[0], category_name=rp[1]) return new_category except Exception as e: print('Category not found:', e) return
def get_categories(): s = select( [categories_table.c.categoryID, categories_table.c.category_name]) rp = connection.execute(s) categories_collection = [ Category.from_sqlalchemy(categoryID=i[0], category_name=i[1]) for i in rp ] return categories_collection
def add_category(): ''' Planned change: move manual category creation code to the objects.py file ''' new_category = Category.from_input() if new_category.category_name != '.': db.add_category(new_category) print('New category created: {0}'.format(new_category.name)) else: print('Invalid name, new category not created.')
def get_category(category_id): #returns a single category s = select([ categories_table.c.categoryID, categories_table.c.category_name ]).where(categories_table.c.categoryID == category_id) rp = connection.execute(s).fetchone() try: new_category = Category.from_sqlalchemy(categoryID=rp[0], category_name=rp[1]) return new_category except Exception as e: print('Category not found:', e) return
def get_categories(): url = 'https://www.themealdb.com/api/json/v1/1/list.php?c=list' f = request.urlopen(url) categories = [] try: data = json.loads(f.read().decode('utf-8')) for category_data in data['meals']: category = Category(category_data['strCategory']) categories.append(category) except (ValueError, KeyError, TypeError): print("JSON format error") return categories
def update_category(): admin = validate_admin() name = request.form['name'] weight = float(request.form['weight']) drops = int(request.form['drops']) try: category_id = int(request.form['id']) category = session.query(Category).get(category_id) category.name = name category.weight = weight category.drops = drops except: session.add(Category(name=name, weight=weight, drops=drops)) session.commit() return "Category %s successfully added/updated." % name
def get_category_by_name(category_snippet): ''' This function is intended to facilitate the search for articles by category using partial titles It gets the category by name and returns the articles by category if there is such a category. ''' #returns a single category s = select( [categories_table.c.categoryID, categories_table.c.category_name]).where( categories_table.c.category_name.ilike( "%{0}%".format(category_snippet))) rp = connection.execute(s).fetchone() try: #new_category = tuple(rp)[0] #take the single element out of nested tuple #new_category = make_category(rp) new_category = Category.from_sqlalchemy(categoryID=rp[0], category_name=rp[1]) return new_category except Exception as e: print('Category not found:', e) return
def make_category(row): return Category(row["categoryID"], row["categoryName"])
def make_category(row): ''' This function takes a row from the database and makes a category. ''' return Category(row[0], row[1])