def validate_app_name(self, app_name): if not app_name: self.error = ContentTypeError.APP_NAME_MISSING raise CommentBadRequest(self.error) if not ContentType.objects.filter(app_label=app_name).exists(): self.error = ContentTypeError.APP_NAME_INVALID.format(app_name=app_name) raise CommentBadRequest(self.error) return app_name
def validate_app_name(self, app_name): if not app_name: self.error = 'app name must be provided' raise CommentBadRequest(self.error) if not ContentType.objects.filter(app_label=app_name).exists(): self.error = f'{app_name} is NOT a valid app name' raise CommentBadRequest(self.error) return app_name
def validate_model_id(self, model_id): if not model_id: self.error = ContentTypeError.MODEL_ID_MISSING raise CommentBadRequest(self.error) try: model_id = int(model_id) except ValueError: self.error = ContentTypeError.ID_NOT_INTEGER.format(var_name='model', id=model_id) raise CommentBadRequest(self.error) return model_id
def validate_model_id(self, model_id): if not model_id: self.error = "model id must be provided" raise CommentBadRequest(self.error) try: model_id = int(model_id) except ValueError: self.error = f'model id must be an integer, {model_id} is NOT' raise CommentBadRequest(self.error) return model_id
def validate_content_type_object(self, app_name, model_name): try: ct_object = ContentType.objects.get(model=model_name, app_label=app_name) except ContentType.DoesNotExist: self.error = ContentTypeError.MODEL_NAME_INVALID.format(model_name=model_name) raise CommentBadRequest(self.error) return ct_object
def validate_comment_object(self, model_id, parent_id): try: comment = Comment.objects.get(id=parent_id, object_id=model_id) except Comment.DoesNotExist: self.error = ContentTypeError.PARENT_ID_INVALID.format(parent_id=parent_id) raise CommentBadRequest(self.error) return comment
def validate_parent_id(self, parent_id): try: parent_id = int(parent_id) except ValueError: self.error = f'the parent id must be an integer, {parent_id} is NOT' raise CommentBadRequest(self.error) return parent_id
def validate_parent_id(self, parent_id): try: parent_id = int(parent_id) except ValueError: self.error = ContentTypeError.ID_NOT_INTEGER.format(var_name='parent', id=parent_id) raise CommentBadRequest(self.error) return parent_id
def validate_model_object(self, app_name, model_name, model_id): ct_object = self.validate_content_type_object(app_name, model_name) model_class = ct_object.model_class() model_query = model_class.objects.filter(id=model_id) if not model_query.exists() and model_query.count() != 1: self.error = ContentTypeError.MODEL_ID_INVALID.format(model_id=model_id, model_name=model_name) raise CommentBadRequest(self.error) return model_query.first()
def validate_model_object(self, app_name, model_name, model_id): ct_object = self.validate_content_type_object(app_name, model_name) model_class = ct_object.model_class() model_query = model_class.objects.filter(id=model_id) if not model_query.exists() and model_query.count() != 1: self.error = f'{model_id} is NOT a valid model id for the model {model_name}' raise CommentBadRequest(self.error) return model_query.first()
def validate_content_type_object(self, app_name, model_name): try: ct_object = ContentType.objects.get(model=model_name, app_label=app_name) except ContentType.DoesNotExist: self.error = f'{model_name} is NOT a valid model name' raise CommentBadRequest(self.error) return ct_object
def test_create_custom_error_without_drf_installed(self): with patch.dict(sys.modules, {'rest_framework.exceptions': None}): from importlib import reload reload(sys.modules['comment.exceptions']) from comment.exceptions import CommentBadRequest exception = CommentBadRequest() self.assertEqual(exception.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(exception.detail, self._default_detail)
def validate_comment_object(self, model_id, parent_id): try: comment = Comment.objects.get(id=parent_id, object_id=model_id) except Comment.DoesNotExist: self.error = ( f'{parent_id} is NOT a valid id for a parent comment or ' 'the parent comment does NOT belong to the provided model object' ) raise CommentBadRequest(self.error) return comment
def test_create_custom_error_with_params(self): detail = 'not found' exception = CommentBadRequest(detail=detail, status_code=404) self.assertEqual(exception.status_code, status.HTTP_404_NOT_FOUND) self.assertEqual(exception.detail, detail)
def validate_model_name(self, model_name): if not model_name: self.error = "model name must be provided" raise CommentBadRequest(self.error) return str(model_name).lower()
def validate_model_name(self, model_name): if not model_name: self.error = ContentTypeError.MODEL_NAME_MISSING raise CommentBadRequest(self.error) return str(model_name).lower()
def test_can_create_custom_error_without_params(self): exception = CommentBadRequest() self.assertEqual(exception.status_code, status.HTTP_400_BAD_REQUEST) self.assertEqual(exception.detail, self._default_detail)