FIxed counter (now in relative AJAX mode) and admin page

This commit is contained in:
Claudio Maggioni (maggicl) 2019-08-05 15:57:50 +02:00
parent 80597b1a8a
commit 3a359cce40
6 changed files with 59 additions and 45 deletions

View File

@ -1,4 +1,5 @@
from django.contrib import admin from django.contrib import admin
from .models import ArrowCount from .models import ArrowCount, Target
admin.site.register(ArrowCount) admin.site.register(ArrowCount)
admin.site.register(Target)

View File

@ -11,6 +11,10 @@ class Target(models.Model):
primary_key = True primary_key = True
) )
def __str__(self):
return str(self.user) + ' yearly goal: ' + str(self.target)
class ArrowCount(models.Model): class ArrowCount(models.Model):
user = models.ForeignKey( user = models.ForeignKey(
settings.AUTH_USER_MODEL, settings.AUTH_USER_MODEL,
@ -20,4 +24,4 @@ class ArrowCount(models.Model):
count = models.PositiveIntegerField(_('Arrow count for the day')) count = models.PositiveIntegerField(_('Arrow count for the day'))
def __str__(self): def __str__(self):
return self.date.strftime("%x") + ": " + str(self.count) return str(self.user) + ' on ' + self.date.strftime("%x") + ": " + str(self.count)

View File

@ -2,30 +2,23 @@
// vim: set ts=2 sw=2 et tw=80: // vim: set ts=2 sw=2 et tw=80:
var acForm = $('#edit-arrowcount-form'), 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({ $.ajax({
url: acForm.attr('data-update-ajax'), url: acForm.attr('data-update-ajax'),
data: acForm.serialize(), data: {
'mode': mode,
'value': n,
'csrfmiddlewaretoken': csrf.val()
},
method: 'POST', method: 'POST',
success: function(e) { success: function(e) {
if(!e.success) { if(!e.success) {
M.toast({html: gettext('Error while updating') + ': ' + e.error}) M.toast({html: gettext('Error while updating') + ': ' + e.error})
} } else if (mode === 'relative') {
} input.val(e.count);
});
}
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);
} }
} }
}); });
@ -34,22 +27,18 @@ function arrowCountFetchAjax(next) {
$('#id_count').keypress(function() { $('#id_count').keypress(function() {
var count = parseInt(input.val(), 10); var count = parseInt(input.val(), 10);
if(!isNaN(count) && count >= 0) { if(!isNaN(count) && count >= 0) {
arrowCountUpdateAjax(); arrowCountUpdateAjax('absolute', count);
} }
}); });
$('.count-up').click(function(e) { $('.count-up').click(function(e) {
var increment = parseInt(e.currentTarget.getAttribute("data-increment")); var increment = parseInt(e.currentTarget.getAttribute("data-increment"));
arrowCountFetchAjax(function (count) { arrowCountUpdateAjax('relative', increment);
input.val(count + increment);
arrowCountUpdateAjax();
});
}); });
$('.count-down').click(function() { $('.count-down').click(function() {
var count = parseInt(input.val(), 10); var count = parseInt(input.val(), 10);
if(!isNaN(count) && count > 0) { if(!isNaN(count) && count > 0) {
input.val(count - 1); arrowCountUpdateAjax('relative', -1);
arrowCountUpdateAjax();
} }
}); });

View File

@ -214,25 +214,38 @@ def get_arrowcount(ac_id, request):
@login_required @login_required
def arrow_count_update_ajax(request, ac_id): 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: try:
count = int(request.POST.get("count", None)) value = int(request.POST.get("value", None))
except ValueError: except ValueError:
return JsonResponse({ return JsonResponse({
'success': False, 'success': False,
'error': _('count is not a number') 'error': _('value field is not a number')
})
if count == None or count < 0:
return JsonResponse({
'success': False,
'error': _('count is negative or 0')
}) })
tup = get_arrowcount(ac_id, request) tup = get_arrowcount(ac_id, request)
if not tup[0]: if not tup[0]:
return tup[1] return tup[1]
arrow_count = 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: try:
arrow_count.save() arrow_count.save()

View File

@ -8,7 +8,7 @@ msgid ""
msgstr "" msgstr ""
"Project-Id-Version: Arrowcounter\n" "Project-Id-Version: Arrowcounter\n"
"Report-Msgid-Bugs-To: \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" "PO-Revision-Date: 2019-07-27 13:00+0200\n"
"Last-Translator: Claudio Maggioni <maggicl@kolabnow.ch>\n" "Last-Translator: Claudio Maggioni <maggicl@kolabnow.ch>\n"
"Language-Team: Claudio Maggioni <maggicl@kolabnow.ch>\n" "Language-Team: Claudio Maggioni <maggicl@kolabnow.ch>\n"
@ -22,11 +22,11 @@ msgstr ""
msgid "Target to reach" msgid "Target to reach"
msgstr "Obiettivo da raggiungere" msgstr "Obiettivo da raggiungere"
#: counter/models.py:19 #: counter/models.py:23
msgid "Training date" msgid "Training date"
msgstr "Data allenamento" msgstr "Data allenamento"
#: counter/models.py:20 #: counter/models.py:24
msgid "Arrow count for the day" msgid "Arrow count for the day"
msgstr "Conteggio frecce per la data" msgstr "Conteggio frecce per la data"
@ -177,22 +177,26 @@ msgstr "Registrati"
msgid "Remove" msgid "Remove"
msgstr "Rimuovi" msgstr "Rimuovi"
#: counter/views.py:119 #: counter/views.py:116
msgid "page is negative or 0" msgid "page is negative or 0"
msgstr "pagina negativa o uguale a 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" msgid "ArrowCount instance not found or from different user"
msgstr "istanza ArrowCount non trovata o appartenente ad altro utente" msgstr "istanza ArrowCount non trovata o appartenente ad altro utente"
#: counter/views.py:226 #: counter/views.py:222
msgid "count is not a number" msgid "mode not valid"
msgstr "count non è un numero" 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" msgid "count is negative or 0"
msgstr "count è negativo o uguale a 0" msgstr "count è negativo o uguale a 0"
#: counter/views.py:245 #: counter/views.py:255
msgid "count too big" msgid "count too big"
msgstr "conteggio troppo alto" msgstr "conteggio troppo alto"

View File

@ -3,3 +3,6 @@ from django.contrib.auth.models import AbstractUser
class CounterUser(AbstractUser): class CounterUser(AbstractUser):
pass pass
def __str__(self):
return self.username + ' (' + self.email + ')'