passerelle-imio-tax-compute/passerelle_imio_tax_compute/views.py

70 lines
2.2 KiB
Python

# passerelle-imio-tax-compute - 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 json
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 ImioTsTaxCompute
from .forms import ImioTsTaxComputeForm
class SigCreateView(CreateView):
model = ImioTsTaxCompute
form_class = ImioTsTaxComputeForm
template_name = 'passerelle/manage/service_form.html'
class SigUpdateView(UpdateView):
model = ImioTsTaxCompute
form_class = ImioTsTaxComputeForm
class SigDeleteView(DeleteView):
model = ImioTsTaxCompute
def get_success_url(self):
return reverse('manage-home')
class SigDetailView(DetailView):
model = ImioTsTaxCompute
class EvalView(View, SingleObjectMixin):
model = ImioTsTaxCompute
def get(self, request, *args, **kwargs):
return self.post(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
data = dict([(x, request.GET[x]) for x in request.GET.keys()])
if request.body:
payload = json.loads(request.body)
data.update(payload.get("fields"))
expression = self.get_object().formule
code_object = compile(expression, "<string>", "exec")
eval(code_object, data)
result = data.get("result")
return utils.response_for_json(request, {'data': result})