def retrieve(self, request, pk=None): """ Return for admin, return for owner, return for user that is invited, 404 everyone else """ queryset = Projects.objects.all() user = self.request.user project = get_object_or_404(queryset, pk=pk) if user.is_superuser: serializer = ProjectSerializer(project) return Response(serializer.data) if user == project.user: serializer = ProjectSerializer(project) return Response(serializer.data) # Special case for invited user's user_invite_for_project = ProjectUsers.objects.filter( daco_email=user.email, project=pk) is_user_invited = len(user_invite_for_project) > 0 if is_user_invited: serializer = ProjectSerializer(project) return Response(serializer.data) # Otherwise return 404 return Response(status=status.HTTP_404_NOT_FOUND)
def put(self, request, pk): project = self.get_object(pk) serializer = ProjectSerializer(project, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def put(self, request, pk): project = self.get_object(pk) serializer = ProjectSerializer(instance=project, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors)
def put(self, request, pk): project = get_object_or_404(Project, id=pk) if project.creator == request.user: serializer = ProjectSerializer(instance=project, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors) return Response(status=status.HTTP_403_FORBIDDEN)
def list_projects_as_json(request): logger.debug(request) start = int(request.GET.get('start', 0)) + 1 length = int(request.GET.get('length', 10)) search_value = None if search_value: query = Q(name__icontains=search_value) | \ Q(created_by__last_name__icontains=search_value) | \ Q(created_by__first_name__icontains=search_value) | \ Q(created_by__username__icontains=search_value) | \ Q(status__icontains=search_value) logger.debug(Project.objects.filter(query).query) objects = Project.objects.filter(query).distinct().order_by( 'date_created') else: objects = Project.objects.all().order_by('date_created') if objects.count() > length: chosen_page = int(math.ceil(float(start) / length)) page = chosen_page else: page = 1 logger.debug("start {0} length {1} page {2}".format(start, length, page)) paginator = Paginator(objects, length) serialzed_objects = ProjectSerializer(Project.objects.all(), many=True) data = { 'recordsTotal': Project.objects.all().count(), 'recordsFiltered': Project.objects.all().count(), 'start': start, 'length': length, 'data': serialzed_objects.data } response = JSONRenderer().render(data) return HttpResponse(response, content_type='application/json')
def get(self, request, pk): project = self.get_object(pk) serializer = ProjectSerializer(project) return Response(serializer.data)
def post(self, request): serializer = ProjectSerializer(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 get(self, request): projects = Project.objects.all() serializer = ProjectSerializer(projects, many=True) return Response(serializer.data)
def get(self, request, pk): project = get_object_or_404(Project, id=pk) if project.creator == request.user: serializer = ProjectSerializer(project) return Response(serializer.data) return Response(status=status.HTTP_403_FORBIDDEN)
def post(self, request): serializer = ProjectSerializer(data=request.data) if serializer.is_valid(): serializer.save(creator=request.user) return Response(serializer.data) return Response(serializer.errors)
def get(self, request): projects = request.user.projects serializer = ProjectSerializer(projects, many=True) return Response(serializer.data)
def project_detail_as_json(request, pk): project = Project.objects.get(pk=pk) response = JSONRenderer().render(ProjectSerializer(project).data) return HttpResponse(response, content_type='application/json')