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 GroupList(request): if request.method == 'GET': contacts = Group.objects.all() serializer = GroupSerializer(contacts, many=True) return Response(serializer.data) elif request.method == 'POST': serializer = GroupSerializer(data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): try: group_icon = request.FILES['group_icon'] except MultiValueDictKeyError: group_icon = None data = {"group_name": request.data.get("group_name"), "created_by": request.user.id, "group_description": request.data.get("group_description"), "group_icon": group_icon} group = GroupSerializer(data=data) if group.is_valid(): instance = group.save() serializer = GroupSerializer(instance, many=False, context={'request': request}) return Response({"success": True, "message": "Group Created", "data": serializer.data}) return Response({"success": False, "message": group.errors})
def post(self, request): serializer = GroupSerializer(data=request.data) serializer.is_valid(raise_exception=True) group = serializer.save(manager=request.user) groups = Group.objects.filter(manager=request.user).all() serializer = GroupSerializer(groups, many=True) return Response({ 'msg': f' вы успешно создали группу {group.name}', 'groups': serializer.data, 'form': { 'name': 'group_name' } })
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ę' })