Attachment 'feedback.py'

Download

   1 # -*- coding: iso-8859-1 -*-
   2 """
   3     MoinMoin - Feedback / Guestbook Page
   4 
   5     This way you can have a simple guestbook in your wiki.
   6 
   7     See: http://www.erzgebirgstraverse.de/moin/G%C3%A4stebuch
   8 
   9     Just copy this file to
  10 
  11     share/moin/YOURWIKI/data/plugin/parser/feedback.py
  12 
  13     Create a new pages with this content:
  14 
  15     #format feedback
  16     
  17     @copyright: 2007 by Thomas Güttler http://www.thomas-guettler.de/
  18     @license: any open source licence you like
  19 
  20     History:
  21 
  22      2007-12-14: Added JavaScript magic to stop spam robots.
  23      
  24 """
  25 
  26 # Python Imports
  27 import re
  28 import cgi
  29 import time
  30 import textwrap
  31 
  32 # MoinMoin Imports
  33 from MoinMoin import wikiutil
  34 from MoinMoin.PageEditor import PageEditor
  35 
  36 Dependencies = []
  37 magic="###EOM###"
  38 
  39 class Parser:
  40     """
  41     Feedback Page
  42     """
  43 
  44     extensions = []
  45     Dependencies = []
  46 
  47     def __init__(self, raw, request, **kw):
  48         """
  49         Store the source text.
  50         """
  51         self.raw = raw
  52         self.request = request
  53         self.form = request.form
  54         self._ = request.getText
  55 
  56     def format(self, formatter):
  57         """
  58         Parse and send the table.
  59         """
  60         errors=[]
  61         def getvar(name):
  62             var=''.join(self.form.get(name, "")).strip()
  63             if magic in var:
  64                 errors.append("Value contains magic: %s" % magic)
  65                 var=""
  66             if len(var)>2000:
  67                 errors.append("Value is too long")
  68                 var=""
  69             return var
  70         frm=getvar("frm")
  71         subject=getvar("subject")
  72         message=getvar("message")
  73 
  74         # This variable is set via javascript in hope that it will stop
  75         # spam robots from submitting advertisment messages.
  76         reject=getvar("reject")
  77         
  78         if frm and subject and message:
  79             if subject.lower().startswith("ok:"):
  80                 subject=subject[3:]
  81                 reject="ok"
  82             else:
  83                 errors.append("Sorry, you have JavaScript disabled. Please start the subject with OK:....")
  84                 subject='OK:%s' % subject
  85             if reject=="ok":
  86                 self.raw="#format feedback\nFrom: %s\nSubject: %s\n" \
  87                          "Date: %s\n%s\n%s\n%s" % (
  88                     frm, subject, time.strftime("%Y-%m-%d %H:%M"), message,
  89                     magic, self.raw)
  90                 pageeditor=PageEditor(self.request, formatter.page.page_name)
  91                 pageeditor._write_file(self.raw)
  92                 message=""
  93                 subject=""
  94         else:
  95             if (frm or subject or message):
  96                 errors.append("Please fill From, Subject and Message")
  97             
  98         messages = self.raw.split('\n%s\n' % magic)
  99         html=[]
 100         for msg in messages:
 101             msg=msg.strip()
 102             if not msg:
 103                 continue
 104             match=re.match(
 105                 r'(#.*\n)?\s*From: (.+?)\nSubject: (.+?)\nDate: (.+?)\n(.*)$',
 106                 msg, re.DOTALL)
 107             assert match, "Broken message %s" % msg
 108             comment, mfrm, msubject, date, msg = match.groups()
 109             msg='\n'.join(textwrap.wrap(msg))
 110             html.append('''
 111             <hr>
 112             <table>
 113              <tr>
 114               <td style="text-align: left">From</td><td>%s</td>
 115              </tr>
 116              <tr>
 117               <td style="text-align: left">Subject</td><td>%s</td>
 118              </tr>
 119              <tr>
 120               <td style="text-align: left">Date</td><td>%s</td>
 121              </tr> 
 122              <tr>
 123               <td colspan="2"><pre style="text-align: left">%s</pre></td>
 124              </tr>
 125             </table>''' % (cgi.escape(mfrm), cgi.escape(msubject),
 126                            cgi.escape(date), cgi.escape(msg)))
 127         errors='\n'.join(["<li>%s" % cgi.escape(e) for e in errors])
 128         if errors:
 129             errors='<ul style="color: red">%s</ul>' % errors
 130 
 131         html='''
 132          <div style="text-align: center; width: 100%%">
 133           <form action="%s" method="post">
 134            <table>
 135             <tr><td>From</td><td><input type="text" name="frm" size="20" value="%s"></td></tr>
 136             <tr><td>Subject</td><td><input type="text" name="subject" size="20" value="%s"></td></tr>
 137             <tr><td>Message</td><td><textarea name="message" cols="50" rows="10">%s</textarea></td></tr>
 138             <tr><td colspan="2"><input type="submit" value=" Submit " onclick="this.form.reject.value='ok';return true"></td></tr>
 139            </table>
 140            <input type="hidden" name="reject" value="unchanged">
 141           </form> 
 142           %s<br>
 143           %s
 144 
 145          </div>''' % (
 146             wikiutil.quoteWikinameURL(formatter.page.page_name),
 147             cgi.escape(frm, True), cgi.escape(subject, True), cgi.escape(message),
 148             errors,
 149             '\n'.join(html))
 150         self.request.write(html)
 151              

Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
  • [get | view] (2007-12-14 21:03:41, 4.8 KB) [[attachment:feedback.py]]
 All files | Selected Files: delete move to page copy to page

You are not allowed to attach a file to this page.