Changeset 7225 for main


Ignore:
Timestamp:
27 Nov 2011, 18:55:06 (13 years ago)
Author:
Henrik Bettermann
Message:

Reorganise contact form pages. Use megrok.layout.Form.

Location:
main/waeup.sirp/trunk/src/waeup/sirp
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • main/waeup.sirp/trunk/src/waeup/sirp/browser/browser.txt

    r7222 r7225  
    4242  >>> browser.open('http://localhost/myuniversity')
    4343  >>> browser.getLink('Enquiries').click()
    44   >>> browser.getControl('Submit').click()
    45   >>> print browser.contents
    46   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
    47   ...Error: All fields must be filled...
    48   ...
    49 
    50   >>> browser.getControl(name='fullname').value = "Bob Tester"
    51   >>> browser.getControl(name='email').value = "xx@yy.zz"
    52   >>> browser.getControl(name='descr').value = "test message"
    53   >>> browser.getControl('Submit').click()
     44  >>> browser.getControl('Send').click()
     45  >>> print browser.contents
     46  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
     47  ...There were errors...
     48  ...
     49
     50  >>> browser.getControl(name='form.fullname').value = "Bob Tester"
     51  >>> browser.getControl(name='form.email_from').value = "xx@yy.zz"
     52  >>> browser.getControl(name='form.body').value = "test message"
     53  >>> browser.getControl('Send').click()
    5454  >>> print browser.contents
    5555  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
     
    185185  ...
    186186
    187 We fill in a wrong email address::
    188 
    189   >>> browser.getControl(name='email').value = "xx@yy"
    190   >>> browser.getControl(name='descr').value = "test message"
    191   >>> browser.getControl('Submit').click()
    192   >>> print browser.contents
    193   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
    194   ...<li class="message">Error: xx@yy is not a valid email address.</li>
    195   ...
    196 
    197 Now we fill the form properly (this will send a real message to
    198 contact@waeup.org)::
     187We fill the form and try to send message to contact@waeup.org)::
    199188
    200189  >>> browser.open('http://localhost/myuniversity/contactadmin')
    201   >>> browser.getControl(name='email').value = "xx@yy.zz"
    202   >>> browser.getControl(name='descr').value = "test message"
    203   >>> browser.getControl('Submit').click()
    204   >>> print browser.contents
    205   <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
    206   ...<li class="message">Your message has been sent.</li>
    207   ...
    208 
    209 If this test fails, chances are, that the local machine has no SMTP
    210 server installed.
     190  >>> browser.getControl(name='form.body').value = "test message"
     191  >>> browser.getControl('Send').click()
     192  >>> print browser.contents
     193  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"...
     194  ...<li class="message">You don't have a user account.</li>
     195  ...
     196
     197We can't send messages without a user account.
    211198
    212199
  • main/waeup.sirp/trunk/src/waeup/sirp/browser/pages.py

    r7223 r7225  
    2626import time
    2727import re
    28 import smtplib
    29 from email.mime.text import MIMEText
    3028from zope import schema
    3129from zope.authentication.interfaces import (
     
    5250    ILocalRolesAssignable, DuplicationError, IConfigurationContainer,
    5351    ISessionConfiguration, ISessionConfigurationAdd,
    54     IPasswordValidator)
     52    IPasswordValidator, IContactForm)
    5553from waeup.sirp.permissions import get_users_with_local_roles, get_all_roles
    5654from waeup.sirp.students.catalog import search as searchstudents
     
    6058from waeup.sirp.widgets.restwidget import ReSTDisplayWidget
    6159from waeup.sirp.authentication import get_principal_role_manager
    62 from waeup.sirp.utils.helpers import get_user_account
     60from waeup.sirp.utils.helpers import get_user_account, send_mail
    6361
    6462grok.context(IWAeUPObject)
     
    454452
    455453#
    456 # Contact forms...
    457 #
    458 class ContactAdminForm(WAeUPPage):
    459     grok.context(IUniversity)
     454# Contact form...
     455#
     456
     457from megrok.layout import Form
     458class ContactAdminForm(Form):
    460459    grok.name('contactadmin')
    461     grok.template('contactadminform')
     460    #grok.context(IUniversity)
     461    #grok.template('contactadminform')
     462    grok.require('waeup.Authenticated')
     463    pnav = 2
     464    form_fields = grok.AutoFields(IContactForm).select('body')
     465
     466    @property
     467    def config(self):
     468        return grok.getSite()['configuration']
     469
     470    def title(self):
     471        userid = self.request.principal.id
     472        return u'Contact %s' % self.config.name_admin
     473
     474    def label(self):
     475        return self.title()
     476
     477    @property
     478    def get_user_account(self):
     479        return get_user_account(self.request)
     480
     481    @grok.action('Send now')
     482    def send(self, *args, **data):
     483        useraccount = self.get_user_account
     484        if useraccount is None:
     485            self.flash("You don't have a user account.")
     486            return
     487        fullname = useraccount.title
     488        email_from = useraccount.email
     489        username = useraccount.name
     490        body = data['body']
     491        email_to = self.config.email_admin
     492        subject = self.config.email_subject
     493        success = send_mail(fullname,username,self.config.name,
     494                  body,email_from,email_to,subject)
     495        if success:
     496            self.flash('Your message has been sent.')
     497        else:
     498            self.flash('A smtp server error occurred.')
     499        return
     500
     501class EnquiriesForm(ContactAdminForm):
     502    grok.name('enquiries')
    462503    grok.require('waeup.Public')
    463504    pnav = 2
    464 
    465     @property
    466     def config(self):
    467         return grok.getSite()['configuration']
    468 
    469     def title(self):
    470         """Return True if the calling user is authenticated.
    471         """
    472         userid = self.request.principal.id
    473         if userid != 'zope.anybody':
    474             tt = u'Contact %s' % self.config.name_admin
     505    form_fields = grok.AutoFields(IContactForm).select(
     506                          'fullname', 'email_from', 'body')
     507
     508    @grok.action('Send now')
     509    def send(self, *args, **data):
     510        email_from = data['email_from']
     511        fullname = data['fullname']
     512        body = data['body']
     513        username = u'None'
     514        email_to = self.config.email_admin
     515        subject = self.config.email_subject
     516        success = send_mail(fullname,username,self.config.name,
     517                  body,email_from,email_to,subject)
     518        if success:
     519            self.flash('Your message has been sent.')
    475520        else:
    476             tt = u'Enquiries'
    477         return tt
    478 
    479     def label(self):
    480         return self.title()
    481 
    482     @property
    483     def get_user_account(self):
    484         return get_user_account(self.request)
    485 
    486     @property
    487     def namefield_label(self):
    488         if self.request.principal.title == 'Applicant':
    489             return 'Access Code'
    490         else:
    491             return 'Full Name'
    492 
    493     def update(self, *args, **kw):
    494         form = self.request.form
    495         if not ('fullname' in form and 'email' in form and 'descr' in form):
    496             return
    497         self.fullname = fullname = form['fullname']
    498         email = form['email']
    499         self.descr = descr = form['descr']
    500         regno =form['regno']
    501         if not (fullname and email and descr):
    502             self.flash('Error: All fields must be filled.')
    503             return
    504         if not re.match("^[a-zA-Z0-9._%-]+@[a-zA-Z0-9._%-]+.[a-zA-Z]{2,6}$",
    505                         email):
    506             self.flash('Error: %s is not a valid email address.' % email)
    507             return
    508         if regno == 'zope.anybody':
    509             regno = 'Anonymous User'
    510         text = """Fullname: %s
    511 Member ID: %s
    512 Description: %s
    513 """
    514         msg = MIMEText(text % (fullname,regno,descr))
    515         msg['From'] = '%s <%s>' % (fullname,email)
    516        
    517         msg['To'] = self.config.email_admin
    518         msg['Subject'] = self.config.email_subject
    519         server = smtplib.SMTP(self.config.smtp_server)
    520         if self.config.smtp_requires_login:
    521             server.login(self.config.smtp_username,self.config.smtp_password)
    522         server.sendmail(email,self.config.email_admin,msg.as_string())
    523 
    524         server.quit()
    525         self.flash('Your message has been sent.')
    526         return
    527 
     521            self.flash('A smtp server error occurred.')
     522        return
    528523
    529524#
  • main/waeup.sirp/trunk/src/waeup/sirp/browser/viewlets.py

    r7205 r7225  
    610610
    611611   
    612 class ContactTab(PrimaryNavTab):
     612class EnquiriesTab(PrimaryNavTab):
    613613    """Contact tab in primary navigation.
    614614
    615     Display tab only for anonymous. Authenticated users can call the
    616     form from the user navigation bar.
     615    Display tab only for anonymous. Authenticated users can call a
     616    contact form from the user navigation bar.
    617617    """
    618618    grok.order(6)
     
    622622    pnav = 2
    623623
    624     # Also zope.manager has role Anonymous.
    625     # To avoid displaying this tab, uncomment the following.
    626     #def tab_title(self):
    627     #    userid = self.request.principal.id
    628     #    if userid != 'zope.anybody':
    629     #        tt = u''
    630     #    else:
    631     #        tt = u'Enquiries'
    632     #    return tt
    633 
    634     #@property
    635     #def active(self):
    636     #    view_pnav = getattr(self.view, 'pnav', 0)
    637     #    userid = self.request.principal.id
    638     #    if view_pnav == self.pnav and userid == 'zope.anybody':
    639     #        return 'active'
    640     #    return ''
    641 
    642624    @property
    643625    def link_target(self):
    644         return self.view.application_url('contactadmin')       
    645 
    646 
     626        return self.view.application_url('enquiries')
    647627
    648628#
  • main/waeup.sirp/trunk/src/waeup/sirp/interfaces.py

    r7223 r7225  
    202202        """
    203203
     204class IContactForm(IWAeUPObject):
     205    """A contact form.
     206    """
     207
     208    email_from = schema.ASCIILine(
     209        title = u'Email Address:',
     210        default = None,
     211        required = True,
     212        constraint=validate_email,
     213        )
     214
     215    email_to = schema.ASCIILine(
     216        title = u'Email to:',
     217        default = None,
     218        required = True,
     219        constraint=validate_email,
     220        )
     221
     222    subject = schema.TextLine(
     223        title = u'Subject:',
     224        required = True,)
     225
     226    fullname = schema.TextLine(
     227        title = u'Full Name:',
     228        required = True,)
     229
     230    body = schema.Text(
     231        title = u'Text:',
     232        required = True,)
     233
     234
    204235class IUserAccount(IWAeUPObject):
    205236    """A user account.
  • main/waeup.sirp/trunk/src/waeup/sirp/utils/helpers.py

    r7196 r7225  
    2323import shutil
    2424import grok
     25import smtplib
     26from email.mime.text import MIMEText
    2527from cStringIO import StringIO
    2628from docutils.core import publish_string
     
    479481    return account
    480482
     483def send_mail(fullname,username,portal,body,email_from,email_to,subject):
     484    """Send an email with data provided by forms.
     485    """
     486    config = grok.getSite()['configuration']
     487    text = """Fullname: %s
     488User Id: %s
     489Portal: %s
     490
     491%s
     492"""
     493    msg = MIMEText(text % (fullname,username,portal,body))
     494    msg['From'] = '%s <%s>' % (fullname,email_from)
     495    msg['To'] = email_to
     496    msg['Subject'] = subject
     497    server = smtplib.SMTP(config.smtp_server)
     498    if config.smtp_requires_login:
     499        server.login(config.smtp_username,config.smtp_password)
     500    try:
     501        server.sendmail(email_from,email_to,msg.as_string())
     502    except:
     503        return False
     504    server.quit()
     505    return True
Note: See TracChangeset for help on using the changeset viewer.