Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 17th, 2012  |  syntax: None  |  size: 1.04 KB  |  hits: 19  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. from __future__ import absolute_import
  2.  
  3. import Image
  4. import hashlib
  5. import os
  6.  
  7. from ..lib.PyQRNative import *
  8. from django import template
  9. from django.conf import settings
  10. from django.http import QueryDict
  11.  
  12. register = template.Library()
  13. QRCODESURL = os.path.join("img", "qrcode")
  14. QRCODESPATH = os.path.join(settings.STATIC_ROOT, QRCODESURL)
  15.  
  16. @register.filter(name="qrcode")
  17. def qrcode(value,args):
  18.  
  19.   qs = QueryDict(args)
  20.   m = hashlib.md5()
  21.   m.update(value)
  22.   md5 = m.hexdigest()
  23.  
  24.   if qs.has_key('size') :
  25.     size=int(qs.get('size'))
  26.   else:
  27.     size=2
  28.  
  29.     if qs.has_key('offset') :
  30.       offset=int(qs.get('offset'))
  31.     else:
  32.       offset=4
  33.  
  34.       qr = QRCode(size, QRErrorCorrectLevel.L)
  35.       qr.addData(value)
  36.       qr.make()
  37.  
  38.       filename = "%s.png" % md5
  39.  
  40.       filepath = QRCODESPATH
  41.  
  42.       if not os.path.exists( filepath ):
  43.         os.makedirs(filepath)
  44.  
  45.  
  46.       qr_image = qr.makeImage(size, offset)
  47.       qr_image.save( os.path.join(filepath, filename) )
  48.  
  49.       return "%s/%s" % (QRCODESURL,filename)