def facility_management_csv(request, facility, group_id=None, zone_id=None, frequency=None, period_start="", period_end="", user_type=None): """NOTE: THIS IS NOT A VIEW FUNCTION""" assert request.method == "POST", "facility_management_csv must be accessed via POST" # Search form for errors. form = DateRangeForm(data=request.POST) if not form.is_valid(): raise Exception(_("Error parsing date range: %(error_msg)s. Please review and re-submit.") % form.errors.as_data()) frequency = frequency or request.GET.get ("frequency", "months") period_start = period_start or form.data["period_start"] period_end = period_end or form.data["period_end"] (period_start, period_end) = _get_date_range(frequency, period_start, period_end) # Basic data context = control_panel_context(request, zone_id=zone_id, facility_id=facility.id) group = group_id and get_object_or_None(FacilityGroup, id=group_id) groups = FacilityGroup.objects.filter(facility=context["facility"]).order_by("name") # coaches = get_users_from_group(user_type="coaches", group_id=group_id, facility=facility) students = get_users_from_group(user_type="students", group_id=group_id, facility=facility) (student_data, group_data) = _get_user_usage_data(students, groups, group_id=group_id, period_start=period_start, period_end=period_end) # (coach_data, coach_group_data) = _get_user_usage_data(coaches, period_start=period_start, period_end=period_end) context.update({ "students": student_data, # raw data # "coaches": coach_data, # raw data }) return context
def facility_management(request, ds, facility, group_id=None, zone_id=None, per_page=25): ungrouped_id = UNGROUPED if request.method == "POST" and request.GET.get("format") == "csv": try: return facility_management_csv(request, facility=facility, group_id=group_id, zone_id=zone_id) except Exception as e: messages.error(request, e) context = control_panel_context(request, zone_id=zone_id, facility_id=facility.id) # Get pagination details coach_page = request.REQUEST.get("coaches_page", "1") coach_per_page = request.REQUEST.get("coaches_per_page", "5") student_page = request.REQUEST.get("students_page", "1") student_per_page = request.REQUEST.get("students_per_page", "25" if group_id else "10") # Basic data group = group_id and get_object_or_None(FacilityGroup, id=group_id) groups = FacilityGroup.objects.filter(facility=context["facility"]).order_by("name") coaches = get_users_from_group(user_type="coaches", group_id=group_id, facility=facility) students = get_users_from_group(user_type="students", group_id=group_id, facility=facility) (student_data, group_data) = _get_user_usage_data(students, groups, group_id=group_id) (coach_data, coach_group_data) = _get_user_usage_data(coaches) coach_pages, coach_urls = paginate_data( request, coach_data.values(), data_type="coaches", page=coach_page, per_page=coach_per_page ) student_pages, student_urls = paginate_data( request, student_data.values(), data_type="students", page=student_page, per_page=student_per_page ) # Now prep the CSV form (even though we won't process it) form = DateRangeForm(data=request.POST) if request.method == "POST" else DateRangeForm() frequency = request.GET.get("frequency", "months") period_start = form.data.get("period_start") period_end = form.data.get("period_end") (period_start, period_end) = _get_date_range(frequency, period_start, period_end) # Collate data for all groups groups = group_data.values() # If group_id exists, extract data for that group if group_id: if group_id == ungrouped_id: group_data = group_data[None] else: group_data = group_data[group_id] else: group_data = {} context.update( { "form": form, "date_range": [str(period_start), str(period_end)], "group": group, "group_id": group_id, "group_data": group_data, "groups": groups, # sends dict if group page, list of group data otherwise "student_pages": student_pages, # paginated data "coach_pages": coach_pages, # paginated data "ds": ds, "page_urls": {"coaches": coach_urls, "students": student_urls}, "ungrouped_id": ungrouped_id, } ) if not settings.CENTRAL_SERVER: context["base_template"] = "distributed/base_manage.html" return context
def facility_management(request, ds, facility, group_id=None, zone_id=None, per_page=25): ungrouped_id = UNGROUPED if request.method == "POST" and request.GET.get("format") == "csv": try: return facility_management_csv(request, facility=facility, group_id=group_id, zone_id=zone_id) except Exception as e: messages.error(request, e) context = control_panel_context(request, zone_id=zone_id, facility_id=facility.id) #Get pagination details coach_page = request.REQUEST.get("coaches_page", "1") coach_per_page = request.REQUEST.get("coaches_per_page", "5") student_page = request.REQUEST.get("students_page", "1") student_per_page = request.REQUEST.get("students_per_page", "25" if group_id else "10") # Basic data group = group_id and get_object_or_None(FacilityGroup, id=group_id) groups = FacilityGroup.objects.filter( facility=context["facility"]).order_by("name") coaches = get_users_from_group(user_type="coaches", group_id=group_id, facility=facility) students = get_users_from_group(user_type="students", group_id=group_id, facility=facility) (student_data, group_data) = _get_user_usage_data(students, groups, group_id=group_id) (coach_data, coach_group_data) = _get_user_usage_data(coaches) coach_pages, coach_urls = paginate_data(request, coach_data.values(), data_type="coaches", page=coach_page, per_page=coach_per_page) student_pages, student_urls = paginate_data(request, student_data.values(), data_type="students", page=student_page, per_page=student_per_page) # Now prep the CSV form (even though we won't process it) form = DateRangeForm( data=request.POST) if request.method == "POST" else DateRangeForm() frequency = request.GET.get("frequency", "months") period_start = form.data.get("period_start") period_end = form.data.get("period_end") (period_start, period_end) = _get_date_range(frequency, period_start, period_end) # Collate data for all groups groups = group_data.values() # If group_id exists, extract data for that group if group_id: if group_id == ungrouped_id: group_id_index = next(index for (index, d) in enumerate(group_data.values()) if d["name"] == _(UNGROUPED)) else: group_id_index = next(index for (index, d) in enumerate(group_data.values()) if d["id"] == group_id) group_data = group_data.values()[group_id_index] else: group_data = {} context.update({ "form": form, "date_range": [str(period_start), str(period_end)], "group": group, "group_id": group_id, "group_data": group_data, "groups": groups, # sends dict if group page, list of group data otherwise "student_pages": student_pages, # paginated data "coach_pages": coach_pages, # paginated data "ds": ds, "page_urls": { "coaches": coach_urls, "students": student_urls, }, "ungrouped_id": ungrouped_id }) if not settings.CENTRAL_SERVER: context["base_template"] = "distributed/base_manage.html" return context