Exemplo n.º 1
0
 def update_post(self, request, **kwargs):
     try:
         post = Post.objects.get(**kwargs)
     except Post.DoesNotExist:
         return response404('Post')
     self.check_object_permissions(request=request, obj=post)
     file = request.FILES.get('file', None)
     image = request.FILES.get('image', None)
     if file:
         post.file = file
         post.save()
         request.data.pop('post')
     if image:
         post.image = image
         post.save()
         request.data.pop('image')
     serializer = PostSerializer(post, data=request.data, partial=True)
     if not serializer.is_valid():
         return response406({
             **serializer.errors, 'message': 'Błąd walidacji'
         })
     serializer.update(post, serializer.validated_data)
     return response200({
         **serializer.data, 'message':
         'Pomyślnie zaktualizowano posta'
     })
Exemplo n.º 2
0
 def messages(self, request, **kwargs):
     user_2 = User.objects.get(id=kwargs.get('id'))
     try:
         thread = Thread.objects.get_or_create(request.user, user_2)
     except Thread.DoesNotExist:
         return response406({'message': 'Nie możesz czatować sam ze sobą!'})
     serializer = ThreadSerializer(thread)
     return response200({**serializer.data})
Exemplo n.º 3
0
 def create_group(self, request):
     serializer = GroupSerializer(data=request.data, partial=True)
     if not serializer.is_valid():
         return response406({
             **serializer.errors, 'message':
             'Złe dane wejściowe'
         })
     serializer.save(owner=request.user)
     return response200(data=serializer.data)
Exemplo n.º 4
0
 def search_for_groups(self, request):
     try:
         search_phrase = request.GET['phrase']
     except MultiValueDictKeyError:
         return response406({'message': 'Złe dane wejściowe'})
     groups = Group.objects.filter(name__icontains=search_phrase)
     final_groups = [
         group for group in groups
         if (request.user not in group.members.all()) and (
             group.owner != request.user)
     ]
     groups_data = GroupSerializer(final_groups, many=True).data
     paginator = PageNumberPagination()
     data = paginator.paginate_queryset(groups_data, request)
     return paginator.get_paginated_response(data)
Exemplo n.º 5
0
 def join_group(self, request, **kwargs):
     try:
         group = Group.objects.get(**kwargs)
     except Group.DoesNotExist:
         return response404('Group')
     pendings = PendingMember.objects.filter(group=group, user=request.user)
     if request.user in group.members.all(
     ) or request.user == group.owner or pendings.exists():
         return response406({'message': 'Jesteś już członkiem tej grupy'})
     pending = PendingMember.objects.create(user=request.user, group=group)
     serializer = PendingMembersSerializer(pending)
     return response200({
         **serializer.data, 'message':
         'Pomyślnie zapisano się na listę oczekujących'
     })
Exemplo n.º 6
0
 def create_comment(self, request, **kwargs):
     try:
         post = Post.objects.get(**kwargs)
     except Post.DoesNotExist:
         return response404('Post')
     self.check_object_permissions(request=request, obj=post.group)
     serializer = CommentSerializer(data=request.data, partial=True)
     if not serializer.is_valid():
         return response406({
             **serializer.errors, 'message': 'Błąd walidacji'
         })
     serializer.save(post=post, owner=request.user)
     return response200({
         **serializer.data, 'message':
         'Pomyślnie utworzono komentarz'
     })
Exemplo n.º 7
0
 def update_comment(self, request, **kwargs):
     try:
         comment = Comment.objects.get(**kwargs)
     except Comment.DoesNotExist:
         return response404('Comment')
     serializer = CommentSerializer(comment,
                                    data=request.data,
                                    partial=True)
     self.check_object_permissions(request=request, obj=comment)
     if not serializer.is_valid():
         return response406({
             **serializer.errors, 'message': 'Błąd walidacji'
         })
     serializer.save()
     return response200({
         **serializer.data, 'message':
         'Pomyślnie zaktualizowano komentarz'
     })
Exemplo n.º 8
0
 def update_group(self, request, **kwargs):
     try:
         group = Group.objects.get(**kwargs)
     except Group.DoesNotExist:
         return response404('Group')
     self.check_object_permissions(request=request, obj=group)
     serializer = GroupSerializer(group, data=request.data, partial=True)
     if 'moderator' in request.data.keys():
         user = User.objects.get(id=request.data.get('moderator'))
         group.moderator = user
         request.data.pop('moderator')
     if 'image' in request.FILES.keys():
         group.image = request.FILES.get('image')
         request.data.pop('image')
     group.save()
     if not serializer.is_valid():
         return response406({
             **serializer.errors, 'message': 'Błąd walidacji'
         })
     serializer.save()
     return response200(
         data={
             **serializer.data, 'message': 'Pomyślnie zaktualizowano grupę'
         })
Exemplo n.º 9
0
 def create_post(self, request, **kwargs):
     try:
         group = Group.objects.get(**kwargs)
     except Group.DoesNotExist:
         return response404('Group')
     self.check_object_permissions(request=request, obj=group)
     serializer = PostSerializer(data=request.data, partial=True)
     if not serializer.is_valid():
         return response406({
             **serializer.errors, 'message': 'Błąd walidacji'
         })
     post = serializer.save(group=group, owner=request.user)
     file = request.FILES.get('file', None)
     image = request.FILES.get('image', None)
     if file:
         post.file = file
         post.save()
     if image:
         post.image = image
         post.save()
     return response200({
         **serializer.data, 'message':
         'Pomyślnie utworzono post'
     })