def remove_time_table(self, db_session: Session, time_table_id: int): time_table = db_session.query(TimeTable).get(time_table_id) if time_table_id is None: raise NoResultFound('Time Table not found') db_session.delete(time_table) db_session.commit() return time_table
def remove_manager(self, db_session: Session, manager_id): manager = db_session.query(Manager).get(manager_id) if manager is None: raise NoResultFound("Manager not found") db_session.delete(manager) db_session.commit() return manager
def set_settings(self, db_session: Session, setting_to_set: SettingSet): new_setting = Setting(welcome_speech=setting_to_set.welcome_speech, color_button=setting_to_set.color_button, community_id=setting_to_set.community_id) db_session.add(new_setting) db_session.commit() db_session.refresh(new_setting) return new_setting
def add_time_table(self, db_session: Session, time_to_set: TimeTableSet): new_time_table = TimeTable(manager_id=time_to_set.manager_id, day_of_the_week=time_to_set.day_of_the_week, start_work=time_to_set.start_work, end_work=time_to_set.end_work) db_session.add(new_time_table) db_session.commit() db_session.refresh(new_time_table) return new_time_table
def upgrade(self): cnx = connector.connect(host=self.config.host, user=self.config.username, password=self.config.password) db_cursor = cnx.cursor() db_cursor.execute( "SELECT SCHEMA_NAME FROM INFORMATION_SCHEMA.SCHEMATA WHERE SCHEMA_NAME = '{0}'" .format(self.config.database)) db_exists = db_cursor.fetchone() if not db_exists: try: db_cursor.execute("CREATE DATABASE {0}".format( self.config.database)) except Exception as ex: message = 'Could not create database: ' + ex.msg logging.error(message) raise Exception(message) cnx.database = self.config.database session = Session(cnx) # do it. session.execute(""" CREATE TABLE IF NOT EXISTS upgrade_history ( id INTEGER PRIMARY KEY AUTO_INCREMENT NOT NULL, upgrade_date DATETIME, version INTEGER )""") session.execute("SELECT IFNULL(MAX(version),0) FROM upgrade_history") db_version = session.fetch_results()[0][0] # first row first col script = self.__create_upgrade_script() max_version = 0 for query in script: if query['version'] > db_version: if query['version'] > max_version: max_version = query['version'] session.execute(query['sql']) if max_version > db_version: session.execute( """INSERT INTO upgrade_history (upgrade_date, version) VALUES('{date}', '{version}');""".format( date=datetime.now().isoformat(), version=max_version)) logginghelper.log_debug('Upgraded database to version ' + str(max_version)) session.commit() session.close()
def update_settings(self, db_session: Session, community_id: int, setting_to_update: SettingUpdate): update_setting = db_session.query(Setting).filter_by( community_id=community_id).first() if update_setting is None: raise NoResultFound('Setting not found') update_setting.welcome_speech = setting_to_update.welcome_speech update_setting.color_button = setting_to_update.color_button db_session.add(update_setting) db_session.commit() db_session.refresh(update_setting) return update_setting
def add_manager(self, db_session: Session, community_id: int, manager_to_add: ManagerCreate): community = (db_session.query(Community).options( lazyload("managers")).get(community_id)) manager = Manager( phone=manager_to_add.phone, name=manager_to_add.name, is_blocked=manager_to_add.is_blocked, ) community.managers.append(manager) db_session.add(community) db_session.commit() db_session.refresh(community) return community
def update_time_table(self, db_session: Session, time_table_id: int, time_to_update: TimeTableUpdate): update_time = db_session.query(TimeTable).filter_by( id=time_table_id).first() if update_time is None: raise NoResultFound('Time Table not found') update_time.day_of_the_week = time_to_update.day_of_the_week update_time.start_work = time_to_update.start_work update_time.end_work = time_to_update.end_work db_session.add(update_time) db_session.commit() db_session.refresh(update_time) return update_time
def remove(self, db_session: Session, community_id: int, user_id: int): community = (db_session.query(Community).options( lazyload("admins")).get(community_id)) if community is None: raise NoResultFound("Community not found") user = db_session.query(User).get(user_id) if user is None: raise NoResultFound("User not found") if user not in community.admins: raise Exception("User is not admin of this community") db_session.delete(community) db_session.commit() return community
def download_recipe_meta_data(cls): """Downloads recipe metadata""" session = Session() # loading bar based on recipe count recipe_count = 1 recipe_batch_count = cls.get_recipe_batch_count() cls.logger.info('Parsing and saving recipe meta data') with tqdm(total=recipe_batch_count, unit=' recipes') as pbar: for page in range(1, cls.max_api_page): req = requests.get(RECIPE_API, params={'page': page}) soup = BeautifulSoup(req.text, 'html.parser') section = soup.find('section', {'id': 'archive-recipes'}) recipes = section.findAll('li') for recipe_html in recipes: if recipe_count > RECIPE_BATCH_COUNT: return slug = recipe_html.a['href'].replace( '/plant-based-recipes/', '') recipe = get_or_create(session, Recipe, slug=slug) recipe.name = recipe_html.img['title'] cls.logger.info('Parsing recipe: ' + recipe.name) recipe.slug = slug recipe.url = 'https://' + cls.hostname + \ str(recipe_html.a['href']) recipe.origin = SERVICE_CODE recipe.country = 'US' assets = [] thumbnail_url = recipe_html.img['src'] thumbnail = get_or_create( session, Asset, url=thumbnail_url) thumbnail.type = 'thumbnail' thumbnail.url = thumbnail_url assets.append(thumbnail) recipe.assets = assets session.add(recipe) session.commit() pbar.update(1) recipe_count += 1 page += 1
def create(self, db_session: Session, community_to_create: CommunityCreate, user_id: int): info = services.vk_service.get_community_info( community_to_create.api_key, community_to_create.community_vk_id) community = Community( community_vk_id=community_to_create.community_vk_id, avatar_url=info["photo_200"], name=info["name"], ) user = db_session.query(User).get(user_id) if user is not None: community.admins.append(user) db_session.add(community) db_session.commit() db_session.refresh(community) return community
def create_user(self, db_session: Session, user_to_create: UserCreate): existing_user = self.get_by_username(db_session, user_to_create.username) if existing_user is not None: raise Exception('User already exists') new_user = User( username=user_to_create.username, first_name=user_to_create.first_name, last_name=user_to_create.last_name, is_admin=user_to_create.is_admin, vk_id=user_to_create.vk_id, avatar_url=user_to_create.avatar_url, email=user_to_create.email, phone=user_to_create.phone, password=user_to_create.password, ) new_user.set_password(user_to_create.password) db_session.add(new_user) db_session.commit() db_session.refresh(new_user) return new_user
def download_recipe_data(cls): session = Session() recipes_dto = session.query(Recipe).filter( Recipe.origin == SERVICE_CODE).all() cls.logger.info('Parsing and saving recipe data') for recipe_dto in tqdm(recipes_dto, unit=' recipes'): cls.logger.info('Downloading recipe: ' + recipe_dto.name) recipe_html = requests.get(recipe_dto.url) soup = BeautifulSoup(recipe_html.text, 'html.parser') # Main Image tag = soup.find('source', {'media': '(max-width: 1199px)'}) image_url = tag['srcset'] image = get_or_create(session, Asset, url=image_url) image.type = 'image' image.url = image_url recipe_dto.assets.append(image) # Description description = soup.find( 'section', {'class': 'recipe-description'}).find('p') recipe_dto.description = description.text # Summary uls = soup.find('div', {'class': 'recipe-side-note'}).findAll('ul') li = uls[0].findAll('li') prep = li[0].text.split(':') time = prep[1].replace('minutes', 'M').replace(' ', '') recipe_dto.time = time[:time.find('M')+1] servings = li[1].text.split(':') recipe_dto.servings = servings[1] # Nutrition for li in uls[1].findAll('li'): nutrition = li.text.split(':') nutrition_name = nutrition[0].strip() nutrition_code = nutrition_name.lower().replace(' ', '-') nutrition_dto = get_or_create( session, Nutrition, code=nutrition_code) nutrition_dto.name = nutrition_name nutrition_amount = nutrition[1].strip() nutrition_unit = None if nutrition_code == 'calories': nutrition_unit = 'cal' else: nutrition_unit = 'g' recipe_nutrition_dto = get_or_create( session, RecipeNutrition, recipe=recipe_dto, nutrition=nutrition_dto) recipe_nutrition_dto.amount = nutrition_amount recipe_nutrition_dto.unit = nutrition_unit # Ingredients main_recipe = soup.find('section', {'class': 'main-recipe'}) ingredients = main_recipe.find('ol').findAll('li') ingredient_parser = IngredientParser() for ingredient in ingredients: recipe_ingredient_dtos = ingredient_parser.clense_ingredients( ingredient.string) if recipe_ingredient_dtos: for recipe_ingredient_dto in recipe_ingredient_dtos: ingredient_dto = get_or_create( session, Ingredient, code=recipe_ingredient_dto.ingredient.code) ingredient_dto.name = recipe_ingredient_dto.ingredient.name recipe_ingredient = get_or_create( session, RecipeIngredient, recipe=recipe_dto, ingredient=ingredient_dto) recipe_ingredient.ingredient = ingredient_dto if recipe_ingredient_dto.amount is not None: recipe_ingredient.amount = recipe_ingredient_dto.amount if recipe_ingredient_dto.unit is not None: recipe_ingredient.unit = recipe_ingredient_dto.unit # Instructions steps = soup.find( 'section', {'class': 'recipe-instruct'}).findAll('div', {'class': 'row'}) stepNbr = 1 for step in steps[1:]: instruction_dto = get_or_create( session, Instruction, recipe=recipe_dto, step=stepNbr) instruction_dto.description = step.find( 'p', {'class': 'instruction-description'}).text instruction_image_dto = get_or_create( session, Asset, instruction=instruction_dto, type='image') instruction_image_dto.url = step.find('img')['src'] stepNbr += 1 session.commit()