summaryrefslogtreecommitdiffstats
path: root/django/sp_sso/invite/views.py
blob: e5b21fd32f4cb340a0b7dfbef4f52c8d8503be9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
import logging
from django.views.generic import FormView
from django.utils.translation import ugettext_lazy as _

from .forms import InvitationForm
from .utils import do_invite, get_invitaton_attributes_mapping, \
        get_affectations_from_eppn

from saml.utils import render_message


logger = logging.getLogger('django')

MSG_INVITATION_SENT = _("Your invitation has been sent.")


def invitation_sent(request):
    return render_message(request, MSG_INVITATION_SENT)


class InvitationFormView(FormView):
    form_class = InvitationForm
    template_name = 'invite/invitation_form.html'
    success_url = '/invite/sent'

    def get_initial(self):
        initial = super(InvitationFormView, self).get_initial()

        # User is logged in the identity provider
        if 'mellon_session' in self.request.session:

            data = self.request.session['mellon_session']
            # Fetch SSO attributes
            for attribute_key, attribute_value in \
                    get_invitaton_attributes_mapping().items():
                if data.get(attribute_key):
                    attribute_element = data.get(attribute_key)[0]
                    initial[attribute_value] = attribute_element

            etablissement, unite = get_affectations_from_eppn(
                    data.get('ep_principal_name',[None])[0])

            initial['hote_etablissement'] = etablissement
            initial['hote_unite'] = unite
            initial['hote_commentaire'] = '''EduPersonPrincipalName de 
                    l\'invitant'''
        return initial

    def form_valid(self, form):
        invitation = {}
        post_dict = self.request.POST
        logger.info("Invitation POST data : {}".format(''))

        # Craft any relevant invitation data
        for form_attribute in self.form_class.base_fields.keys():
            sanitized_form_entry = post_dict.get(form_attribute, False)
            if sanitized_form_entry:
                invitation[form_attribute] = sanitized_form_entry

        multiple_email_invitation = {}

        # Send as many invites as there are email adresses (blank-separated)
        for email in invitation['email'].split(' '):
            multiple_email_invitation = invitation.copy()
            multiple_email_invitation['email'] = email
            do_invite(multiple_email_invitation)

        return super(InvitationFormView, self).form_valid(form)