From 159aa21c06ec9e9870bd310581ca037de8088e87 Mon Sep 17 00:00:00 2001 From: praticamentetilde Date: Thu, 9 Aug 2018 19:31:50 +0200 Subject: [PATCH] counter CRUD done (except list pagination in templates) --- counter/static/css/main.css | 4 +++ counter/static/js/count_edit.js | 26 +++++++++++--- counter/templates/counter/edit.html | 5 +-- counter/templates/counter/list.html | 11 ++++-- counter/urls.py | 12 +++++-- counter/views.py | 55 +++++++++++++++++++++++++++-- 6 files changed, 99 insertions(+), 14 deletions(-) diff --git a/counter/static/css/main.css b/counter/static/css/main.css index 4c0712b..feec187 100644 --- a/counter/static/css/main.css +++ b/counter/static/css/main.css @@ -70,3 +70,7 @@ form .card-action button[type=submit] { font-size: 10vh; line-height: 12.5vh; } + +.inline-block { + display: inline-block; +} diff --git a/counter/static/js/count_edit.js b/counter/static/js/count_edit.js index eb4b026..8bb6cd0 100644 --- a/counter/static/js/count_edit.js +++ b/counter/static/js/count_edit.js @@ -1,15 +1,33 @@ -"use strict"; +'use strict'; -var input = $('#id_count'); +var acForm = $('#edit-arrowcount-form'), + input = acForm.find('#id_count'); + +function arrowCountUpdateAjax() { + $.ajax({ + url: acForm.attr('data-update-ajax'), + data: acForm.serialize(), + method: 'POST', + success: function(e) { + if(!e.success) { + M.toast({html: gettext('Error while updating') + ': ' + e.error}) + } + } + }); +} $('.count-up').click(function() { var count = parseInt(input.val(), 10); - if(!isNaN(count)) + if(!isNaN(count) && count >= 0) { input.val(count + 1); + arrowCountUpdateAjax(); + } }); $('.count-down').click(function() { var count = parseInt(input.val(), 10); - if(!isNaN(count) && count > 0) + if(!isNaN(count) && count > 0) { input.val(count - 1); + arrowCountUpdateAjax(); + } }); diff --git a/counter/templates/counter/edit.html b/counter/templates/counter/edit.html index f603d03..c056240 100644 --- a/counter/templates/counter/edit.html +++ b/counter/templates/counter/edit.html @@ -10,8 +10,9 @@ {% block content %}

Update count

-
-
+ +
{% csrf_token %}
diff --git a/counter/templates/counter/list.html b/counter/templates/counter/list.html index 5d25ddb..4fcda9e 100644 --- a/counter/templates/counter/list.html +++ b/counter/templates/counter/list.html @@ -18,9 +18,16 @@ {{ c.date|date }} {{ c.count }} - - Editedit + + edit + + {% csrf_token %} + + {% empty %} diff --git a/counter/urls.py b/counter/urls.py index 5e9e4e1..7cf462d 100644 --- a/counter/urls.py +++ b/counter/urls.py @@ -1,10 +1,16 @@ from django.urls import path - +from django.contrib.auth.decorators import login_required from . import views urlpatterns = [ path('', views.index, name='index'), path('count/list', views.arrow_count_list, name='count_list'), - path('count/new', views.NewArrowCount.as_view(), name='count_new'), - path('count/edit/', views.EditArrowCount.as_view(), name='count_edit'), + path('count/new', login_required(views.NewArrowCount.as_view()), + name='count_new'), + path('count/edit/', login_required(views.EditArrowCount.as_view()), + name='count_edit'), + path('count/delete/', login_required(views.DeleteArrowCount.as_view()), + name='count_delete'), + path('count/edit//ajax', login_required(views \ + .arrow_count_update_ajax), name='count_edit_ajax'), ] diff --git a/counter/views.py b/counter/views.py index ab90f2c..2453a62 100644 --- a/counter/views.py +++ b/counter/views.py @@ -1,5 +1,5 @@ from django.shortcuts import render -from django.http import HttpResponse +from django.http import HttpResponse, JsonResponse from django.views import generic from django.template import loader from django.urls import reverse_lazy @@ -8,6 +8,7 @@ from .forms import ArrowCountForm from django.contrib.auth.decorators import login_required from django.conf import settings from django.core.exceptions import SuspiciousOperation +from django.utils.translation import gettext as _ def index(request): template = loader.get_template('index.html') @@ -23,7 +24,7 @@ def arrow_count_list(request): page = int(page) if page <= 0: - raise SuspiciousOperation("page is negative or 0") + raise SuspiciousOperation(_("page is negative or 0")) start = settings.ITEMS_PER_PAGE * (page - 1) finish = settings.ITEMS_PER_PAGE + start @@ -48,4 +49,52 @@ class EditArrowCount(generic.UpdateView): def get_object(self, queryset=None): obj = ArrowCount.objects.get(id=self.kwargs['id']) - return obj + return obj + + def get(self, request, *args, **kwargs): + super().get(self, request, *args, **kwargs) + context_data = self.get_context_data() + context_data.update(ac_id=self.kwargs['id']) + return self.render_to_response(context_data) + +class DeleteArrowCount(generic.DeleteView): + model = ArrowCount + success_url = reverse_lazy('count_list') + + def get_object(self, queryset=None): + obj = ArrowCount.objects.get(id=self.kwargs['id']) + return obj + +@login_required +def arrow_count_update_ajax(request, ac_id): + try: + count = int(request.POST.get("count", None)) + except ValueError: + return JsonResponse({ + 'success': False, + 'error': _('count is not a number') + }) + + if count == None or count < 0: + return JsonResponse({ + 'success': False, + 'error': _('count is negative or 0') + }) + + arrow_count = ArrowCount.objects.filter( + id=ac_id, user=request.user)[0] + + if arrow_count == None: + return JsonResponse({ + 'success': False, + 'error': _('ArrowCount instance not found or from different user') + }) + + arrow_count.count = count + arrow_count.save() + + return JsonResponse({ + 'success': True, + 'count': arrow_count.count + }) +