109 lines
3.7 KiB
Python
109 lines
3.7 KiB
Python
# passerelle-maarch
|
|
# 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 requests
|
|
from suds.client import Client
|
|
|
|
from django.core.urlresolvers import reverse
|
|
from django.views.generic import DetailView
|
|
from django.views.generic.edit import CreateView, UpdateView, DeleteView
|
|
from django.core.cache import cache
|
|
|
|
from passerelle import utils
|
|
|
|
from .soap import get_client, client_to_jsondict, recursive_asdict
|
|
from .models import MaarchManagement
|
|
from .forms import MaarchManagementForm, MaarchManagementUpdateForm
|
|
|
|
class MaarchManagementDetailView(DetailView):
|
|
model = MaarchManagement
|
|
template_name = 'passerelle_maarch/detail.html'
|
|
|
|
|
|
class MaarchManagementCreateView(CreateView):
|
|
model = MaarchManagement
|
|
form_class = MaarchManagementForm
|
|
template_name = 'passerelle/manage/service_form.html'
|
|
|
|
|
|
class MaarchManagementUpdateView(UpdateView):
|
|
model = MaarchManagement
|
|
form_class = MaarchManagementUpdateForm
|
|
template_name = 'passerelle/manage/service_form.html'
|
|
|
|
|
|
class MaarchManagementDeleteView(DeleteView):
|
|
model = MaarchManagement
|
|
template_name = 'passerelle/manage/service_confirm_delete.html'
|
|
|
|
def get_success_url(self):
|
|
return reverse('manage-home')
|
|
|
|
|
|
class MaarchDetailView(DetailView):
|
|
model = MaarchManagement
|
|
|
|
def get_client(self):
|
|
return get_client(maarch=self.get_object())
|
|
|
|
def get_data(self, request, *args, **kwargs):
|
|
raise NotImplementedError
|
|
|
|
@utils.protected_api('can_access')
|
|
def get(self, request, *args, **kwargs):
|
|
data = self.get_data(request, *args, **kwargs)
|
|
return utils.response_for_json(request, data)
|
|
|
|
|
|
class MaarchPingView(MaarchDetailView):
|
|
def get_data(self, request, *args, **kwargs):
|
|
client = self.get_client()
|
|
res = {'ping': 'pong'}
|
|
if 'debug' in request.GET:
|
|
res['client'] = client_to_jsondict(client)
|
|
return res
|
|
|
|
|
|
class MaarchResourceView(MaarchDetailView):
|
|
def get_data(self, request, resource_id=None, table_name=None, adr_table_name=None, *args, **kwargs):
|
|
client = self.get_client()
|
|
if resource_id:
|
|
if not adr_table_name:
|
|
adr_table_name = 'adr_x'
|
|
results = client.service.viewResource(int(resource_id),
|
|
table_name, adr_table_name, True)
|
|
else:
|
|
if 'where' in request.GET:
|
|
searchParams = client.factory.create('searchParams')
|
|
searchParams.whereClause = request.GET.get('where')
|
|
else:
|
|
searchParams = ''
|
|
results = client.service.Demo_searchResources(searchParams)
|
|
return recursive_asdict(results)
|
|
|
|
|
|
class MaarchContactView(MaarchDetailView):
|
|
def get_data(self, request, contact_id=None, *args, **kwargs):
|
|
client = self.get_client()
|
|
if contact_id:
|
|
searchParams = {'whereClause': "contact_id = '%s'" % contact_id}
|
|
elif 'where' in request.GET:
|
|
searchParams = {'whereClause': request.GET.get('where')}
|
|
else:
|
|
searchParams = ''
|
|
results = client.service.listContacts(searchParams)
|
|
return recursive_asdict(results)
|