counter CRUD done (except list pagination in templates)
This commit is contained in:
parent
39139553e9
commit
159aa21c06
6 changed files with 99 additions and 14 deletions
|
@ -70,3 +70,7 @@ form .card-action button[type=submit] {
|
||||||
font-size: 10vh;
|
font-size: 10vh;
|
||||||
line-height: 12.5vh;
|
line-height: 12.5vh;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.inline-block {
|
||||||
|
display: inline-block;
|
||||||
|
}
|
||||||
|
|
|
@ -1,15 +1,33 @@
|
||||||
"use strict";
|
'use strict';
|
||||||
|
|
||||||
var input = $('#id_count');
|
var acForm = $('#edit-arrowcount-form'),
|
||||||
|
input = acForm.find('#id_count');
|
||||||
|
|
||||||
|
function arrowCountUpdateAjax() {
|
||||||
|
$.ajax({
|
||||||
|
url: acForm.attr('data-update-ajax'),
|
||||||
|
data: acForm.serialize(),
|
||||||
|
method: 'POST',
|
||||||
|
success: function(e) {
|
||||||
|
if(!e.success) {
|
||||||
|
M.toast({html: gettext('Error while updating') + ': ' + e.error})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
$('.count-up').click(function() {
|
$('.count-up').click(function() {
|
||||||
var count = parseInt(input.val(), 10);
|
var count = parseInt(input.val(), 10);
|
||||||
if(!isNaN(count))
|
if(!isNaN(count) && count >= 0) {
|
||||||
input.val(count + 1);
|
input.val(count + 1);
|
||||||
|
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) {
|
||||||
input.val(count - 1);
|
input.val(count - 1);
|
||||||
|
arrowCountUpdateAjax();
|
||||||
|
}
|
||||||
});
|
});
|
||||||
|
|
|
@ -10,8 +10,9 @@
|
||||||
|
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h1 class="center">Update count</h1>
|
<h1 class="center">Update count</h1>
|
||||||
<form method="post">
|
<form method="post" id="edit-arrowcount-form"
|
||||||
<div class="col s12 card" id="edit-arrowcount-form">
|
data-update-ajax="{% url "count_edit_ajax" ac_id %}">
|
||||||
|
<div class="col s12 card">
|
||||||
<div class="card-content">
|
<div class="card-content">
|
||||||
{% csrf_token %}
|
{% csrf_token %}
|
||||||
<div class="row">
|
<div class="row">
|
||||||
|
|
|
@ -18,9 +18,16 @@
|
||||||
<td>{{ c.date|date }}</td>
|
<td>{{ c.date|date }}</td>
|
||||||
<td>{{ c.count }}</td>
|
<td>{{ c.count }}</td>
|
||||||
<td>
|
<td>
|
||||||
<a href="{% url "count_edit" c.id %}" class="btn waves-effect waves-light">
|
<a href="{% url "count_edit" c.id %}"
|
||||||
Edit<i class="material-icons right">edit</i>
|
class="btn waves-effect waves-light">
|
||||||
|
<i class="material-icons">edit</i>
|
||||||
</a>
|
</a>
|
||||||
|
<form class="inline-block" method="post" action="{% url "count_delete" c.id %}">
|
||||||
|
{% csrf_token %}
|
||||||
|
<button type="submit" class="red btn waves-effect waves-light">
|
||||||
|
<i class="material-icons">delete</i>
|
||||||
|
</button>
|
||||||
|
</form>
|
||||||
</td>
|
</td>
|
||||||
</tr>
|
</tr>
|
||||||
{% empty %}
|
{% empty %}
|
||||||
|
|
|
@ -1,10 +1,16 @@
|
||||||
from django.urls import path
|
from django.urls import path
|
||||||
|
from django.contrib.auth.decorators import login_required
|
||||||
from . import views
|
from . import views
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('', views.index, name='index'),
|
path('', views.index, name='index'),
|
||||||
path('count/list', views.arrow_count_list, name='count_list'),
|
path('count/list', views.arrow_count_list, name='count_list'),
|
||||||
path('count/new', views.NewArrowCount.as_view(), name='count_new'),
|
path('count/new', login_required(views.NewArrowCount.as_view()),
|
||||||
path('count/edit/<int:id>', views.EditArrowCount.as_view(), name='count_edit'),
|
name='count_new'),
|
||||||
|
path('count/edit/<int:id>', login_required(views.EditArrowCount.as_view()),
|
||||||
|
name='count_edit'),
|
||||||
|
path('count/delete/<int:id>', login_required(views.DeleteArrowCount.as_view()),
|
||||||
|
name='count_delete'),
|
||||||
|
path('count/edit/<int:ac_id>/ajax', login_required(views \
|
||||||
|
.arrow_count_update_ajax), name='count_edit_ajax'),
|
||||||
]
|
]
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
from django.shortcuts import render
|
from django.shortcuts import render
|
||||||
from django.http import HttpResponse
|
from django.http import HttpResponse, JsonResponse
|
||||||
from django.views import generic
|
from django.views import generic
|
||||||
from django.template import loader
|
from django.template import loader
|
||||||
from django.urls import reverse_lazy
|
from django.urls import reverse_lazy
|
||||||
|
@ -8,6 +8,7 @@ from .forms import ArrowCountForm
|
||||||
from django.contrib.auth.decorators import login_required
|
from django.contrib.auth.decorators import login_required
|
||||||
from django.conf import settings
|
from django.conf import settings
|
||||||
from django.core.exceptions import SuspiciousOperation
|
from django.core.exceptions import SuspiciousOperation
|
||||||
|
from django.utils.translation import gettext as _
|
||||||
|
|
||||||
def index(request):
|
def index(request):
|
||||||
template = loader.get_template('index.html')
|
template = loader.get_template('index.html')
|
||||||
|
@ -23,7 +24,7 @@ def arrow_count_list(request):
|
||||||
page = int(page)
|
page = int(page)
|
||||||
|
|
||||||
if page <= 0:
|
if page <= 0:
|
||||||
raise SuspiciousOperation("page is negative or 0")
|
raise SuspiciousOperation(_("page is negative or 0"))
|
||||||
|
|
||||||
start = settings.ITEMS_PER_PAGE * (page - 1)
|
start = settings.ITEMS_PER_PAGE * (page - 1)
|
||||||
finish = settings.ITEMS_PER_PAGE + start
|
finish = settings.ITEMS_PER_PAGE + start
|
||||||
|
@ -49,3 +50,51 @@ class EditArrowCount(generic.UpdateView):
|
||||||
def get_object(self, queryset=None):
|
def get_object(self, queryset=None):
|
||||||
obj = ArrowCount.objects.get(id=self.kwargs['id'])
|
obj = ArrowCount.objects.get(id=self.kwargs['id'])
|
||||||
return obj
|
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.kwargs['id'])
|
||||||
|
return self.render_to_response(context_data)
|
||||||
|
|
||||||
|
class DeleteArrowCount(generic.DeleteView):
|
||||||
|
model = ArrowCount
|
||||||
|
success_url = reverse_lazy('count_list')
|
||||||
|
|
||||||
|
def get_object(self, queryset=None):
|
||||||
|
obj = ArrowCount.objects.get(id=self.kwargs['id'])
|
||||||
|
return obj
|
||||||
|
|
||||||
|
@login_required
|
||||||
|
def arrow_count_update_ajax(request, ac_id):
|
||||||
|
try:
|
||||||
|
count = int(request.POST.get("count", 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')
|
||||||
|
})
|
||||||
|
|
||||||
|
arrow_count = ArrowCount.objects.filter(
|
||||||
|
id=ac_id, user=request.user)[0]
|
||||||
|
|
||||||
|
if arrow_count == None:
|
||||||
|
return JsonResponse({
|
||||||
|
'success': False,
|
||||||
|
'error': _('ArrowCount instance not found or from different user')
|
||||||
|
})
|
||||||
|
|
||||||
|
arrow_count.count = count
|
||||||
|
arrow_count.save()
|
||||||
|
|
||||||
|
return JsonResponse({
|
||||||
|
'success': True,
|
||||||
|
'count': arrow_count.count
|
||||||
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue