1 # Copyright 2011 GRNET S.A. All rights reserved.
3 # Redistribution and use in source and binary forms, with or
4 # without modification, are permitted provided that the following
7 # 1. Redistributions of source code must retain the above
8 # copyright notice, this list of conditions and the following
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.
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.
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.
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
44 from astakos.im.settings import DEFAULT_CONTACT_EMAIL, DEFAULT_FROM_EMAIL, SITENAME, BASEURL, DEFAULT_ADMIN_EMAIL
45 from astakos.im.models import Invitation
47 logger = logging.getLogger(__name__)
49 def send_verification(user, template_name='im/activation_email.txt'):
51 Send email to user to verify his/her email and activate his/her account.
53 Raises SMTPException, socket.error
55 url = '%s?auth=%s&next=%s' % (urljoin(BASEURL, reverse('astakos.im.views.activate')),
56 quote(user.auth_token),
58 message = render_to_string(template_name, {
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)
68 def send_notification(user, template_name='im/admin_notification.txt'):
70 Send email to DEFAULT_ADMIN_EMAIL to notify for a new user registration.
72 Raises SMTPException, socket.error
74 if not DEFAULT_ADMIN_EMAIL:
76 message = render_to_string(template_name, {
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)
85 def send_invitation(invitation, template_name='im/invitation.txt'):
87 Send invitation email.
89 Raises SMTPException, socket.error
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,
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)
103 def send_greeting(user, email_template_name='im/welcome_email.txt'):
107 Raises SMTPException, socket.error
109 subject = _('Welcome to %s alpha2 testing' % SITENAME)
110 message = render_to_string(email_template_name, {
112 'url': urljoin(BASEURL, reverse('astakos.im.views.index')),
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)
120 def activate(user, email_template_name='im/welcome_email.txt'):
122 Activates the specific user and sends email.
124 Raises SMTPException, socket.error
126 user.is_active = True
128 send_greeting(user, email_template_name)
130 def _generate_invitation_code():
132 code = randint(1, 2L**63 - 1)
134 Invitation.objects.get(code=code)
135 # An invitation with this code already exists, try again
136 except Invitation.DoesNotExist:
139 def invite(inviter, username, realname, email_template_name='im/welcome_email.txt'):
141 Send an invitation email and upon success reduces inviter's invitation by one.
143 Raises SMTPException, socket.error
145 code = _generate_invitation_code()
146 invitation = Invitation(inviter=inviter,
151 send_invitation(invitation, email_template_name)
152 inviter.invitations = max(0, inviter.invitations - 1)