Functionality

Email Functionality

Verify Email (Chained validator)

In order to verify email address, we will create 2 fields where we ask for email, and we will verify they are the same::

    Email = TextField(validator=Email)
    verify_email = TextField(validator=Email)

Then before you initialte the widget do::

#Chained Validator
from formencode import Schema
from formencode.validators import FieldsMatch

emailValidator = Schema(chained_validators=(FieldsMatch('Email',
                   'verify_email',
                    messages={'invalidNoMatch':"Emails do not match"}),))

Then call your instance and pass the extra validator::

#then, we create an instance of this form
address_form = AddressForm("address_form", action='saveaddress', validator=emailValidator)

You can easly do the same with password validation

Send Confirmation email

1. Install turbomail 3.0dev or higher. 2. Add this to yourpackage.lib.app_globals.py::

   def __init__(self):
       # ...
       from turbomail.adapters import tm_pylons
       tm_pylons.start_extension()

3. In the [default] section of development/deployment.ini add::

   mail.on = true
   mail.manager = immediate
   mail.brand =
   mail.transport = smtp
   mail.smtp.server = your.mail.server
   mail.smtp.debug =
   mail.encoding = utf-8
   mail.utf8qp.on = true

4. In your root.py do::

   import turbomail
   message=turbomail.Message("from@example.com,'to@example.com','Thank you for Registering')
   message.plain="Hello this is a test"
   turbomail.send(message)

If you want to send a confirmation link you need to add a filed to your model table, generate the confirmation number, and email it to registering user.

To generate confirmation number you can use::

import uuid
address.email_confirmation_uuid = str(uuid.uuid4())

To create a confirmation function that will accept that url do::

   @expose()
   def confirmaddress(self, email_confirmation_uuid,**kw):
       #print kw
       #print email_confirmation_uuid
       record=DBSession.query(model.Addressbook).filter(model.Addressbook.email_confirmation_uuid==email_confirmation_uuid).one()
       record.email_confirmed=True
       flash("Email Address Confirmed Successfully.")
       raise redirect("/addaddress")

Authorization

Show/Allow based on page name

<span py:choose="">
<span py:when="defined('page') and 'someword' in page"> Put some code here </span>
<span py:otherwise=""><a href="${tg.url('/')}">Home</a> Back to your Quickstart Home page </span>
</span>

<span py:choose="">
<span py:when="defined('page') and page=='IT'"> Put some code here </span>
<span py:otherwise=""><a href="${tg.url('/')}">Home</a> Back to your Quickstart Home page </span>
</span>

Show/Allow based on authority and permissions

With groups and roles, we get:

    USER:
    - username
    - password
    - belongs to group A
    - also belongs to groups B, C
    - has roles 'author', 'editor', 'admin'

    GROUP or ROLE:
    - group/role ID
    - can_view_x?
    - can_view_y?
    - can_modify_y?

\ *See websetup.py file for details on how to add permissions. The file contains code like:

    manager = model.User()
    manager.user_name = u'manager'
    manager.display_name = u'Example manager'
    manager.email_address = u'manager@somedomain.com'
    manager.password = u'managepass'

    model.DBSession.add(manager)

Database Tricks

Autoload Database

Add these import statements

from sqlalchemy import Table
from sqlalchemy.orm import mapper, relation

Create a python class before the init_model(engine) class.

class Recall(object):
    def __init__(self, **kw):
        """automatically mapping attributes"""
        for key, value in kw.iteritems():
            setattr(self, key, value)

Inside the def init_model(engine) add the following at the end:

    recall_table = Table('recall_db', metadata, autoload=True,autoload_with=engine)

    mapper(Recall, recall_table,primary_key=[recall_table.c.RECORD_ID])
  1. The class Recall creates a python object class that we will map our database. Its important for it to be before and outside of init_model. As long as its outside of init_model then tg2 admin interface will be able to see it and will mount it in the /admin interface.

  2. recall_table is a table definition that tells our program to use recall_db, autoload it with the engine we registered, and use the metadata that we setup.

  3. mapper is mapping our table with python object. Since we didn't have a primary key in the table we told the mapper to use one of the columns.

Done. Your table is autoloaded now and you can see it in admin interface.

MyWiki: TurboGears2/Functionality (last edited 2009-09-26 23:38:28 by LukaszSzybalski)