Skip to content

Commit

Permalink
Expand queryset in views. Solve minor bugs in serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
juliolugo96 committed Jul 12, 2019
1 parent f01aeae commit bf5f57c
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 25 deletions.
17 changes: 9 additions & 8 deletions api/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,20 +86,23 @@ class TaskSerializer(serializers.ModelSerializer):
board = serializers.PrimaryKeyRelatedField(
queryset=Board.objects.all())
task_to_user = AssigneeSerializer(many=True, required=False)
due_date = serializers.DateField()

class Meta:
model = Task
fields = ('id', 'title', 'description', 'due_date', 'priority', 'task_file',
'board', 'task_to_user')
'board', 'task_to_user', 'created_at',)
read_only_fields = ('created_at',)

def create(self, validated_data):
try:
users_data = validated_data.pop('task_to_user')
except KeyError as k:
users_data = {}
task = Project.objects.create(**validated_data)
# for data in users_data:
# UserProject.objects.create(task=task, **data)
task = Task.objects.create(**validated_data)
for data in users_data:
user = CustomUser.objects.get(pk=data)
Assignee.objects.create(task=task, **data)

return task

Expand Down Expand Up @@ -136,13 +139,11 @@ class Meta:
def create(self, validated_data):

request = self.context['request']


# print(request.data['project_photo'])

try:
# validated_data['project_to_user'] = json.loads(
# request.data['project_to_user'])
# validated_data['project_to_user'] = json.loads(
# request.data['project_to_user'])
validated_data['project_photo'] = request.data['project_photo']
except KeyError as k:
users_data = {}
Expand Down
9 changes: 4 additions & 5 deletions api/signals.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.db.models.signals import post_save
from django.dispatch import receiver
from users.models import CustomUser
from api.models import Board, Project, Comment, Assignee, Preferences, UserProject, Notification, UserNotification
from api.models import *
from asgiref.sync import async_to_sync
from channels.layers import get_channel_layer

Expand Down Expand Up @@ -73,7 +73,7 @@ def send_invitations(sender, instance, created, **kwargs):
user = instance.user
create_notification("project", user, instance)
payload = {
"project": instance.project.id,
"project": instance.project,
"notifier_type": "project",
"role": instance.role
}
Expand All @@ -88,9 +88,8 @@ def send_assignations(sender, instance, created, **kwargs):
user = instance.user
create_notification("assignation", user, instance)
payload = {
"project": instance.project.id,
"notifier_type": "project",
"role": instance.role
"task": instance.task,
"notifier_type": "assignation",
}
content = insert_content(payload)
# send_notification(user, content)
34 changes: 22 additions & 12 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ class AssigneeViewSet(viewsets.ModelViewSet):

def get_queryset(self):
task_id = self.request.query_params.get('task')
try:
task = Task.objects.get(pk=task_id)
except:
return Assignee.objects.none()
if task_id is not None:
try:
task = Task.objects.get(pk=task_id)
except:
return Assignee.objects.none()
else:
return Assignee.objects.all()

return Assignee.objects.filter(task=task)


Expand Down Expand Up @@ -65,10 +69,13 @@ class TaskViewSet(viewsets.ModelViewSet):

def get_queryset(self):
board_id = self.request.query_params.get('board')
try:
board = Board.objects.get(pk=board_id)
except:
return Task.objects.none()
if(board_id is not None):
try:
board = Board.objects.get(pk=board_id)
except:
return Task.objects.none()
else:
return Task.objects.all()

return Task.objects.filter(board=board)

Expand All @@ -83,10 +90,13 @@ class CommentViewSet(viewsets.ModelViewSet):

def get_queryset(self):
task_id = self.request.query_params.get('task')
try:
task = Task.objects.get(pk=task_id)
except:
return Comment.objects.none()
if task_id is not None:
try:
task = Task.objects.get(pk=task_id)
except:
return Comment.objects.none()
else:
return Comment.objects.all()

return Comment.objects.filter(task=task)

Expand Down

0 comments on commit bf5f57c

Please sign in to comment.