def test_rating_to_json_returns_json(): expected = { 'title': 'hello', 'description': 'world' } rating = Rating(title='hello', description='world') result = rating.to_json() assert result == expected
def rating_book_request_impl(args): try: book_name = args['book_name'] author_name = args['author_name'] category_name = args['category_name'] rating = args['rating'] if len(book_name) == 0: raise InvalidBookNameException if rating < 0 or rating > 5: raise InvalidRatingException book = find_book_with_name(book_name) if book is None: book = Book(book_name, author_name, category_name) db.session.add(book) rating = Rating(rating, book) db.session.add(rating) book.ratings.append(rating) db.session.commit() return Response(True, "Book Rating Done", BookSchema().dumps(book).data).output() except Exception as e: return json.dumps({"error": str(e)})
def db_seed(): user = User(email='*****@*****.**', first_name='Abc', last_name='Abc', password='******') db_session.add(user) db_session.commit() db_session.bulk_insert_mappings(Category, [dict(name="Mexican", description="Mexican style food (don't say!)"), dict(name="Japanese", description="Japanese style food")] ) db_session.commit() mexican = Category.query.first() db_session.bulk_insert_mappings(Ingredient, [dict(name="Mac & Cheese"), dict(name='Milk')] ) db_session.commit() ingredients = Ingredient.query.all() mac_and_cheese = ingredients[0] milk = ingredients[1] recipe = Recipe(name='Mac & Cheese', servings='1 person', preparation_time='15 min', photo_path='fakepath') recipe.steps.append(Step(number=1,title='Open the box',instructions='Get the box of Mac & cheese you bought and open it. Watch out that opening those boxes can be tricky.')) recipe.steps.append(Step(number=2,title='Follow the instructions on the box',instructions="Read whatever is written in the box and follow it. You're welcome.")) recipe.categories.append(mexican) recipe.ingredients_recipes.append(IngredientRecipe(ingredient_id=mac_and_cheese.id, quantity='1', unit='box')) recipe.ingredients_recipes.append(IngredientRecipe(ingredient_id=milk.id, quantity='half', unit='pint')) user.recipes.append(recipe) db_session.add(user) db_session.commit() rating = Rating() rating.recipe = recipe rating.user = user rating.rating = 5 db_session.add(rating) db_session.commit() saved = Saved() saved.recipe = recipe saved.user = user db_session.add(saved) db_session.commit() comment = Comment(text='I like my recipe.', recipe_id=recipe.id, user_id=user.id) db_session.add(comment) db_session.commit() db_session.close()
def test_user_calculates_rating_when_hours_600_999(): expected = Rating(title='Even my mum has more hours on candy crush', description="She's over level 9000") rating = Rating_calc().get_rating(750) assert rating.title == expected.title assert rating.description == expected.description
def get_rating(self, hours: int): if hours >= 0 and hours <= 39: return Rating(title='What even are games?', description='Seriously what are they???') if hours >= 40 and hours <= 299: return Rating(title='You might aswell just play mobile games', description='Sponsored by RAID: Shadow Legends') if hours >= 300 and hours <= 599: return Rating( title= 'You gotta pump those numbers up. Those are rookie numbers', description='I myself have more than 1000 hours') if hours >= 600 and hours <= 999: return Rating(title='Even my mum has more hours on candy crush', description="She's over level 9000") if hours >= 1000 and hours <= 1999: return Rating(title='Its all civilisation isnt it?', description="Just one more turn") if hours >= 2000 and hours <= 3999: return Rating(title='You have a healthy balance', description="Not too much but not too little") if hours >= 4000 and hours <= 5999: return Rating( title='Are you going pro??', description="* insert wannabe esports pro starter pack *") if hours >= 6000 and hours <= 7999: return Rating( title='Certified Hardcore Gamer', description= "Get your certificate here: www.ImaHardcoreGamer.com") if hours >= 8000 and hours <= 9999: return Rating( title='Dude. Are you okay?', description="When was the last time you went outside??") if hours >= 1000: return Rating(title='You need to seek medical help', description=" https://www.nhs.uk/")
def test_user_calculates_rating_when_hours_8000_9999(): expected = Rating(title='Dude. Are you okay?', description="When was the last time you went outside??") rating = Rating_calc().get_rating(8500) assert rating.title == expected.title assert rating.description == expected.description
def test_user_calculates_rating_when_hours_more_10_000(): expected = Rating(title='You need to seek medical help', description=" https://www.nhs.uk/") rating = Rating_calc().get_rating(15_000) assert rating.title == expected.title assert rating.description == expected.description
def test_user_calculates_rating_when_hours_1000_1999(): expected = Rating(title='Its all civilisation isnt it?', description="Just one more turn") rating = Rating_calc().get_rating(1500) assert rating.title == expected.title assert rating.description == expected.description
def test_user_calculates_rating_when_hours_2000_3999(): expected = Rating(title='You have a healthy balance', description="Not too much but not too little") rating = Rating_calc().get_rating(2999) assert rating.title == expected.title assert rating.description == expected.description
def test_user_calculates_rating_when_hours_50_299(): expected = Rating(title='You might aswell just play mobile games', description='Sponsored by RAID: Shadow Legends') rating = Rating_calc().get_rating(190) assert rating.title == expected.title assert rating.description == expected.description
def test_user_calculates_rating_when_hours_0_49(): expected = Rating(title='What even are games?', description='Seriously what are they???') rating = Rating_calc().get_rating(30) assert rating.title == expected.title assert rating.description == expected.description
def test_user_calculates_rating_when_hours_6000_7999(): expected = Rating( title='Certified Hardcore Gamer', description="Get your certificate here: www.ImaHardcoreGamer.com") rating = Rating_calc().get_rating(6500) assert rating.title == expected.title assert rating.description == expected.description
def test_user_calculates_rating_when_hours_4000_5999(): expected = Rating( title='Are you going pro??', description="* insert wannabe esports pro starter pack *") rating = Rating_calc().get_rating(5000) assert rating.title == expected.title assert rating.description == expected.description
def test_user_calculates_rating_when_hours_300_599(): expected = Rating( title='You gotta pump those numbers up. Those are rookie numbers', description='I myself have more than 1000 hours') rating = Rating_calc().get_rating(400) assert rating.title == expected.title assert rating.description == expected.description
def create_rating(self): try: user_id = self.view.get_int('user id') if user_id is None: raise ValueError(self.view.show_error('user id')) app_id = self.view.get_int('app id') if app_id is None: raise ValueError(self.view.show_error('app id')) rating = self.view.get_float('rating') if rating is None or not 1.0 <= rating <= 5.0: raise ValueError(self.view.show_error('rating')) rating_date = self.view.get_date('rating date') if rating_date is None: raise ValueError(self.view.show_error('rating date')) comment = self.view.get_str('comment') return Rating(0, user_id, app_id, rating, rating_date, comment) except Exception as err: self.view.show_error(err)
def edit_rating(self, app_id, user_id): rating = self.model.get_rating(app_id, user_id) self.view.list_str(rating, 'Rating') options = ['user_id', 'app_id', 'rating', 'rating_date', 'comment'] while True: try: self.view.numerated_array(options) option = self.view.get_int('number') if option == 0: user_id = self.view.get_int('user_id') if user_id is None: raise ValueError(self.view.show_error('user_id')) rating['user_id'] = user_id elif option == 1: app_id = self.view.get_int('app_id') if app_id is None: raise ValueError(self.view.show_error('app_id')) rating['app_id'] = app_id elif option == 2: value = self.view.get_float('rating') if value is None or not 1.0 <= value <= 5.0: raise ValueError(self.view.show_error('rating')) rating['rating'] = value elif option == 3: rating_date = self.view.get_date('rating date') if rating_date is None: raise ValueError(self.view.show_error('rating date')) rating['rating_date'] = rating_date elif option == 4: comment = self.view.get_str('comment') rating['comment'] = comment else: raise ValueError('You need to enter action') return Rating(rating['link_id'], rating['user_id'], rating['app_id'], rating['rating'], rating['rating_date'], rating['comment']) except Exception as err: self.view.show_error(err)
def get(self, item_id): item = Item.get_by_id(item_id=item_id) if item is None: return item_not_found() ratings = Rating.get_by_item(item_id=item.id) return rating_list_schema.dump(ratings).data, HTTPStatus.OK
def load_recipes(cls, current_user, limit=8, page=1, order_by="created_at DESC", join=[], where=[]): attributes = ["id", "name", "servings", "preparation_time", "photo_file", "created_at", "creator_id"] if page == 1: offset = "" else: offset = "OFFSET %s" % adapt(str(limit * (page - 1))).getquoted()[1:-1] join_sql = "" if len(join) > 0: join_list = [] for join_desc in join: if join_desc == "avg_ratings": join_list.append( "LEFT OUTER JOIN (SELECT recipe_id, AVG(rating) AS avg_rating FROM ratings GROUP BY recipe_id) AS avg_ratings ON recipes.id=avg_ratings.recipe_id" ) if join_desc == "saved": join_list.append( "INNER JOIN saved ON recipes.id=saved.recipe_id AND saved.user_id=%s" % adapt(str(current_user.id)).getquoted() ) join_sql = " ".join(join_list) where_array = where where_sql = "" if len(where_array) > 0: where_sql += "WHERE " + (" AND ".join(where_array)) result = engine.execute( "SELECT %s FROM recipes %s %s ORDER BY %s LIMIT %s %s" % (",".join(attributes), join_sql, where_sql, order_by, adapt(str(limit)).getquoted(), offset) ) dicts = [] for values in result: attrs = {attributes[i]: value for i, value in enumerate(values)} dicts.append(attrs) recipes = cls.create_from_dicts(dicts) page_count = 1 recipe_count = 0 if len(recipes) > 0: # get total recipe count result = engine.execute("SELECT COUNT(*) FROM recipes %s %s" % (join_sql, where_sql)) recipe_count = None for result_tuple in result: recipe_count = result_tuple[0] page_count = int(math.ceil(float(recipe_count) / limit)) if page_count == 0: page_count = 1 # get avg ratings recipe_ids = [str(recipe.id) for recipe in recipes] ratings = Rating.avg_rating_by_recipe(recipe_ids) # get favorited favorites = Saved.favorites(recipe_ids, current_user.id) # inject things on recipes for recipe in recipes: recipe.avg_rating = ratings[recipe.id] recipe.round_rating = round(recipe.avg_rating) recipe.is_user_favorite = recipe.id in favorites return recipes, page_count, recipe_count
def rate(recipe_id=None, rating=None): if recipe_id != None and rating != None: Rating.rate(g.current_user, recipe_id, rating) return ''
def ratings(self): from models.rating import Rating return Rating.gql('WHERE folder_list = :1', self.key())
def test_user_calculates_rating(user): expected = Rating(title='What even are games?', description='Seriously what are they???') assert user.rating.title == expected.title assert user.rating.description == expected.description