Fixed bugs and added yearly target
This commit is contained in:
parent
32ff5b7ea0
commit
36fcc8d9c9
10 changed files with 134 additions and 15 deletions
2
.gitignore
vendored
2
.gitignore
vendored
|
@ -76,7 +76,7 @@ coverage.xml
|
|||
docs/_build/
|
||||
|
||||
# PyBuilder
|
||||
target/
|
||||
/target/
|
||||
|
||||
|
||||
### Django ###
|
||||
|
|
|
@ -1,6 +1,6 @@
|
|||
from django import forms
|
||||
from django.conf import settings
|
||||
from .models import ArrowCount
|
||||
from .models import ArrowCount, Target
|
||||
|
||||
class ArrowCountForm(forms.ModelForm):
|
||||
date = forms.DateField(widget=forms.DateInput(
|
||||
|
@ -10,3 +10,9 @@ class ArrowCountForm(forms.ModelForm):
|
|||
class Meta:
|
||||
model = ArrowCount
|
||||
fields = ['date', 'count']
|
||||
|
||||
class TargetForm(forms.ModelForm):
|
||||
|
||||
class Meta:
|
||||
model = Target
|
||||
fields = ['target']
|
||||
|
|
28
counter/migrations/0002_auto_20190726_2244.py
Normal file
28
counter/migrations/0002_auto_20190726_2244.py
Normal file
|
@ -0,0 +1,28 @@
|
|||
# Generated by Django 2.2.3 on 2019-07-26 20:44
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('user', '0001_initial'),
|
||||
('counter', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Target',
|
||||
fields=[
|
||||
('target', models.PositiveIntegerField(verbose_name='Target to reach')),
|
||||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, primary_key=True, serialize=False, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
migrations.AlterField(
|
||||
model_name='arrowcount',
|
||||
name='date',
|
||||
field=models.DateField(verbose_name='Training date'),
|
||||
),
|
||||
]
|
|
@ -2,6 +2,14 @@ from django.db import models
|
|||
from django.conf import settings
|
||||
from django.contrib.auth.models import User
|
||||
|
||||
class Target(models.Model):
|
||||
target = models.PositiveIntegerField('Target to reach')
|
||||
user = models.ForeignKey(
|
||||
settings.AUTH_USER_MODEL,
|
||||
on_delete=models.CASCADE,
|
||||
primary_key = True
|
||||
)
|
||||
|
||||
class ArrowCount(models.Model):
|
||||
user = models.ForeignKey(
|
||||
settings.AUTH_USER_MODEL,
|
||||
|
|
|
@ -36,14 +36,14 @@ form ul, form span.helptext {
|
|||
}
|
||||
|
||||
form ul {
|
||||
background: #ffecb3;
|
||||
background: #ffecb3;
|
||||
}
|
||||
|
||||
form ul.errorlist {
|
||||
background: #ffcdd2;
|
||||
}
|
||||
|
||||
form .card-action button[type=submit] {
|
||||
form .card-action button {
|
||||
background: none !important;
|
||||
border: none;
|
||||
cursor: pointer;
|
||||
|
@ -57,11 +57,13 @@ form .card-action button[type=submit] {
|
|||
margin-right: auto;
|
||||
}
|
||||
|
||||
#edit-arrowcount-form input {
|
||||
#edit-arrowcount-form input,
|
||||
#edit-target-form input {
|
||||
text-align: center
|
||||
}
|
||||
|
||||
#edit-arrowcount-form #id_count {
|
||||
#edit-arrowcount-form #id_count,
|
||||
#edit-target-form #id_target {
|
||||
font-size: 3em;
|
||||
padding: .25em 0;
|
||||
}
|
||||
|
|
|
@ -5,17 +5,23 @@
|
|||
{% block content %}
|
||||
<h1 class="center">Arrow counter</h1>
|
||||
<h5 class="center">A simple online arrow counter</h5>
|
||||
{% if user.is_authenticated %}
|
||||
<p class="center" style="margin-bottom: 3rem">
|
||||
<a class="btn waves-effect waves-light" href="{% url "count_export" %}">
|
||||
Excel Data (CSV) <i class="material-icons right">file_download</i>
|
||||
</a>
|
||||
</p>
|
||||
|
||||
{% if user.is_authenticated %}
|
||||
<div class="card-panel grey lighten-2">
|
||||
<h5 class="center">Arrows shot this year:</h5>
|
||||
<p class="center"><strong style="font-size: 2.7rem">{{ yearArrows }}</strong></p>
|
||||
</div>
|
||||
{% if diffTarget is not False %}
|
||||
<div class="card-panel lighten-2 {% if diffTarget < 0 %}red{% elif diffTarget is 0 %}yellow
|
||||
{% else %}green{% endif %}">
|
||||
<h5 class="center">Difference with target:</h5>
|
||||
<p class="center"><strong style="font-size: 2.7rem">{{ diffTarget }}</strong></p>
|
||||
</div>
|
||||
{% endif %}
|
||||
<div class="card-panel grey lighten-2">
|
||||
<h5 class="center">Arrows shot this month:</h5>
|
||||
<p class="center"><strong style="font-size: 2.7rem">{{ monthArrows }}</strong></p>
|
||||
|
|
|
@ -14,6 +14,7 @@
|
|||
<li class="divider" tabindex="-1"></li>
|
||||
{% if user.is_authenticated %}
|
||||
<li><a href="{% url "count_list" %}">Counts</a></li>
|
||||
<li><a href="{% url "target_edit" %}">Set yearly target</a></li>
|
||||
<li><a href="{% url "stats" %}">Weekly stats</a></li>
|
||||
<li class="divider" tabindex="-1"></li>
|
||||
{% if user.is_superuser %}
|
||||
|
|
23
counter/templates/target/edit.html
Normal file
23
counter/templates/target/edit.html
Normal file
|
@ -0,0 +1,23 @@
|
|||
{% extends "base.html" %}
|
||||
|
||||
{% load static %}
|
||||
|
||||
{% block title %}Set yearly target{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h1 class="center">Set yearly target</h1>
|
||||
<form method="post" id="edit-target-form">
|
||||
<div class="col s12 card">
|
||||
<div class="card-content">
|
||||
{% csrf_token %}
|
||||
<div class="row">
|
||||
{{ form.as_p }}
|
||||
</div>
|
||||
</div>
|
||||
<div class="card-action">
|
||||
<a href="#"><button type="submit">Save</button></a>
|
||||
<a href="{% url "target_delete" %}"><button type="button">Remove</button></a>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
{% endblock %}
|
|
@ -11,6 +11,10 @@ urlpatterns = [
|
|||
name='count_new'),
|
||||
path('count/edit/<int:id>', login_required(views.EditArrowCount.as_view()),
|
||||
name='count_edit'),
|
||||
path('target/edit', login_required(views.EditTarget.as_view()),
|
||||
name='target_edit'),
|
||||
path('target/delete', views.target_delete,
|
||||
name='target_delete'),
|
||||
path('count/delete/<int:id>', login_required(views.DeleteArrowCount.as_view()),
|
||||
name='count_delete'),
|
||||
path('count/edit/<int:ac_id>/ajax', login_required(views \
|
||||
|
|
|
@ -1,12 +1,12 @@
|
|||
# vim: set ts=4 sw=4 et tw=80:
|
||||
|
||||
from django.shortcuts import render
|
||||
from django.shortcuts import render, redirect
|
||||
from django.http import HttpResponse, JsonResponse
|
||||
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 .models import ArrowCount, Target
|
||||
from .forms import ArrowCountForm, TargetForm
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.conf import settings
|
||||
from django.core.exceptions import SuspiciousOperation
|
||||
|
@ -35,20 +35,32 @@ def index(request):
|
|||
yearArrows = ArrowCount.objects.filter(user=request.user) \
|
||||
.filter(date__range=(date(now.year, 1, 1), date(now.year, 12, 31))) \
|
||||
.aggregate(s=Sum('count'))
|
||||
yearArrows = yearArrows['s'] if yearArrows['s'] is not None else 0
|
||||
|
||||
diff_target = False
|
||||
try:
|
||||
target = Target.objects.get(user=request.user).target
|
||||
days_this_year = (date(now.year + 1, 1, 1) - date(now.year, 1, 1)).days
|
||||
diff_target = round(yearArrows - (now.timetuple().tm_yday / days_this_year) * \
|
||||
target)
|
||||
except Target.DoesNotExist:
|
||||
pass
|
||||
|
||||
monthArrows = ArrowCount.objects.filter(user=request.user) \
|
||||
.filter(date__year=now.year, date__month=now.month) \
|
||||
.aggregate(s=Sum('count'))
|
||||
|
||||
weekday = now.isoweekday()
|
||||
weekday = now.isoweekday() - 1
|
||||
weekArrows = ArrowCount.objects.filter(user=request.user) \
|
||||
.filter(date__range=(now - timedelta(days=weekday), now + timedelta(days=6-weekday))) \
|
||||
.filter(date__range=(now - timedelta(days=weekday), \
|
||||
now + timedelta(days=6-weekday))) \
|
||||
.aggregate(s=Sum('count'))
|
||||
|
||||
return HttpResponse(template.render({
|
||||
'yearArrows': 0 if yearArrows['s'] is None else yearArrows['s'],
|
||||
'yearArrows': yearArrows,
|
||||
'monthArrows': 0 if monthArrows['s'] is None else monthArrows['s'],
|
||||
'weekArrows': 0 if weekArrows['s'] is None else weekArrows['s']
|
||||
'weekArrows': 0 if weekArrows['s'] is None else weekArrows['s'],
|
||||
'diffTarget': diff_target
|
||||
}, request))
|
||||
else:
|
||||
return HttpResponse(template.render({}), request)
|
||||
|
@ -143,6 +155,26 @@ class EditArrowCount(generic.UpdateView):
|
|||
context_data.update(ac_id=self.kwargs['id'])
|
||||
return self.render_to_response(context_data)
|
||||
|
||||
class EditTarget(generic.UpdateView):
|
||||
form_class = TargetForm
|
||||
success_url = reverse_lazy('index')
|
||||
template_name = 'target/edit.html'
|
||||
|
||||
def get_object(self, queryset=None):
|
||||
try:
|
||||
obj = Target.objects.get(user=self.request.user)
|
||||
except Target.DoesNotExist:
|
||||
obj = Target(user=self.request.user, target=0)
|
||||
obj.save()
|
||||
return obj
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
super().get(self, request, *args, **kwargs)
|
||||
context_data = self.get_context_data()
|
||||
context_data.update(ac_id=self.request.user)
|
||||
return self.render_to_response(context_data)
|
||||
|
||||
|
||||
class DeleteArrowCount(generic.DeleteView):
|
||||
model = ArrowCount
|
||||
success_url = reverse_lazy('count_list')
|
||||
|
@ -151,6 +183,15 @@ class DeleteArrowCount(generic.DeleteView):
|
|||
obj = ArrowCount.objects.get(id=self.kwargs['id'])
|
||||
return obj
|
||||
|
||||
@login_required
|
||||
def target_delete(request):
|
||||
try:
|
||||
to_delete = Target.objects.get(user=request.user)
|
||||
to_delete.delete()
|
||||
except Target.DoesNotExist:
|
||||
pass
|
||||
return redirect('index')
|
||||
|
||||
@login_required
|
||||
def arrow_count_update_ajax(request, ac_id):
|
||||
try:
|
||||
|
|
Loading…
Reference in a new issue