from django.shortcuts import render from django.http import HttpResponse from django.shortcuts import render_to_response from django.template import RequestContext from certificate.models import Scilab_participant, Certificate, Event, Scilab_speaker, Scilab_workshop import subprocess import os from string import Template # Create your views here. def download(request): context = {} err = "" ci = RequestContext(request) cur_path = os.path.dirname(os.path.realpath(__file__)) certificate_path = '{0}/certificate_template/'.format(cur_path) if request.method == 'POST': paper = request.POST.get('paper', None) workshop = request.POST.get('workshop', None) email = request.POST.get('email').strip() type = request.POST.get('type') if type == 'P': user = Scilab_participant.objects.filter(email=email) if not user: context["notregistered"] = 1 return render_to_response('download.html', context, context_instance=ci) else: user = user[0] elif type == 'A': if paper: user = Scilab_speaker.objects.filter(email=email, paper=paper) user = [user[0]] else: user = Scilab_speaker.objects.filter(email=email) if not user: context["notregistered"] = 1 return render_to_response('download.html', context, context_instance=ci) if len(user) > 1: context['user_papers'] = user context['v'] = 'paper' return render_to_response('download.html', context, context_instance=ci) else: user = user[0] paper = user.paper elif type == 'W': if workshop: user = Scilab_workshop.objects.filter(email=email, workshops=workshop) user = [user[0]] else: user = Scilab_workshop.objects.filter(email=email) if not user: context["notregistered"] = 1 return render_to_response('download.html', context, context_instance=ci) print user if len(user) > 1: context['workshops'] = user context['v'] = 'workshop' return render_to_response('download.html', context, context_instance=ci) else: user = user[0] workshop = user.workshops name = user.name purpose = user.purpose year = '14' id = int(user.id) hexa = hex(id).replace('0x','').zfill(6).upper() serial_no = '{0}{1}{2}{3}'.format(purpose, year, hexa, type) qrcode = '{0}\n{1}'.format(name, serial_no) try: old_user = Certificate.objects.get(email=email, serial_no=serial_no) certificate = create_certificate(certificate_path, name, qrcode, type, paper, workshop) if not certificate[1]: old_user.counter = old_user.counter + 1 old_user.save() return certificate[0] except Certificate.DoesNotExist: certificate = create_certificate(certificate_path, name, qrcode, type, paper, workshop) if not certificate[1]: certi_obj = Certificate(name=name, email=email, serial_no=serial_no, counter=1) certi_obj.save() return certificate[0] if certificate[1]: _clean_certificate_certificate(certificate_path) context['error'] = True return render_to_response('download.html', context, ci) return render_to_response('download.html', context, ci) def verify(request): context = {} ci = RequestContext(request) detail = None if request.method == 'POST': serial_no = request.POST.get('serial_no').strip() try: certificate = Certificate.objects.get(serial_no=serial_no) except Certificate.DoesNotExist: context["invalidserial"] = 1 return render_to_response('verify.html', context, context_instance=ci) else: name = certificate.name purpose, year, type = _get_detail(serial_no) if type == 'P': detail = '{0} had attended {1} {2}'.format(name, purpose, year) elif type == 'A': detail = '{0} had presented paper in the {1} {2}'.format(name, purpose, year) elif type == 'W': detail = '{0} had attended workshop in the {1} {2}'.format(name, purpose, year) context['detail'] = detail return render_to_response('verify.html', context, ci) return render_to_response('verify.html',{}, ci) def _get_detail(serial_no): if serial_no[0:3] == 'SLC': purpose = 'Scilab Conference' elif serial_no[0:3] == 'SPC': purpose = 'SciPy India' if serial_no[3:5] == '14': year = '2014' elif serial_no[3:5] == '15': year = '2015' #if serial_no[-1] == 'P': # type = 'Participant' #elif serial_no[-1] == 'A': #type = 'Paper' #elif serial_no[-1] == 'W': # type = 'Workshop' return purpose, year, serial_no[-1] def create_certificate(certificate_path, name, qrcode, type, paper=None, workshop=None): error = False try: if type == 'P': template = 'template_SLC2014Pcertificate' file_name = 'SLC2014Pcertificate' elif type == 'A': template = 'template_SLC2014Acertificate' file_name = 'SLC2014Acertificate' elif type == 'W': template = 'template_SLC2014Wcertificate' file_name = 'SLC2014Wcertificate' template_file = open('{0}{1}'.format\ (certificate_path, template), 'r') content = Template(template_file.read()) template_file.close() if type == 'P': content_tex = content.safe_substitute(name=name, code=qrcode) elif type == 'A': content_tex = content.safe_substitute(name=name, code=qrcode, paper=paper) elif type == 'W': content_tex = content.safe_substitute(name=name, code=qrcode, workshop=workshop) 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) 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' % (file_name) response.write(pdf.read()) _clean_certificate_certificate(certificate_path) return [response, False] else: error = True except Exception, e: print e error = True return [None, error] def _clean_certificate_certificate(path): clean_process = subprocess.Popen('make -C {0} clean'.format(path), shell=True) clean_process.wait() def _make_certificate_certificate(path, type): if type == 'P': command = 'participant_cert' elif type == 'A': command = 'paper_cert' elif type == 'W': command = 'workshop_cert' process = subprocess.Popen('timeout 15 make -C {0} {1}'.format(path, command), stderr = subprocess.PIPE, shell = True) err = process.communicate()[1] return process.returncode, err