def put(self, idd): data = self.__parse_request__() review = ReviewModel.find_by_id(idd) if not review: return { 'message': "There is no review with ['id': {}]".format(idd) }, 404 review.delete_from_db() user = AccountModel.find_by_id(data.get('user_id')) book = BookModel.find_by_id(data.get('book_id')) if not user or not book: if not user: return { 'message': "There is no account with ['id': {}]".format( data.get('user_id')) }, 404 if not book: return { 'message': "There is no book with ['id': {}]".format( data.get('book_id')) }, 404 new_review = ReviewModel(data.get('title'), data.get('user_id'), data.get('book_id'), data.get('date'), data.get('valuation'), data.get('comment')) new_review.save_to_db() user.reviews.append(new_review) book.reviews.append(new_review) return new_review.json(), 200
def delete(self, idd): exists = ReviewModel.find_by_id(idd) if not exists: return { 'message': "There is no review with ['id': {}], therefore it cannot be deleted" .format(idd) }, 404 user = AccountModel.find_by_id(exists.user_id) book = BookModel.find_by_id(exists.book_id) exists.delete_from_db() return { 'message': "Review with ['id': {}] has successfully been deleted".format(idd) }, 200
def test_put_review(self): response = self.app.post( 'api/review', data={ "user_id": 1, "book_id": 1, "title": "asd", "date": "25/11/2090 11:25", "valuation": 5, "comment": "lel" }, headers={ 'Authorization': 'Basic ' + base64.b64encode( bytes( str(self.acc.id) + ":" + json.loads(self.resp_account_admin.data)['token'], 'ascii')).decode('ascii') }, follow_redirects=True) response = self.app.put( 'api/review/1', data={ "user_id": 1, "book_id": 1, "title": "asd", "date": "25/11/2090 11:25", "valuation": 4, "comment": "lel" }, headers={ 'Authorization': 'Basic ' + base64.b64encode( bytes( str(self.acc.id) + ":" + json.loads(self.resp_account_admin.data)['token'], 'ascii')).decode('ascii') }, follow_redirects=True) rev = ReviewModel.find_by_id(1) self.assertEqual(200, response.status_code) self.assertEqual(4, rev.valuation)
def get(self, idd): review = ReviewModel.find_by_id(idd) if review: return review.json(), 200 return {'message': "Review with ['id': {}] not found".format(idd)}, 404