+
+
+
+
+
+
+
+ Django Calendar App
+
+
+
+ {% block content %}
+ {% endblock %}
+
+
+
+
+
+
+
+ {% block script %}
+ {% endblock %}
+
+
\ No newline at end of file
diff --git a/cal/templates/cal/calendar.html b/cal/templates/cal/calendar.html
new file mode 100644
index 0000000..0a104b0
--- /dev/null
+++ b/cal/templates/cal/calendar.html
@@ -0,0 +1,5 @@
+{% extends 'cal/base.html' %}
+
+{% block content %}
+{{ calendar }}
+{% endblock %}
\ No newline at end of file
diff --git a/cal/urls.py b/cal/urls.py
index 0cba310..9c29758 100644
--- a/cal/urls.py
+++ b/cal/urls.py
@@ -3,5 +3,6 @@ from . import views
app_name = 'cal'
urlpatterns = [
- url(r'', views.index, name='index'),
+ url(r'^index/$', views.index, name='index'),
+ url(r'^calendar/$', views.CalendarView.as_view(), name='calendar'),
]
diff --git a/cal/utils.py b/cal/utils.py
new file mode 100644
index 0000000..3bd1fa6
--- /dev/null
+++ b/cal/utils.py
@@ -0,0 +1,40 @@
+from datetime import datetime, timedelta
+from calendar import HTMLCalendar
+from .models import Event
+
+class Calendar(HTMLCalendar):
+ def __init__(self, year=None, month=None):
+ self.year = year
+ self.month = month
+ super(Calendar, self).__init__()
+
+ # formats a day as a td
+ # filter events by day
+ def formatday(self, day, events):
+ events_per_day = events.filter(start_time__day=day)
+ d = ''
+ for event in events_per_day:
+ d += f'
{event.title}
'
+
+ if day != 0:
+ return f"
{day}
{d}
"
+ return '
'
+
+ # formats a week as a tr
+ def formatweek(self, theweek, events):
+ week = ''
+ for d, weekday in theweek:
+ week += self.formatday(d, events)
+ return f'
{week}
'
+
+ # 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)
+
+ cal = f'
\n'
+ cal += f'{self.formatmonthname(self.year, self.month, withyear=withyear)}\n'
+ cal += f'{self.formatweekheader()}\n'
+ for week in self.monthdays2calendar(self.year, self.month):
+ cal += f'{self.formatweek(week, events)}\n'
+ return cal
diff --git a/cal/views.py b/cal/views.py
index 4171384..bc862ca 100644
--- a/cal/views.py
+++ b/cal/views.py
@@ -1,8 +1,29 @@
+from datetime import datetime
from django.shortcuts import render
from django.http import HttpResponse
+from django.views import generic
+from django.utils.safestring import mark_safe
-# Create your views here.
-
+from .models import *
+from .utils import Calendar
def index(request):
return HttpResponse('hello')
+
+class CalendarView(generic.ListView):
+ model = Event
+ template_name = 'cal/calendar.html'
+
+ def get_context_data(self, **kwargs):
+ context = super().get_context_data(**kwargs)
+ d = get_date(self.request.GET.get('day', None))
+ cal = Calendar(d.year, d.month)
+ html_cal = cal.formatmonth(withyear=True)
+ context['calendar'] = mark_safe(html_cal)
+ return context
+
+def get_date(req_day):
+ if req_day:
+ year, month = (int(x) for x in req_day.split('-'))
+ return date(year, month, day=1)
+ return datetime.today()
\ No newline at end of file
diff --git a/djangocalendar/settings.py b/djangocalendar/settings.py
index a650488..af200ef 100644
--- a/djangocalendar/settings.py
+++ b/djangocalendar/settings.py
@@ -37,6 +37,7 @@ INSTALLED_APPS = [
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
+ 'cal.apps.CalConfig',
]
MIDDLEWARE = [
diff --git a/djangocalendar/urls.py b/djangocalendar/urls.py
index 98bde03..1d034a1 100644
--- a/djangocalendar/urls.py
+++ b/djangocalendar/urls.py
@@ -17,6 +17,6 @@ from django.contrib import admin
from django.urls import path, include
urlpatterns = [
- path('', include('cal.urls')),
path('admin/', admin.site.urls),
+ path('', include('cal.urls')),
]