clean some js

- clean buttons code on account and checkout
- merge CheckoutRead and kpsul_checkout_data views (the first won)

APIModelObject interface
- add url_create, url_update, url_update_for
- rename url_object_for and url_object to url_read_for and url_read
This commit is contained in:
Aurélien Delobelle 2017-04-05 03:43:51 +02:00
parent 9d2298a089
commit efbcde163b
6 changed files with 155 additions and 81 deletions

View file

@ -52,6 +52,33 @@ from .statistic import daynames, monthnames, weeknames, \
this_morning, this_monday_morning, this_first_month_day, \
tot_ventes
# source : docs.djangoproject.com/fr/1.10/topics/class-based-views/mixins/
class JSONResponseMixin(object):
"""
A mixin that can be used to render a JSON response.
"""
def render_to_json_response(self, context, **response_kwargs):
"""
Returns a JSON response, transforming 'context' to make the payload.
"""
print(context)
return JsonResponse(
self.get_data(context),
**response_kwargs
)
def get_data(self, context):
"""
Returns an object that will be serialized as JSON by json.dumps().
"""
# Note: This is *EXTREMELY* naive; in reality, you'll need
# to do much more complex handling to ensure that arbitrary
# objects -- such as Django model instances or querysets
# -- can be serialized as JSON.
return context
class Home(TemplateView):
template_name = "kfet/home.html"
@ -618,18 +645,44 @@ class CheckoutCreate(SuccessMessageMixin, CreateView):
return super(CheckoutCreate, self).form_valid(form)
# Checkout - Read
class CheckoutRead(DetailView):
class CheckoutRead(JSONResponseMixin, DetailView):
model = Checkout
template_name = 'kfet/checkout_read.html'
context_object_name = 'checkout'
def get_context_data(self, **kwargs):
context = super(CheckoutRead, self).get_context_data(**kwargs)
context['statements'] = context['checkout'].statements.order_by('-at')
context = super().get_context_data(**kwargs)
checkout = self.object
if self.request.GET.get('last_statement'):
context['laststatement'] = checkout.statements.latest('at')
else:
context['statements'] = checkout.statements.order_by('-at')
return context
def render_to_response(self, context, **kwargs):
if self.request.GET.get('format') == 'json':
data = model_to_dict(
context['checkout'],
fields=['id', 'name', 'balance', 'valid_from', 'valid_to']
)
if 'laststatement' in context:
last_statement = context['laststatement']
last_statement_data = model_to_dict(
last_statement,
fields=['id', 'at', 'balance_new', 'balance_old', 'by']
)
last_statement_data['by'] = str(last_statement.by)
# ``at`` is not editable, so skipped by ``model_to_dict``
last_statement_data['at'] = last_statement.at
data['laststatement'] = last_statement_data
return self.render_to_json_response(data)
else:
return super().render_to_response(context, **kwargs)
# Checkout - Update
class CheckoutUpdate(SuccessMessageMixin, UpdateView):
@ -897,28 +950,6 @@ def kpsul_get_settings(request):
return JsonResponse(data)
@teamkfet_required
def kpsul_checkout_data(request, pk):
checkout = get_object_or_404(Checkout, pk=pk)
data = model_to_dict(
checkout,
fields=['id', 'name', 'balance', 'valid_from', 'valid_to']
)
if request.GET.get('last_statement'):
last_statement = checkout.statements.latest('at')
last_statement_data = model_to_dict(
last_statement,
fields=['id', 'at', 'balance_new', 'balance_old', 'by']
)
last_statement_data['by'] = str(last_statement.by)
# ``at`` is not editable, so skipped by ``model_to_dict``
last_statement_data['at'] = last_statement.at
data['laststatement'] = last_statement_data
return JsonResponse(data)
@teamkfet_required
def kpsul_update_addcost(request):
addcost_form = AddcostForm(request.POST)
@ -2003,29 +2034,6 @@ class SupplierUpdate(SuccessMessageMixin, UpdateView):
# ---------------
# Vues génériques
# ---------------
# source : docs.djangoproject.com/fr/1.10/topics/class-based-views/mixins/
class JSONResponseMixin(object):
"""
A mixin that can be used to render a JSON response.
"""
def render_to_json_response(self, context, **response_kwargs):
"""
Returns a JSON response, transforming 'context' to make the payload.
"""
return JsonResponse(
self.get_data(context),
**response_kwargs
)
def get_data(self, context):
"""
Returns an object that will be serialized as JSON by json.dumps().
"""
# Note: This is *EXTREMELY* naive; in reality, you'll need
# to do much more complex handling to ensure that arbitrary
# objects -- such as Django model instances or querysets
# -- can be serialized as JSON.
return context
class JSONDetailView(JSONResponseMixin,