Add ajax-select app and improve some templates

This commit is contained in:
Skia
2016-08-19 23:24:23 +02:00
parent bfb2dc5f82
commit 7e90e657a7
25 changed files with 468 additions and 411 deletions

View File

@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('counter', '0013_auto_20160818_1736'),
]
operations = [
migrations.AlterField(
model_name='refilling',
name='date',
field=models.DateTimeField(verbose_name='date'),
),
migrations.AlterField(
model_name='selling',
name='date',
field=models.DateTimeField(verbose_name='date'),
),
]

View File

@ -1,4 +1,6 @@
{% extends "core/base.jinja" %}
{% from "core/macros.jinja" import user_mini_profile %}
{% macro add_product(id, content) %}
<form method="post" action="{{ url('counter:click', counter_id=counter.id, user_id=customer.user.id) }}" class="inline" style="display:inline">
@ -21,13 +23,12 @@
{% endblock %}
{% block content %}
<h3>{% trans %}Counter{% endtrans %}</h3>
<h4>{{ counter }}</h4>
<p><strong>{% trans %}Club: {% endtrans %}</strong> {{ counter.club }}</p>
<div>
<div id="user_info">
<h5>{% trans %}Customer{% endtrans %}</h5>
<p>{{ customer.user.get_display_name() }}, {{ customer.amount }} €</p>
{{ user_mini_profile(customer.user) }}
<p>{% trans %}Amount: {% endtrans %}{{ customer.amount }} €</p>
</div>
{% if counter.type == 'BAR' %}
<div>
@ -48,7 +49,7 @@
<form method="post" action="{{ url('counter:click', counter_id=counter.id, user_id=customer.user.id) }}">
{% csrf_token %}
<input type="hidden" name="action" value="code">
<input type="input" name="code" value="" autofocus />
<input type="input" name="code" value="" autofocus id="code_field"/>
<input type="submit" value="{% trans %}Go{% endtrans %}" />
</form>
<p>{% trans %}Basket: {% endtrans %}</p>
@ -79,5 +80,45 @@
{% endblock %}
{% block script %}
{{ super() }}
<script>
$( function() {
var products = [
{% for p in counter.products.all() %}
{
value: "{{ p.code }}",
label: "{{ p.name }}",
tags: "{{ p.code }} {{ p.name }}",
},
{% endfor %}
];
var quantity = "";
var search = "";
var pattern = /^(\d+x)?(.*)/i;
$( "#code_field" ).autocomplete({
select: function (event, ui) {
event.preventDefault();
$("#code_field").val(quantity + ui.item.value);
},
focus: function (event, ui) {
event.preventDefault();
$("#code_field").val(quantity + ui.item.value);
},
source: function( request, response ) {
var res = pattern.exec(request.term);
quantity = res[1] || "";
search = res[2];
var matcher = new RegExp( $.ui.autocomplete.escapeRegex( search ), "i" );
response($.grep( products, function( value ) {
value = value.tags;
return matcher.test( value );
}));
},
});
});
</script>
{% endblock %}

View File

@ -0,0 +1,19 @@
{% extends "core/base.jinja" %}
{% block title %}
{% trans obj=object %}Edit {{ obj }}{% endtrans %}
{% endblock %}
{% block content %}
<h2>{% trans obj=object %}Edit {{ obj }}{% endtrans %}</h2>
<form action="" method="post">
{% csrf_token %}
<p>{{ form.sellers.errors }}<label for="{{ form.sellers.name }}">{{ form.sellers.label }}</label> {{ form.sellers }}</p>
<p><input type="submit" value="{% trans %}Save{% endtrans %}" /></p>
<p>{{ form.products.errors }}<label for="{{ form.products.name }}">{{ form.products.label }}</label> {{ form.products }}</p>
<p><input type="submit" value="{% trans %}Save{% endtrans %}" /></p>
</form>
{% endblock %}

View File

@ -10,13 +10,19 @@
<h3>{% trans %}Counter admin list{% endtrans %}</h3>
<ul>
{% for c in counter_list %}
<li>
{% if c.type == "EBOUTIC" %}
<li><a href="{{ url('eboutic:main') }}">{{ c }}</a> -
<a href="{{ url('counter:admin', counter_id=c.id) }}">{% trans %}Edit{% endtrans %}</a></li>
<a href="{{ url('eboutic:main') }}">{{ c }}</a> -
{% else %}
<li><a href="{{ url('counter:details', counter_id=c.id) }}">{{ c }}</a> -
<a href="{{ url('counter:admin', counter_id=c.id) }}">{% trans %}Edit{% endtrans %}</a></li>
<a href="{{ url('counter:details', counter_id=c.id) }}">{{ c }}</a> -
{% endif %}
{% if user.can_edit(c) %}
<a href="{{ url('counter:admin', counter_id=c.id) }}">{% trans %}Edit{% endtrans %}</a> -
{% endif %}
{% if user.is_owner(c) %}
<a href="{{ url('counter:prop_admin', counter_id=c.id) }}">{% trans %}Props{% endtrans %}</a>
{% endif %}
</li>
{% endfor %}
</ul>
{% else %}

View File

@ -8,6 +8,7 @@ urlpatterns = [
url(r'^(?P<counter_id>[0-9]+)/login$', CounterLogin.as_view(), name='login'),
url(r'^(?P<counter_id>[0-9]+)/logout$', CounterLogout.as_view(), name='logout'),
url(r'^admin/(?P<counter_id>[0-9]+)$', CounterEditView.as_view(), name='admin'),
url(r'^admin/(?P<counter_id>[0-9]+)/prop$', CounterEditPropView.as_view(), name='prop_admin'),
url(r'^admin$', CounterListView.as_view(), name='admin_list'),
url(r'^admin/new$', CounterCreateView.as_view(), name='new'),
url(r'^admin/delete/(?P<counter_id>[0-9]+)$', CounterDeleteView.as_view(), name='delete'),

View File

@ -14,8 +14,11 @@ from django.db import DataError, transaction
import re
from datetime import date, timedelta
from ajax_select.fields import AutoCompleteSelectField, AutoCompleteSelectMultipleField
from ajax_select import make_ajax_form, make_ajax_field
from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMixin
from core.views.forms import SelectUser
from subscription.models import Subscriber
from subscription.views import get_subscriber
from counter.models import Counter, Customer, Product, Selling, Refilling, ProductType
@ -29,8 +32,7 @@ class GetUserForm(forms.Form):
some nickname, first name, or last name (TODO)
"""
code = forms.CharField(label="Code", max_length=10, required=False)
id = forms.IntegerField(label="ID", required=False)
# TODO: add a nice JS widget to search for users
id = AutoCompleteSelectField('users', required=False, label=_("Select user"), help_text=None)
def as_p(self):
self.fields['code'].widget.attrs['autofocus'] = True
@ -327,15 +329,31 @@ class CounterListView(CanViewMixin, ListView):
model = Counter
template_name = 'counter/counter_list.jinja'
class CounterEditView(CanEditPropMixin, UpdateView):
class CounterEditForm(forms.ModelForm):
class Meta:
model = Counter
fields = ['sellers', 'products']
sellers = make_ajax_field(Counter, 'sellers', 'users', show_help_text=False)
products = make_ajax_field(Counter, 'products', 'products')
class CounterEditView(CanEditMixin, UpdateView):
"""
Edit a counter's main informations (for the counter's manager)
"""
model = Counter
form_class = CounterEditForm
pk_url_kwarg = "counter_id"
template_name = 'counter/counter_edit.jinja'
def get_success_url(self):
return reverse_lazy('counter:admin', kwargs={'counter_id': self.object.id})
class CounterEditPropView(CanEditPropMixin, UpdateView):
"""
Edit a counter's main informations (for the counter's admin)
"""
model = Counter
form_class = modelform_factory(Counter, fields=['name', 'club', 'type', 'sellers', 'products'],
widgets={
'products':CheckboxSelectMultiple,
'sellers':CheckboxSelectMultiple})
form_class = modelform_factory(Counter, fields=['name', 'club', 'type'])
pk_url_kwarg = "counter_id"
template_name = 'core/edit.jinja'
@ -399,13 +417,20 @@ class ProductCreateView(CanCreateMixin, CreateView):
'selling_price', 'special_selling_price', 'icon', 'club']
template_name = 'core/create.jinja'
class ProductEditForm(forms.ModelForm):
class Meta:
model = Product
fields = ['name', 'description', 'product_type', 'code', 'purchase_price',
'selling_price', 'special_selling_price', 'icon', 'club']
counters = make_ajax_field(Product, 'counters', 'counters', show_help_text=False, label='Counters', help_text="Guy",
required=False) # TODO FIXME
class ProductEditView(CanEditPropMixin, UpdateView):
"""
An edit view for the admins
"""
model = Product
fields = ['name', 'description', 'product_type', 'code', 'purchase_price',
'selling_price', 'special_selling_price', 'icon', 'club']
form_class = ProductEditForm
pk_url_kwarg = "product_id"
template_name = 'core/edit.jinja'
# TODO: add management of the 'counters' ForeignKey