def representatives(request, filter=None, action=None): if action: if action == "mailout": if request.method == "POST": representative_ids = logic.get_action_ids(request) representatives = [get_object_or_404(User, pk=rid) for rid in representative_ids] return redirect(mailer.logic.new(representatives, request.user)) representatives = User.objects\ .select_related('baseprofile', 'baseprofile__representativeprofile__organisation__name')\ .filter(baseprofile__representativeprofile__isnull=False)\ .order_by('first_name') export_filename = "representatives-export.xls" if ("mailer_new" in request.GET): return redirect(mailer.logic.new(representatives, request.user)) if 'export' in request.REQUEST: if request.REQUEST['export'] == 'xls': return xls.XlsResponse([ xls.Column("First name", lambda x: x.first_name), xls.Column("Last name", lambda x: x.last_name), xls.Column("Email", lambda x: x.email), xls.Column("Organisation", lambda x: x.baseprofile.representativeprofile.organisation.name), ], representatives, export_filename) return render_to_response("profiles/representatives.html", { 'representatives': representatives, 'filter': filter, 'action': action, }, context_instance=RequestContext(request))
def representatives(request, filter=None, action=None): if action: if action == "mailout": if request.method == "POST": representative_ids = logic.get_action_ids(request) representatives = [ get_object_or_404(User, pk=rid) for rid in representative_ids ] return redirect(mailer.logic.new(representatives, request.user)) representatives = User.objects\ .select_related('baseprofile', 'baseprofile__representativeprofile__organisation__name')\ .filter(baseprofile__representativeprofile__isnull=False)\ .order_by('first_name') export_filename = "representatives-export.xls" if ("mailer_new" in request.GET): return redirect(mailer.logic.new(representatives, request.user)) if 'export' in request.REQUEST: if request.REQUEST['export'] == 'xls': return xls.XlsResponse([ xls.Column("First name", lambda x: x.first_name), xls.Column("Last name", lambda x: x.last_name), xls.Column("Email", lambda x: x.email), xls.Column( "Organisation", lambda x: x.baseprofile. representativeprofile.organisation.name), ], representatives, export_filename) return render_to_response("profiles/representatives.html", { 'representatives': representatives, 'filter': filter, 'action': action, }, context_instance=RequestContext(request))
def volunteers(request, position_id=None, filter=None, action=None, organisation_id=''): if not request.user.is_staff: organisation_id = request.user.get_profile( ).representativeprofile.organisation.id position = None if action: if action == "recommend": position = get_object_or_404(Position, pk=position_id) if request.method == "POST": volunteer_ids = logic.get_action_ids(request) volunteers = [ get_object_or_404(User, pk=vid) for vid in volunteer_ids ] positions.logic.recommend_position(request, position, volunteers) if action == "mailout": if request.method == "POST": volunteer_ids = logic.get_action_ids(request) volunteers = [ get_object_or_404(User, pk=vid) for vid in volunteer_ids ] return redirect( mailer.logic.new(volunteers, request.user, organisation_id)) type_filter_form = VolunteerTypeFilterForm(request.GET) type_filter_form.is_valid() grad_filter = type_filter_form.cleaned_data['postgrad'] international_filter = type_filter_form.cleaned_data['international'] course_filter = type_filter_form.cleaned_data['course'] category_filter = type_filter_form.cleaned_data['category'] faculty_filter = type_filter_form.cleaned_data['faculty'] hours_filter = type_filter_form.cleaned_data['hours'] year_filter = type_filter_form.cleaned_data['year'] column_filter_form = ColumnFilterForm(request.GET) column_filter_form.is_valid() columns = column_filter_form.cleaned_data['columns'] if grad_filter == []: grad_filter = all_of(GRAD_CHOICES) if international_filter == []: international_filter = all_of(INTERNATIONAL_CHOICES) if columns == []: columns = VOLUNTEER_COLUMN_CHOICES_DEFAULT type_filter_form.data = { 'postgrad': grad_filter, 'international': international_filter, 'course': course_filter, 'faculty': faculty_filter, 'category': category_filter, 'hours': hours_filter, 'year': year_filter, } column_filter_form.data = { 'columns': columns, } # The __in operator doesn't work for NULL. Instead, __isnull must be used. grad_filter_exp = Q( baseprofile__volunteerprofile__postgrad__in=booleanise(grad_filter)) if 'None' in grad_filter: grad_filter_exp |= Q( baseprofile__volunteerprofile__postgrad__isnull=True) # In the special case that all ineternational choices are chosen, we also select records # for students who don't have an international status. if set(international_filter) == set(all_of(INTERNATIONAL_CHOICES)): international_filter.append("") international_filter_exp = Q( baseprofile__volunteerprofile__international__in=international_filter) ### BASE QUERY ### volunteers = User.objects\ .filter(baseprofile__is_volunteer=True, baseprofile__archived=(filter == 'deactivated'))\ .filter(international_filter_exp)\ .filter(grad_filter_exp)\ .order_by('first_name') ### ORGANISATION FILTER ### organisation = None if organisation_id: volunteers = volunteers.filter( commitment__organisation__id=organisation_id) organisation = Organisation.objects.get(pk=organisation_id) ### ADD FILTERS ### if course_filter: volunteers = volunteers.filter( baseprofile__volunteerprofile__course__icontains=course_filter) if year_filter and year_filter != "ALL": volunteers = volunteers.filter( baseprofile__volunteerprofile__year__exact=int(year_filter)) if faculty_filter: volunteers = volunteers.filter( baseprofile__volunteerprofile__school=faculty_filter.name) if category_filter: volunteers = volunteers.filter( baseprofile__volunteerprofile__categories=category_filter) if hours_filter == "HAS_HOURS": volunteers = volunteers.filter(timerecord__isnull=False) if hours_filter == "NO_HOURS": volunteers = volunteers.filter(timerecord__isnull=True) ### ADD FOREIGN COLUMNS ### if 'year' in columns: volunteers = volunteers.select_related( 'baseprofile__volunteerprofile__year') if 'course' in columns: volunteers = volunteers.select_related( 'baseprofile__volunteerprofile__course') ### ADD COMPLEX COLUMNS ### if 'offers' in columns: volunteers = volunteers.annotate(Count('offers_made')) if 'last_accepted' in columns or filter == 'uncommitted': volunteers = volunteers.annotate( last_accepted=Max('offers_made__time_representative_accepted')) if 'last_offered' in columns or filter == 'inactive': volunteers = volunteers.annotate( last_offered=Max('offers_made__time_volunteer_accepted')) ### ADDITIONAL FILTERING (should be converted to generic filters above) ### export_filename = "volunteers-export.xls" if filter == 'uncommitted': # Filter by volunteers who have never been accepted by a representative. volunteers = volunteers.filter(last_accepted__isnull=True) export_filename = "uncommitted-volunteers-export.xls" if filter == 'inactive': # Filter by volunteers who have never agreed to a position. volunteers = volunteers.filter(last_offered__isnull=True) export_filename = "inactive-volunteers-export.xls" if 'export' in request.REQUEST: if request.REQUEST['export'] == 'xls': xls_columns = [ xls.Column("First name", lambda x: x.first_name), xls.Column("Last name", lambda x: x.last_name), xls.Column("Email", lambda x: x.email), ] if settings.FEATURE_DISPLAY_STUDENT_ID: xls_columns.append( xls.Column( "Student ID", lambda x: x.get_profile().volunteerprofile.student_id)) if "course" in columns: xls_columns.append( xls.Column( "Course", lambda x: x.baseprofile.volunteerprofile.course)) if "registered" in columns: xls_columns.append( xls.Column("Registered", lambda x: x.date_joined)) if "grad_year" in columns: xls_columns.append( xls.Column("Grad. year", lambda x: x.baseprofile.volunteerprofile.year)) if "offers" in columns: xls_columns.append( xls.Column("Offers", lambda x: x.offers_made__count)) if "last_accepted" in columns: xls_columns.append( xls.Column("Last accepted", lambda x: x.last_accepted)) if "hours_logged" in columns: xls_columns.append( xls.Column("Hours logged", lambda x: x.get_profile().total_hours())) xls_columns.append( xls.Column( "Hours confirmed", lambda x: x.get_profile().total_confirmed_hours())) return xls.XlsResponse(xls_columns, volunteers, export_filename) ### POST_PROCESSING ### return render_to_response("profiles/volunteers.html", { 'volunteers': volunteers, 'organisation': organisation, 'filter': filter, 'tform': type_filter_form, 'cform': column_filter_form, 'columns': columns, 'action': action, 'position': position, }, context_instance=RequestContext(request))
def volunteers(request, position_id=None, filter=None, action=None, organisation_id=''): if not request.user.is_staff: organisation_id = request.user.get_profile().representativeprofile.organisation.id position = None if action: if action == "recommend": position = get_object_or_404(Position, pk=position_id) if request.method == "POST": volunteer_ids = logic.get_action_ids(request) volunteers = [get_object_or_404(User, pk=vid) for vid in volunteer_ids] positions.logic.recommend_position(request, position, volunteers) if action == "mailout": if request.method == "POST": volunteer_ids = logic.get_action_ids(request) volunteers = [get_object_or_404(User, pk=vid) for vid in volunteer_ids] return redirect(mailer.logic.new(volunteers, request.user, organisation_id)) type_filter_form = VolunteerTypeFilterForm(request.GET) type_filter_form.is_valid() grad_filter = type_filter_form.cleaned_data['postgrad'] international_filter = type_filter_form.cleaned_data['international'] course_filter = type_filter_form.cleaned_data['course'] category_filter = type_filter_form.cleaned_data['category'] faculty_filter = type_filter_form.cleaned_data['faculty'] hours_filter = type_filter_form.cleaned_data['hours'] year_filter = type_filter_form.cleaned_data['year'] column_filter_form = ColumnFilterForm(request.GET) column_filter_form.is_valid() columns = column_filter_form.cleaned_data['columns'] if grad_filter == []: grad_filter = all_of(GRAD_CHOICES) if international_filter == []: international_filter = all_of(INTERNATIONAL_CHOICES) if columns == []: columns = VOLUNTEER_COLUMN_CHOICES_DEFAULT type_filter_form.data = { 'postgrad': grad_filter, 'international': international_filter, 'course': course_filter, 'faculty': faculty_filter, 'category': category_filter, 'hours': hours_filter, 'year': year_filter, } column_filter_form.data = { 'columns': columns, } # The __in operator doesn't work for NULL. Instead, __isnull must be used. grad_filter_exp = Q(baseprofile__volunteerprofile__postgrad__in=booleanise(grad_filter)) if 'None' in grad_filter: grad_filter_exp |= Q(baseprofile__volunteerprofile__postgrad__isnull=True) # In the special case that all ineternational choices are chosen, we also select records # for students who don't have an international status. if set(international_filter) == set(all_of(INTERNATIONAL_CHOICES)): international_filter.append("") international_filter_exp = Q(baseprofile__volunteerprofile__international__in=international_filter) ### BASE QUERY ### volunteers = User.objects\ .filter(baseprofile__is_volunteer=True, baseprofile__archived=(filter == 'deactivated'))\ .filter(international_filter_exp)\ .filter(grad_filter_exp)\ .order_by('first_name') ### ORGANISATION FILTER ### organisation = None if organisation_id: volunteers = volunteers.filter(commitment__organisation__id=organisation_id) organisation = Organisation.objects.get(pk=organisation_id) ### ADD FILTERS ### if course_filter: volunteers = volunteers.filter(baseprofile__volunteerprofile__course__icontains=course_filter) if year_filter and year_filter != "ALL": volunteers = volunteers.filter(baseprofile__volunteerprofile__year__exact=int(year_filter)) if faculty_filter: volunteers = volunteers.filter(baseprofile__volunteerprofile__school=faculty_filter.name) if category_filter: volunteers = volunteers.filter(baseprofile__volunteerprofile__categories=category_filter) if hours_filter == "HAS_HOURS": volunteers = volunteers.filter(timerecord__isnull=False) if hours_filter == "NO_HOURS": volunteers = volunteers.filter(timerecord__isnull=True) ### ADD FOREIGN COLUMNS ### if 'year' in columns: volunteers = volunteers.select_related('baseprofile__volunteerprofile__year') if 'course' in columns: volunteers = volunteers.select_related('baseprofile__volunteerprofile__course') ### ADD COMPLEX COLUMNS ### if 'offers' in columns: volunteers = volunteers.annotate(Count('offers_made')) if 'last_accepted' in columns or filter == 'uncommitted': volunteers = volunteers.annotate(last_accepted=Max('offers_made__time_representative_accepted')) if 'last_offered' in columns or filter == 'inactive': volunteers = volunteers.annotate(last_offered=Max('offers_made__time_volunteer_accepted')) ### ADDITIONAL FILTERING (should be converted to generic filters above) ### export_filename = "volunteers-export.xls" if filter == 'uncommitted': # Filter by volunteers who have never been accepted by a representative. volunteers = volunteers.filter(last_accepted__isnull=True) export_filename = "uncommitted-volunteers-export.xls" if filter == 'inactive': # Filter by volunteers who have never agreed to a position. volunteers = volunteers.filter(last_offered__isnull=True) export_filename = "inactive-volunteers-export.xls" if 'export' in request.REQUEST: if request.REQUEST['export'] == 'xls': xls_columns = [ xls.Column("First name", lambda x: x.first_name), xls.Column("Last name", lambda x: x.last_name), xls.Column("Email", lambda x: x.email), ] if settings.FEATURE_DISPLAY_STUDENT_ID: xls_columns.append(xls.Column("Student ID", lambda x: x.get_profile().volunteerprofile.student_id)) if "course" in columns: xls_columns.append(xls.Column("Course", lambda x: x.baseprofile.volunteerprofile.course)) if "registered" in columns: xls_columns.append(xls.Column("Registered", lambda x: x.date_joined)) if "grad_year" in columns: xls_columns.append(xls.Column("Grad. year", lambda x: x.baseprofile.volunteerprofile.year)) if "offers" in columns: xls_columns.append(xls.Column("Offers", lambda x: x.offers_made__count)) if "last_accepted" in columns: xls_columns.append(xls.Column("Last accepted", lambda x: x.last_accepted)) if "hours_logged" in columns: xls_columns.append(xls.Column("Hours logged", lambda x: x.get_profile().total_hours())) xls_columns.append(xls.Column("Hours confirmed", lambda x: x.get_profile().total_confirmed_hours())) return xls.XlsResponse(xls_columns, volunteers, export_filename) ### POST_PROCESSING ### return render_to_response("profiles/volunteers.html", { 'volunteers': volunteers, 'organisation': organisation, 'filter': filter, 'tform': type_filter_form, 'cform': column_filter_form, 'columns': columns, 'action': action, 'position': position, }, context_instance=RequestContext(request))