def extra_filters(request, list): built_filter = Utility.build_filter(FILTER_FIELDS, request.query_params, model) query = dict() order_by = request.query_params.get('order_by', '-created').replace('.', '__') for key in built_filter: query['{}__iexact'.format(key)] = built_filter[key] try: list = model.objects.filter(**query).order_by(order_by) except Exception as e: print (e) return list
def extra_filters(request, list): if 'q' not in request.query_params: query = [] if request.user.is_superuser is not True and request.user.is_staff is not True: user_appointments = Appointments.objects.filter(host=request.user._id).values_list('visitor___id', flat=True) query.append(Q(_id__in=user_appointments) | Q(created_by=request.user._id)) built_filter = Utility.build_filter(FILTER_FIELDS, request.query_params, model) order_by = request.query_params.get('order_by', '-created').replace('.', '__') for key in built_filter: pin = dict() if 'search' in request.query_params: pin['{}__icontains'.format(key)] = built_filter[key] else: pin['{}__iexact'.format(key)] = built_filter[key] query.append(Q(**pin)) try: list = model.objects.filter(*query).order_by(order_by) except Exception as e: print (e) return list
def extra_filters(request, list): if 'load' in request.query_params: load = request.query_params.get('load') def in_progress(): today = date.today() checked_in = AppointmentLogs.objects.filter( checked_in__year=today.year, checked_in__month=today.month, checked_in__day=today.day, checked_out=None, is_removed=False ).values_list('appointment', flat=True) query = dict( _id__in=checked_in, is_removed=False, is_expired=False ) if request.query_params.get('host', None) is not None: query['host'] = request.query_params['host'] if request.user.is_superuser is not True and request.user.is_staff is not True: query['host'] = request.user._id return Appointments.objects.filter(**query) def upcoming(): today = date.today() checked_in = AppointmentLogs.objects.filter( checked_in__year=today.year, checked_in__month=today.month, checked_in__day=today.day, checked_out=None, is_removed=False ).values_list('appointment', flat=True) query = [ Q(start_date__gte=today) | Q(start_date__lte=today), Q(end_date__gte=today), Q(is_expired=False), Q(is_approved=True), Q(is_removed=False), ] if request.query_params.get('host', None) is not None: query.append(Q(host=request.query_params['host'])) if request.user.is_superuser is not True and request.user.is_staff is not True: query.append(Q(host=request.user._id)) return Appointments.objects.filter(*query).exclude(_id__in=checked_in) def awaiting(): today = date.today() query = [ Q(start_date__gte=today) | Q(start_date__lte=today), Q(end_date__gte=today), Q(is_expired=False), Q(is_approved=None), Q(is_removed=False) ] if request.query_params.get('host', None) is not None: query.append(Q(host=request.query_params['host'])) if request.user.is_superuser is not True and request.user.is_staff is not True: query.append(Q(host=request.user._id)) return Appointments.objects.filter(*query) def rejected(): today = date.today() query = dict( is_approved=False, end_date__gt=today, is_removed=False ) if request.query_params.get('host', None) is not None: query['host'] = request.query_params['host'] if request.user.is_superuser is not True and request.user.is_staff is not True: query['host'] = request.user._id return Appointments.objects.filter(**query) if load == 'upcoming': return upcoming() if load == 'pending': return awaiting() if load == 'rejected': return rejected() if load == 'in-progress': return in_progress() if 'q' not in request.query_params: built_filter = Utility.build_filter(FILTER_FIELDS, request.query_params, model) query = dict() search_query = [] if request.user.is_superuser is not True and request.user.is_staff is not True: query['host'] = request.user._id order_by = request.query_params.get('order_by', '-created').replace('.', '__') for key in built_filter: if 'search' in request.query_params: if key == 'host': search_query.append(Q(**{'host__last_name__icontains': built_filter[key]}) | Q(**{'host__first_name__icontains': built_filter[key]})) elif key == 'visitor': search_query.append(Q(**{'visitor__last_name__icontains': built_filter[key]}) | Q(**{'visitor__first_name__icontains': built_filter[key]})) else: search_query.append(Q(**{'{}__icontains'.format(key): built_filter[key]})) else: query['{}__iexact'.format(key)] = built_filter[key] try: if 'search' in request.query_params: list = model.objects.filter(*search_query).order_by(order_by) else: list = model.objects.filter(**query).order_by(order_by) except Exception as e: print (e) return list