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

This commit is contained in:
Claudio Maggioni 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 .models import ArrowCount
from .models import ArrowCount, Target
admin.site.register(ArrowCount)
admin.site.register(Target)

View File

@ -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)

View File

@ -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);
}
});

View File

@ -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()

View File

@ -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 <maggicl@kolabnow.ch>\n"
"Language-Team: Claudio Maggioni <maggicl@kolabnow.ch>\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"

View File

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