File: PageFeedback.py

package info (click to toggle)
cherokee 0.7.2-4
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 8,808 kB
  • ctags: 6,577
  • sloc: ansic: 45,071; python: 9,628; sh: 9,468; makefile: 1,639; xml: 61; perl: 32
file content (108 lines) | stat: -rw-r--r-- 3,001 bytes parent folder | download
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
106
107
108
from Page import *
from Table import *
from time import asctime

INET_WARNING = """
A working connection to the Internet is required for this form to work.
"""

CHEROKEE_FEEDBACK_URL = "/cgi-bin/feedback"

REPORT_TEMPLATE = """
=====================================================
Name:  %(name)s <%(email)s>
Date:  %(date)s
=====================================================

Message
-------
%(body)s

Configuration
-------------
%(conf)s
"""

THANKS_TEXT = """
<h1>Report sent</h1>
<p>Thank you a million for your feedback. We do appreciate your help.</p>

%(reply)s

<h2>Report</h2>
<pre>
%(report)s
</pre>
"""

class PageFeedback (PageMenu, FormHelper):
    def __init__ (self, cfg):
        PageMenu.__init__ (self, 'feedback', cfg)
        FormHelper.__init__ (self, 'feedback', cfg)

    def _op_render (self):
        content = self._render_content()
        self.AddMacroContent ('title', 'Feedback')
        self.AddMacroContent ('content', content)
        return Page.Render(self)

    def _render_content (self):
        txt = '<h1>Feedback</h1>'
        txt += self.Dialog(INET_WARNING, 'important-information')

        table = Table(2)
        self.AddTableEntry (table, 'Name',  'name')
        self.AddTableEntry (table, 'EMail', 'email')
        self.AddTableCheckbox (table, 'Include configuration', 'include_conf', True)
        txt += str(table)

        txt += """<p>Message:</p>
                  <p><textarea name="body" id="body" rows="20" style="width:100%%;"></textarea></p>
               """
        form = Form ("/%s" % (self._id))
        return form.Render(txt)

    def _send_report (self, text):
        import httplib, urllib

        params  = urllib.urlencode({'report': text})
        headers = {"Content-type": "application/x-www-form-urlencoded",
                   "Accept":       "text/plain"}

        conn = httplib.HTTPConnection("www.cherokee-project.org")
        conn.request("POST", CHEROKEE_FEEDBACK_URL, params, headers)
        response = conn.getresponse()
        data = response.read()
        conn.close()
        return data
        
    def _op_apply_changes (self, uri, post):
        include_conf = (post.get_val('include_conf') == 'on')
        if include_conf:
            configuration = str(self._cfg)
        else:
            configuration = 'Not included'
        
        params = {
            'name':  post.pop('name',  ''),
            'email': post.pop('email', ''),
            'body':  post.pop('body',  ''),
            'date':  asctime(),
            'conf':  configuration
        }

        txt = REPORT_TEMPLATE % params
        reply = self._send_report(txt)

        return self._op_render_thanks(txt, reply)

    def _op_render_thanks (self, text, reply):
        params = {
            'report': text,
            'reply':  self.Indent(reply)
        }

        self.AddMacroContent ('title', 'Feedback: Thank you')
        self.AddMacroContent ('content', THANKS_TEXT % params)
        return Page.Render(self)