welco/welco/contacts/views.py

76 lines
2.6 KiB
Python
Raw Normal View History

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-07-18 20:33:48 +02:00
from django import template
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
from django.views.generic import TemplateView
2015-09-24 12:28:56 +02:00
from welco.utils import get_wcs_data
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)
return context
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']
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)
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)
context['user_id'] = user_id
return context
contact_detail_fragment = ContactDetailFragmentView.as_view()