django-publicauth / auth / utils.py

def unicode_urlencode(params):
    """A unicode aware version of urllib.urlencode"""
    from urllib import urlencode

    if isinstance(params, dict):
        params = params.items()
    return urlencode([(k, isinstance(v, unicode) and v.encode('utf-8') or v) for k, v in params])


def generate_hash(string=None):
    import time
    try:                                                           
        from hashlib import md5                                    
    except ImportError:                                            
        import md5                                                 
        md5 = md5.new

    if not string:
        string = str(time.time())
    hash = md5(string).hexdigest()
    return hash


def build_redirect_url(request, default_url):
    """
    Retrieve redirect url from session.

    Use default if retrieved one is broken or not safe.
    """
    url = request.session.get('login_redirect_url')
    if not url or '//' in url or ' ' in url:
        url = default_url
    try:
        del request.session['login_redirect_url']
    except KeyError:
        pass
    return url


def parse_template(template_path, **kwargs):
    """                                     
    Load and render template.               

    First line of template should contain the subject of email.
    Return tuple with subject and content.                     
    """                                                        
    import re

    from django.template.loader import get_template
    from django.template import Context

    template = get_template(template_path)
    context = Context(kwargs)             
    data = template.render(context).strip()
    subject, content = re.split(r'\r?\n', data, 1)
    return (subject.strip(), content.strip())     


def email_template(rcpt, template_path, **kwargs):
    """
    Load, render and email template.

    **kwargs may contain variables for template rendering.
    """
    from django.conf import settings
    from django.core.mail import send_mail 

    subject, content = parse_template(template_path, **kwargs)
    count = send_mail(subject, content, settings.DEFAULT_FROM_EMAIL,
                      [rcpt], fail_silently=True)
    return bool(count)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.