From c3eb1b27d405af9686b541a7ad442efbe9313cd7 Mon Sep 17 00:00:00 2001 From: Hui Wen Date: Tue, 24 Jul 2018 23:10:26 -0700 Subject: [PATCH] First version of calendar done --- cal/admin.py | 2 ++ cal/migrations/0001_initial.py | 24 ++++++++++++++++++++ cal/models.py | 7 ++++++ cal/static/cal/css/styles.css | 37 ++++++++++++++++++++++++++++++ cal/templates/cal/base.html | 34 ++++++++++++++++++++++++++++ cal/templates/cal/calendar.html | 5 +++++ cal/urls.py | 3 ++- cal/utils.py | 40 +++++++++++++++++++++++++++++++++ cal/views.py | 25 +++++++++++++++++++-- djangocalendar/settings.py | 1 + djangocalendar/urls.py | 2 +- 11 files changed, 176 insertions(+), 4 deletions(-) create mode 100644 cal/migrations/0001_initial.py create mode 100644 cal/static/cal/css/styles.css create mode 100644 cal/templates/cal/base.html create mode 100644 cal/templates/cal/calendar.html create mode 100644 cal/utils.py diff --git a/cal/admin.py b/cal/admin.py index 8c38f3f..342cc9a 100644 --- a/cal/admin.py +++ b/cal/admin.py @@ -1,3 +1,5 @@ from django.contrib import admin +from cal.models import Event # Register your models here. +admin.site.register(Event) \ No newline at end of file diff --git a/cal/migrations/0001_initial.py b/cal/migrations/0001_initial.py new file mode 100644 index 0000000..5e655a0 --- /dev/null +++ b/cal/migrations/0001_initial.py @@ -0,0 +1,24 @@ +# Generated by Django 2.0.6 on 2018-07-04 18:14 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Event', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('title', models.CharField(max_length=200)), + ('description', models.TextField()), + ('start_time', models.DateTimeField()), + ('end_time', models.DateTimeField()), + ], + ), + ] diff --git a/cal/models.py b/cal/models.py index 71a8362..8636940 100644 --- a/cal/models.py +++ b/cal/models.py @@ -1,3 +1,10 @@ from django.db import models # Create your models here. + + +class Event(models.Model): + title = models.CharField(max_length=200) + description = models.TextField() + start_time = models.DateTimeField() + end_time = models.DateTimeField() diff --git a/cal/static/cal/css/styles.css b/cal/static/cal/css/styles.css new file mode 100644 index 0000000..4d0c879 --- /dev/null +++ b/cal/static/cal/css/styles.css @@ -0,0 +1,37 @@ +.calendar { + width: 100%; + font-size: 13px; +} + +.month { + font-size: 25px; +} + +tr, td { + border: 1px solid black; +} + +th { + padding: 10px; + text-align: center; + font-size: 18px; +} + +td { + width: 200px; + height: 150px; + padding: 20px 0px 0px 5px; +} + +.date { + font-size: 16px; +} + +ul { + height: 100%; + padding: 0px 5px 0px 20px; +} + +a { + color: #17a2b8; +} \ No newline at end of file diff --git a/cal/templates/cal/base.html b/cal/templates/cal/base.html new file mode 100644 index 0000000..87bbc73 --- /dev/null +++ b/cal/templates/cal/base.html @@ -0,0 +1,34 @@ +{% load staticfiles %} + + + + + + + + + + + + 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}" + 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')), ]