def units(self, request, **kwargs): obj = self.get_object() if not request.user.has_perm("screenshot.edit", obj.component): raise PermissionDenied() if "unit_id" not in request.data: raise ParseError("Missing unit_id parameter") try: source_string = obj.component.source_translation.unit_set.get( pk=int(request.data["unit_id"])) except (Unit.DoesNotExist, ValueError) as error: return Response( data={ "result": "Unsuccessful", "detail": force_str(error) }, status=status.HTTP_400_BAD_REQUEST, ) obj.units.add(source_string) serializer = ScreenshotSerializer(obj, context={"request": request}) return Response( serializer.data, status=status.HTTP_200_OK, )
def screenshots(self, request, **kwargs): obj = self.get_object() queryset = Screenshot.objects.filter(component=obj).order_by("id") page = self.paginate_queryset(queryset) serializer = ScreenshotSerializer(page, many=True, context={"request": request}) return self.get_paginated_response(serializer.data)
def create(self, request, *args, **kwargs): required_params = ["name", "image", "project_slug", "component_slug"] for param in required_params: if param not in request.data: raise ParseError("Missing {param} parameter".format(param=param)) try: project = request.user.allowed_projects.get( slug=request.data["project_slug"] ) component = Component.objects.filter(project=project).get( slug=request.data["component_slug"] ) except (Project.DoesNotExist, Component.DoesNotExist, ValueError) as error: raise ParseError(str(error), "invalid") if not request.user.has_perm("screenshot.add", component): self.permission_denied(request, "Can not add screenshot.") with transaction.atomic(): serializer = ScreenshotSerializer( data=request.data, context={"request": request} ) serializer.is_valid(raise_exception=True) serializer.save( component=component, user=request.user, image=request.data["image"] ) return Response(serializer.data, status=HTTP_201_CREATED)