def get(self, request): """ Generate a new favorite if the search in db return nothing. If the db return something this favorite is deleted. """ username = request.GET["username"] line_number = request.GET["line_number"] user = BusinemeUser.objects.get(username=username) busline = Busline.objects.get(line_number=line_number) try: favorite = Favorite.objects.get(user=user, busline=busline) favorite.delete() json_data = serialize(favorite) except Favorite.DoesNotExist: message = ReturnMessage() new_favorite = Favorite() new_favorite.user = user new_favorite.busline = busline new_favorite.save() json_data = message.return_message(STATUS_NOT_FOUND) return JsonResponse(json_data, content_type="application/json")
def get_user(self, user_id): """Return a user given a id""" try: user = BusinemeUser.objects.get(pk=user_id) json_data = serialize(user) except BusinemeUser.DoesNotExist: message = ReturnMessage() json_data = message.return_message(STATUS_NOT_FOUND) return JsonResponse(json_data, content_type='application/json')
def get_busline(self, busline_id): """ Obtains the required busline based in line's number. """ try: busline = Busline.objects.get(pk=busline_id) json_data = serialize(busline) except Busline.DoesNotExist: message = ReturnMessage() json_data = message.return_message(STATUS_NOT_FOUND) return JsonResponse(json_data, content_type="application/json")
class TestReturnMessage(TestCase): def setUp(self): self.response_message = ReturnMessage() """ This method test is used for ensure the correct message status based in status code when everything is okay. """ def test_return_for_200(self): message = self.response_message.return_message( STATUS_CODE_OK) self.assertEquals(message['return_message'], 'Everything Worked.') """ This method test is used for ensure the correct message status based in status code when one thing is created with success. """ def test_return_for_201(self): message = self.response_message.return_message( STATUS_CODE_CREATED) self.assertEquals(message['return_message'], 'Successfully Created.') """ This method test is used for ensure the correct message status based in status code when one search not found the results. """ def test_return_for_404(self): message = self.response_message.return_message( STATUS_CODE_NOT_FOUND) self.assertEquals(message['return_message'], 'Not Found.') """ This method test is used for ensure the correct message status based in status code when occurs one Internal Error. """ def test_return_for_500(self): message = self.response_message.return_message( STATUS_CODE_SERVER_ERROR) self.assertEquals(message['return_message'], 'Internal Server Error.') """ This method test is used for ensure the correct message status based in status code when something went wrong. """ def test_return_for_wrong(self): message = self.response_message.return_message( STATUS_UNDEFINED) self.assertEquals(message['return_message'], 'Something went wrong.')
def get_post(self, post_id): """ Obtains the required terminal based in line's number. """ try: post = Post.objects.get(pk=post_id) json_data = serialize(post) except Post.DoesNotExist: message = ReturnMessage() json_data = message.return_message(STATUS_NOT_FOUND) return JsonResponse(json_data, content_type="application/json")
class TestReturnMessage(TestCase): def setUp(self): self.response_message = ReturnMessage() """ This method test is used for ensure the correct message status based in status code when everything is okay. """ def test_return_for_200(self): message = self.response_message.return_message(STATUS_CODE_OK) self.assertEquals(message['return_message'], 'Everything Worked.') """ This method test is used for ensure the correct message status based in status code when one thing is created with success. """ def test_return_for_201(self): message = self.response_message.return_message(STATUS_CODE_CREATED) self.assertEquals(message['return_message'], 'Successfully Created.') """ This method test is used for ensure the correct message status based in status code when one search not found the results. """ def test_return_for_404(self): message = self.response_message.return_message(STATUS_CODE_NOT_FOUND) self.assertEquals(message['return_message'], 'Not Found.') """ This method test is used for ensure the correct message status based in status code when occurs one Internal Error. """ def test_return_for_500(self): message = self.response_message.return_message( STATUS_CODE_SERVER_ERROR) self.assertEquals(message['return_message'], 'Internal Server Error.') """ This method test is used for ensure the correct message status based in status code when something went wrong. """ def test_return_for_wrong(self): message = self.response_message.return_message(STATUS_UNDEFINED) self.assertEquals(message['return_message'], 'Something went wrong.')
def setUp(self): self.response_message = ReturnMessage()