Guest User

custom_contact_form

a guest
Feb 10th, 2011
257
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.46 KB | None | 0 0
  1. ####################################################################################
  2. # model
  3. ####################################################################################
  4.  
  5. #DEFAULT SIGNATURE
  6. signature = db.Table(db,'signature',
  7.                 Field('created_on', 'datetime',
  8.                 default=request.now,writable=False,readable=False,
  9.                 label=T('Created On')),
  10.             Field('updated_on', 'datetime',
  11.                 update=request.now,writable=False,readable=False,
  12.                 label=T('Updated On')),
  13.             )
  14.  
  15.  
  16. #CONTACT TABLE
  17. db.define_table('contato',
  18.         Field('name',label=T('Name')),
  19.         Field('email',label=T('Email')),
  20.         Field('message','text',label=T('Message')),
  21.         Field('is_sent','boolean',label=T('Sent?')),
  22.         Field('remote_ip',label=T('Sender IP')),
  23.         Field('remote_browser',label=T('Sender browser')),
  24.         signature,
  25.         format=lambda r:'%s - %s' % (r.nome,r.email)
  26.         )
  27.  
  28. #DEFAULTS
  29. db.contato.remote_ip.default = request.client
  30. db.contato.remote_browser.default = request.env.http_user_agent
  31. db.contato.is_sent.default = False
  32.  
  33. #VALIDATORS
  34. db.contato.name.requires=IS_NOT_EMPTY(error_message=T('Please fill your name'))
  35. db.contato.email.requires=[
  36.         IS_NOT_EMPTY(error_message=T('Please fill your email address')),
  37.         IS_EMAIL(error_message=T('Please enter a valid email address'))
  38.         ]
  39. db.contato.message.requires=[
  40.         IS_NOT_EMPTY(error_message=T('You have to write a message, at least with 10 chars')),
  41.         IS_LENGTH(minsize=10,error_message='Fill up a text with mininum %(min)s chars'),
  42.         ]
  43.  
  44.  
  45.  
  46.  
  47.  
  48. ###################################################################################
  49. # controller
  50. ###################################################################################
  51. def contact():
  52.     # Hide control fields
  53.     db.contato.is_sent.readable = \
  54.     db.contato.is_sent.writable = \
  55.     db.contato.remote_ip.readable = \
  56.     db.contato.remote_ip.writable = \
  57.     db.contato.remote_browser.readable = \
  58.     db.contato.remote_browser.writable = False
  59.    
  60.     #create the form
  61.     form = SQLFORM(db.contato, formstyle='divs')
  62.    
  63.     #hide the default submit button
  64.     form.element(_type='submit')['_type'] = 'hidden'
  65.  
  66.     #Include checkboxes in the form
  67.     __div = DIV(_id='contato_checkboxes')
  68.     __div.append(LABEL(T('Do you want to receive our news?'),BR()))
  69.     for check in ['menu','news']:
  70.         __div.append(INPUT(_type='checkbox',_name=check, _id='check_%s' % check))
  71.         __div.append(LABEL(T(check),_for=check))
  72.    
  73.  
  74.     form.element().append(__div)
  75.  
  76.     #append a new submit button
  77.     __div = DIV(_id='contato_submit')
  78.     __div.append(DIV(INPUT(_type='submit',_value=T('Submit')),_class='w2p_fw'))
  79.     form.element().append(__div)
  80.  
  81.     if form.accepts(request.vars, session):
  82.         __message = """
  83.                %(name)s / %(email)s / %(message)s
  84.         """ % form.vars
  85.        
  86.         __status = mail.send(
  87.                 to='[email protected]',
  88.                 reply_to=form.vars.email,
  89.                 subject=T('Website contact message'),
  90.                 message=[None,__message],
  91.                 )
  92.  
  93.         if __status:
  94.             db(db.contato.id==form.vars.id).update(is_sent=True)
  95.             status = 'sent'
  96.         else:
  97.             status = 'send_error'
  98.  
  99.     elif form.errors:
  100.         status = 'form_error'
  101.     else:
  102.         status = None
  103.  
  104.     return locals()
  105.  
  106.  
  107. #########################################################################
  108. # View
  109. #########################################################################
  110. {{if status == 'sent':}}
  111.     <span class='mailmessage'>Enviado</span>
  112. {{elif status == 'send_error':}}
  113.     <span class='mailmessage'>Erro ao enviar</span>
  114. {{else:}}
  115.     {{=form}}
  116. {{pass}}
Advertisement
Add Comment
Please, Sign In to add comment