add CNUD views (#10076)

This commit is contained in:
Josue Kouka 2016-02-25 18:06:44 +01:00
parent f4ce873eb2
commit e150ca6d12
2 changed files with 167 additions and 0 deletions

View File

@ -0,0 +1,162 @@
# passerelle - uniform access to multiple data sources and services
# Copyright (C) 2015-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 json
import logging
import requests
from django.utils.decorators import method_decorator
from django.views.decorators.csrf import csrf_exempt
from django.views.generic.base import View
from django.views.generic.detail import SingleObjectMixin
from django.http import HttpResponse
from .models import ParisPocGru
from passerelle import utils
class MissingKeyException(Exception):
http_status = 400
class DemandNotFoundException(Exception):
http_status = 404
class DemandBaseView(View, SingleObjectMixin):
model = ParisPocGru
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
url_prefix = '/notification/v0'
self.ws = self.get_object()
self.ws.get_access_token()
self.ws.url += url_prefix
self.json_data = json.loads(request.body)
return super(DemandBaseView, self).dispatch(request, *args, **kwargs)
def validate_data(self, fields):
"""Validate post data
"""
payload = {}
missing_key = set(fields).difference(set(self.json_data.get('fields').keys()))
if missing_key:
raise MissingKeyException('%s is required' %(missing_key.pop()))
for field in fields:
payload[field] = self.json_data.get('fields')[field]
return payload
def check_request_response(self, response):
try:
error = response.json().get('error', None)
if error:
raise DemandNotFoundException('demand not found')
except (AttributeError,):
return response
class CreateDemandView(DemandBaseView):
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super(CreateDemandView, self).dispatch(request, *args, **kwargs)
@utils.to_json('api')
def post(self, request, *args, **kwargs):
data = self.validate_data([
'id_status_crm','id_demand_type',
'status_text', 'user_guid'
])
response = requests.post(self.ws.url + '/demand/create',
headers = self.ws.headers,
data = data
)
demand_id = response.json()
if demand_id < 0 : # -1, creation failed
return {'err': 'demand creation failed'}
display_id = response.json()
return {'display_id': display_id}
class UpdateDemandView(DemandBaseView):
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super(UpdateDemandView, self).dispatch(request, *args, **kwargs)
@utils.to_json('api')
def post(self, request, *args, **kwargs):
data = self.validate_data([
'id_demand', 'id_status_crm', 'status_text'
])
response = requests.post(self.ws.url + '/demand/update',
headers = self.ws.headers,
data = data
)
response = self.check_request_response(response)
return {'display_id': response.json()}
class NotifyDemandView(DemandBaseView):
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super(NotifyDemandView, self).dispatch(request, *args, **kwargs)
@utils.to_json('api')
def post(self, request, *args, **kwargs):
data = self.validate_data([
'id_demand', 'notification_object',
'notification_message', 'notification_sender'
])
response = requests.post(self.ws.url + '/demand/notify',
headers = self.ws.headers,
data = data
)
response = self.check_request_response(response)
return {'display_id': response.json()}
class DeleteDemandView(DemandBaseView):
@method_decorator(csrf_exempt)
def dispatch(self, request, *args, **kwargs):
return super(DeleteDemandView, self).dispatch(request, *args, **kwargs)
@utils.to_json('api')
def post(self, request, *args, **kwargs):
data = self.validate_data(['id_demand'])
response = requests.post(self.ws.url + '/demand/delete',
headers = self.ws.headers,
data = data
)
response = self.check_request_response(response)
return {'display_id': response.json()}

View File

@ -20,11 +20,16 @@ from django.contrib.auth.decorators import login_required
from passerelle.urls_utils import decorated_includes, required, app_enabled
from views import *
from out_views import *
public_urlpatterns = patterns('',
url(r'^(?P<slug>[\w,-]+)/$', ParisPocGruDetailView.as_view(), name='paris-poc-gru-view'),
url(r'^(?P<slug>[\w,-]+)/endpoint/$', EndpointView.as_view(), name='paris-poc-gru-endpoint'),
url(r'^(?P<slug>[\w,-]+)/create/$', CreateDemandView.as_view(), name='paris-poc-gru-create-demand'),
url(r'^(?P<slug>[\w,-]+)/update/$', UpdateDemandView.as_view(), name='paris-poc-gru-update-demand'),
url(r'^(?P<slug>[\w,-]+)/notify/$', NotifyDemandView.as_view(), name='paris-poc-gru-notify-demand'),
url(r'^(?P<slug>[\w,-]+)/delete/$', DeleteDemandView.as_view(), name='paris-poc-gru-delete-demand'),
)
management_urlpatterns = patterns('',