-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- add serializers and return flag object in serialized form when a request of correct form is made.
- Loading branch information
1 parent
f26ce46
commit 558aaca
Showing
7 changed files
with
152 additions
and
44 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
from django.contrib.auth import get_user_model | ||
from rest_framework import serializers | ||
|
||
from flag.models import Flag | ||
|
||
|
||
User = get_user_model() | ||
|
||
|
||
class UserSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = User | ||
fields = ('id', 'username') | ||
lookup_field = 'username' | ||
|
||
|
||
class FlagSerializer(serializers.ModelSerializer): | ||
class Meta: | ||
model = Flag | ||
fields = ('creator', 'count', 'state', 'verbose_state', 'moderator', 'is_flagged', 'reporters') | ||
read_only_fields = ('creator', 'moderator', 'reporters') | ||
|
||
creator = UserSerializer() | ||
moderator = UserSerializer() | ||
verbose_state = serializers.SerializerMethodField() | ||
reporters = serializers.SerializerMethodField() | ||
|
||
def __init__(self, *args, **kwargs): | ||
context = kwargs['context'] | ||
self.model_obj = context.get('model_obj') | ||
self.user = context.get('user') | ||
super().__init__(*args, **kwargs) | ||
|
||
@staticmethod | ||
def get_verbose_state(obj): | ||
return obj.get_verbose_state(obj.state) | ||
|
||
def get_reporters(self, obj): | ||
flag_obj = Flag.objects.get_flag(self.model_obj) | ||
return [ | ||
{'id': instance.user.id, 'username': instance.user.username} for instance in flag_obj.flags.all() | ||
] |
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
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
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
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
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
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