def is_current_user_admin(self, db_session: Session, user_id: int, community_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 and not user.is_admin: return False return True
def get_time_table(self, db_session: Session, manager_id: int, day: str) -> TimeTable: time_table = db_session.query(TimeTable).filter_by(manager_id=manager_id)\ .filter_by(day_of_the_week=day)\ .order_by(TimeTable.start_work)\ .all() return time_table
def get_by_vk_id(self, db_session: Session, community_vk_id: int): community = (db_session.query(Community).options( lazyload("managers"), lazyload("admins")).filter_by( community_vk_id=community_vk_id).first()) if community is None: raise NoResultFound("Community not found") return community
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 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 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 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 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 get_settings(self, db_session: Session, community_id: int) -> Setting: settings = db_session.query(Setting).filter_by( community_id=community_id).first() return settings
def get(self, db_session: Session, community_id: int): community = (db_session.query(Community).options( lazyload("managers"), lazyload("admins")).get(community_id)) if community is None: raise NoResultFound("Community not found") return community
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()
def get_communities(self, db_session: Session, user_id: int): user = db_session.query(User).options(lazyload('admin_communities')).filter_by(id=user_id).first() return user.admin_communities
def get_user_by_id(self, db_session: Session, user_id: int): user = db_session.query(User).options(lazyload('admin_communities')).get(user_id) if user is None: raise NoResultFound('User not found') return user
def get_by_username(self, db_session: Session, username: str) -> User: user = db_session.query(User).filter_by(username=username).first() return user