def __init__(self, value): if len(value) > 6: hash_value = sha512(value).hexdigest() image = MakeQRImage(value) s = StringIO() image.save(s, format="PNG") qr_image = s.getvalue() super(QRStore, self).__init__(key_name=hash_value, qr_image=qr_image) else: raise QRValueError("QR value too short.")
def qr_code(string): image = MakeQRImage(string,block_in_pixels = 2) png = StringIO() image.save(png,"PNG") content = base64.b64encode(png.getvalue()).replace('\n', '') return '<img style="vertical-align:middle;" src="data:image/png;base64,%s" />' % content
def print_tickets_view(request, preorder_id, secret): if EVENT_DOWNLOAD_DATE and datetime.datetime.now() < datetime.datetime.strptime(EVENT_DOWNLOAD_DATE,'%Y-%m-%d %H:%M:%S'): messages.error(request, _("Tickets cannot be downloaded yet, please try again shortly before the event.")) return redirect("my-tickets") preorder = get_object_or_404(CustomPreorder, Q(pk=preorder_id), Q(user_id=request.user.pk), Q(unique_secret=secret)) # what to do if this preorder is not yet marked as paid? if not preorder.paid: messages.error(request, _("You cannot download your ticket until you paid for it.")) return redirect("my-tickets") # check if this ticket is eligible for an invoice address and has not yet one saved single_ticket_over_limit = False billing_address = False if not preorder.get_billing_address(): for tposition in preorder.get_tickets(): amount = float(tposition['t'].price) * int(tposition['amount']) if amount >= EVENT_BILLING_ADDRESS_LIMIT: single_ticket_over_limit = True break if single_ticket_over_limit: if request.POST: p = request.POST else: p = None form = BillingAddressForm(p) if not request.POST.get('without_billingaddress') == 'yes': if form.is_valid(): billing_address = PreorderBillingAddress() billing_address.company = form.cleaned_data['company'] billing_address.firstname = form.cleaned_data['firstname'] billing_address.lastname = form.cleaned_data['lastname'] billing_address.address1 = form.cleaned_data['address1'] billing_address.address2 = form.cleaned_data['address2'] billing_address.city = form.cleaned_data['city'] billing_address.zip = form.cleaned_data['zip'] billing_address.country = form.cleaned_data['country'] billing_address.preorder = preorder billing_address.save() else: limit = EVENT_BILLING_ADDRESS_LIMIT return render_to_response('billingaddress.html', locals(), context_instance=RequestContext(request)) from pyqrcode import MakeQRImage from fpdf import FPDF import time from django.template.defaultfilters import floatformat from os import remove pdf=FPDF('P', 'pt', 'A4') #initialisation pdf.add_font(family='dejavu', fname="%sdejavu/DejaVuSans.ttf" % settings.STATIC_ROOT, uni=True) pdf.add_font(family='dejavu', style="B", fname="%sdejavu/DejaVuSans-Bold.ttf" % settings.STATIC_ROOT, uni=True) pdf.add_font(family='dejavu', style="I", fname="%sdejavu/DejaVuSans-ExtraLight.ttf" % settings.STATIC_ROOT, uni=True) font = 'dejavu' ############################################# delete_files = [] for position in preorder.get_positions(): #Print a ticket page for each preorder position #Fix for old tickets, probably no longer needed if not position.uuid: from uuid import uuid4 position.uuid = str(uuid4()) position.save() #create the QR code for the current ticket position qrcode = MakeQRImage(position.uuid) qrcode.save('%stmp/%s.jpg' % (settings.STATIC_ROOT, position.uuid), format="JPEG") #add new page pdf.add_page() pdf.set_right_margin(0) ticket = position.ticket #PDF "header" pdf.image('%s%s' % (settings.STATIC_ROOT, settings.EVENT_LOGO), 15, 15, 200, 96) #pdf.set_font(font,'B',27) #pdf.text(20,50,"%s" % 'SIGINT 2013') pdf.set_font(font,'I',6) pdf.text(110,100,"%s" % 'July 5th - July 7th') pdf.text(110,107,"%s" % 'Mediapark, Cologne, Germany') pdf.text(110,114,"%s" % 'https://sigint.ccc.de/') pdf.set_font(font,'I',40) # if price > 150, this is an invoice if ticket.price < 150 and ticket.price > 0: pass #pdf.text(220,100,"RECEIPT") elif ticket.price >= 150: pdf.text(220,90,"RECEIPT") pdf.set_font(font,'B',40) pdf.text(220,50,"ONLINE TICKET") # print billing address - if eligible if ticket.price >= EVENT_BILLING_ADDRESS_LIMIT: if preorder.get_billing_address() or billing_address: from django.utils.encoding import smart_str pdf.set_font('Arial','B',13) pdf.text(20,150,"Billing address") pdf.set_font('Arial','',10) if not billing_address: billing_address = preorder.get_billing_address() ytmp = 0 if billing_address.company: pdf.text(20,170,"%s" % billing_address.company) ytmp+=12 pdf.text(20,170+ytmp,"%s %s" % (billing_address.firstname, billing_address.lastname)) pdf.text(20,182+ytmp,"%s" % billing_address.address1) if billing_address.address2: pdf.text(20,194+ytmp,"%s" % billing_address.address2) ytmp+=12 pdf.text(20,194+ytmp,"%s %s" % (billing_address.zip, billing_address.city)) pdf.text(20,206+ytmp,"%s" % billing_address.country) # print ticket table pdf.set_font(font,'I',15) pdf.text(20,260,"Type") if ticket.price > 0: pdf.text(350,260,"Price") i = 0 pdf.set_font(font,'B',20) pdf.set_y(270+i) pdf.set_x(20) pdf.set_right_margin(250) pdf.set_left_margin(17) pdf.write(17, "\n%s"%ticket.name) pdf.set_left_margin(20) pdf.set_font(font,'B',20) pdf.set_left_margin(20) if ticket.price > 0: pdf.text(350, 302, "%s %s" % (str(floatformat(ticket.price, 2)), "€" if ticket.currency == "EUR" else ticket.currency)) pdf.set_font(font,'',11) price_vat = str(floatformat(float(ticket.price)-float(ticket.price)/(float(ticket.tax_rate)/100+1), 2)) price_net = str(floatformat(float(ticket.price)/(float(ticket.tax_rate)/100+1), 2)) #pdf.text(350, 320, "incl. %s%% VAT: %s %s" % (ticket.tax_rate, price_vat, ticket.currency)) #if ticket.price >= 150: pdf.set_font(font,'',7) pdf.text(350, 314, "%(price_net)s %(currency)s net + %(tax_rate)s%% VAT (%(price_vat)s %(currency)s) = %(price)s %(currency)s total" % ({ 'tax_rate': ticket.tax_rate, 'price': ticket.price, 'price_net': price_net, 'price_vat': price_vat, 'currency': "€" if ticket.currency == "EUR" else ticket.currency, })) ## special tickets special_tickets = { 'Speaker Ticket': 'SPEAKER', 'Booth Operator': 'BOOTH', 'Member of the Press': 'PRESS' } if ticket.name in special_tickets.keys(): pdf.set_font(font,'B',72) pdf.text(pdf.w/2-(pdf.get_string_width(special_tickets[ticket.name])/2), 490, '%s' % special_tickets[ticket.name]) ## special tickets i = i + 50 # print qr code pdf.image('%stmp/%s.jpg' % (settings.STATIC_ROOT, position.uuid), 300, 540, 300, 300) # save file url to "delete array" delete_files.append('%stmp/%s.jpg' % (settings.STATIC_ROOT, position.uuid)) # print human readable ticket code pdf.set_font(font,'I',8) pdf.text(23, 790, 'Payment reference: %s-%s' % (settings.EVENT_PAYMENT_PREFIX, preorder.unique_secret[:10])) pdf.text(23, 800, '%s' % position.uuid) pdf.text(23, 810, '%s' % preorder.unique_secret) #PDF "Footer" # print invoice information pdf.set_font(font, '', 15) pdf.set_y(550) pdf.write(20, '%s' % settings.EVENT_INVOICE_ADDRESS) pdf.set_font(font, '', 10) pdf.set_y(640) if ticket.price > 0: pdf.write(15, '%s' % settings.EVENT_INVOICE_LEGAL) pdf.set_font(font, '', 10) pdf.set_y(680) pdf.write(15, 'Issued: %s' % time.strftime('%Y-%m-%d %H:%M', time.gmtime())) pdf.set_font(font, '', 8) pdf.set_y(720) pdf.set_right_margin(300) if ticket.price > 0 and ticket.price < 150: pdf.write(10, "Bis zu einem Ticketpreis von 150,00 EUR gilt das Ticket gleichzeitig als Kleinbetragsrechnung im Sinne von § 33 UStDV. Umtausch und Rückgabe ausgeschlossen.") elif ticket.price >= 150: pdf.write(10, "Umtausch und Rückgabe ausgeschlossen.") response = HttpResponse(mimetype="application/pdf") response['Content-Disposition'] = 'inline; filename=%s-%s.pdf' % (settings.EVENT_PAYMENT_PREFIX, preorder.unique_secret[:10]) #response['Content-Length'] = in_memory.tell() response.write(pdf.output('', 'S')) # delete qrcode for f in delete_files: remove(f) return response