def create(cls, request): # Check for images and tags images = request.data.get('images', []) tags = request.data.get('tags', []) # Deserialize POST data serializer = ComplaintPostSerializer(data=request.data, context={'request': request}) # Save if valid if serializer.is_valid(): complaint = serializer.save() # Create and save all tags if present for tag in tags: if TagUris.objects.filter(tag_uri=tag).exists(): exist_tag = TagUris.objects.get(tag_uri=tag) complaint.tags.add(exist_tag.id) else: tag_name = TagUris(tag_uri=tag) tag_name.save() complaint.tags.add(tag_name) # Create and save all images if present for image in images: ComplaintMedia.objects.create(complaint=complaint, image_url=image) # Return new serialized response return Response(ComplaintSerializer( Complaints.objects.get(id=complaint.id)).data, status=201)
def retrieve(self, request, pk): complaint = self.get_complaint(pk) serialized = ComplaintSerializer(complaint, context={ 'request': request }).data return Response(serialized)
def list(cls, request): """Get a list of non-deleted complaints. To filter by current user, add a query parameter {?filter}""" # Get the list of complaints excluding objects marked deleted complaint = Complaints.objects.exclude(status='Deleted') # Check if the user specific filter is present if 'filter' in request.GET: complaint = complaint.filter(created_by=request.user.profile) # Filter for a particular word search if 'search' in request.GET: val = request.query_params.get('search') complaint = complaint.filter(description__icontains=val) # For multiple tags and single tags if 'tags' in request.GET: val = request.query_params.getlist('tags') clauses = (Q(tags__tag_uri__icontains=p) for p in val) query = reduce(operator.or_, clauses) complaint = complaint.filter(query) # Serialize and return serialized = ComplaintSerializer(complaint, context={ 'request': request }, many=True).data return Response(serialized)
def retrieve(self, request, pk): """ComplaintViewSet to get the complaints""" complaint = self.get_complaint(pk) serialized = ComplaintSerializer(complaint, context={ 'request': request }).data return Response(serialized)
def list(cls, request): """Get a list of non-deleted complaints. To filter by current user, add a query parameter {?filter}""" # Get the list of complaints excluding objects marked deleted complaint = Complaints.objects.exclude(status='Deleted') # Check if the user specific filter is present if 'filter' in request.GET: complaint = complaint.filter(created_by=request.user.profile) # Serialize and return serialized = ComplaintSerializer(complaint, context={ 'request': request }, many=True).data return Response(serialized)
def list(self, request): """Get a list of non-deleted complaints. To filter by current user, add a query parameter {?filter}""" # Get the list of complaints excluding objects marked deleted complaints = self.queryset.prefetch_related( 'subscriptions', 'users_up_voted').exclude(status='Deleted') # Check if the user specific filter is present if 'filter' in request.GET and request.user.is_authenticated: complaints = complaints.filter(created_by=request.user.profile) # Filter for a particular word search if 'search' in request.GET: val = request.query_params.get('search') complaints = complaints.filter(description__icontains=val) # For multiple tags and single tags if 'tags' in request.GET: val = request.query_params.getlist('tags') clauses = (Q(tags__tag_uri__icontains=p) for p in val) query = reduce(operator.or_, clauses) complaints = complaints.filter(query) # Paginate the complaint page using the helper function complaints = query_from_num(request, 10, complaints) # Serialize and return serialized = ComplaintSerializer(complaints, context={ 'request': request }, many=True).data for complaint_object, serialized_object in zip(complaints, serialized): is_sub = request.user.is_authenticated and request.user.profile in complaint_object.subscriptions.all( ) serialized_object['is_subscribed'] = is_sub upvoted = request.user.is_authenticated and request.user.profile in complaint_object.users_up_voted.all( ) serialized_object['upvoted'] = upvoted return Response(serialized)
def retrieve(self, request, pk): """ComplaintViewSet to get the complaints""" complaint = self.get_complaint(pk, queryset=self.queryset.prefetch_related( 'subscriptions', 'users_up_voted')) serialized = ComplaintSerializer(complaint, context={ 'request': request }).data is_sub = request.user.is_authenticated and request.user.profile in complaint.subscriptions.all( ) serialized['is_subscribed'] = is_sub upvoted = request.user.is_authenticated and request.user.profile in complaint.users_up_voted.all( ) serialized['upvoted'] = upvoted return Response(serialized)
def subscribe(self, request, pk): """Subscribe or Un-Subscribe from a complaint {?action}=0,1""" complaint = self.get_complaint(pk) value = request.GET.get("action") if value is None: return Response({"message": "{?action} is required"}, status=400) # Check possible actions if value == "0": complaint.subscriptions.remove(self.request.user.profile) elif value == "1": complaint.subscriptions.add(self.request.user.profile) else: return Response({"message": "Invalid Action"}, status=400) return Response(ComplaintSerializer( Complaint.objects.get(id=complaint.id)).data, status=200)
def up_vote(self, request, pk): """UpVote or un-UpVote a complaint {?action}=0,1""" complaint = self.get_complaint(pk) value = request.GET.get("action") if value is None: return Response({"message": "{?action} is required"}, status=400) # Check possible actions if value == "0": complaint.users_up_voted.remove(self.request.user.profile) elif value == "1": complaint.users_up_voted.add(self.request.user.profile) else: return Response({"message": "Invalid Action"}, status=400) return Response(ComplaintSerializer( Complaints.objects.get(id=complaint.id)).data, status=200)
def create(cls, request): # Check for images and tags images = request.data.get('images', []) tags = request.data.get('tags', []) # Deserialize POST data serializer = ComplaintPostSerializer(data=request.data, context={'request': request}) # Save if valid if serializer.is_valid(): complaint = serializer.save() # Create and save all tags if present for tag in tags: if ComplaintTag.objects.filter(tag_uri=tag).exists(): exist_tag = ComplaintTag.objects.get(tag_uri=tag) complaint.tags.add(exist_tag.id) else: tag_name = ComplaintTag(tag_uri=tag) tag_name.save() complaint.tags.add(tag_name) # Create and save all images if present for image in images: ComplaintImage.objects.create(complaint=complaint, image_url=image) # Add the complaint creator to the subscribers list if settings.COMPLAINT_AUTO_SUBSCRIBE: complaint.subscriptions.add(complaint.created_by) complaint.save() # Return new serialized response return Response(ComplaintSerializer( Complaint.objects.get(id=complaint.id)).data, status=201)
def update(self, request, pk): complaint = self.get_complaint(pk) complaint.users_up_voted.add(self.request.user.profile) return Response( ComplaintSerializer(Complaints.objects.get(id=complaint.id)).data)