diff options
Diffstat (limited to 'certificate/views.py')
-rw-r--r-- | certificate/views.py | 244 |
1 files changed, 231 insertions, 13 deletions
diff --git a/certificate/views.py b/certificate/views.py index 04fa09a..a8fa759 100644 --- a/certificate/views.py +++ b/certificate/views.py @@ -2,12 +2,13 @@ from django.shortcuts import render from django.http import HttpResponse from django.shortcuts import render_to_response, redirect from django.template import RequestContext -from certificate.models import Scilab_participant, Certificate, Event, Scilab_speaker, Scilab_workshop, Question, Answer, FeedBack, Scipy_participant, Scipy_speaker +from certificate.models import Scilab_participant, Certificate, Event, Scilab_speaker, Scilab_workshop, Question, Answer, FeedBack, Scipy_participant, Scipy_speaker, Drupal_camp import subprocess import os from string import Template +import hashlib from certificate.forms import FeedBackForm - +from collections import OrderedDict # Create your views here. def index(request): @@ -96,39 +97,103 @@ def download(request): context['message'] = 'You can download the certificate' return render_to_response('download.html', context, ci) -def verify(request): +def verification(serial, _type): context = {} - ci = RequestContext(request) - detail = None - if request.method == 'POST': - serial_no = request.POST.get('serial_no').strip() + if _type == 'key': try: - certificate = Certificate.objects.get(serial_no=serial_no) + certificate = Certificate.objects.get(short_key=serial) + name = certificate.name.title() + paper = certificate.paper + workshop = certificate.workshop + serial_no = certificate.serial_no + certificate.verified += 1 + certificate.save() + purpose, year, type = _get_detail(serial_no) + if type == 'P': + if purpose == 'Drupal Mumbai Camp': + drupal_user = Drupal_camp.objects.get(email=certificate.email) + DAY = drupal_user.attendance + if DAY == 1: + day = 'Day 1' + elif DAY == 2: + day = 'Day 2' + elif DAY == 3: + day = 'Day 1 and Day 2' + detail = OrderedDict([('Name', name), ('Attended', day), + ('Event', purpose), ('Year', year)]) + else: + detail = '{0} had attended {1} {2}'.format(name, purpose, year) + elif type == 'A': + detail = '{0} had presented paper on {3} in the {1} {2}'.format\ + (name, purpose, year, paper) + elif type == 'W': + detail = '{0} had attended workshop on {3} in the {1} {2}'.format\ + (name, purpose, year, workshop) + context['serial_key'] = True except Certificate.DoesNotExist: + detail = 'User does not exist' context["invalidserial"] = 1 - return render_to_response('verify.html', context, context_instance=ci) - else: + context['detail'] = detail + return context + if _type == 'number': + try: + certificate = Certificate.objects.get(serial_no=serial) name = certificate.name.title() paper = certificate.paper workshop = certificate.workshop certificate.verified += 1 certificate.save() - purpose, year, type = _get_detail(serial_no) + purpose, year, type = _get_detail(serial) if type == 'P': - detail = '{0} had attended {1} {2}'.format(name, purpose, year) + if purpose == 'Drupal Mumbai Camp': + drupal_user = Drupal_camp.objects.get(email=certificate.email) + DAY = drupal_user.attendance + if DAY == 1: + day = 'Day 1' + elif DAY == 2: + day = 'Day 2' + elif DAY == 3: + day = 'Day 1 and Day 2' + detail = {} + detail['Name'] = name + detail['Attended'] = day + detail['Event'] = purpose + detail['Year'] = year + else: + detail = '{0} had attended {1} {2}'.format(name, purpose, year) elif type == 'A': detail = '{0} had presented paper on {3} in the {1} {2}'.format(name, purpose, year, paper) elif type == 'W': detail = '{0} had attended workshop on {3} in the {1} {2}'.format(name, purpose, year, workshop) context['detail'] = detail - return render_to_response('verify.html', context, ci) + except Certificate.DoesNotExist: + context["invalidserial"] = 1 + return context + + +def verify(request, serial_key=None): + context = {} + ci = RequestContext(request) + detail = None + if serial_key is not None: + context = verification(serial_key, 'key') + return render_to_response('verify.html', context, ci) + if request.method == 'POST': + serial_no = request.POST.get('serial_no').strip() + context = verification(serial_no, 'number') + if 'invalidserial' in context: + context = verification(serial_no, 'key') + return render_to_response('verify.html', context, ci) return render_to_response('verify.html',{}, ci) def _get_detail(serial_no): + purpose = None if serial_no[0:3] == 'SLC': purpose = 'Scilab Conference' elif serial_no[0:3] == 'SPC': purpose = 'SciPy India' + elif serial_no[0:3] == 'DCM': + purpose = 'Drupal Mumbai Camp' if serial_no[3:5] == '14': year = '2014' @@ -394,3 +459,156 @@ def create_scipy_certificate(certificate_path, name, qrcode, type, paper, worksh print e error = True return [None, error] + + +def drupal_feedback(request): + context = {} + ci = RequestContext(request) + form = FeedBackForm() + questions = Question.objects.filter(purpose='DMC') + if request.method == 'POST': + form = FeedBackForm(request.POST) + if form.is_valid(): + data = form.cleaned_data + try: + FeedBack.objects.get(email=data['email'].strip(), purpose='DMC') + context['message'] = 'You have already submitted the feedback. You can download your certificate.' + return render_to_response('drupal_download.html', context, ci) + except FeedBack.DoesNotExist: + feedback = FeedBack() + feedback.name = data['name'].strip() + feedback.email = data['email'].strip() + feedback.purpose = 'DMC' + feedback.submitted = True + feedback.save() + for question in questions: + answered = request.POST.get('{0}'.format(question.id), None) + answer = Answer() + answer.question = question + answer.answer = answered.strip() + answer.save() + feedback.answer.add(answer) + feedback.save() + context['message'] = 'Thank you for the feedback. You can download your certificate.' + return render_to_response('drupal_download.html', context, ci) + + context['form'] = form + context['questions'] = questions + + return render_to_response('drupal_feedback.html', context, ci) + +def drupal_download(request): + context = {} + err = "" + ci = RequestContext(request) + cur_path = os.path.dirname(os.path.realpath(__file__)) + certificate_path = '{0}/drupal_template/'.format(cur_path) + + if request.method == 'POST': + email = request.POST.get('email').strip() + type = request.POST.get('type', 'P') + paper = None + workshop = None + if type == 'P': + user = Drupal_camp.objects.filter(email=email) + if not user: + context["notregistered"] = 1 + return render_to_response('drupal_download.html', + context, context_instance=ci) + else: + user = user[0] + fname = user.firstname + lname = user.lastname + name = '{0} {1}'.format(fname, lname) + purpose = user.purpose + DAY = user.attendance + if DAY == 1: + day = 'Day 1' + elif DAY == 2: + day = 'Day 2' + elif DAY == 3: + day = 'Day 1 and Day 2' + else: + context['notregistered'] = 2 + return render_to_response('drupal_download.html', context, + context_instance=ci) + year = '15' + id = int(user.id) + hexa = hex(id).replace('0x','').zfill(6).upper() + serial_no = '{0}{1}{2}{3}'.format(purpose, year, hexa, type) + serial_key = (hashlib.sha1(serial_no)).hexdigest() + file_name = '{0}{1}'.format(email,id) + file_name = file_name.replace('.', '') + try: + old_user = Certificate.objects.get(email=email, serial_no=serial_no) + qrcode = 'Verify at: http://fossee.in/certificates/verify/{0} '.format(old_user.short_key) + details = {'name': name, 'day': day, 'serial_key': old_user.short_key} + certificate = create_drupal_certificate(certificate_path, details, + qrcode, type, paper, workshop, file_name) + if not certificate[1]: + old_user.counter = old_user.counter + 1 + old_user.save() + return certificate[0] + except Certificate.DoesNotExist: + uniqueness = False + num = 5 + while not uniqueness: + present = Certificate.objects.filter(short_key__startswith=serial_key[0:num]) + if not present: + short_key = serial_key[0:num] + uniqueness = True + else: + num += 1 + qrcode = 'Verify at: http://fossee.in/certificates/verify/{0} '.format(short_key) + details = {'name': name, 'day': day, 'serial_key': short_key} + certificate = create_drupal_certificate(certificate_path, details, + qrcode, type, paper, workshop, file_name) + if not certificate[1]: + certi_obj = Certificate(name=name, email=email, + serial_no=serial_no, counter=1, workshop=workshop, + paper=paper, serial_key=serial_key, short_key=short_key) + certi_obj.save() + return certificate[0] + + if certificate[1]: + _clean_certificate_certificate(certificate_path, file_name) + context['error'] = True + return render_to_response('drupal_download.html', context, ci) + context['message'] = 'You can download the certificate' + return render_to_response('drupal_download.html', context, ci) + + +def create_drupal_certificate(certificate_path, name, qrcode, type, paper, workshop, file_name): + error = False + try: + download_file_name = None + template = 'template_DCM2015Pcertificate' + download_file_name = 'DCM2015Pcertificate.pdf' + + template_file = open('{0}{1}'.format\ + (certificate_path, template), 'r') + content = Template(template_file.read()) + template_file.close() + + content_tex = content.safe_substitute(name=name['name'].title(), + day=name['day'], serial_key = name['serial_key'], qr_code=qrcode) + create_tex = open('{0}{1}.tex'.format\ + (certificate_path, file_name), 'w') + create_tex.write(content_tex) + create_tex.close() + return_value, err = _make_certificate_certificate(certificate_path, + type, file_name) + if return_value == 0: + pdf = open('{0}{1}.pdf'.format(certificate_path, file_name) , 'r') + response = HttpResponse(content_type='application/pdf') + response['Content-Disposition'] = 'attachment; \ + filename=%s' % (download_file_name) + response.write(pdf.read()) + _clean_certificate_certificate(certificate_path, file_name) + return [response, False] + else: + error = True + except Exception, e: + print e + error = True + return [None, error] |