Added counter edit pages (no AJAX)

This commit is contained in:
Claudio Maggioni 2018-08-09 18:06:25 +02:00
parent f06207001d
commit 39139553e9
16 changed files with 227 additions and 17 deletions

1
.tern-port Normal file
View file

@ -0,0 +1 @@
46275

View file

@ -54,6 +54,7 @@ INSTALLED_APPS = [
MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
@ -118,7 +119,7 @@ AUTH_PASSWORD_VALIDATORS = [
# Internationalization
# https://docs.djangoproject.com/en/2.1/topics/i18n/
LANGUAGE_CODE = 'en-us'
LANGUAGE_CODE = 'it'
TIME_ZONE = 'Europe/Rome'

View file

@ -15,11 +15,14 @@ Including another URLconf
"""
from django.contrib import admin
from django.urls import path, include
from django.conf.urls.i18n import i18n_patterns
from django.views.i18n import JavaScriptCatalog
urlpatterns = [
urlpatterns = i18n_patterns(
path('', include('counter.urls')),
path('admin/', admin.site.urls),
path('accounts/', include('user.urls')),
path('accounts/', include('django.contrib.auth.urls')),
]
path('jsi18n/', JavaScriptCatalog.as_view(), name='javascript-catalog'),
)

12
counter/forms.py Normal file
View file

@ -0,0 +1,12 @@
from django import forms
from django.conf import settings
from .models import ArrowCount
class ArrowCountForm(forms.ModelForm):
date = forms.DateField(widget=forms.DateInput(
format='%Y-%m-%d',
attrs={'class': 'datepicker'}))
class Meta:
model = ArrowCount
fields = ['date', 'count']

View file

@ -7,7 +7,7 @@ class ArrowCount(models.Model):
settings.AUTH_USER_MODEL,
on_delete=models.CASCADE
)
date = models.DateField('Training date', auto_now = True)
date = models.DateField('Training date')
count = models.PositiveIntegerField('Arrow count for the day')
def __str__(self):

View file

@ -34,3 +34,39 @@ form ul {
form ul.errorlist {
background: #ffcdd2;
}
form .card-action button[type=submit] {
background: none !important;
border: none;
cursor: pointer;
padding: 0 !important;
color: inherit;
text-transform: uppercase;
}
.col .card-content .row {
margin-left: auto;
margin-right: auto;
}
.count-up, .count-down {
width: 100%;
}
.count-up {
height: 25vh;
}
.count-down {
height: 12.5vh;
}
.count-up .material-icons {
font-size: 20vh;
line-height: 25vh;
}
.count-down .material-icons {
font-size: 10vh;
line-height: 12.5vh;
}

View file

@ -0,0 +1,15 @@
"use strict";
var input = $('#id_count');
$('.count-up').click(function() {
var count = parseInt(input.val(), 10);
if(!isNaN(count))
input.val(count + 1);
});
$('.count-down').click(function() {
var count = parseInt(input.val(), 10);
if(!isNaN(count) && count > 0)
input.val(count - 1);
});

View file

@ -1,3 +1,52 @@
var i18n = {
cancel: gettext('Cancel'),
clear: gettext('Clear'),
done: gettext('Ok'),
previousMonth: '',
nextMonth: '',
months: [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December'
],
monthsShort: [],
weekdays: [
'Sunday',
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday'],
weekdaysShort: [],
weekdaysAbbrev: []
};
i18n.months.forEach(function(val, index) {
i18n.months[index] = gettext(val);
i18n.monthsShort[index] = i18n.months[index].substring(0,3);
});
i18n.weekdays.forEach(function(val, index) {
i18n.weekdays[index] = gettext(val);
i18n.weekdaysShort[index] = i18n.weekdays[index].substring(0,3);
i18n.weekdaysAbbrev[index] = i18n.weekdays[index][0];
});
$(document).ready(function() {
$('.sidenav').sidenav();
$('.fixed-action-btn').floatingActionButton();
$('.datepicker').datepicker({
format: 'yyyy-mm-dd',
i18n: i18n
});
});

View file

@ -1,9 +1,9 @@
{% load static %}
{% load i18n %}
<!DOCTYPE html>
<html lang="en">
<head>
<title>{% block title %}{% endblock %} | Arrow Counter</title>
<meta charset="utf-8">
@ -34,6 +34,16 @@
<main>{% block content %}{% endblock %}</main>
</div>
<script src="{% url 'javascript-catalog' %}"></script>
<script>
Object.assign(django.catalog, {
{% with 'Monday Tuesday Wednesday Thursday Friday Saturday Sunday' as list %}
{% for weekday in list.split %}
'{{ weekday }}': '{% trans weekday %}',
{% endfor %}
{% endwith %}
});
</script>
<script
src="https://code.jquery.com/jquery-3.1.1.min.js"
integrity="sha256-hVVnYaiADRTO2PzUGmuLJr8BLUSjGIZsDYGmIJLv2b8="

View file

@ -0,0 +1,38 @@
{% extends "base.html" %}
{% load static %}
{% block title %}Update count{% endblock %}
{% block scripts %}
<script src="{% static "js/count_edit.js" %}"></script>
{% endblock %}
{% block content %}
<h1 class="center">Update count</h1>
<form method="post">
<div class="col s12 card" id="edit-arrowcount-form">
<div class="card-content">
{% csrf_token %}
<div class="row">
{{ form.as_p }}
</div>
<div class="row">
<button class="count-up green waves-effect waves-light 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>
</button>
</div>
</div>
<div class="card-action">
<a href="#"><button type="submit">Save</button></a>
</div>
</div>
</form>
{% endblock %}

View file

@ -9,13 +9,19 @@
<tr>
<th>Date</th>
<th>Arrow count</th>
<th>Edit</th>
</tr>
</thead>
<tbody>
{% for c in counts %}
<tr>
<td>{{ c.date }}</td>
<td>{{ c.date|date }}</td>
<td>{{ c.count }}</td>
<td>
<a href="{% url "count_edit" c.id %}" class="btn waves-effect waves-light">
Edit<i class="material-icons right">edit</i>
</a>
</td>
</tr>
{% empty %}
<tr>
@ -24,4 +30,10 @@
{% endfor %}
</tbody>
</table>
<div class="fixed-action-btn">
<a class="btn-floating btn-large waves-effect waves-light red"
href="{% url "count_new" %}">
<i class="material-icons">add</i>
</a>
</div>
{% endblock %}

View file

@ -0,0 +1,18 @@
{% extends "base.html" %}
{% block title %}New count{% endblock %}
{% block content %}
<h1 class="center">New count</h1>
<form method="post">
<div class="col s12 card" id="new-arrowcount-form">
<div class="card-content">
{% csrf_token %}
{{ form.as_p }}
</div>
<div class="card-action">
<a href="#"><button type="submit">Save</button></a>
</div>
</div>
</form>
{% endblock %}

View file

@ -5,4 +5,6 @@ from . import views
urlpatterns = [
path('', views.index, name='index'),
path('count/list', views.arrow_count_list, name='count_list'),
path('count/new', views.NewArrowCount.as_view(), name='count_new'),
path('count/edit/<int:id>', views.EditArrowCount.as_view(), name='count_edit'),
]

View file

@ -1,7 +1,10 @@
from django.shortcuts import render
from django.http import HttpResponse
from django.views import generic
from django.template import loader
from django.urls import reverse_lazy
from .models import ArrowCount
from .forms import ArrowCountForm
from django.contrib.auth.decorators import login_required
from django.conf import settings
from django.core.exceptions import SuspiciousOperation
@ -24,6 +27,25 @@ def arrow_count_list(request):
start = settings.ITEMS_PER_PAGE * (page - 1)
finish = settings.ITEMS_PER_PAGE + start
counts = ArrowCount.objects.filter(user = request.user)[start:finish]
counts = ArrowCount.objects.order_by('-date') \
.filter(user = request.user)[start:finish]
template = loader.get_template('counter/list.html')
return HttpResponse(template.render({'counts': counts}, request))
class NewArrowCount(generic.CreateView):
form_class = ArrowCountForm
success_url = reverse_lazy('count_list')
template_name = 'counter/new.html'
def form_valid(self, form):
form.instance.user = self.request.user
return super().form_valid(form)
class EditArrowCount(generic.UpdateView):
form_class = ArrowCountForm
success_url = reverse_lazy('count_list')
template_name = 'counter/edit.html'
def get_object(self, queryset=None):
obj = ArrowCount.objects.get(id=self.kwargs['id'])
return obj

View file

@ -1,12 +1,3 @@
#login-form .card-content .row, #registration-form .card-content .row {
margin-bottom: 0;
}
#login-form button[type=submit], #registration-form button[type=submit] {
background: none !important;
border: none;
cursor: pointer;
padding: 0 !important;
color: inherit;
text-transform: uppercase;
}

View file

@ -17,7 +17,7 @@
{{ form.as_p }}
</div>
<div class="card-action">
<a href="#"><button type="submit">Register</button></a>
<a href="#"><button type="submit">register</button></a>
</div>
</div>
</form>