Adapt checkout json view

This commit is contained in:
Ludovic Stephan 2021-02-28 23:03:37 +01:00
parent d537fce257
commit b7ebd77afb
3 changed files with 15 additions and 18 deletions

View file

@ -360,7 +360,9 @@ class KPsulAccountForm(forms.ModelForm):
class KPsulCheckoutForm(forms.Form): class KPsulCheckoutForm(forms.Form):
checkout = forms.ModelChoiceField( checkout = forms.ModelChoiceField(
queryset=None, widget=forms.Select(attrs={"id": "id_checkout_select"}) queryset=None,
widget=forms.Select(attrs={"id": "id_checkout_select"}),
empty_label=None,
) )
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):

View file

@ -208,7 +208,7 @@ urlpatterns = [
# ----- # -----
path("k-psul/", views.kpsul, name="kfet.kpsul"), path("k-psul/", views.kpsul, name="kfet.kpsul"),
path( path(
"k-psul/checkout_data", "k-psul/checkout_data/<int:pk>",
views.kpsul_checkout_data, views.kpsul_checkout_data,
name="kfet.kpsul.checkout_data", name="kfet.kpsul.checkout_data",
), ),

View file

@ -927,20 +927,10 @@ def account_read_json(request, trigramme):
@teamkfet_required @teamkfet_required
def kpsul_checkout_data(request): def kpsul_checkout_data(request, pk):
pk = request.POST.get("pk", 0)
if not pk:
pk = 0
data = ( data = (
Checkout.objects.annotate( Checkout.objects.annotate(
last_statement_by_first_name=F( last_statement_by=F("statements__by__trigramme"),
"statements__by__cofprofile__user__first_name"
),
last_statement_by_last_name=F(
"statements__by__cofprofile__user__last_name"
),
last_statement_by_trigramme=F("statements__by__trigramme"),
last_statement_balance=F("statements__balance_new"), last_statement_balance=F("statements__balance_new"),
last_statement_at=F("statements__at"), last_statement_at=F("statements__at"),
) )
@ -951,20 +941,25 @@ def kpsul_checkout_data(request):
.order_by("statements__at") .order_by("statements__at")
.values( .values(
"id", "id",
"name",
"balance", "balance",
"valid_from", "valid_from",
"valid_to", "valid_to",
"last_statement_balance", "last_statement_balance",
"last_statement_at", "last_statement_at",
"last_statement_by_trigramme", "last_statement_by",
"last_statement_by_last_name",
"last_statement_by_first_name",
) )
.last() .last()
) )
if data is None: if data is None:
raise Http404 raise Http404
prefix = "last_statement_"
ls_dict = {}
for (key, value) in data.items():
if key.startswith(prefix):
ls_dict[key[len(prefix) :]] = value
data["last_statement"] = ls_dict
return JsonResponse(data) return JsonResponse(data)