Commit 1529af32 authored by Skia's avatar Skia

Add token handling form in launderette

parent 31ecb50c
Pipeline #93 passed with stage
in 2 minutes and 4 seconds
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('launderette', '0009_remove_token_product'),
]
operations = [
migrations.AlterModelOptions(
name='token',
options={'verbose_name': 'Token', 'ordering': ['name']},
),
]
# -*- coding: utf-8 -*-
from __future__ import unicode_literals
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('launderette', '0010_auto_20160806_1242'),
]
operations = [
migrations.AlterModelOptions(
name='token',
options={'ordering': ['type', 'name'], 'verbose_name': 'Token'},
),
]
from django.db import models
from django.db import models, DataError
from django.utils.translation import ugettext_lazy as _
from django.conf import settings
from django.core.urlresolvers import reverse
......@@ -41,7 +41,7 @@ class Launderette(models.Model):
class Machine(models.Model):
name = models.CharField(_('name'), max_length=30)
launderette = models.ForeignKey(Launderette, related_name='machines', verbose_name=_('launderette'))
type = models.CharField(_('type'), max_length=10, choices=[('WASHING', _('Washing')), ('DRYING', _('Drying'))])
type = models.CharField(_('type'), max_length=10, choices=settings.SITH_LAUNDERETTE_MACHINE_TYPES)
is_working = models.BooleanField(_('is working'), default=True)
class Meta:
......@@ -59,18 +59,25 @@ class Machine(models.Model):
return "%s %s" % (self._meta.verbose_name, self.name)
def get_absolute_url(self):
return reverse('launderette:launderette_details', kwargs={"launderette_id": self.launderette.id})
return reverse('launderette:launderette_admin', kwargs={"launderette_id": self.launderette.id})
class Token(models.Model):
name = models.CharField(_('name'), max_length=5)
launderette = models.ForeignKey(Launderette, related_name='tokens', verbose_name=_('launderette'))
type = models.CharField(_('type'), max_length=10, choices=[('WASHING', _('Washing')), ('DRYING', _('Drying'))])
type = models.CharField(_('type'), max_length=10, choices=settings.SITH_LAUNDERETTE_MACHINE_TYPES)
borrow_date = models.DateTimeField(_('borrow date'), null=True, blank=True)
user = models.ForeignKey(Subscriber, related_name='tokens', verbose_name=_('user'), null=True, blank=True)
class Meta:
verbose_name = _('Token')
unique_together = ('name', 'launderette', 'type')
ordering = ['type', 'name']
def save(self, *args, **kwargs):
if self.name == "":
raise DataError(_("Token name can not be blank"))
else:
super(Token, self).save(*args, **kwargs)
def is_owned_by(self, user):
"""
......@@ -85,7 +92,7 @@ class Token(models.Model):
class Slot(models.Model):
start_date = models.DateTimeField(_('start date'))
type = models.CharField(_('type'), max_length=10, choices=[('WASHING', _('Washing')), ('DRYING', _('Drying'))])
type = models.CharField(_('type'), max_length=10, choices=settings.SITH_LAUNDERETTE_MACHINE_TYPES)
machine = models.ForeignKey(Machine, related_name='slots', verbose_name=_('machine'))
token = models.ForeignKey(Token, related_name='slots', verbose_name=_('token'), blank=True, null=True)
user = models.ForeignKey(Subscriber, related_name='slots', verbose_name=_('user'))
......
......@@ -7,12 +7,6 @@
{% block content %}
<h3>{% trans %}Selling{% endtrans %}</h3>
<p><a href="{{ url('launderette:main_click', launderette_id=launderette.id) }}">{% trans %}Sell{% endtrans %}</a></p>
<h3>{% trans %}Tokens{% endtrans %}</h3>
<ul>
{% for t in launderette.tokens.all() %}
<li>{{ t }}</li>
{% endfor %}
</ul>
<hr>
<h3>{% trans %}Machines{% endtrans %}</h3>
<p><a href="{{ url('launderette:machine_new') }}?launderette={{ launderette.id }}">{% trans %}New machine{% endtrans %}</a></p>
......@@ -22,6 +16,42 @@
<a href="{{ url('launderette:machine_delete', machine_id=m.id) }}">{% trans %}Delete{% endtrans %}</a></li>
{% endfor %}
</ul>
<hr>
<h3>{% trans %}Tokens{% endtrans %}</h3>
<p>
<form method="post" action="">
{% csrf_token %}
{{ form.as_p() }}
<p><input type="submit" value="{% trans %}Go{% endtrans %}" /></p>
</form>
</p>
<p>
<table>
<thead>
<tr>
<td>{% trans %}Type{% endtrans %}</td>
<td>{% trans %}Name{% endtrans %}</td>
<td>{% trans %}User{% endtrans %}</td>
<td>{% trans %}Since{% endtrans %}</td>
</tr>
</thead>
<tbody>
{% for t in launderette.tokens.all() %}
<tr>
<td>{{ t.get_type_display() }}</td>
<td>{{ t.name }}</td>
{% if t.user %}
<td>{{ t.user.get_display_name() }}</td>
<td>{{ t.borrow_date|date(DATETIME_FORMAT) }} - {{ t.borrow_date|time(DATETIME_FORMAT) }}</td>
{% else %}
<td></td>
<td></td>
{% endif %}
</tr>
{% endfor %}
</tbody>
</table>
</p>
{% endblock %}
......
......@@ -10,7 +10,7 @@
<h3>{% trans %}Launderette admin list{% endtrans %}</h3>
<ul>
{% for l in launderette_list %}
<li><a href="{{ url('launderette:launderette_details', launderette_id=l.id) }}">{{ l }}</a> -
<li><a href="{{ url('launderette:launderette_admin', launderette_id=l.id) }}">{{ l }}</a> -
<a href="{{ url('launderette:launderette_edit', launderette_id=l.id) }}">{% trans %}Edit{% endtrans %}</a></li>
{% endfor %}
</ul>
......
......@@ -10,7 +10,7 @@ urlpatterns = [
url(r'^(?P<launderette_id>[0-9]+)/click$', LaunderetteMainClickView.as_view(), name='main_click'),
url(r'^(?P<launderette_id>[0-9]+)/click/(?P<user_id>[0-9]+)$', LaunderetteClickView.as_view(), name='click'),
url(r'^admin$', LaunderetteListView.as_view(), name='launderette_list'),
url(r'^admin/(?P<launderette_id>[0-9]+)$', LaunderetteDetailView.as_view(), name='launderette_details'),
url(r'^admin/(?P<launderette_id>[0-9]+)$', LaunderetteAdminView.as_view(), name='launderette_admin'),
url(r'^admin/(?P<launderette_id>[0-9]+)/edit$', LaunderetteEditView.as_view(), name='launderette_edit'),
url(r'^admin/new$', LaunderetteCreateView.as_view(), name='launderette_new'),
url(r'^admin/machine/new$', MachineCreateView.as_view(), name='machine_new'),
......
......@@ -11,7 +11,7 @@ from django.utils.translation import ugettext as _
from django.utils import dateparse
from django.core.urlresolvers import reverse_lazy
from django.conf import settings
from django.db import transaction
from django.db import transaction, DataError
from django import forms
from django.template import defaultfilters
from django.utils import formats
......@@ -142,11 +142,82 @@ class LaunderetteCreateView(CanCreateMixin, CreateView):
form.instance.counter = c
return super(LaunderetteCreateView, self).form_valid(form)
class LaunderetteDetailView(CanEditPropMixin, DetailView):
class ManageTokenForm(forms.Form):
action = forms.ChoiceField(choices=[("BACK", _("Back")), ("ADD", _("Add")), ("DEL", _("Delete"))], label=_("Action"))
token_type = forms.ChoiceField(choices=settings.SITH_LAUNDERETTE_MACHINE_TYPES, label=_("Type"))
tokens = forms.CharField(max_length=512, widget=forms.widgets.Textarea, label=_("Tokens, separated by spaces"))
def process(self, launderette):
cleaned_data = self.cleaned_data
token_list = cleaned_data['tokens'].strip(" ").split(" ")
token_type = cleaned_data['token_type']
self.data = {}
if cleaned_data['action'] == "BACK":
for t in token_list:
try:
tok = Token.objects.filter(launderette=launderette, type=token_type, name=t).first()
tok.borrow_date = None
tok.user = None
tok.save()
except:
self.add_error(None, _("Token %(token_name)s does not exists") % {'token_name': t})
elif cleaned_data['action'] == "ADD":
for t in token_list:
try:
Token(launderette=launderette, type=token_type, name=t).save()
except DataError as e:
self.add_error(None, e)
except:
self.add_error(None, _("Token %(token_name)s already exists") % {'token_name': t})
elif cleaned_data['action'] == "DEL":
for t in token_list:
try:
Token.objects.filter(launderette=launderette, type=token_type, name=t).delete()
except:
self.add_error(None, _("Token %(token_name)s does not exists") % {'token_name': t})
class LaunderetteAdminView(CanEditPropMixin, BaseFormView, DetailView):
"""The admin page of the launderette"""
model = Launderette
pk_url_kwarg = "launderette_id"
template_name = 'launderette/launderette_detail.jinja'
template_name = 'launderette/launderette_admin.jinja'
form_class = ManageTokenForm
def get(self, request, *args, **kwargs):
self.object = self.get_object()
return super(LaunderetteAdminView, self).get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
self.object = self.get_object()
form = self.get_form()
return super(LaunderetteAdminView, self).post(request, *args, **kwargs)
form.launderette = self.object
if form.is_valid():
return self.form_valid(form)
else:
return self.form_invalid(form)
def form_valid(self, form):
"""
We handle here the redirection, passing the user id of the asked customer
"""
form.process(self.object)
if form.is_valid():
return super(LaunderetteAdminView, self).form_valid(form)
else:
return super(LaunderetteAdminView, self).form_invalid(form)
def get_context_data(self, **kwargs):
"""
We handle here the login form for the barman
"""
kwargs = super(LaunderetteAdminView, self).get_context_data(**kwargs)
if self.request.method == "GET":
kwargs['form'] = self.get_form()
return kwargs
def get_success_url(self):
return reverse_lazy('launderette:launderette_admin', args=self.args, kwargs=self.kwargs)
class GetLaunderetteUserForm(GetUserForm):
def clean(self):
......
This diff is collapsed.
......@@ -336,6 +336,7 @@ with open('./sith/et_keys/pubkey.pem') as f:
SITH_EBOUTIC_PUB_KEY = f.read()
# Launderette variables
SITH_LAUNDERETTE_MACHINE_TYPES = [('WASHING', _('Washing')), ('DRYING', _('Drying'))]
SITH_LAUNDERETTE_PRICES = {
'WASHING': 1.0,
'DRYING': 0.75,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment