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; 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-arrowcount-form input,
#edit-target-form input { #edit-target-form input {
text-align: center text-align: center;
} }
#edit-arrowcount-form #id_count, #edit-arrowcount-form #id_count,
#edit-target-form #id_target { #edit-target-form #id_target {
font-size: 3em; font-size: 2.5em;
padding: .25em 0; padding: .25em 0;
} }
.count-up, .count-down { .edit-arrowcount.onebyone {
display: inline-block;
width: 100%; 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 { .count-up {
height: 8rem; width: calc(70% - .45rem - 2px);
margin-right: .4rem;
} }
.count-down { .count-down {
height: 5rem; width: calc(30% - .45rem - 1px);
margin-left: .4rem;
} }
.count-up .material-icons { @media screen and (max-width: 359px) {
font-size: 7rem; .count-up, .count-down {
line-height: 8rem; width: 100%;
} margin-left: 0;
margin-right: 0;
}
.count-down .material-icons { .count-down {
font-size: 4.5rem; height: 4rem;
line-height: 5rem; font-size: 3rem;
margin-top: 1rem;
}
} }
.inline-block { .inline-block {

View File

@ -1,4 +1,5 @@
'use strict'; 'use strict';
// 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');
@ -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); var count = parseInt(input.val(), 10);
if(!isNaN(count) && count >= 0) { if(!isNaN(count) && count >= 0) {
input.val(count + 1);
arrowCountUpdateAjax(); 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() { $('.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) {

View File

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

View File

@ -19,4 +19,6 @@ urlpatterns = [
name='count_delete'), name='count_delete'),
path('count/edit/<int:ac_id>/ajax', login_required(views \ path('count/edit/<int:ac_id>/ajax', login_required(views \
.arrow_count_update_ajax), name='count_edit_ajax'), .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 pass
return redirect('index') 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 @login_required
def arrow_count_update_ajax(request, ac_id): def arrow_count_update_ajax(request, ac_id):
try: try:
@ -209,18 +232,19 @@ def arrow_count_update_ajax(request, ac_id):
'error': _('count is negative or 0') 'error': _('count is negative or 0')
}) })
arrow_count = ArrowCount.objects.filter( count = get_arrowcount(ac_id, request)
id=ac_id, user=request.user)[0] 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({ return JsonResponse({
'success': False, 'success': False,
'error': _('ArrowCount instance not found or from different user') 'error': _('count too big')
}) })
arrow_count.count = count
arrow_count.save()
return JsonResponse({ return JsonResponse({
'success': True, 'success': True,
'count': arrow_count.count 'count': arrow_count.count

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-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" "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"
@ -76,15 +76,19 @@ msgstr ""
"\">Torna alla homepage</a>.\n" "\">Torna alla homepage</a>.\n"
#: counter/templates/base.html:9 counter/templates/base.html:25 #: 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" msgid "Arrow counter"
msgstr "Contafrecce" 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 #: counter/templates/counter/edit.html:7 counter/templates/counter/edit.html:14
msgid "Update count" msgid "Update count"
msgstr "Aggiorna conteggio" 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 #: counter/templates/target/edit.html:20
msgid "Save" msgid "Save"
msgstr "Salva" msgstr "Salva"
@ -122,10 +126,6 @@ msgstr "Nuovo conteggio"
msgid "Home" msgid "Home"
msgstr "Home" msgstr "Home"
#: counter/templates/index.html:9
msgid "A simple online arrow counter"
msgstr "Un semplice contafrecce online"
#: counter/templates/index.html:17 #: counter/templates/index.html:17
msgid "Arrows shot this year:" msgid "Arrows shot this year:"
msgstr "Frecce tirate quest'anno:" msgstr "Frecce tirate quest'anno:"
@ -181,14 +181,18 @@ msgstr "Rimuovi"
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: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" msgid "count is not a number"
msgstr "count non è un numero" msgstr "count non è un numero"
#: counter/views.py:209 #: counter/views.py:232
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:218 #: counter/views.py:245
msgid "ArrowCount instance not found or from different user" msgid "count too big"
msgstr "istanza ArrowCount non trovata o appartenente ad altro utente" msgstr "conteggio troppo alto"