84 lines
2.8 KiB
Python
84 lines
2.8 KiB
Python
# passerelle-imio-liege-lisrue - passerelle connector to Lisrue webservice
|
|
# Copyright (C) 2016 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 requests
|
|
import unicodedata
|
|
|
|
from django.views.generic.detail import SingleObjectMixin, DetailView
|
|
from django.views.generic.edit import CreateView, UpdateView, DeleteView
|
|
from django.views.generic.base import View, RedirectView
|
|
from django.core.urlresolvers import reverse
|
|
from django.http import HttpResponseBadRequest
|
|
|
|
from passerelle import utils
|
|
|
|
from .models import ImioLiegeLisrue
|
|
from .forms import ImioLiegeLisrueForm
|
|
|
|
class SigCreateView(CreateView):
|
|
model = ImioLiegeLisrue
|
|
form_class = ImioLiegeLisrueForm
|
|
template_name = 'passerelle/manage/service_form.html'
|
|
|
|
|
|
class SigUpdateView(UpdateView):
|
|
model = ImioLiegeLisrue
|
|
form_class = ImioLiegeLisrueForm
|
|
|
|
|
|
class SigDeleteView(DeleteView):
|
|
model = ImioLiegeLisrue
|
|
|
|
def get_success_url(self):
|
|
return reverse('manage-home')
|
|
|
|
|
|
class SigDetailView(DetailView):
|
|
model = ImioLiegeLisrue
|
|
|
|
|
|
class VoiesView(View, SingleObjectMixin):
|
|
model = ImioLiegeLisrue
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
url = self.get_object().service_url
|
|
url += 'jsonlisrue/'
|
|
|
|
if 'q' in request.GET and request.GET['q']:
|
|
q = unicodedata.normalize('NFKD', request.GET['q']).encode('ascii', 'ignore')
|
|
url += q.lower()
|
|
|
|
result = requests.get(url, headers={'Accept': 'application/json'},
|
|
verify=self.get_object().verify_cert).json()
|
|
if isinstance(result['findResponse'], list):
|
|
lisrues = result['findResponse']
|
|
elif isinstance(result['findResponse'], dict) and 'return' in result['findResponse']:
|
|
lisrues = [result['findResponse']['return']]
|
|
else:
|
|
lisrues = []
|
|
|
|
streets = []
|
|
for item in lisrues:
|
|
name = item.get('particuleMinuscule') or ''
|
|
if name and not name.endswith("'"):
|
|
name += ' '
|
|
name += item.get('libelleMinuscule')
|
|
streets.append({
|
|
'id': item.get('id'),
|
|
'text': name})
|
|
|
|
return utils.response_for_json(request, {'data': streets})
|