diff --git a/launderette/admin.py b/launderette/admin.py index 8c38f3f3..3f7c0d27 100644 --- a/launderette/admin.py +++ b/launderette/admin.py @@ -1,3 +1,9 @@ from django.contrib import admin +from launderette.models import * + # Register your models here. +admin.site.register(Launderette) +admin.site.register(Machine) +admin.site.register(Token) +admin.site.register(Slot) diff --git a/launderette/migrations/0001_initial.py b/launderette/migrations/0001_initial.py deleted file mode 100644 index deacb3bd..00000000 --- a/launderette/migrations/0001_initial.py +++ /dev/null @@ -1,47 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ] - - operations = [ - migrations.CreateModel( - name='Launderette', - fields=[ - ('id', models.AutoField(serialize=False, auto_created=True, verbose_name='ID', primary_key=True)), - ('name', models.CharField(max_length=30, verbose_name='name')), - ], - options={ - 'verbose_name': 'Launderette', - }, - ), - migrations.CreateModel( - name='Machine', - fields=[ - ('id', models.AutoField(serialize=False, auto_created=True, verbose_name='ID', primary_key=True)), - ('name', models.CharField(max_length=30, verbose_name='name')), - ('is_working', models.BooleanField(default=True, verbose_name='is working')), - ('launderette', models.ForeignKey(to='launderette.Launderette', related_name='machines', verbose_name='launderette')), - ], - options={ - 'verbose_name': 'Machine', - }, - ), - migrations.CreateModel( - name='Token', - fields=[ - ('id', models.AutoField(serialize=False, auto_created=True, verbose_name='ID', primary_key=True)), - ('name', models.CharField(max_length=30, verbose_name='name')), - ('type', models.CharField(choices=[('WASHING', 'Washing'), ('DRYING', 'Drying')], max_length=10, verbose_name='type')), - ('launderette', models.ForeignKey(to='launderette.Launderette', related_name='tokens', verbose_name='launderette')), - ], - options={ - 'verbose_name': 'Token', - }, - ), - ] diff --git a/launderette/migrations/0001_squashed_0006_auto_20160729_0050.py b/launderette/migrations/0001_squashed_0006_auto_20160729_0050.py new file mode 100644 index 00000000..d447b26b --- /dev/null +++ b/launderette/migrations/0001_squashed_0006_auto_20160729_0050.py @@ -0,0 +1,66 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + replaces = [('launderette', '0001_initial'), ('launderette', '0002_auto_20160728_1858'), ('launderette', '0003_launderette_sellers'), ('launderette', '0004_auto_20160728_1922'), ('launderette', '0005_auto_20160729_0049'), ('launderette', '0006_auto_20160729_0050')] + + dependencies = [ + ('subscription', '0002_auto_20160718_1805'), + ] + + operations = [ + migrations.CreateModel( + name='Launderette', + fields=[ + ('id', models.AutoField(auto_created=True, verbose_name='ID', primary_key=True, serialize=False)), + ('name', models.CharField(verbose_name='name', max_length=30)), + ], + options={ + 'verbose_name': 'Launderette', + }, + ), + migrations.CreateModel( + name='Machine', + fields=[ + ('id', models.AutoField(auto_created=True, verbose_name='ID', primary_key=True, serialize=False)), + ('name', models.CharField(verbose_name='name', max_length=30)), + ('is_working', models.BooleanField(verbose_name='is working', default=True)), + ('launderette', models.ForeignKey(related_name='machines', verbose_name='launderette', to='launderette.Launderette')), + ], + options={ + 'verbose_name': 'Machine', + }, + ), + migrations.CreateModel( + name='Token', + fields=[ + ('id', models.AutoField(auto_created=True, verbose_name='ID', primary_key=True, serialize=False)), + ('name', models.IntegerField(verbose_name='name')), + ('type', models.CharField(verbose_name='type', choices=[('WASHING', 'Washing'), ('DRYING', 'Drying')], max_length=10)), + ('launderette', models.ForeignKey(related_name='tokens', verbose_name='launderette', to='launderette.Launderette')), + ], + options={ + 'verbose_name': 'Token', + }, + ), + migrations.CreateModel( + name='Slot', + fields=[ + ('id', models.AutoField(auto_created=True, verbose_name='ID', primary_key=True, serialize=False)), + ('start_date', models.DateTimeField(verbose_name='start date')), + ('type', models.CharField(verbose_name='type', choices=[('WASHING', 'Washing'), ('DRYING', 'Drying')], max_length=10)), + ('machine', models.ForeignKey(related_name='slots', verbose_name='machine', to='launderette.Machine')), + ('token', models.ForeignKey(related_name='slots', null=True, verbose_name='token', blank=True, to='launderette.Token')), + ('user', models.ForeignKey(related_name='slots', verbose_name='user', to='subscription.Subscriber')), + ], + ), + migrations.AddField( + model_name='launderette', + name='sellers', + field=models.ManyToManyField(verbose_name='sellers', related_name='launderettes', blank=True, to='subscription.Subscriber'), + ), + ] diff --git a/launderette/migrations/0002_auto_20160728_1858.py b/launderette/migrations/0002_auto_20160728_1858.py deleted file mode 100644 index f8aa6b6d..00000000 --- a/launderette/migrations/0002_auto_20160728_1858.py +++ /dev/null @@ -1,39 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('subscription', '0002_auto_20160718_1805'), - ('launderette', '0001_initial'), - ] - - operations = [ - migrations.CreateModel( - name='Slot', - fields=[ - ('id', models.AutoField(primary_key=True, serialize=False, verbose_name='ID', auto_created=True)), - ('start_date', models.DateTimeField(verbose_name='start date')), - ('type', models.CharField(max_length=10, verbose_name='type', choices=[('WASHING', 'Washing'), ('DRYING', 'Drying')])), - ('machine', models.ForeignKey(verbose_name='machine', related_name='slots', to='launderette.Machine')), - ], - ), - migrations.AlterField( - model_name='token', - name='name', - field=models.IntegerField(verbose_name='name'), - ), - migrations.AddField( - model_name='slot', - name='token', - field=models.ForeignKey(verbose_name='token', related_name='slots', to='launderette.Token'), - ), - migrations.AddField( - model_name='slot', - name='user', - field=models.ForeignKey(verbose_name='user', related_name='slots', to='subscription.Subscriber'), - ), - ] diff --git a/launderette/migrations/0002_auto_20160729_0138.py b/launderette/migrations/0002_auto_20160729_0138.py new file mode 100644 index 00000000..3d1cd8ec --- /dev/null +++ b/launderette/migrations/0002_auto_20160729_0138.py @@ -0,0 +1,18 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('launderette', '0001_squashed_0006_auto_20160729_0050'), + ] + + operations = [ + migrations.AlterModelOptions( + name='slot', + options={'verbose_name': 'Slot'}, + ), + ] diff --git a/launderette/migrations/0003_launderette_sellers.py b/launderette/migrations/0003_launderette_sellers.py deleted file mode 100644 index de418c0f..00000000 --- a/launderette/migrations/0003_launderette_sellers.py +++ /dev/null @@ -1,20 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('subscription', '0002_auto_20160718_1805'), - ('launderette', '0002_auto_20160728_1858'), - ] - - operations = [ - migrations.AddField( - model_name='launderette', - name='sellers', - field=models.ManyToManyField(to='subscription.Subscriber', verbose_name='sellers', related_name='launderettes'), - ), - ] diff --git a/launderette/migrations/0004_auto_20160728_1922.py b/launderette/migrations/0004_auto_20160728_1922.py deleted file mode 100644 index 6bcbdbe8..00000000 --- a/launderette/migrations/0004_auto_20160728_1922.py +++ /dev/null @@ -1,19 +0,0 @@ -# -*- coding: utf-8 -*- -from __future__ import unicode_literals - -from django.db import migrations, models - - -class Migration(migrations.Migration): - - dependencies = [ - ('launderette', '0003_launderette_sellers'), - ] - - operations = [ - migrations.AlterField( - model_name='launderette', - name='sellers', - field=models.ManyToManyField(blank=True, to='subscription.Subscriber', verbose_name='sellers', related_name='launderettes'), - ), - ] diff --git a/launderette/models.py b/launderette/models.py index 945a9766..d458d2e2 100644 --- a/launderette/models.py +++ b/launderette/models.py @@ -48,6 +48,9 @@ class Machine(models.Model): return True return False + def __str__(self): + return "%s - Launderette: %s - Working: %s" % (self.name, self.launderette, self.is_working) + class Token(models.Model): name = models.IntegerField(_('name')) launderette = models.ForeignKey(Launderette, related_name='tokens', verbose_name=_('launderette')) @@ -68,9 +71,16 @@ class Slot(models.Model): start_date = models.DateTimeField(_('start date')) type = models.CharField(_('type'), max_length=10, choices=[('WASHING', _('Washing')), ('DRYING', _('Drying'))]) machine = models.ForeignKey(Machine, related_name='slots', verbose_name=_('machine')) - token = models.ForeignKey(Token, related_name='slots', verbose_name=_('token')) + token = models.ForeignKey(Token, related_name='slots', verbose_name=_('token'), blank=True, null=True) user = models.ForeignKey(Subscriber, related_name='slots', verbose_name=_('user')) + class Meta: + verbose_name = _('Slot') + def full_clean(self): return super(Slot, self).full_clean() + def __str__(self): + return str(self.user) + " - " + str(self.start_date) + + diff --git a/launderette/templates/launderette/launderette_book.jinja b/launderette/templates/launderette/launderette_book.jinja index af30c6c1..d7f54d01 100644 --- a/launderette/templates/launderette/launderette_book.jinja +++ b/launderette/templates/launderette/launderette_book.jinja @@ -4,13 +4,38 @@ {% trans %}Launderette{% endtrans %} {% endblock %} +{% macro choose(date) %} +
+ {% csrf_token %} + +
+{% endmacro %} + + {% block content %} {% if request.user.is_in_group(settings.SITH_MAIN_MEMBERS_GROUP) %} - + + + + {% for day in planning.keys() %} + + {% endfor %} + + + + {% for i in range(0, 24) %} + + {% for hours in planning.values() %} + + {% endfor %} + + {% endfor %} + +
{{ day|date('l') }}
+ {% if hours[i] %} + {{ hours[i]|localtime|time(TIME_FORMAT) }} {{ choose(hours[i]) }} + {% endif %} +
{% endif %} {% endblock %} diff --git a/launderette/templates/launderette/launderette_book_choose.jinja b/launderette/templates/launderette/launderette_book_choose.jinja new file mode 100644 index 00000000..af30c6c1 --- /dev/null +++ b/launderette/templates/launderette/launderette_book_choose.jinja @@ -0,0 +1,21 @@ +{% extends "core/base.jinja" %} + +{% block title %} +{% trans %}Launderette{% endtrans %} +{% endblock %} + +{% block content %} +{% if request.user.is_in_group(settings.SITH_MAIN_MEMBERS_GROUP) %} + +{% endif %} +{% endblock %} + + + + + + diff --git a/launderette/views.py b/launderette/views.py index 9862d3a7..7c2fa6f5 100644 --- a/launderette/views.py +++ b/launderette/views.py @@ -1,14 +1,20 @@ +from datetime import datetime, timedelta +from collections import OrderedDict + from django.shortcuts import render from django.views.generic import ListView, DetailView, RedirectView, TemplateView from django.views.generic.edit import UpdateView, CreateView, DeleteView, ProcessFormView, FormMixin from django.forms.models import modelform_factory from django.forms import CheckboxSelectMultiple from django.utils.translation import ugettext as _ +from django.utils.timezone import make_aware +from django.utils import dateparse from django.conf import settings from core.models import Page from core.views import CanViewMixin, CanEditMixin, CanEditPropMixin, CanCreateMixin from launderette.models import Launderette, Token, Machine, Slot +from subscription.views import get_subscriber # For users @@ -25,14 +31,55 @@ class LaunderetteMainView(TemplateView): class LaunderetteBookMainView(CanViewMixin, ListView): """Choose which launderette to book""" model = Launderette - template_name = 'launderette/launderette_book.jinja' + template_name = 'launderette/launderette_book_choose.jinja' -class LaunderetteBookView(TemplateView): +class LaunderetteBookView(DetailView): """Display the launderette schedule""" model = Launderette pk_url_kwarg = "launderette_id" template_name = 'launderette/launderette_book.jinja' + def post(self, request, *args, **kwargs): + self.object = self.get_object() + if 'slot' in request.POST.keys() and request.user.is_authenticated(): + subscriber = get_subscriber(request.user) + if subscriber.is_subscribed(): + date = dateparse.parse_datetime(request.POST['slot']) + for m in self.object.machines.filter(is_working=True).all(): + slot = Slot.objects.filter(start_date=date, machine=m).first() + print(slot) + if slot is None: + Slot(user=subscriber, start_date=date, machine=m, type="WASHING").save() + print("Saved") + break + return super(LaunderetteBookView, self).get(request, *args, **kwargs) + + @staticmethod + def date_iterator(startDate, endDate, delta=timedelta(days=1)): + currentDate = startDate + while currentDate < endDate: + yield currentDate + currentDate += delta + + def get_context_data(self, **kwargs): + """ Add page to the context """ + kwargs = super(LaunderetteBookView, self).get_context_data(**kwargs) + kwargs['planning'] = OrderedDict() + start_date = make_aware(datetime.now().replace(hour=0, minute=0, second=0, microsecond=0)) + for date in LaunderetteBookView.date_iterator(start_date, start_date+timedelta(days=6), timedelta(days=1)): + kwargs['planning'][date] = [] + for h in LaunderetteBookView.date_iterator(date, date+timedelta(days=1), timedelta(hours=1)): + free = False + for m in self.object.machines.filter(is_working=True).all(): + s = Slot.objects.filter(start_date=h, machine=m).first() + if s is None: + free = True + if free and make_aware(datetime.now()) < h: + kwargs['planning'][date].append(h) + else: + kwargs['planning'][date].append(None) + print("Taken") + return kwargs # For admins