def put(self, request, pk, format=None): camp = Campaign.objects.get(id=pk) serializer = CampaignSerializer(camp, data=request.data) if serializer.is_valid(): serializer.save() return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, format=None): camps = Campaign.objects.all() if request.query_params.get('id'): camps = camps.filter( title__contains=request.query_params.get('id')) serializer = CampaignSerializer(camps, many=True) return Response(serializer.data)
def get(self, request, format=None): campaigns = Campaign.objects.all() if len(campaigns) > 0: # if len(campaigns) > 100: # campaigns = Campaign.objects.filter()[:100] serializer = CampaignSerializer(campaigns, many=True) return Response(serializer.data) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get(self, request, format=None): camp = Campaign.objects.all()[:500] if request.query_params.get('title'): camp = camp.filter( title__contains=request.query_params.get('title')) elif request.query_params.get('category_id'): camp = camp.filter( category_id__contains=request.query_params.get('category_id')) serializer = CampaignSerializer(camp, many=True) return Response(serializer.data)
def get(self, request, pk, format=None): camp = Campaign.objects.get(id=pk) serializer = CampaignSerializer(camp) return Response(serializer.data)
def post(self, request, format=None): serializer = CampaignSerializer(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, format=None): search_in = request.data.get('search_in', 'everything') search = request.data.get('search') campaigns = [] if search: query = search.split(" ") if search_in == 'everything': q_object = Q(title__icontains=query[0]) | Q( user_first_name__icontains=query[0]) | Q( location_city__icontains=query[0]) | Q( location_country__icontains=query[0]) query = query[1:] for term in query: q_object.add( Q(title__icontains=term) | Q(user_first_name__icontains=term) | Q(location_city__icontains=term) | Q(location_country__icontains=term), Q.AND) q_object_match = Q(title__iexact=search) | Q( user_first_name__iexact=search) | Q( location_city__iexact=search) | Q( location_country__iexact=search) if search_in == 'title': q_object = Q(title__icontains=query[0]) query = query[1:] for term in query: q_object.add(Q(title__icontains=term), Q.AND) q_object_match = Q(title__iexact=search) if search_in == 'user_first_name': q_object = Q(user_first_name__icontains=query[0]) query = query[1:] for term in query: q_object.add(Q(user_first_name__icontains=term), Q.AND) q_object_match = Q(user_first_name__iexact=search) if search_in == 'location_city': q_object = Q(location_city__icontains=query[0]) query = query[1:] for term in query: q_object.add(Q(location_city__icontains=term), Q.AND) q_object_match = Q(location_city__iexact=search) if search_in == 'location_country': q_object = Q(location_country__icontains=query[0]) query = query[1:] for term in query: q_object.add(Q(location_country__icontains=term), Q.AND) q_object_match = Q(location_country__iexact=search) #Get query results count for exact exactCount = Campaign.objects.filter(q_object_match).count() search_done = True if exactCount >= 100 else False if search_done == True: campaigns = Campaign.objects.filter(q_object_match)[:100] else: containLimit = 100 - exactCount # Get IDs for excluding duplicates from icontains queryset exactIDs = Campaign.objects.filter(q_object_match).values_list( 'id', flat=True) campaignsExact = Campaign.objects.filter(q_object_match) #Get query results for contain while excluding the exact results and limited total results to 100 campaignsContain = Campaign.objects.filter(q_object).exclude( id__in=exactIDs)[:containLimit] #combine both queries while preserving order (iexact query then icontain query) campaigns = list(campaignsExact) + list(campaignsContain) campSerializer = CampaignSerializer(campaigns, many=True) return Response(campSerializer.data)
def get(self, request, format=None): camp = Campaign.objects.all() serializer = CampaignSerializer(camp, many=True) return Response(serializer.data)
def get(self, request, format=None): cats = Campaign.objects.all() if request.query_params.get('campaign_id'): cats = cats.filter( campaign_id__contains=request.query_params.get('campaign_id')) if request.query_params.get('category'): cats = cats.filter( category__contains=request.query_params.get('category')) if request.query_params.get('current_amount'): # Check if they want more or less than if request.query_params.get('current_amount_more_than'): # They want to get those that are more than or equal to cats = cats.filter(current_amount__gte=request.query_params. get('current_amount')) else: # They want less than or equal to cats = cats.filter(current_amount__lte=request.query_params. get('current_amount')) if request.query_params.get('goal'): # Check if they want more or less than if request.query_params.get('goal_more_than') == True: # They want to get those that are more than or equal to cats = cats.filter(goal__gte=request.query_params.get('goal')) else: # They want less than or equal to cats = cats.filter(goal__lte=request.query_params.get('goal')) if request.query_params.get('donators'): # Check if they want more or less than if request.query_params.get('donators_more_than'): # They want to get those that are more than or equal to cats = cats.filter( donators__gte=request.query_params.get('donators')) else: # They want less than or equal to cats = cats.filter( donators__lte=request.query_params.get('donators')) if request.query_params.get('days_active'): # Check if they want more or less than if request.query_params.get('days_active_more_than'): # They want to get those that are more than or equal to cats = cats.filter( days_active__gte=request.query_params.get('days_active')) else: # They want less than or equal to cats = cats.filter( days_active__lte=request.query_params.get('days_active')) if request.query_params.get('title'): cats = cats.filter( title__contains=request.query_params.get('title')) if request.query_params.get('has_beneficiary'): cats = cats.filter( title__contains=request.query_params.get('has_beneficiary')) if request.query_params.get('user_id'): cats = cats.filter( title__contains=request.query_params.get('user_id')) if request.query_params.get('created_at'): # Check if they want more or less than if request.query_params.get('created_at_more_than'): # They want to get those that are more than or equal to cats = cats.filter( created_at__gte=request.query_params.get('created_at')) else: # They want less than or equal to cats = cats.filter( created_at__lte=request.query_params.get('created_at')) if request.query_params.get('launch_date'): # Check if they want more or less than if request.query_params.get('launch_date_more_than'): # They want to get those that are more than or equal to cats = cats.filter( launch_date__gte=request.query_params.get('launch_date')) else: # They want less than or equal to cats = cats.filter( launch_date__lte=request.query_params.get('launch_date')) if request.query_params.get('campaign_hearts'): # Check if they want more or less than if request.query_params.get('campaign_hearts_more_than'): # They want to get those that are more than or equal to cats = cats.filter(campaign_hearts__gte=request.query_params. get('campaign_hearts')) else: # They want less than or equal to cats = cats.filter(campaign_hearts__lte=request.query_params. get('campaign_hearts')) if request.query_params.get('social_share_total'): # Check if they want more or less than if request.query_params.get('social_share_total_more_than'): # They want to get those that are more than or equal to cats = cats.filter(social_share_total__gte=request. query_params.get('social_share_total')) else: # They want less than or equal to cats = cats.filter(social_share_total__lte=request. query_params.get('social_share_total')) if request.query_params.get('location_city'): cats = cats.filter( title__contains=request.query_params.get('location_city')) if request.query_params.get('location_country'): cats = cats.filter( title__contains=request.query_params.get('location_country')) if request.query_params.get('is_charity'): cats = cats.filter( title__contains=request.query_params.get('is_charity')) if request.query_params.get('charity_valid'): cats = cats.filter( title__contains=request.query_params.get('charity_valid')) if request.query_params.get('charity_npo_id'): cats = cats.filter( title__contains=request.query_params.get('charity_npo_id')) if request.query_params.get('charity_name'): cats = cats.filter( title__contains=request.query_params.get('charity_name')) serializer = CampaignSerializer(cats, many=True) return Response(serializer.data)