summaryrefslogtreecommitdiffstats
path: root/welco/contacts/views.py
blob: f2fe2d6bf19f5cb5dca8ce1254e1003623b93463 (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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# welco - multichannel request processing
# Copyright (C) 2015  Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

import json
import random
import requests
import time

from django import template
from django.conf import settings
from django.contrib.contenttypes.models import ContentType
from django.core.exceptions import PermissionDenied
from django.http import HttpResponse
from django.template import RequestContext
from django.views.decorators.csrf import csrf_exempt
from django.views.generic import TemplateView, FormView

from welco.utils import get_wcs_data, sign_url

from .forms import ContactAddForm

class HomeZone(object):
    def __init__(self, request):
        self.request = request

    def render(self):
        tmpl = template.loader.get_template('contacts/zone.html')
        return tmpl.render(request=self.request)


class ContactsZone(TemplateView):
    template_name = 'contacts/zone.html'

    def get_context_data(self, **kwargs):
        context = super(ContactsZone, self).get_context_data(**kwargs)
        context['source_pk'] = self.request.GET.get('source_pk')
        if 'source_pk' in self.request.GET:
            source_class = ContentType.objects.get(
                    id=self.request.GET['source_type']).model_class()
            source_object = source_class.objects.get(id=self.request.GET['source_pk'])
            context['contact_user_id'] = source_object.contact_id
        return context

    def post(self, request, *args, **kwargs):
        if 'user_id' in request.POST:
            source_class = ContentType.objects.get(
                    id=self.request.POST['source_type']).model_class()
            source_object = source_class.objects.get(id=self.request.POST['source_pk'])
            source_object.contact_id = request.POST['user_id']
            source_object.save()
        return HttpResponse('ok')

zone = csrf_exempt(ContactsZone.as_view())


def search_json(request):
    user_groups = set([x.name for x in request.user.groups.all()])
    for channel in settings.CHANNEL_ROLES:
        channel_groups = set(settings.CHANNEL_ROLES[channel])
        if user_groups.intersection(channel_groups):
            break
    else:
        raise PermissionDenied()

    query = request.GET.get('q')
    if query:
        result = get_wcs_data('api/users/', {'q': query, 'limit': 10})
        if result.get('err') != 0:
            raise Exception('error %r' % result)
        for user in result.get('data'):
            user['title'] = user['user_display_name']
            more = [user.get('user_var_address'), user.get('user_var_phone'),
                user.get('user_var_mobile'), user.get('user_var_email')]
            user['more'] = ' / '.join([x for x in more if x])
            if user.get('user_roles'):
                user['roles'] = ' / '.join([r['text'] for r in user['user_roles']])
            user['slug'] = 'user-%s' % user['user_id']
    else:
        result = {'data': []}
    response = HttpResponse(content_type='application/json')
    json.dump(result, response, indent=2)
    return response


class ContactDetailFragmentView(TemplateView):
    template_name = 'contacts/contact_detail_fragment.html'

    def get_context_data(self, **kwargs):
        context = super(ContactDetailFragmentView, self).get_context_data(**kwargs)

        user_id = self.kwargs.get('slug').split('-')[-1]
        user_details = get_wcs_data('api/users/%s/' % user_id)
        context.update(user_details)
        context['forms'] = get_wcs_data('api/users/%s/forms' % user_id).get('data') or []
        context['user_id'] = user_id

        if 'source_pk' in self.request.GET:
            source_class = ContentType.objects.get(
                    id=self.request.GET['source_type']).model_class()
            source_object = source_class.objects.get(id=self.request.GET['source_pk'])
            context['is_pinned_user'] = bool(source_object.contact_id == user_id)

        return context

contact_detail_fragment = ContactDetailFragmentView.as_view()


class ContactAdd(FormView):
    template_name = 'contacts/contact_add.html'
    form_class = ContactAddForm

    def post(self, request):
        form = self.get_form(self.get_form_class())
        msg = {}
        for field_key in form.fields:
            if form[field_key].value():
                msg[field_key] = form[field_key].value()

        msg['password'] = str(random.SystemRandom().random())
        msg['send_registration_email'] = getattr(settings, 'CONTACT_SEND_REGISTRATION_EMAIL', True)

        authentic_site = settings.KNOWN_SERVICES.get('authentic').values()[0]
        authentic_url = authentic_site.get('url')
        authentic_orig = authentic_site.get('orig')
        authentic_secret = authentic_site.get('secret')

        url = authentic_url + 'api/users/?orig=%s' % authentic_orig
        signed_url = sign_url(url, authentic_secret)

        authentic_response = requests.post(
                signed_url,
                data=json.dumps(msg),
                headers={'Content-type': 'application/json'})

        user_uuid = authentic_response.json().get('uuid')
        for i in range(100):
            try:
                user_details = get_wcs_data('api/users/%s/' % user_uuid)
                result = {'data': {'user_id': user_details.get('id')}}
                break
            except requests.HTTPError as e:
                if e.response.status_code != 404:
                    raise
            time.sleep(0.1)
        else:
            result = {'err': 1, 'data': 'timeout when calling wcs'}
        response = HttpResponse(content_type='application/json')
        json.dump(result, response, indent=2)
        return response

contact_add = csrf_exempt(ContactAdd.as_view())