Created
December 1, 2019 08:44
-
-
Save Daniiarz/560958cfa3d3bccf836e3719327533b7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Role(models.Model): | |
""" | |
Role model. Used as a model to group user to specific group depending on their type of work | |
""" | |
name = models.CharField(max_length=50) | |
def __str__(self): | |
return self.name |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RoleSerializer(serializers.ModelSerializer): | |
""" | |
Class for serializing role objects | |
""" | |
class Meta: | |
model = Role | |
fields = ( | |
"id", | |
"name" | |
) | |
read_only_fields = ('id',) | |
class RoleDeleteSerializer(serializers.ModelSerializer): | |
""" | |
Class for deleting role instances | |
""" | |
class Meta: | |
model = Role | |
fields = ( | |
"id", | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def test_delete_role(self): | |
""" | |
Testing deletion of role model | |
""" | |
role = RoleFactory(name="Admin") | |
payload = { | |
'id': role.id | |
} | |
response = self.client.delete(ROLES_URL, payload) | |
self.assertEqual(response.status_code, status.HTTP_204_NO_CONTENT) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class RoleViews(ListCreateAPIView, DestroyModelMixin): | |
""" | |
Class for creating, listing and deleting course models | |
""" | |
model = Role | |
queryset = Role.objects.all() | |
def get_serializer_class(self): | |
""" | |
Method for getting serializer depending on request method | |
""" | |
if self.request.method == "DELETE": | |
return serializers.RoleDeleteSerializer | |
return serializers.RoleSerializer | |
def destroy(self, request, *args, **kwargs): | |
instance = self.model.objects.get(request.data["id"]) | |
self.perform_destroy(instance) | |
print(request.data) | |
return Response(status=status.HTTP_204_NO_CONTENT) | |
def perform_destroy(self, instance): | |
instance.delete() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment