2018-07-31 06:07:14 +00:00
|
|
|
from datetime import datetime, timedelta, date
|
|
|
|
from django.shortcuts import render, get_object_or_404
|
|
|
|
from django.http import HttpResponse, HttpResponseRedirect
|
2018-07-25 06:10:26 +00:00
|
|
|
from django.views import generic
|
2018-07-31 06:07:14 +00:00
|
|
|
from django.urls import reverse
|
2018-07-25 06:10:26 +00:00
|
|
|
from django.utils.safestring import mark_safe
|
2018-07-31 06:07:14 +00:00
|
|
|
import calendar
|
2018-07-02 06:06:17 +00:00
|
|
|
|
2018-07-25 06:10:26 +00:00
|
|
|
from .models import *
|
|
|
|
from .utils import Calendar
|
2018-07-31 06:07:14 +00:00
|
|
|
from .forms import EventForm
|
2018-07-02 06:06:17 +00:00
|
|
|
|
|
|
|
def index(request):
|
|
|
|
return HttpResponse('hello')
|
2018-07-25 06:10:26 +00:00
|
|
|
|
|
|
|
class CalendarView(generic.ListView):
|
|
|
|
model = Event
|
|
|
|
template_name = 'cal/calendar.html'
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = super().get_context_data(**kwargs)
|
2018-07-31 06:07:14 +00:00
|
|
|
d = get_date(self.request.GET.get('month', None))
|
2018-07-25 06:10:26 +00:00
|
|
|
cal = Calendar(d.year, d.month)
|
|
|
|
html_cal = cal.formatmonth(withyear=True)
|
|
|
|
context['calendar'] = mark_safe(html_cal)
|
2018-07-31 06:07:14 +00:00
|
|
|
context['prev_month'] = prev_month(d)
|
|
|
|
context['next_month'] = next_month(d)
|
2018-07-25 06:10:26 +00:00
|
|
|
return context
|
|
|
|
|
2018-07-31 06:07:14 +00:00
|
|
|
def get_date(req_month):
|
|
|
|
if req_month:
|
|
|
|
year, month = (int(x) for x in req_month.split('-'))
|
2018-07-25 06:10:26 +00:00
|
|
|
return date(year, month, day=1)
|
2018-07-31 06:07:14 +00:00
|
|
|
return datetime.today()
|
|
|
|
|
|
|
|
def prev_month(d):
|
|
|
|
first = d.replace(day=1)
|
|
|
|
prev_month = first - timedelta(days=1)
|
|
|
|
month = 'month=' + str(prev_month.year) + '-' + str(prev_month.month)
|
|
|
|
return month
|
|
|
|
|
|
|
|
def next_month(d):
|
|
|
|
days_in_month = calendar.monthrange(d.year, d.month)[1]
|
|
|
|
last = d.replace(day=days_in_month)
|
|
|
|
next_month = last + timedelta(days=1)
|
|
|
|
month = 'month=' + str(next_month.year) + '-' + str(next_month.month)
|
|
|
|
return month
|
|
|
|
|
|
|
|
def event(request, event_id=None):
|
|
|
|
instance = Event()
|
|
|
|
if event_id:
|
|
|
|
instance = get_object_or_404(Event, pk=event_id)
|
|
|
|
else:
|
|
|
|
instance = Event()
|
|
|
|
|
|
|
|
form = EventForm(request.POST or None, instance=instance)
|
|
|
|
if request.POST and form.is_valid():
|
|
|
|
form.save()
|
|
|
|
return HttpResponseRedirect(reverse('cal:calendar'))
|
|
|
|
return render(request, 'cal/event.html', {'form': form})
|