views.py 3.93 KB
Newer Older
1 2
import logging

3 4 5
from django.conf import settings
from django.core.mail import send_mail
from django.contrib.sites.models import Site
6
from django.shortcuts import render
7 8
from django.template.loader import render_to_string
from django.utils.translation import ugettext_lazy as _
9 10 11
from django.contrib.auth.models import User
from django.views.decorators.cache import never_cache
from django import forms
12
from registration.models import RegistrationProfile
13 14 15 16
from accounts.models import UserProfile

from edumanage.forms import UserProfileForm
from edumanage.models import Institution
17 18

logger = logging.getLogger(__name__)
19

20

21
@never_cache
22 23 24
def activate(request, activation_key):
    account = None
    if request.method == "GET":
25 26
        # Normalize before trying anything with it.
        activation_key = activation_key.lower()
27 28 29
        try:
            rp = RegistrationProfile.objects.get(activation_key=activation_key)
        except RegistrationProfile.DoesNotExist:
30 31 32 33 34 35 36 37
            return render(
                request,
                'registration/activate.html',
                {
                    'account': account,
                    'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS
                }
            )
38
        try:
39
            user_profile = rp.user.get_profile()
40
        except UserProfile.DoesNotExist:
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
            return render(
                request,
                'registration/activate.html',
                {
                    'account': account,
                    'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS
                }
            )
        form = UserProfileForm(instance=user_profile)
        form.fields['user'] = forms.ModelChoiceField(
            queryset=User.objects.filter(pk=rp.user.pk), empty_label=None
        )
        form.fields['institution'] = forms.ModelChoiceField(
            queryset=Institution.objects.all(), empty_label=None
        )
        return render(
            request,
            'registration/activate_edit.html',
            {
                'account': account,
                'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS,
                'form': form
            }
        )
65 66 67 68 69
    if request.method == "POST":
        request_data = request.POST.copy()
        try:
            user = User.objects.get(pk=request_data['user'])
            up = user.get_profile()
70 71 72
            up.institution = Institution.objects.get(
                pk=request_data['institution']
            )
73
            up.save()
Kroustouris Stauros's avatar
Kroustouris Stauros committed
74

75
        except:
76 77 78 79 80 81 82 83 84 85
            return render(
                request,
                'registration/activate_edit.html',
                {
                    'account': account,
                    'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS
                }
            )
        # Normalize before trying anything with it.
        activation_key = activation_key.lower()
86 87 88
        try:
            rp = RegistrationProfile.objects.get(activation_key=activation_key)
            account = RegistrationProfile.objects.activate_user(activation_key)
89 90
            up.is_social_active = True
            up.save()
91
            logger.info('Activating user %s' % rp.user.username)
92
        except Exception as e:
93
            logger.info('An error occured: %s' % e)
94
            pass
Kroustouris Stauros's avatar
Kroustouris Stauros committed
95

96 97
        if account:
            # A user has been activated
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
            email = render_to_string(
                'registration/activation_complete.txt',
                {
                    'site': Site.objects.get_current(),
                    'user': account
                }
            )
            send_mail(
                _('%sUser account activated') % settings.EMAIL_SUBJECT_PREFIX,
                email,
                settings.SERVER_EMAIL,
                account.email.split(';')
            )
        return render(
            request,
            'registration/activate.html',
            {
                'account': account,
                'expiration_days': settings.ACCOUNT_ACTIVATION_DAYS
            }
        )