First version of calendar done
This commit is contained in:
parent
bdef3ca660
commit
c3eb1b27d4
|
@ -1,3 +1,5 @@
|
|||
from django.contrib import admin
|
||||
from cal.models import Event
|
||||
|
||||
# Register your models here.
|
||||
admin.site.register(Event)
|
24
cal/migrations/0001_initial.py
Normal file
24
cal/migrations/0001_initial.py
Normal file
|
@ -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()),
|
||||
],
|
||||
),
|
||||
]
|
|
@ -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()
|
||||
|
|
37
cal/static/cal/css/styles.css
Normal file
37
cal/static/cal/css/styles.css
Normal file
|
@ -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;
|
||||
}
|
34
cal/templates/cal/base.html
Normal file
34
cal/templates/cal/base.html
Normal file
|
@ -0,0 +1,34 @@
|
|||
{% load staticfiles %}
|
||||
<!doctype html>
|
||||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<!-- Required meta tags -->
|
||||
<meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
|
||||
<!-- Bootstrap CSS -->
|
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
|
||||
crossorigin="anonymous">
|
||||
<link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.0.10/css/all.css" integrity="sha384-+d0P83n9kaQMCwj8F4RJB66tzIwOKmrdb46+porD/OvrJ+37WqIM7UoBtwHO6Nlg"
|
||||
crossorigin="anonymous">
|
||||
<link rel="stylesheet" type="text/css" href="{% static 'cal/css/styles.css' %}">
|
||||
<title>Django Calendar App</title>
|
||||
</head>
|
||||
<body>
|
||||
|
||||
{% block content %}
|
||||
{% endblock %}
|
||||
|
||||
<!-- Optional JavaScript -->
|
||||
<!-- jQuery first, then Popper.js, then Bootstrap JS -->
|
||||
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
|
||||
crossorigin="anonymous"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
|
||||
crossorigin="anonymous"></script>
|
||||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
|
||||
crossorigin="anonymous"></script>
|
||||
|
||||
{% block script %}
|
||||
{% endblock %}
|
||||
</body>
|
||||
</html>
|
5
cal/templates/cal/calendar.html
Normal file
5
cal/templates/cal/calendar.html
Normal file
|
@ -0,0 +1,5 @@
|
|||
{% extends 'cal/base.html' %}
|
||||
|
||||
{% block content %}
|
||||
{{ calendar }}
|
||||
{% endblock %}
|
|
@ -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'),
|
||||
]
|
||||
|
|
40
cal/utils.py
Normal file
40
cal/utils.py
Normal file
|
@ -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'<li> {event.title} </li>'
|
||||
|
||||
if day != 0:
|
||||
return f"<td><span class='date'>{day}</span><ul> {d} </ul></td>"
|
||||
return '<td></td>'
|
||||
|
||||
# formats a week as a tr
|
||||
def formatweek(self, theweek, events):
|
||||
week = ''
|
||||
for d, weekday in theweek:
|
||||
week += self.formatday(d, events)
|
||||
return f'<tr> {week} </tr>'
|
||||
|
||||
# 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'<table border="0" cellpadding="0" cellspacing="0" class="calendar">\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
|
25
cal/views.py
25
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()
|
|
@ -37,6 +37,7 @@ INSTALLED_APPS = [
|
|||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'cal.apps.CalConfig',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
|
|
|
@ -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')),
|
||||
]
|
||||
|
|
Loading…
Reference in a new issue