~jdong/nsp/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
'''
Class library for grouping elements together into a form
'''
import elements
import json
import cStringIO
from PIL import Image
import zlib
import base64
import pickle

try:
  import PyQrcodec as qr
except:
  raise "You do not have PyQrcodec installed. See http://www.pedemonte.eu/pyqr/index.py/pyqrhome"



class Form(object):
  '''
  A representation of a grouping of form elements that comprise a form
  '''

  def __init__(self, title, description, elements=[]):
      '''
      Initialize a form:

      title: Name of the form
      description: The description of the contents of the form.
      elements: An ordered list (or list like object) containing the items in
      the form.
      '''
      self.properties={}
      self['title']=title
      self['description']=description
      self['elements']=elements
  def __getitem__(self, item):
      return self.properties[item]
  def __setitem__(self, item, val):
      self.properties[item]=val
  def dump_qrstring(self):
    '''
    Return a small QR-encodable string describing the elements
    on the form.

    Will be returned in the order of items on the form.
    '''
    import zlib
    out={}
    out['format']=1
    out['elements']=[]
    for element in self['elements']:
      out['elements'].append(element.dump_qrstring())
      
    out=base64.b64encode(zlib.compress(pickle.dumps(out, -1),9))
    print "Dumping: ", out
    return out
  def dump_json(self):
      '''
      Dump the form into a JSON
      '''
      representation={'elements': []}
      for k in self.properties:
         if k == "elements":
             for e in self.properties[k]:
                 representation[k].append(e.dump_json())
         else:
             representation[k]=self.properties[k]
      return json.dumps(representation, sort_keys=True, indent=4)
  def mkqrimg(self):
    '''
    Makes a QR image as a BASE64 png
    '''
    img=qr.encode(self.dump_qrstring(), image_width=100)[1]
    out=cStringIO.StringIO()
    img.save(out, format='PNG')
    b64=base64.b64encode(out.getvalue())
    return b64
    
    
  def dump_html(self):
      '''
      Dump the form as an HTML document
      '''
      out=cStringIO.StringIO()
      print >>out, "<html>"
      print >>out, "<head>"
      print >>out, '<style type="text/css">'
      print >>out, open('form.css').read()
      print >>out, "</style>"
      print >>out, "<title>", self['title'], "</title>"
      print >>out, "</head>"
      print >>out, "<body>"
      print >>out, '<img src="data:image/png;base64,%s" />' % self.mkqrimg()
      print >>out, "<h2>Description:</h2>"
      print >>out, "<p>%s</p>" % self['description']
      
      for element in self['elements']:
          print >>out,  \
          '<span class="item" >',\
                element.dump_html(), "</span>"

      print >>out, "</body>"
      print >>out, "</html>"
      return out.getvalue()