Statistics
| Branch: | Tag: | Revision:

root / snf-astakos-app / astakos / im / functions.py @ c37263ea

History | View | Annotate | Download (5.8 kB)

1
# Copyright 2011 GRNET S.A. All rights reserved.
2
# 
3
# Redistribution and use in source and binary forms, with or
4
# without modification, are permitted provided that the following
5
# conditions are met:
6
# 
7
#   1. Redistributions of source code must retain the above
8
#      copyright notice, this list of conditions and the following
9
#      disclaimer.
10
# 
11
#   2. Redistributions in binary form must reproduce the above
12
#      copyright notice, this list of conditions and the following
13
#      disclaimer in the documentation and/or other materials
14
#      provided with the distribution.
15
# 
16
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27
# POSSIBILITY OF SUCH DAMAGE.
28
# 
29
# The views and conclusions contained in the software and
30
# documentation are those of the authors and should not be
31
# interpreted as representing official policies, either expressed
32
# or implied, of GRNET S.A.
33

    
34
import logging
35

    
36
from django.utils.translation import ugettext as _
37
from django.template.loader import render_to_string
38
from django.core.mail import send_mail
39
from django.core.urlresolvers import reverse
40
from urllib import quote
41
from urlparse import urljoin
42
from random import randint
43

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

    
47
logger = logging.getLogger(__name__)
48

    
49
def send_verification(user, template_name='im/activation_email.txt'):
50
    """
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 is needed' % 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.
106
    
107
    Raises SMTPException, socket.error
108
    """
109
    subject = _('Welcome to %s alpha2 testing' % SITENAME)
110
    message = render_to_string(email_template_name, {
111
                'user': user,
112
                'url': urljoin(BASEURL, reverse('astakos.im.views.index')),
113
                'baseurl': BASEURL,
114
                'site_name': SITENAME,
115
                'support': DEFAULT_CONTACT_EMAIL})
116
    sender = DEFAULT_FROM_EMAIL
117
    send_mail(subject, message, sender, [user.email])
118
    logger.info('Sent greeting %s', user)
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

    
130
def _generate_invitation_code():
131
    while True:
132
        code = randint(1, 2L**63 - 1)
133
        try:
134
            Invitation.objects.get(code=code)
135
            # An invitation with this code already exists, try again
136
        except Invitation.DoesNotExist:
137
            return code
138

    
139
def invite(inviter, username, realname, email_template_name='im/welcome_email.txt'):
140
    """
141
    Send an invitation email and upon success reduces inviter's invitation by one.
142
    
143
    Raises SMTPException, socket.error
144
    """
145
    code = _generate_invitation_code()
146
    invitation = Invitation(inviter=inviter,
147
                            username=username,
148
                            code=code,
149
                            realname=realname)
150
    invitation.save()
151
    send_invitation(invitation, email_template_name)
152
    inviter.invitations = max(0, inviter.invitations - 1)
153
    inviter.save()