def create_admin_csv(request, job_slug): job = get_object_or_404(Job.objects.all(), slug=job_slug) try: administrator = JobUser.objects.get(user=request.user, job=job) except JobUser.DoesNotExist: if not request.user.is_superuser: return HttpResponse(status=401, content="401 Unauthorized Access", mimetype="text/plain") response = HttpResponse(mimetype="text/csv") response["Content-Disposition"] = 'attachment; filename=%s.csv' % job_slug writer = csv.writer(response) writer.writerow([ "Student ID", "First Name", "Last Name", "Gender", "Email", "Address", "GPA", "Ethnicity", "Status", "Interview Location", "Interview Date" ]) persons = _get_persons_for_job(job) for application in job.application_set.all(): if not application.applicationcomponentpart_set.all(): continue person = persons[application.applicant.user.username] address = person.get_address_by_type("MA") if address: mailing_address = address.street_line_1 if address.street_line_2: mailing_address = mailing_address + " " + address.street_line_2 if address.street_line_3: mailing_address = mailing_address + " " + address.street_line_3 mailing_address = "%s %s, %s %s" % ( mailing_address, address.city, address.state, address.zip_code) else: mailing_address = "" try: interview = Interview.objects.get(job=application.job, application=application) interview_location = interview.location interview_date = interview.datetime.strftime( "%A, %B %e at %I:%M %p") except Interview.DoesNotExist: interview_location = "None" interview_date = "None" writer.writerow([ person.student_id, person.first_name, person.last_name, person.gender, person.email, mailing_address, person.gpa, person.ethnicity, application.status, interview_location, interview_date ]) return response
def create_admin_csv(request, job_slug): job = get_object_or_404(Job.objects.all(), slug=job_slug) try: administrator = JobUser.objects.get(user=request.user, job=job) except JobUser.DoesNotExist: if not request.user.is_superuser: return HttpResponse(status=401, content="401 Unauthorized Access", mimetype="text/plain") response = HttpResponse(mimetype="text/csv") response["Content-Disposition"] = 'attachment; filename=%s.csv' % job_slug writer = csv.writer(response) writer.writerow(["Student ID", "First Name", "Last Name", "Gender", "Email", "Address", "GPA", "Ethnicity", "Status", "Interview Location", "Interview Date"]) persons = _get_persons_for_job(job) for application in job.application_set.all(): if not application.applicationcomponentpart_set.all(): continue person = persons[application.applicant.user.username] address = person.get_address_by_type("MA") if address: mailing_address = address.street_line_1 if address.street_line_2: mailing_address = mailing_address + " " + address.street_line_2 if address.street_line_3: mailing_address = mailing_address + " " + address.street_line_3 mailing_address = "%s %s, %s %s" % (mailing_address, address.city, address.state, address.zip_code) else: mailing_address = "" try: interview = Interview.objects.get(job=application.job, application=application) interview_location = interview.location interview_date = interview.datetime.strftime("%A, %B %e at %I:%M %p") except Interview.DoesNotExist: interview_location = "None" interview_date = "None" writer.writerow([person.student_id, person.first_name, person.last_name, person.gender, person.email, mailing_address, person.gpa, person.ethnicity, application.status, interview_location, interview_date]) return response
def admin(request, job_slug): post_data = request.POST or None job = get_object_or_404(Job.objects.all(), slug=job_slug) try: administrator = JobUser.objects.get(user=request.user, job=job) except JobUser.DoesNotExist: if not request.user.is_superuser: return HttpResponse(content="401 Unauthorized: Access is denied due to invalid credentials.", mimetype="text/plain", status=401) else: administrator = True persons = _get_persons_for_job(job) apps = [] forms = [] addresses = ["Birnam", "Ridgeway", "Buchanan", "Edens", "Fairhaven", "Higginson", "Highland", "Mathes", "Nash"] for application in job.application_set.all(): if not application.applicationcomponentpart_set.all(): continue person = persons[application.applicant.user.username] user = application.applicant.user applicant_full = Applicant.objects.get(user=user) app = {} app['username'] = person.username or person.student_id app['first_name'] = person.first_name app['last_name'] = person.last_name app['gpa'] = person.gpa app['is_submitted'] = application.is_submitted try: interview = Interview.objects.get(job=application.job, application=application) app['interview_date'] = interview.datetime.strftime("%B, %e at %I:%M %p") except Interview.DoesNotExist: app['interview_date'] = "None" addy = "" for address in person.addresses: if address.street_line_1.partition(' ')[0] in addresses: addy = address.street_line_1 if addy: app["address"] = addy else: app["address"] = "Off campus" # app["conduct_id"] = has_conduct(person) try: instance = AdminApplication.objects.get(application=application) except AdminApplication.DoesNotExist: if application.is_submitted or application.end_datetime: status = ApplicationStatus.objects.get(status="Submitted") else: status = ApplicationStatus.objects.get(status="In Progress") instance = AdminApplication(status=status, application=application) prefix = "%s" % (application.id) form = AdminApplicationForm(post_data, prefix=prefix, instance=instance) app["form"] = form app["application"] = application if administrator and form.is_valid(): form.save() if form.initial["status"] != form.cleaned_data["status"].id: if form.cleaned_data["status"].status not in [u'Position Offered', u'Position Accepted']: try: application_email = ApplicationEmail.objects.get(status=form.cleaned_data["status"], job=job) except ApplicationEmail.DoesNotExist: application_email = None if not settings.DEBUG: if application_email and person.email: subject = application_email.subject from_email = application_email.sender to_email = (person.email,) try: message_content = Template(application_email.content) message = message_content.substitute(name=person.first_name) except KeyError, e: subject = "KeyError in application email" message = "%s in application email id: %s" % (e.message, application_email.name) mail_admins(subject, message) email = EmailMessage(subject, message, from_email, to_email) email.send() comment = "%s email has been sent" % (form.cleaned_data["status"]) user = request.user or None content_type = ContentType.objects.get(name="application") Comment.objects.create(content_type=content_type, object_pk=application.id, site=Site.objects.get_current(), user=user, comment=comment) elif application_email: subject = "%s %s for position %s has no email" % (person.first_name, person.last_name, job) message = "%s %s (%s) does not have an email address. They applied for %s and were supposed to receive %s email." % (person.first_name, person.last_name, person.student_id, job, application_email.subject) mail_admins(subject, message) from_email = application_email.sender to_email = (application_email.sender, ) email = EmailMessage(subject, message, from_email, to_email) email.send() comment = "%s no email could be sent" % (form.cleaned_data["status"]) user = request.user or None content_type = ContentType.objects.get(name="application") Comment.objects.create(content_type=content_type, object_pk=application.id, site=Site.objects.get_current(), user=user, comment=comment) apps.append(app) forms.append(form)
def admin(request, job_slug): post_data = request.POST or None job = get_object_or_404(Job.objects.all(), slug=job_slug) try: administrator = JobUser.objects.get(user=request.user, job=job) except JobUser.DoesNotExist: if not request.user.is_superuser: return HttpResponse( content= "401 Unauthorized: Access is denied due to invalid credentials.", mimetype="text/plain", status=401) else: administrator = True persons = _get_persons_for_job(job) apps = [] forms = [] addresses = [ "Birnam", "Ridgeway", "Buchanan", "Edens", "Fairhaven", "Higginson", "Highland", "Mathes", "Nash" ] for application in job.application_set.all(): if not application.applicationcomponentpart_set.all(): continue person = persons[application.applicant.user.username] user = application.applicant.user applicant_full = Applicant.objects.get(user=user) app = {} app['username'] = person.username or person.student_id app['first_name'] = person.first_name app['last_name'] = person.last_name app['gpa'] = person.gpa app['is_submitted'] = application.is_submitted try: interview = Interview.objects.get(job=application.job, application=application) app['interview_date'] = interview.datetime.strftime( "%B, %e at %I:%M %p") except Interview.DoesNotExist: app['interview_date'] = "None" addy = "" for address in person.addresses: if address.street_line_1.partition(' ')[0] in addresses: addy = address.street_line_1 if addy: app["address"] = addy else: app["address"] = "Off campus" # app["conduct_id"] = has_conduct(person) try: instance = AdminApplication.objects.get(application=application) except AdminApplication.DoesNotExist: if application.is_submitted or application.end_datetime: status = ApplicationStatus.objects.get(status="Submitted") else: status = ApplicationStatus.objects.get(status="In Progress") instance = AdminApplication(status=status, application=application) prefix = "%s" % (application.id) form = AdminApplicationForm(post_data, prefix=prefix, instance=instance) app["form"] = form app["application"] = application if administrator and form.is_valid(): form.save() if form.initial["status"] != form.cleaned_data["status"].id: if form.cleaned_data["status"].status not in [ u'Position Offered', u'Position Accepted' ]: try: application_email = ApplicationEmail.objects.get( status=form.cleaned_data["status"], job=job) except ApplicationEmail.DoesNotExist: application_email = None if not settings.DEBUG: if application_email and person.email: subject = application_email.subject from_email = application_email.sender to_email = (person.email, ) try: message_content = Template( application_email.content) message = message_content.substitute( name=person.first_name) except KeyError, e: subject = "KeyError in application email" message = "%s in application email id: %s" % ( e.message, application_email.name) mail_admins(subject, message) email = EmailMessage(subject, message, from_email, to_email) email.send() comment = "%s email has been sent" % ( form.cleaned_data["status"]) user = request.user or None content_type = ContentType.objects.get( name="application") Comment.objects.create( content_type=content_type, object_pk=application.id, site=Site.objects.get_current(), user=user, comment=comment) elif application_email: subject = "%s %s for position %s has no email" % ( person.first_name, person.last_name, job) message = "%s %s (%s) does not have an email address. They applied for %s and were supposed to receive %s email." % ( person.first_name, person.last_name, person.student_id, job, application_email.subject) mail_admins(subject, message) from_email = application_email.sender to_email = (application_email.sender, ) email = EmailMessage(subject, message, from_email, to_email) email.send() comment = "%s no email could be sent" % ( form.cleaned_data["status"]) user = request.user or None content_type = ContentType.objects.get( name="application") Comment.objects.create( content_type=content_type, object_pk=application.id, site=Site.objects.get_current(), user=user, comment=comment) apps.append(app) forms.append(form)