views.py 4.48 KB
Newer Older
Skia's avatar
Skia committed
1
2
3
from datetime import datetime, timedelta
from collections import OrderedDict

Skia's avatar
Skia committed
4
5
6
7
8
9
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 _
Skia's avatar
Skia committed
10
11
from django.utils.timezone import make_aware
from django.utils import dateparse
Skia's avatar
Skia committed
12
13
14
15
16
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
Skia's avatar
Skia committed
17
from subscription.views import get_subscriber
Skia's avatar
Skia committed
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33

# For users

class LaunderetteMainView(TemplateView):
    """Main presentation view"""
    template_name = 'launderette/launderette_main.jinja'

    def get_context_data(self, **kwargs):
        """ Add page to the context """
        kwargs = super(LaunderetteMainView, self).get_context_data(**kwargs)
        kwargs['page'] = Page.objects.filter(name='launderette').first()
        return kwargs

class LaunderetteBookMainView(CanViewMixin, ListView):
    """Choose which launderette to book"""
    model = Launderette
Skia's avatar
Skia committed
34
    template_name = 'launderette/launderette_book_choose.jinja'
Skia's avatar
Skia committed
35

Skia's avatar
Skia committed
36
class LaunderetteBookView(DetailView):
Skia's avatar
Skia committed
37
38
39
40
41
    """Display the launderette schedule"""
    model = Launderette
    pk_url_kwarg = "launderette_id"
    template_name = 'launderette/launderette_book.jinja'

Skia's avatar
Skia committed
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
    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
Skia's avatar
Skia committed
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

# For admins

class LaunderetteListView(CanViewMixin, ListView):
    """Choose which launderette to administer"""
    model = Launderette
    template_name = 'launderette/launderette_list.jinja'

class LaunderetteDetailView(CanViewMixin, DetailView):
    """The admin page of the launderette"""
    model = Launderette
    pk_url_kwarg = "launderette_id"
    template_name = 'launderette/launderette_detail.jinja'

class LaunderetteEditView(CanViewMixin, UpdateView):
    """Edit a launderette"""
    model = Launderette
    pk_url_kwarg = "launderette_id"
    form_class = modelform_factory(Launderette, fields=['name', 'sellers'],
            widgets={'sellers':CheckboxSelectMultiple})
    template_name = 'core/edit.jinja'

class LaunderetteCreateView(CanCreateMixin, CreateView):
    """Create a new launderette"""
    model = Launderette
    fields = ['name', 'sellers']
    template_name = 'core/create.jinja'