- Timestamp:
- 27 Nov 2011, 18:55:06 (13 years ago)
- 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 42 42 >>> browser.open('http://localhost/myuniversity') 43 43 >>> browser.getLink('Enquiries').click() 44 >>> browser.getControl('S ubmit').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='f ullname').value = "Bob Tester"51 >>> browser.getControl(name=' email').value = "xx@yy.zz"52 >>> browser.getControl(name=' descr').value = "test message"53 >>> browser.getControl('S ubmit').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() 54 54 >>> print browser.contents 55 55 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"... … … 185 185 ... 186 186 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):: 187 We fill the form and try to send message to contact@waeup.org):: 199 188 200 189 >>> 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 197 We can't send messages without a user account. 211 198 212 199 -
main/waeup.sirp/trunk/src/waeup/sirp/browser/pages.py
r7223 r7225 26 26 import time 27 27 import re 28 import smtplib29 from email.mime.text import MIMEText30 28 from zope import schema 31 29 from zope.authentication.interfaces import ( … … 52 50 ILocalRolesAssignable, DuplicationError, IConfigurationContainer, 53 51 ISessionConfiguration, ISessionConfigurationAdd, 54 IPasswordValidator )52 IPasswordValidator, IContactForm) 55 53 from waeup.sirp.permissions import get_users_with_local_roles, get_all_roles 56 54 from waeup.sirp.students.catalog import search as searchstudents … … 60 58 from waeup.sirp.widgets.restwidget import ReSTDisplayWidget 61 59 from waeup.sirp.authentication import get_principal_role_manager 62 from waeup.sirp.utils.helpers import get_user_account 60 from waeup.sirp.utils.helpers import get_user_account, send_mail 63 61 64 62 grok.context(IWAeUPObject) … … 454 452 455 453 # 456 # Contact forms... 457 # 458 class ContactAdminForm(WAeUPPage): 459 grok.context(IUniversity) 454 # Contact form... 455 # 456 457 from megrok.layout import Form 458 class ContactAdminForm(Form): 460 459 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 501 class EnquiriesForm(ContactAdminForm): 502 grok.name('enquiries') 462 503 grok.require('waeup.Public') 463 504 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.') 475 520 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 528 523 529 524 # -
main/waeup.sirp/trunk/src/waeup/sirp/browser/viewlets.py
r7205 r7225 610 610 611 611 612 class ContactTab(PrimaryNavTab):612 class EnquiriesTab(PrimaryNavTab): 613 613 """Contact tab in primary navigation. 614 614 615 Display tab only for anonymous. Authenticated users can call the616 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. 617 617 """ 618 618 grok.order(6) … … 622 622 pnav = 2 623 623 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.id628 # if userid != 'zope.anybody':629 # tt = u''630 # else:631 # tt = u'Enquiries'632 # return tt633 634 #@property635 #def active(self):636 # view_pnav = getattr(self.view, 'pnav', 0)637 # userid = self.request.principal.id638 # if view_pnav == self.pnav and userid == 'zope.anybody':639 # return 'active'640 # return ''641 642 624 @property 643 625 def link_target(self): 644 return self.view.application_url('contactadmin') 645 646 626 return self.view.application_url('enquiries') 647 627 648 628 # -
main/waeup.sirp/trunk/src/waeup/sirp/interfaces.py
r7223 r7225 202 202 """ 203 203 204 class 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 204 235 class IUserAccount(IWAeUPObject): 205 236 """A user account. -
main/waeup.sirp/trunk/src/waeup/sirp/utils/helpers.py
r7196 r7225 23 23 import shutil 24 24 import grok 25 import smtplib 26 from email.mime.text import MIMEText 25 27 from cStringIO import StringIO 26 28 from docutils.core import publish_string … … 479 481 return account 480 482 483 def 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 488 User Id: %s 489 Portal: %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.