Added end buttons and fixed cache bug in edit arrowcount

This commit is contained in:
Claudio Maggioni (maggicl) 2019-08-01 13:48:35 +02:00
parent f88486eace
commit f2f8b57e0a
6 changed files with 137 additions and 41 deletions

View file

@ -69,37 +69,72 @@ form .card-action button {
margin-right: auto;
}
.edit-arrowcount.title {
font-size: 3rem;
}
.edit-arrowcount.ends {
text-align: center;
margin: .5rem;
margin-bottom: calc(20px - .5rem);
}
.edit-arrowcount.ends button {
font-size: 1.5rem;
font-weight: bold;
width: 5.5rem;
height: 4rem;
display: inline-block;
margin: .4rem;
}
#edit-arrowcount-form input,
#edit-target-form input {
text-align: center
text-align: center;
}
#edit-arrowcount-form #id_count,
#edit-target-form #id_target {
font-size: 3em;
font-size: 2.5em;
padding: .25em 0;
}
.count-up, .count-down {
.edit-arrowcount.onebyone {
display: inline-block;
width: 100%;
text-align: center;
}
.count-up, .count-down {
height: 7rem;
font-size: 5rem;
text-align: center;
padding: 0;
font-family: 'Verdana', sans-serif;
}
.count-up {
height: 8rem;
width: calc(70% - .45rem - 2px);
margin-right: .4rem;
}
.count-down {
height: 5rem;
width: calc(30% - .45rem - 1px);
margin-left: .4rem;
}
.count-up .material-icons {
font-size: 7rem;
line-height: 8rem;
}
@media screen and (max-width: 359px) {
.count-up, .count-down {
width: 100%;
margin-left: 0;
margin-right: 0;
}
.count-down .material-icons {
font-size: 4.5rem;
line-height: 5rem;
.count-down {
height: 4rem;
font-size: 3rem;
margin-top: 1rem;
}
}
.inline-block {

View file

@ -1,4 +1,5 @@
'use strict';
// vim: set ts=2 sw=2 et tw=80:
var acForm = $('#edit-arrowcount-form'),
input = acForm.find('#id_count');
@ -16,14 +17,35 @@ function arrowCountUpdateAjax() {
});
}
$('.count-up').click(function() {
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);
}
}
});
}
$('#id_count').keypress(function() {
var count = parseInt(input.val(), 10);
if(!isNaN(count) && count >= 0) {
input.val(count + 1);
arrowCountUpdateAjax();
}
});
$('.count-up').click(function(e) {
var increment = parseInt(e.currentTarget.getAttribute("data-increment"));
arrowCountFetchAjax(function (count) {
input.val(count + increment);
arrowCountUpdateAjax();
});
});
$('.count-down').click(function() {
var count = parseInt(input.val(), 10);
if(!isNaN(count) && count > 0) {

View file

@ -11,8 +11,9 @@
{% endblock %}
{% block content %}
<h1 class="center">{% trans "Update count" %}</h1>
<h1 class="center edit-arrowcount title">{% trans "Update count" %}</h1>
<form method="post" id="edit-arrowcount-form"
data-fetch-ajax="{% url "count_fetch_ajax" ac_id %}"
data-update-ajax="{% url "count_edit_ajax" ac_id %}">
<div class="col s12 card">
<div class="card-content">
@ -20,17 +21,25 @@
<div class="row">
{{ form.as_p }}
</div>
<div class="row">
<button class="count-up green waves-effect waves-light btn-large"
<div class="row edit-arrowcount onebyone">
<button class="count-up green waves-effect btn-large"
type="button" data-increment="1">
+
</button>
<button class="count-down red waves-effect btn-large"
type="button">
<i class="large material-icons">add</i>
-
</button>
</div>
<div class="row">
<button class="count-down red waves-effect waves-light btn-large"
type="button">
<i class="large material-icons">remove</i>
<div class="row edit-arrowcount ends">
{% with '3 4 5 6 7 8 9 10' as list %}
{% for number in list.split %}
<button class="count-up btn waves-effect cyan"
type="button" data-increment="{{ number }}">
+ {{ number }}
</button>
{% endfor %}
{% endwith %}
</div>
</div>
<div class="card-action">

View file

@ -19,4 +19,6 @@ urlpatterns = [
name='count_delete'),
path('count/edit/<int:ac_id>/ajax', login_required(views \
.arrow_count_update_ajax), name='count_edit_ajax'),
path('count/get/<int:ac_id>/ajax', login_required(views \
.arrow_count_fetch_ajax), name='count_fetch_ajax'),
]

View file

@ -193,6 +193,29 @@ def target_delete(request):
pass
return redirect('index')
@login_required
def arrow_count_fetch_ajax(request, ac_id):
count = get_arrowcount(ac_id, request)
if not count[0]:
return count[1]
else:
return JsonResponse({
'success': True,
'count': count[1]
})
def get_arrowcount(ac_id, request):
arrow_count = ArrowCount.objects.filter(
id=ac_id, user=request.user)[0]
if arrow_count == None:
return (False, JsonResponse({
'success': False,
'error': _('ArrowCount instance not found or from different user')
}))
else:
return (True, arrow_count)
@login_required
def arrow_count_update_ajax(request, ac_id):
try:
@ -209,18 +232,19 @@ def arrow_count_update_ajax(request, ac_id):
'error': _('count is negative or 0')
})
arrow_count = ArrowCount.objects.filter(
id=ac_id, user=request.user)[0]
count = get_arrowcount(ac_id, request)
if not count[0]:
return count[1]
arrow_count.count = count[1]
if arrow_count == None:
try:
arrow_count.save()
except psycopg2.errors.NumericValueOutOfRange:
return JsonResponse({
'success': False,
'error': _('ArrowCount instance not found or from different user')
'error': _('count too big')
})
arrow_count.count = count
arrow_count.save()
return JsonResponse({
'success': True,
'count': arrow_count.count

View file

@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: Arrowcounter\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2019-07-27 13:51+0200\n"
"POT-Creation-Date: 2019-08-01 13:47+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"
@ -76,15 +76,19 @@ msgstr ""
"\">Torna alla homepage</a>.\n"
#: counter/templates/base.html:9 counter/templates/base.html:25
#: counter/templates/index.html:8
#: counter/templates/base.html:65 counter/templates/index.html:8
msgid "Arrow counter"
msgstr "Contafrecce"
#: counter/templates/base.html:67 counter/templates/index.html:9
msgid "A simple online arrow counter"
msgstr "Un semplice contafrecce online"
#: counter/templates/counter/edit.html:7 counter/templates/counter/edit.html:14
msgid "Update count"
msgstr "Aggiorna conteggio"
#: counter/templates/counter/edit.html:37 counter/templates/counter/new.html:17
#: counter/templates/counter/edit.html:46 counter/templates/counter/new.html:17
#: counter/templates/target/edit.html:20
msgid "Save"
msgstr "Salva"
@ -122,10 +126,6 @@ msgstr "Nuovo conteggio"
msgid "Home"
msgstr "Home"
#: counter/templates/index.html:9
msgid "A simple online arrow counter"
msgstr "Un semplice contafrecce online"
#: counter/templates/index.html:17
msgid "Arrows shot this year:"
msgstr "Frecce tirate quest'anno:"
@ -181,14 +181,18 @@ msgstr "Rimuovi"
msgid "page is negative or 0"
msgstr "pagina negativa o uguale a 0"
#: counter/views.py:203
#: counter/views.py:214
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:209
#: counter/views.py:232
msgid "count is negative or 0"
msgstr "count è negativo o uguale a 0"
#: counter/views.py:218
msgid "ArrowCount instance not found or from different user"
msgstr "istanza ArrowCount non trovata o appartenente ad altro utente"
#: counter/views.py:245
msgid "count too big"
msgstr "conteggio troppo alto"