2015-07-18 20:33:48 +02:00
|
|
|
# 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/>.
|
|
|
|
|
2015-09-24 12:28:56 +02:00
|
|
|
import json
|
2015-11-16 14:32:31 +01:00
|
|
|
import random
|
|
|
|
import requests
|
|
|
|
import time
|
2015-09-24 12:28:56 +02:00
|
|
|
|
2015-07-18 20:33:48 +02:00
|
|
|
from django import template
|
2015-11-16 14:32:31 +01:00
|
|
|
from django.conf import settings
|
2015-09-29 18:05:27 +02:00
|
|
|
from django.contrib.contenttypes.models import ContentType
|
2015-09-24 12:28:56 +02:00
|
|
|
from django.http import HttpResponse
|
2015-07-18 20:33:48 +02:00
|
|
|
from django.template import RequestContext
|
|
|
|
from django.views.decorators.csrf import csrf_exempt
|
2015-11-16 10:19:44 +01:00
|
|
|
from django.views.generic import TemplateView, FormView
|
2015-07-18 20:33:48 +02:00
|
|
|
|
2015-09-29 18:05:27 +02:00
|
|
|
from welco.qualif.models import Association
|
2015-11-16 14:32:31 +01:00
|
|
|
from welco.utils import get_wcs_data, get_authentic_url
|
2015-09-24 12:28:56 +02:00
|
|
|
|
2015-11-16 10:19:44 +01:00
|
|
|
from .forms import ContactAddForm
|
|
|
|
|
2015-07-18 20:33:48 +02:00
|
|
|
class HomeZone(object):
|
|
|
|
def __init__(self, request):
|
|
|
|
self.request = request
|
|
|
|
|
|
|
|
def render(self):
|
|
|
|
context = RequestContext(self.request)
|
|
|
|
tmpl = template.loader.get_template('contacts/zone.html')
|
|
|
|
return tmpl.render(context)
|
|
|
|
|
|
|
|
|
|
|
|
class ContactsZone(TemplateView):
|
|
|
|
template_name = 'contacts/zone.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(ContactsZone, self).get_context_data(**kwargs)
|
2015-09-29 18:05:27 +02:00
|
|
|
if 'source_pk' in self.request.GET:
|
2015-10-09 14:01:59 +02:00
|
|
|
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
|
2015-07-18 20:33:48 +02:00
|
|
|
return context
|
|
|
|
|
2015-10-09 14:01:59 +02:00
|
|
|
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')
|
|
|
|
|
2015-07-18 20:33:48 +02:00
|
|
|
zone = csrf_exempt(ContactsZone.as_view())
|
|
|
|
|
|
|
|
|
|
|
|
def search_json(request):
|
|
|
|
query = request.GET.get('q')
|
2015-09-24 12:28:56 +02:00
|
|
|
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']
|
2015-09-25 10:28:14 +02:00
|
|
|
more = [user.get('user_var_address'), user.get('user_var_phone'),
|
|
|
|
user.get('user_var_birthdate')]
|
|
|
|
user['more'] = ' / '.join([x for x in more if x])
|
2015-09-24 12:28:56 +02:00
|
|
|
user['slug'] = 'user-%s' % user['user_id']
|
|
|
|
else:
|
|
|
|
result = {'data': []}
|
2015-07-18 20:33:48 +02:00
|
|
|
response = HttpResponse(content_type='application/json')
|
2015-09-24 12:28:56 +02:00
|
|
|
json.dump(result, response, indent=2)
|
2015-07-18 20:33:48 +02:00
|
|
|
return response
|
2015-09-24 12:28:56 +02:00
|
|
|
|
|
|
|
|
|
|
|
class ContactDetailFragmentView(TemplateView):
|
|
|
|
template_name = 'contacts/contact_detail_fragment.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super(ContactDetailFragmentView, self).get_context_data(**kwargs)
|
2015-10-28 17:04:23 +01:00
|
|
|
|
2015-09-24 12:28:56 +02:00
|
|
|
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)
|
2015-11-10 12:04:01 +01:00
|
|
|
print context['forms']
|
2015-09-24 12:28:56 +02:00
|
|
|
context['user_id'] = user_id
|
2015-10-28 17:04:23 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2015-09-24 12:28:56 +02:00
|
|
|
return context
|
|
|
|
|
|
|
|
contact_detail_fragment = ContactDetailFragmentView.as_view()
|
2015-11-16 10:19:44 +01:00
|
|
|
|
|
|
|
|
|
|
|
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:
|
2015-11-16 14:32:31 +01:00
|
|
|
if form[field_key].value():
|
|
|
|
msg[field_key] = form[field_key].value()
|
|
|
|
|
|
|
|
msg['password'] = str(random.SystemRandom().random())
|
|
|
|
|
|
|
|
authentic_response = requests.post(
|
|
|
|
get_authentic_url() + 'api/users/',
|
|
|
|
data=json.dumps(msg),
|
|
|
|
headers={'Content-type': 'application/json'},
|
|
|
|
auth=settings.AUTHENTIC_AUTH_TUPLE)
|
|
|
|
|
|
|
|
user_uuid = authentic_response.json().get('uuid')
|
2015-11-17 15:30:48 +01:00
|
|
|
for i in range(50):
|
2015-11-16 14:32:31 +01:00
|
|
|
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'}
|
2015-11-16 10:19:44 +01:00
|
|
|
response = HttpResponse(content_type='application/json')
|
|
|
|
json.dump(result, response, indent=2)
|
|
|
|
return response
|
|
|
|
|
|
|
|
contact_add = csrf_exempt(ContactAdd.as_view())
|