Statistics
| Branch: | Tag: | Revision:

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

History | View | Annotate | Download (3.9 kB)

1 f36af44a Antony Chazapis
# Copyright 2011 GRNET S.A. All rights reserved.
2 f36af44a Antony Chazapis
# 
3 f36af44a Antony Chazapis
# Redistribution and use in source and binary forms, with or
4 f36af44a Antony Chazapis
# without modification, are permitted provided that the following
5 f36af44a Antony Chazapis
# conditions are met:
6 f36af44a Antony Chazapis
# 
7 f36af44a Antony Chazapis
#   1. Redistributions of source code must retain the above
8 f36af44a Antony Chazapis
#      copyright notice, this list of conditions and the following
9 f36af44a Antony Chazapis
#      disclaimer.
10 f36af44a Antony Chazapis
# 
11 f36af44a Antony Chazapis
#   2. Redistributions in binary form must reproduce the above
12 f36af44a Antony Chazapis
#      copyright notice, this list of conditions and the following
13 f36af44a Antony Chazapis
#      disclaimer in the documentation and/or other materials
14 f36af44a Antony Chazapis
#      provided with the distribution.
15 f36af44a Antony Chazapis
# 
16 f36af44a Antony Chazapis
# THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
17 f36af44a Antony Chazapis
# OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 f36af44a Antony Chazapis
# WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
19 f36af44a Antony Chazapis
# PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
20 f36af44a Antony Chazapis
# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 f36af44a Antony Chazapis
# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 f36af44a Antony Chazapis
# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
23 f36af44a Antony Chazapis
# USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
24 f36af44a Antony Chazapis
# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25 f36af44a Antony Chazapis
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
26 f36af44a Antony Chazapis
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 f36af44a Antony Chazapis
# POSSIBILITY OF SUCH DAMAGE.
28 f36af44a Antony Chazapis
# 
29 f36af44a Antony Chazapis
# The views and conclusions contained in the software and
30 f36af44a Antony Chazapis
# documentation are those of the authors and should not be
31 f36af44a Antony Chazapis
# interpreted as representing official policies, either expressed
32 f36af44a Antony Chazapis
# or implied, of GRNET S.A.
33 f36af44a Antony Chazapis
34 f36af44a Antony Chazapis
import logging
35 f36af44a Antony Chazapis
36 f36af44a Antony Chazapis
from django.utils.translation import ugettext as _
37 f36af44a Antony Chazapis
from django.template.loader import render_to_string
38 f36af44a Antony Chazapis
from django.core.mail import send_mail
39 f36af44a Antony Chazapis
from django.core.urlresolvers import reverse
40 f36af44a Antony Chazapis
from urlparse import urljoin
41 f36af44a Antony Chazapis
from random import randint
42 f36af44a Antony Chazapis
43 eeb3fe84 Sofia Papagiannaki
from astakos.im.settings import DEFAULT_CONTACT_EMAIL, DEFAULT_FROM_EMAIL, SITENAME, BASEURL
44 f36af44a Antony Chazapis
from astakos.im.models import Invitation
45 f36af44a Antony Chazapis
46 f36af44a Antony Chazapis
logger = logging.getLogger(__name__)
47 f36af44a Antony Chazapis
48 fc28d013 Sofia Papagiannaki
def activate(user, email_template_name='im/welcome_email.txt'):
49 f36af44a Antony Chazapis
    """
50 f36af44a Antony Chazapis
    Activates the specific user and sends email.
51 f36af44a Antony Chazapis
    
52 f36af44a Antony Chazapis
    Raises SMTPException, socket.error
53 f36af44a Antony Chazapis
    """
54 f36af44a Antony Chazapis
    user.is_active = True
55 f36af44a Antony Chazapis
    user.save()
56 f36af44a Antony Chazapis
    subject = _('Welcome to %s' % SITENAME)
57 f36af44a Antony Chazapis
    message = render_to_string(email_template_name, {
58 f36af44a Antony Chazapis
                'user': user,
59 fc28d013 Sofia Papagiannaki
                'url': urljoin(BASEURL, reverse('astakos.im.views.index')),
60 f36af44a Antony Chazapis
                'baseurl': BASEURL,
61 f36af44a Antony Chazapis
                'site_name': SITENAME,
62 ef0c73c4 Sofia Papagiannaki
                'support': DEFAULT_CONTACT_EMAIL})
63 d552ecb7 Antony Chazapis
    sender = DEFAULT_FROM_EMAIL
64 f36af44a Antony Chazapis
    send_mail(subject, message, sender, [user.email])
65 f36af44a Antony Chazapis
    logger.info('Sent greeting %s', user)
66 f36af44a Antony Chazapis
67 f36af44a Antony Chazapis
def _generate_invitation_code():
68 f36af44a Antony Chazapis
    while True:
69 f36af44a Antony Chazapis
        code = randint(1, 2L**63 - 1)
70 f36af44a Antony Chazapis
        try:
71 f36af44a Antony Chazapis
            Invitation.objects.get(code=code)
72 f36af44a Antony Chazapis
            # An invitation with this code already exists, try again
73 f36af44a Antony Chazapis
        except Invitation.DoesNotExist:
74 f36af44a Antony Chazapis
            return code
75 f36af44a Antony Chazapis
76 f36af44a Antony Chazapis
def invite(inviter, username, realname):
77 f36af44a Antony Chazapis
    """
78 f36af44a Antony Chazapis
    Send an invitation email and upon success reduces inviter's invitation by one.
79 f36af44a Antony Chazapis
    
80 f36af44a Antony Chazapis
    Raises SMTPException, socket.error
81 f36af44a Antony Chazapis
    """
82 f36af44a Antony Chazapis
    code = _generate_invitation_code()
83 f36af44a Antony Chazapis
    invitation = Invitation(inviter=inviter,
84 f36af44a Antony Chazapis
                            username=username,
85 f36af44a Antony Chazapis
                            code=code,
86 f36af44a Antony Chazapis
                            realname=realname)
87 f36af44a Antony Chazapis
    invitation.save()
88 f36af44a Antony Chazapis
    subject = _('Invitation to %s' % SITENAME)
89 f36af44a Antony Chazapis
    url = '%s?code=%d' % (urljoin(BASEURL, reverse('astakos.im.views.signup')), code)
90 f36af44a Antony Chazapis
    message = render_to_string('im/invitation.txt', {
91 f36af44a Antony Chazapis
                'invitation': invitation,
92 f36af44a Antony Chazapis
                'url': url,
93 f36af44a Antony Chazapis
                'baseurl': BASEURL,
94 f36af44a Antony Chazapis
                'service': SITENAME,
95 ef0c73c4 Sofia Papagiannaki
                'support': DEFAULT_CONTACT_EMAIL})
96 d552ecb7 Antony Chazapis
    sender = DEFAULT_FROM_EMAIL
97 f36af44a Antony Chazapis
    send_mail(subject, message, sender, [invitation.username])
98 f36af44a Antony Chazapis
    logger.info('Sent invitation %s', invitation)
99 f36af44a Antony Chazapis
    inviter.invitations = max(0, inviter.invitations - 1)
100 d552ecb7 Antony Chazapis
    inviter.save()