from django.contrib.auth.decorators import login_required from django.http import JsonResponse from web.models import Subject from web.views import e500_error from web.views.notifications import get_subjects_with_no_visit, get_subjects_with_reminder from web.views.subject import SUBJECT_LIST_GENERIC, SUBJECT_LIST_NO_VISIT, SUBJECT_LIST_REQUIRE_CONTACT @login_required def cities(request): X = Subject.objects.filter(city__isnull=False).values_list('city').distinct() return JsonResponse({ "cities": [x[0] for x in X] }) @login_required def countries(request): X = Subject.objects.filter(country__isnull=False).values_list('country').distinct() return JsonResponse({ "countries": [x[0] for x in X] }) @login_required def referrals(request): X = Subject.objects.filter(referral__isnull=False).values_list('referral').distinct() return JsonResponse({ "referrals": [x[0] for x in X] }) @login_required def get_subjects(request, type): if type == SUBJECT_LIST_GENERIC: return Subject.objects.all() elif type == SUBJECT_LIST_NO_VISIT: return get_subjects_with_no_visit(request.user) elif type == SUBJECT_LIST_REQUIRE_CONTACT: return get_subjects_with_reminder(request.user) else: raise TypeError("Unknown query type: " + type) def get_subjects_order(subjects, order_column, order_direction): result = subjects if order_direction == "asc": order_direction = "" else: order_direction = "-" if order_column == "first_name": result = subjects.order_by(order_direction + 'first_name') elif order_column == "last_name": result = subjects.order_by(order_direction + 'last_name') elif order_column == "nd_number": result = subjects.order_by(order_direction + 'nd_number') elif order_column == "screening_number": result = subjects.order_by(order_direction + 'screening_number') elif order_column == "default_location": result = subjects.order_by(order_direction + 'default_location') elif order_column == "dead": result = subjects.order_by(order_direction + 'dead') elif order_column == "resigned": result = subjects.order_by(order_direction + 'resigned') elif order_column == "information_sent": result = subjects.order_by(order_direction + 'information_sent') elif order_column == "postponed": result = subjects.order_by(order_direction + 'postponed') return result def get_subjects_filtered(subjects, filters): result = subjects for row in filters: column = row[0] value = row[1] if column == "first_name": result = result.filter(first_name__icontains=value) elif column == "last_name": result = result.filter(last_name__icontains=value) elif column == "nd_number": result = result.filter(nd_number__icontains=value) elif column == "screening_number": result = result.filter(screening_number__icontains=value) elif column == "dead": result = result.filter(dead=(value == "true")) elif column == "resigned": result = result.filter(resigned=(value == "true")) elif column == "postponed": result = result.filter(postponed=(value == "true")) elif column == "information_sent": result = result.filter(information_sent=(value == "true")) elif column == "default_location": result = result.filter(default_location=value) elif column == "": pass else: print "UNKNOWN filter: " print row return result @login_required def subjects(request, type): try: # id of the query from dataTable: https://datatables.net/manual/server-side draw = int(request.GET.get("draw", "-1")) start = int(request.GET.get("start", "0")) length = int(request.GET.get("length", "10")) order = int(request.GET.get("order[0][column]", "0")) order_dir = request.GET.get("order[0][dir]", "asc") order_column = request.GET.get("columns[" + str(order) + "][data]", "last_name") filters = [] column_id = 0 while request.GET.get("columns[" + str(column_id) + "][search][value]", "unknown") != "unknown": val = request.GET.get("columns[" + str(column_id) + "][search][value]", "unknown") if val != "": filters.append([request.GET.get("columns[" + str(column_id) + "][data]"), val]) column_id += 1 all_subjects = get_subjects(request, type) count = all_subjects.count() ordered_subjects = get_subjects_order(all_subjects, order_column, order_dir) filtered_subjects = get_subjects_filtered(ordered_subjects, filters) sliced_subjects = filtered_subjects[start:(start + length)] subjects = sliced_subjects count_filtered = filtered_subjects.count() data = [] for subject in subjects: data.append(serialize_subject(subject)) return JsonResponse({ "draw": draw, "recordsTotal": count, "recordsFiltered": count_filtered, "data": data, }) except: return e500_error(request) def get_yes_no(val): if val: return "YES" else: return "NO" def serialize_subject(subject): location = "" if subject.default_location is not None: location = subject.default_location.name result = { "first_name": subject.first_name, "last_name": subject.last_name, "nd_number": subject.nd_number, "screening_number": subject.screening_number, "default_location": location, "dead": get_yes_no(subject.dead), "resigned": get_yes_no(subject.resigned), "postponed": get_yes_no(subject.postponed), "information_sent": get_yes_no(subject.information_sent), "id": subject.id, } return result