def get_post(self, request, **kwargs): try: post = Post.objects.get(id=kwargs.get('id')) except Post.DoesNotExist: return response404('Post') self.check_object_permissions(request=request, obj=post.group) return response200(PostSerializer(post).data)
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' })
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})
def delete_group(self, request, **kwargs): try: group = Group.objects.get(**kwargs) except Group.DoesNotExist: return response404('Group') self.check_object_permissions(request=request, obj=group) group.delete() return response200({'message': 'Pomyślnie usunięto grupę'})
def delete_comment(self, request, **kwargs): try: comment = Comment.objects.get(**kwargs) except Comment.DoesNotExist: return response404('Post') self.check_object_permissions(request=request, obj=comment) comment.delete() return response200({'message': 'Pomyślnie usunięto posta'})
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)
def leave_group(self, request, **kwargs): try: group = Group.objects.get(**kwargs) except Group.DoesNotExist: return response404('Group') self.check_object_permissions(request=request, obj=group) group.members.remove(request.user) group.save() return response200({'message': 'Pomyślnie opuściłeś grupę'})
def details(self, request, **kwargs): try: group = Group.objects.get(id=kwargs.get('id')) except Group.DoesNotExist: return response404('Group') self.check_object_permissions(request=request, obj=group) return response200({ **GroupSerializer(group).data, 'message': 'Pomyślnie dodano grupę' })
def drop_member(self, request, **kwargs): try: group = Group.objects.get(**kwargs) user = User.objects.get(id=request.data.get('id')) except Group.DoesNotExist: return response404('Group') except User.DoesNotExist: return response404('Użytkownik nie znaleziony') self.check_object_permissions(request=request, obj=group) group.members.remove(user) group.save() return response200( {'message': 'Pomyślnie usunięto użytkownika z grupy'})
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' })
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' })
def manage_pending_member(self, request, **kwargs): try: group = Group.objects.get(**kwargs) pending = PendingMember.objects.get( user_id=request.data.get('user_id'), group_id=group.id) except Group.DoesNotExist: return response404('Group') except PendingMember.DoesNotExist: return response404('Użytkownik') self.check_object_permissions(request=request, obj=group) if request.method == 'POST': group.members.add(pending.user) message = 'Pomyślnie zaakceptowano użytkownika' else: message = 'Pomyślnie odrzucono użytkownika' pending.delete() return response200({'message': message})
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' })
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ę' })
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' })