Turned into menu scheduler
This commit is contained in:
parent
41741ab37f
commit
21df0a32d3
9 changed files with 57 additions and 17 deletions
|
@ -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',)
|
||||
|
|
|
@ -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'<a href="{url}"> {self.title} </a>'
|
||||
return f'<a href="{url}" style="color:burlywood;"> {self.brekky or "-"} </a><br/><a href="{url}" style="color:blue;"> {self.lunch or "-"} </a><br/><a href="{url}" style="color:green;"> {self.dinner or "-"} </a>'
|
||||
|
|
|
@ -1,4 +1,4 @@
|
|||
{% load staticfiles %}
|
||||
{% load static %}
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
|
||||
|
|
29
cal/templates/registration/login.html
Normal file
29
cal/templates/registration/login.html
Normal file
|
@ -0,0 +1,29 @@
|
|||
|
||||
|
||||
<html>
|
||||
|
||||
<head>
|
||||
<link rel="stylesheet" href="/static/frontend/assets/css/login.css">
|
||||
<link href="https://fonts.googleapis.com/css?family=Ubuntu" rel="stylesheet">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1" />
|
||||
<link rel="stylesheet" href="path/to/font-awesome/css/font-awesome.min.css">
|
||||
<title>Login</title>
|
||||
</head>
|
||||
|
||||
<body>
|
||||
<div class="main">
|
||||
<p class="sign" align="center">Login</p>
|
||||
<form class="form1" method="post">
|
||||
{% csrf_token %}
|
||||
<input class="un " type="text" align="center" name="username" placeholder="Benutzername">
|
||||
<input class="pass" type="password" name="password" align="center" placeholder="Passwort">
|
||||
<button class="submit" align="center">Einloggen</button>
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
||||
|
|
@ -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<event_id>\d+)/$', views.event, name='event_edit'),
|
||||
]
|
||||
|
|
11
cal/utils.py
11
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'<li> {event.get_html_url} </li>'
|
||||
d += event.get_html_url
|
||||
|
||||
if not d:
|
||||
d = f'<a href="{reverse_lazy("cal:event_new")}?date={self.year}-{self.month}-{day}">Create Menu</a>'
|
||||
|
||||
if day != 0:
|
||||
return f"<td><span class='date'>{day}</span><ul> {d} </ul></td>"
|
||||
|
@ -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'<table border="0" cellpadding="0" cellspacing="0" class="calendar">\n'
|
||||
cal += f'{self.formatmonthname(self.year, self.month, withyear=withyear)}\n'
|
||||
|
|
|
@ -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'))
|
||||
|
|
|
@ -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')),
|
||||
]
|
||||
|
|
2
requirements.txt
Normal file
2
requirements.txt
Normal file
|
@ -0,0 +1,2 @@
|
|||
django
|
||||
mysqlclient
|
Loading…
Reference in a new issue