From 3a359cce409dcac59d8d4c8e42282b19415749a2 Mon Sep 17 00:00:00 2001 From: "Claudio Maggioni (maggicl)" Date: Mon, 5 Aug 2019 15:57:50 +0200 Subject: [PATCH] FIxed counter (now in relative AJAX mode) and admin page --- counter/admin.py | 3 ++- counter/models.py | 6 +++++- counter/static/js/count_edit.js | 37 ++++++++++++--------------------- counter/views.py | 31 +++++++++++++++++++-------- locale/it/LC_MESSAGES/django.po | 24 ++++++++++++--------- user/models.py | 3 +++ 6 files changed, 59 insertions(+), 45 deletions(-) diff --git a/counter/admin.py b/counter/admin.py index 060547f..d38fc7b 100644 --- a/counter/admin.py +++ b/counter/admin.py @@ -1,4 +1,5 @@ from django.contrib import admin -from .models import ArrowCount +from .models import ArrowCount, Target admin.site.register(ArrowCount) +admin.site.register(Target) diff --git a/counter/models.py b/counter/models.py index cf55144..65f8191 100644 --- a/counter/models.py +++ b/counter/models.py @@ -11,6 +11,10 @@ class Target(models.Model): primary_key = True ) + def __str__(self): + return str(self.user) + ' yearly goal: ' + str(self.target) + + class ArrowCount(models.Model): user = models.ForeignKey( settings.AUTH_USER_MODEL, @@ -20,4 +24,4 @@ class ArrowCount(models.Model): count = models.PositiveIntegerField(_('Arrow count for the day')) def __str__(self): - return self.date.strftime("%x") + ": " + str(self.count) + return str(self.user) + ' on ' + self.date.strftime("%x") + ": " + str(self.count) diff --git a/counter/static/js/count_edit.js b/counter/static/js/count_edit.js index ab94ab6..e0e28a5 100644 --- a/counter/static/js/count_edit.js +++ b/counter/static/js/count_edit.js @@ -2,30 +2,23 @@ // vim: set ts=2 sw=2 et tw=80: var acForm = $('#edit-arrowcount-form'), - input = acForm.find('#id_count'); + input = acForm.find('#id_count'), + csrf = acForm.find('[name="csrfmiddlewaretoken"]'); -function arrowCountUpdateAjax() { +function arrowCountUpdateAjax(mode, n) { $.ajax({ url: acForm.attr('data-update-ajax'), - data: acForm.serialize(), + data: { + 'mode': mode, + 'value': n, + 'csrfmiddlewaretoken': csrf.val() + }, method: 'POST', success: function(e) { if(!e.success) { M.toast({html: gettext('Error while updating') + ': ' + e.error}) - } - } - }); -} - -function arrowCountFetchAjax(next) { - $.ajax({ - url: acForm.attr('data-fetch-ajax'), - method: 'GET', - success: function(e) { - if(!e.success) { - M.toast({html: gettext('Error while updating') + ': ' + e.error}) - } else { - next(e.count); + } else if (mode === 'relative') { + input.val(e.count); } } }); @@ -34,22 +27,18 @@ function arrowCountFetchAjax(next) { $('#id_count').keypress(function() { var count = parseInt(input.val(), 10); if(!isNaN(count) && count >= 0) { - arrowCountUpdateAjax(); + arrowCountUpdateAjax('absolute', count); } }); $('.count-up').click(function(e) { var increment = parseInt(e.currentTarget.getAttribute("data-increment")); - arrowCountFetchAjax(function (count) { - input.val(count + increment); - arrowCountUpdateAjax(); - }); + arrowCountUpdateAjax('relative', increment); }); $('.count-down').click(function() { var count = parseInt(input.val(), 10); if(!isNaN(count) && count > 0) { - input.val(count - 1); - arrowCountUpdateAjax(); + arrowCountUpdateAjax('relative', -1); } }); diff --git a/counter/views.py b/counter/views.py index 37c4afd..de5e743 100644 --- a/counter/views.py +++ b/counter/views.py @@ -214,25 +214,38 @@ def get_arrowcount(ac_id, request): @login_required def arrow_count_update_ajax(request, ac_id): + mode = request.POST.get("mode", '').lower() + + if mode != 'absolute' and mode != 'relative': + return JsonResponse({ + 'success': False, + 'error': _('mode not valid') + }) + isRelative = mode == 'relative' + try: - count = int(request.POST.get("count", None)) + value = int(request.POST.get("value", 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') + 'error': _('value field is not a number') }) tup = get_arrowcount(ac_id, request) if not tup[0]: return tup[1] arrow_count = tup[1] - arrow_count.count = count + + if isRelative: + arrow_count.count += value + else: + arrow_count.count = value + + if arrow_count.count < 0: + return JsonResponse({ + 'success': False, + 'error': _('count is negative or 0') + }) try: arrow_count.save() diff --git a/locale/it/LC_MESSAGES/django.po b/locale/it/LC_MESSAGES/django.po index e8fedc5..b8f0810 100644 --- a/locale/it/LC_MESSAGES/django.po +++ b/locale/it/LC_MESSAGES/django.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: Arrowcounter\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-08-01 13:47+0200\n" +"POT-Creation-Date: 2019-08-05 15:54+0200\n" "PO-Revision-Date: 2019-07-27 13:00+0200\n" "Last-Translator: Claudio Maggioni \n" "Language-Team: Claudio Maggioni \n" @@ -22,11 +22,11 @@ msgstr "" msgid "Target to reach" msgstr "Obiettivo da raggiungere" -#: counter/models.py:19 +#: counter/models.py:23 msgid "Training date" msgstr "Data allenamento" -#: counter/models.py:20 +#: counter/models.py:24 msgid "Arrow count for the day" msgstr "Conteggio frecce per la data" @@ -177,22 +177,26 @@ msgstr "Registrati" msgid "Remove" msgstr "Rimuovi" -#: counter/views.py:119 +#: counter/views.py:116 msgid "page is negative or 0" msgstr "pagina negativa o uguale a 0" -#: counter/views.py:214 +#: counter/views.py:210 msgid "ArrowCount instance not found or from different user" msgstr "istanza ArrowCount non trovata o appartenente ad altro utente" -#: counter/views.py:226 -msgid "count is not a number" -msgstr "count non è un numero" +#: counter/views.py:222 +msgid "mode not valid" +msgstr "campo 'mode' non valido" -#: counter/views.py:232 +#: counter/views.py:231 +msgid "value field is not a number" +msgstr "il campo 'value' non è un numero" + +#: counter/views.py:247 msgid "count is negative or 0" msgstr "count è negativo o uguale a 0" -#: counter/views.py:245 +#: counter/views.py:255 msgid "count too big" msgstr "conteggio troppo alto" diff --git a/user/models.py b/user/models.py index 3b17d45..2fafda4 100644 --- a/user/models.py +++ b/user/models.py @@ -3,3 +3,6 @@ from django.contrib.auth.models import AbstractUser class CounterUser(AbstractUser): pass + + def __str__(self): + return self.username + ' (' + self.email + ')'