def sla_check(request, filter): # Get all volunteers and the number of offers against their name. offers = Offer.objects.filter(time_staff_accepted__isnull=False, time_representative_accepted__isnull=True, time_volunteer_accepted__isnull=False, time_withdrawn__isnull=True) if filter == 'recent': # Filter by volunteers who have never been accepted by a representative. offers = offers.filter( time_staff_accepted__gte=datetime.datetime.now() - datetime.timedelta(days=31)) export_filename = "sla-export.xls" if 'export' in request.REQUEST: if request.REQUEST['export'] == 'xls': return xls.XlsResponse([ xls.Column("Volunteer", lambda x: x.volunteer.get_full_name()), xls.Column("Organisation", lambda x: x.position.organisation.name), xls.Column("Offer Received", lambda x: x.time_staff_accepted), xls.Column("Days Awaiting Response", lambda x: x.days_since_time_staff_accepted()), ], offers, export_filename) return render_to_response("reports/sla.html", { 'offers': offers, 'filter': filter, }, context_instance=RequestContext(request))
def marketing(request, filter): # Get all volunteers and the number of offers against their name. volunteers = User.objects.filter(baseprofile__is_volunteer=True)\ .select_related('baseprofile__volunteerprofile__referrer')\ .order_by('-date_joined') export_filename = "marketing-export.xls" if 'export' in request.REQUEST: if request.REQUEST['export'] == 'xls': return xls.XlsResponse([ xls.Column("Date joined", lambda x: x.date_joined), xls.Column("First name", lambda x: x.first_name), xls.Column("Last name", lambda x: x.last_name), xls.Column( "How did you hear about...?", lambda x: x.baseprofile.volunteerprofile.referrer if x.baseprofile.volunteerprofile else ""), ], volunteers, export_filename) return render_to_response("profiles/marketing.html", { 'volunteers': volunteers, 'filter': filter, }, context_instance=RequestContext(request))
def inactive_volunteers(request): volunteers = User.objects\ .filter(baseprofile__is_volunteer=True, baseprofile__archived=False)\ .annotate(last_offer=Max('offers_made__time_volunteer_accepted'))\ .filter(last_offer__isnull=True)\ .order_by('date_joined') now = datetime.datetime.now() for volunteer in volunteers: volunteer.age = (now - volunteer.date_joined).days volunteer.received_offer_count = Offer.objects\ .filter(volunteer=volunteer)\ .filter(time_volunteer_accepted__isnull=True)\ .count() export_filename = "inactive-volunteers-export.xls" if 'export' in request.REQUEST: if request.REQUEST['export'] == 'xls': return xls.XlsResponse([ xls.Column("Volunteer", lambda x: x.get_full_name()), xls.Column("Registered", lambda x: x.date_joined), xls.Column("Account Age", lambda x: x.age), xls.Column("# Recommendations", lambda x: x.received_offer_count), ], volunteers, export_filename) return render_to_response("reports/inactive_volunteers.html", { 'volunteers': volunteers, }, context_instance=RequestContext(request))
def awards(request): awards = Award.objects.all() export_filename = "awards-export.xls" if 'export' in request.REQUEST: if request.REQUEST['export'] == 'xls': return xls.XlsResponse([ xls.Column("Award", lambda x: x.award.name), xls.Column("First name", lambda x: x.user.first_name), xls.Column("Last name", lambda x: x.user.last_name), xls.Column("Date", lambda x: x.date_awarded), ], awards, export_filename) return render_to_response("reports/awards.html", { 'awards': awards, }, 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 training_report(request): '''Report allowing admins to view who attended which training events in a given date period.''' dform = report_forms.DateRangeForm(request.GET) if dform.is_valid(): start = dform.cleaned_data['date_start'] end = dform.cleaned_data['date_end'] end = end + timedelta(days=1) attendees = models.Attendee.objects.filter(date_time__gte=start, date_time__lte=end) events = models.Event.objects.filter(date_time__gte=start, date_time__lte=end) if 'attendee-export' in request.REQUEST: export_filename = "training-attendee-export.xls" if request.REQUEST['attendee-export'] == 'xls': return xls.XlsResponse([ xls.Column("Event", lambda x: x.event.training.title), xls.Column("First name", lambda x: x.user.first_name), xls.Column("Last name", lambda x: x.user.last_name), xls.Column("Date/Time", lambda x: x.date_time), xls.Column("Present?", lambda x: x.confirmed), ], attendees, export_filename) elif 'events-export' in request.REQUEST: export_filename = "training-events-export.xls" if request.REQUEST['events-export'] == 'xls': return xls.XlsResponse([ xls.Column("Title", lambda x: x.training.title), xls.Column("Date/Time", lambda x: x.date_time), xls.Column("Location", lambda x: x.location), xls.Column("# Attendees", lambda x: len(x.attendee_set.all())), ], events, export_filename) template = "training/report.html" context = { "attendees": attendees, "events": events, } return render(request, template, context)
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 all(request, filter): positions = Position.objects.all() filter_form = None # Columns column_filter_form = ColumnFilterForm(request.GET) column_filter_form.is_valid() columns = column_filter_form.cleaned_data['columns'] if columns == []: columns = POSITION_COLUMN_CHOICES_DEFAULT column_filter_form.data = {'columns': columns} filter_form = PositionFilterForm() if 'filtered' in request.REQUEST: filter_form = PositionFilterForm(request.REQUEST) category_form = PositionCategoryForm(request.REQUEST) if category_form.is_valid(): if category_form.cleaned_data['category']: positions = positions.filter( category__in=category_form.cleaned_data['category']) if not request.user.is_staff: # Don't show unapproved or delisted positions to non-staff. positions = positions.filter(approved=True, active=True) # Only allow default columns columns = POSITION_COLUMN_CHOICES_DEFAULT else: if filter_form.is_valid(): # Approval filtering. au = filter_form.cleaned_data['approved'] if 'a' in au and 'u' in au: pass elif 'a' in au: positions = positions.filter(approved=True) elif 'u' in au: positions = positions.filter(approved=False) # Listing filtering. ld = filter_form.cleaned_data['listed'] if 'l' in ld and 'd' in ld: pass elif 'l' in ld: positions = positions.filter(active=True) elif 'd' in ld: positions = positions.filter(active=False) if settings.FEATURE_ORGANISATION_CATEGORIES and filter_form.cleaned_data[ 'categories']: # Category filtering. positions = positions.filter( organisation__category__in=filter_form. cleaned_data['categories']) else: # When there is no filter form, apply defaults. positions = positions.filter(active=True) export_filename = "opportunities_export.xls" if filter == 'no-offers': # Count how many volunteer-accepted offers each position has, # and require the count to be 0. positions = positions\ .exclude(offer__time_volunteer_accepted__isnull=False) export_filename = "no_offers_export.xls" elif filter == 'five-pending': # Count offers that the volunteer has accepted # but the representative didn't accept # but that haven't been rejected. positions = positions\ .filter( offer__time_volunteer_accepted__isnull=False, offer__time_representative_accepted__isnull=True, offer__time_withdrawn__isnull=True, )\ .annotate(num_offers=Count('offer'))\ .filter(num_offers__gte=5) export_filename = "five_offers_pending_export.xls" if 'export' in request.REQUEST and request.user.is_staff: if request.REQUEST['export'] == 'xls': return xls.XlsResponse([ xls.Column("Opportunity", lambda x: x.name), xls.Column("Organisation", lambda x: x.organisation.name), xls.Column("Representative", lambda x: x.representative.get_full_name()), xls.Column("Email", lambda x: x.representative.email), ], positions, export_filename) return render_to_response("positions/all.html", { 'positions': positions, 'filter': filter, 'filter_form': filter_form, 'category_form': category_form, 'cform': column_filter_form, 'columns': columns, }, context_instance=RequestContext(request))