From 21df0a32d352d0d20fc044334a67e141734ebde9 Mon Sep 17 00:00:00 2001 From: Klaus-Uwe Mitterer Date: Sat, 16 May 2020 21:06:08 +0200 Subject: [PATCH] Turned into menu scheduler --- cal/forms.py | 6 ++---- cal/models.py | 10 ++++----- cal/templates/cal/base.html | 2 +- cal/templates/registration/login.html | 29 +++++++++++++++++++++++++++ cal/urls.py | 4 ++-- cal/utils.py | 11 +++++++--- cal/views.py | 9 +++++++-- djangocalendar/urls.py | 1 + requirements.txt | 2 ++ 9 files changed, 57 insertions(+), 17 deletions(-) create mode 100644 cal/templates/registration/login.html create mode 100644 requirements.txt diff --git a/cal/forms.py b/cal/forms.py index 7d3991f..c6046c4 100644 --- a/cal/forms.py +++ b/cal/forms.py @@ -6,13 +6,11 @@ class EventForm(ModelForm): model = Event # datetime-local is a HTML5 input type, format to make date time show on fields widgets = { - 'start_time': DateInput(attrs={'type': 'datetime-local'}, format='%Y-%m-%dT%H:%M'), - 'end_time': DateInput(attrs={'type': 'datetime-local'}, format='%Y-%m-%dT%H:%M'), + 'date': DateInput(format='%Y-%m-%d'), } fields = '__all__' def __init__(self, *args, **kwargs): super(EventForm, self).__init__(*args, **kwargs) # input_formats parses HTML5 datetime-local input to datetime field - self.fields['start_time'].input_formats = ('%Y-%m-%dT%H:%M',) - self.fields['end_time'].input_formats = ('%Y-%m-%dT%H:%M',) + self.fields['date'].input_formats = ('%Y-%m-%d',) diff --git a/cal/models.py b/cal/models.py index f01fe44..92131fd 100644 --- a/cal/models.py +++ b/cal/models.py @@ -2,12 +2,12 @@ from django.db import models from django.urls import reverse class Event(models.Model): - title = models.CharField(max_length=200) - description = models.TextField() - start_time = models.DateTimeField() - end_time = models.DateTimeField() + date = models.DateField() + brekky = models.CharField(max_length=255, blank=True, null=True) + lunch = models.CharField(max_length=255, blank=True, null=True) + dinner = models.CharField(max_length=255, blank=True, null=True) @property def get_html_url(self): url = reverse('cal:event_edit', args=(self.id,)) - return f' {self.title} ' + return f' {self.brekky or "-"}
{self.lunch or "-"}
{self.dinner or "-"} ' diff --git a/cal/templates/cal/base.html b/cal/templates/cal/base.html index 6efa760..54656e4 100644 --- a/cal/templates/cal/base.html +++ b/cal/templates/cal/base.html @@ -1,4 +1,4 @@ -{% load staticfiles %} +{% load static %} diff --git a/cal/templates/registration/login.html b/cal/templates/registration/login.html new file mode 100644 index 0000000..088bbda --- /dev/null +++ b/cal/templates/registration/login.html @@ -0,0 +1,29 @@ + + + + + + + + + + Login + + + +
+

Login

+
+ {% csrf_token %} + + + + + +
+ + + + + + \ No newline at end of file diff --git a/cal/urls.py b/cal/urls.py index 1a45861..d82e072 100644 --- a/cal/urls.py +++ b/cal/urls.py @@ -1,10 +1,10 @@ from django.conf.urls import url +from django.urls import path from . import views app_name = 'cal' urlpatterns = [ - url(r'^index/$', views.index, name='index'), - url(r'^calendar/$', views.CalendarView.as_view(), name='calendar'), + url(r'^$', views.CalendarView.as_view(), name='calendar'), url(r'^event/new/$', views.event, name='event_new'), url(r'^event/edit/(?P\d+)/$', views.event, name='event_edit'), ] diff --git a/cal/utils.py b/cal/utils.py index 0a5e4a6..9a1535a 100644 --- a/cal/utils.py +++ b/cal/utils.py @@ -2,6 +2,8 @@ from datetime import datetime, timedelta from calendar import HTMLCalendar from .models import Event +from django.urls import reverse_lazy + class Calendar(HTMLCalendar): def __init__(self, year=None, month=None): self.year = year @@ -11,10 +13,13 @@ class Calendar(HTMLCalendar): # formats a day as a td # filter events by day def formatday(self, day, events): - events_per_day = events.filter(start_time__day=day) + events_per_day = events.filter(date__day=day) d = '' for event in events_per_day: - d += f'
  • {event.get_html_url}
  • ' + d += event.get_html_url + + if not d: + d = f'Create Menu' if day != 0: return f"{day}" @@ -30,7 +35,7 @@ class Calendar(HTMLCalendar): # formats a month as a table # filter events by year and month def formatmonth(self, withyear=True): - events = Event.objects.filter(start_time__year=self.year, start_time__month=self.month) + events = Event.objects.filter(date__year=self.year, date__month=self.month) cal = f'\n' cal += f'{self.formatmonthname(self.year, self.month, withyear=withyear)}\n' diff --git a/cal/views.py b/cal/views.py index 5250112..00ae0df 100644 --- a/cal/views.py +++ b/cal/views.py @@ -1,9 +1,13 @@ from datetime import datetime, timedelta, date + from django.shortcuts import render, get_object_or_404 from django.http import HttpResponse, HttpResponseRedirect from django.views import generic from django.urls import reverse from django.utils.safestring import mark_safe +from django.contrib.auth.mixins import LoginRequiredMixin +from django.contrib.auth.decorators import login_required + import calendar from .models import * @@ -13,7 +17,7 @@ from .forms import EventForm def index(request): return HttpResponse('hello') -class CalendarView(generic.ListView): +class CalendarView(LoginRequiredMixin, generic.ListView): model = Event template_name = 'cal/calendar.html' @@ -46,6 +50,7 @@ def next_month(d): month = 'month=' + str(next_month.year) + '-' + str(next_month.month) return month +@login_required def event(request, event_id=None): instance = Event() if event_id: @@ -53,7 +58,7 @@ def event(request, event_id=None): else: instance = Event() - form = EventForm(request.POST or None, instance=instance) + form = EventForm(request.POST or None, instance=instance, initial={"date": request.GET.get("date", "")} if not event_id else None) if request.POST and form.is_valid(): form.save() return HttpResponseRedirect(reverse('cal:calendar')) diff --git a/djangocalendar/urls.py b/djangocalendar/urls.py index 1d034a1..37926e5 100644 --- a/djangocalendar/urls.py +++ b/djangocalendar/urls.py @@ -19,4 +19,5 @@ from django.urls import path, include urlpatterns = [ path('admin/', admin.site.urls), path('', include('cal.urls')), + path('accounts/', include('django.contrib.auth.urls')), ] diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..4ab62d1 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,2 @@ +django +mysqlclient