示例#1
0
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 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 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_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_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 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_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 test_rating_to_json_returns_json():
    expected = {
        'title': 'hello',
        'description': 'world'
    }

    rating = Rating(title='hello', description='world')
    result = rating.to_json()

    assert result == expected
示例#14
0
 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)
示例#15
0
	def add_rating(self, rating):
		user_id = rating["user_id"]
		host_id = rating["host_id"]
		stars = rating["stars"]
		comment = rating["comment"]
		session=self.DBSession
		user=session.query(User).filter(User.id==user_id).first()
		host=session.query(User).filter(User.id==host_id).first()
		if not user:
			return "User does not exist"
		if not host:
			return "Host does not exist"
		try:
			self.sync.update_obj(Rating(user=user, host=host, stars=stars, comment=comment))
			print (" > "+user.username+" rated "+host.username+" with "+str(stars)+" stars: "+comment)
			return "SUCCESS"
		except IntegrityError:
			print (" > Failed to add rating made by "+user.username)
			return "FAILURE"
示例#16
0
 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)
示例#17
0
    def post(self, item_id):
        json_data = request.get_json()
        current_user = get_jwt_identity()
        data, errors = rating_schema.load(data=json_data)
        item = Item.get_by_id(item_id=item_id)

        if errors:
            return {
                "message": "Validation errors",
                "errors": errors
            }, HTTPStatus.BAD_REQUEST

        if Rating.get_by_user_item(user_id=current_user,
                                   item_id=item.id) is not None:
            return {"message": "You have already rated this item"}

        rating = Rating(**data)
        rating.user_id = current_user
        rating.item_id = item.id
        rating.save()

        update_ratings(item_id=item.id)

        return rating_schema.dump(rating).data, HTTPStatus.CREATED
示例#18
0
    def post(self):
        data = json.loads(request.data)

        Rating().new_rating(self.get_user_id(), data["recipe"], data["score"])

        return "Success!"
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