Revision 683cf244 snf-astakos-app/astakos/im/functions.py

b/snf-astakos-app/astakos/im/functions.py
37 37
from django.template.loader import render_to_string
38 38
from django.core.mail import send_mail
39 39
from django.core.urlresolvers import reverse
40
from urllib import quote
40 41
from urlparse import urljoin
41 42
from random import randint
42 43

  
43
from astakos.im.settings import DEFAULT_CONTACT_EMAIL, DEFAULT_FROM_EMAIL, SITENAME, BASEURL
44
from astakos.im.settings import DEFAULT_CONTACT_EMAIL, DEFAULT_FROM_EMAIL, SITENAME, BASEURL, DEFAULT_ADMIN_EMAIL
44 45
from astakos.im.models import Invitation
45 46

  
46 47
logger = logging.getLogger(__name__)
47 48

  
48
def activate(user, email_template_name='im/welcome_email.txt'):
49
def send_verification(user, template_name='im/activation_email.txt'):
49 50
    """
50
    Activates the specific user and sends email.
51
    Send email to user to verify his/her email and activate his/her account.
52
    
53
    Raises SMTPException, socket.error
54
    """
55
    url = '%s?auth=%s&next=%s' % (urljoin(BASEURL, reverse('astakos.im.views.activate')),
56
                                    quote(user.auth_token),
57
                                    quote(BASEURL))
58
    message = render_to_string(template_name, {
59
            'user': user,
60
            'url': url,
61
            'baseurl': BASEURL,
62
            'site_name': SITENAME,
63
            'support': DEFAULT_CONTACT_EMAIL})
64
    sender = DEFAULT_FROM_EMAIL
65
    send_mail('%s  alpha2 testing account activation' % SITENAME, message, sender, [user.email])
66
    logger.info('Sent activation %s', user)
67

  
68
def send_notification(user, template_name='im/admin_notification.txt'):
69
    """
70
    Send email to DEFAULT_ADMIN_EMAIL to notify for a new user registration.
71
    
72
    Raises SMTPException, socket.error
73
    """
74
    if not DEFAULT_ADMIN_EMAIL:
75
        return
76
    message = render_to_string(template_name, {
77
            'user': user,
78
            'baseurl': BASEURL,
79
            'site_name': SITENAME,
80
            'support': DEFAULT_CONTACT_EMAIL})
81
    sender = DEFAULT_FROM_EMAIL
82
    send_mail('%s  alpha2 testing account notification' % SITENAME, message, sender, [DEFAULT_ADMIN_EMAIL])
83
    logger.info('Sent admin notification for user %s', user)
84

  
85
def send_invitation(invitation, template_name='im/invitation.txt'):
86
    """
87
    Send invitation email.
88
    
89
    Raises SMTPException, socket.error
90
    """
91
    subject = _('Invitation to %s alpha2 testing' % SITENAME)
92
    url = '%s?code=%d' % (urljoin(BASEURL, reverse('astakos.im.views.signup')), invitation.code)
93
    message = render_to_string('im/invitation.txt', {
94
                'invitation': invitation,
95
                'url': url,
96
                'baseurl': BASEURL,
97
                'site_name': SITENAME,
98
                'support': DEFAULT_CONTACT_EMAIL})
99
    sender = DEFAULT_FROM_EMAIL
100
    send_mail(subject, message, sender, [invitation.username])
101
    logger.info('Sent invitation %s', invitation)
102

  
103
def send_greeting(user, email_template_name='im/welcome_email.txt'):
104
    """
105
    Send welcome email.
51 106
    
52 107
    Raises SMTPException, socket.error
53 108
    """
54
    user.is_active = True
55
    user.save()
56 109
    subject = _('Welcome to %s alpha2 testing' % SITENAME)
57 110
    message = render_to_string(email_template_name, {
58 111
                'user': user,
......
64 117
    send_mail(subject, message, sender, [user.email])
65 118
    logger.info('Sent greeting %s', user)
66 119

  
120
def activate(user, email_template_name='im/welcome_email.txt'):
121
    """
122
    Activates the specific user and sends email.
123
    
124
    Raises SMTPException, socket.error
125
    """
126
    user.is_active = True
127
    user.save()
128
    send_greeting(user, email_template_name)
129

  
67 130
def _generate_invitation_code():
68 131
    while True:
69 132
        code = randint(1, 2L**63 - 1)
......
73 136
        except Invitation.DoesNotExist:
74 137
            return code
75 138

  
76
def invite(inviter, username, realname):
139
def invite(inviter, username, realname, email_template_name='im/welcome_email.txt'):
77 140
    """
78 141
    Send an invitation email and upon success reduces inviter's invitation by one.
79 142
    
......
85 148
                            code=code,
86 149
                            realname=realname)
87 150
    invitation.save()
88
    subject = _('Invitation to %s alpha2 testing' % SITENAME)
89
    url = '%s?code=%d' % (urljoin(BASEURL, reverse('astakos.im.views.signup')), code)
90
    message = render_to_string('im/invitation.txt', {
91
                'invitation': invitation,
92
                'url': url,
93
                'baseurl': BASEURL,
94
                'service': SITENAME,
95
                'support': DEFAULT_CONTACT_EMAIL})
96
    sender = DEFAULT_FROM_EMAIL
97
    send_mail(subject, message, sender, [invitation.username])
98
    logger.info('Sent invitation %s', invitation)
151
    send_invitation(invitation, email_template_name)
99 152
    inviter.invitations = max(0, inviter.invitations - 1)
100 153
    inviter.save()

Also available in: Unified diff