Add the previous/next year/month links to the month view

This commit is contained in:
Gergely Polonkai 2018-07-02 10:53:15 +02:00
parent f41cf43f80
commit 4c03829264
3 changed files with 138 additions and 5 deletions

View File

@ -18,7 +18,7 @@ from datetime import datetime
from functools import wraps from functools import wraps
import os import os
from flask import Flask, current_app, redirect, render_template, url_for from flask import Flask, current_app, redirect, render_template, request, url_for
from flask_babelex import Babel, get_locale as babel_get_locale from flask_babelex import Babel, get_locale as babel_get_locale
from flask_security import SQLAlchemyUserDatastore, current_user, login_required from flask_security import SQLAlchemyUserDatastore, current_user, login_required
@ -40,8 +40,12 @@ def get_locale():
def template_vars(): def template_vars():
now = datetime.utcnow()
return { return {
'lang': babel_get_locale().language, 'lang': babel_get_locale().language,
'now': now,
'now_ts': now.timestamp(),
} }
@ -94,7 +98,12 @@ class CalendarSocialApp(Flask):
if not current_user.is_authenticated: if not current_user.is_authenticated:
return render_template('welcome.html') return render_template('welcome.html')
calendar = GregorianCalendar(datetime.utcnow().timestamp()) try:
timestamp = datetime.fromtimestamp(float(request.args.get('date')))
except TypeError:
timestamp = datetime.utcnow()
calendar = GregorianCalendar(timestamp.timestamp())
return render_template('index.html', calendar=calendar) return render_template('index.html', calendar=calendar)

View File

@ -1,9 +1,19 @@
from datetime import datetime, timedelta from datetime import datetime, timedelta
from functools import wraps
from flask_babelex import lazy_gettext as _ from flask_babelex import lazy_gettext as _
from . import CalendarSystem from . import CalendarSystem
def to_timestamp(func):
@wraps(func)
def decorator(*args, **kwargs):
return func(*args, **kwargs).timestamp()
return decorator
class GregorianCalendar(CalendarSystem): class GregorianCalendar(CalendarSystem):
__name__ = _('Gregorian') __name__ = _('Gregorian')
@ -12,6 +22,21 @@ class GregorianCalendar(CalendarSystem):
START_DAY = 0 START_DAY = 0
END_DAY = 7 END_DAY = 7
month_names = (
_('January'),
_('February'),
_('March'),
_('April'),
_('May'),
_('June'),
_('July'),
_('August'),
_('September'),
_('October'),
_('November'),
_('December'),
)
day_names = ( day_names = (
_('Monday'), _('Monday'),
_('Tuesday'), _('Tuesday'),
@ -47,6 +72,76 @@ class GregorianCalendar(CalendarSystem):
return day_list return day_list
@property
@to_timestamp
def prev_year(self):
return self.timestamp.replace(year=self.timestamp.year - 1)
@property
def prev_year_year(self):
return self.timestamp.replace(year=self.timestamp.year - 1).year
@property
@to_timestamp
def prev_month(self):
if self.timestamp.month == 1:
return self.prev_year.replace(month=12)
return self.timestamp.replace(month=self.timestamp.month - 1)
@property
def prev_month_name(self):
if self.timestamp.month == 1:
ts = self.prev_year.replace(month=12)
else:
ts = self.timestamp.replace(month=self.timestamp.month - 1)
return self.month_names[ts.month - 1]
@property
@to_timestamp
def next_month(self):
if self.timestamp.month == 12:
return self.next_year.replace(month=1)
return self.timestamp.replace(month=self.timestamp.month + 1)
@property
def next_month_name(self):
if self.timestamp.month == 12:
ts = self.prev_year.replace(month=1)
else:
ts = self.timestamp.replace(month=self.timestamp.month + 1)
return self.month_names[ts.month - 1]
@property
@to_timestamp
def next_year(self):
return self.timestamp.replace(year=self.timestamp.year + 1)
@property
def next_year_year(self):
return self.timestamp.replace(year=self.timestamp.year + 1).year
@property
def has_today(self):
now = datetime.utcnow()
month_start_timestamp = self.timestamp.replace(day=1,
hour=0,
minute=0,
second=0,
microsecond=0)
if self.timestamp.month == 12:
next_month = 1
else:
next_month = self.timestamp.month + 1
month_end_timestamp = month_start_timestamp.replace(month=next_month)
return now >= month_start_timestamp and now < month_end_timestamp
def day_events(self, date, user=None): def day_events(self, date, user=None):
from ..models import Event from ..models import Event
@ -55,7 +150,7 @@ class GregorianCalendar(CalendarSystem):
if user: if user:
events = events.filter(Event.user == user) events = events.filter(Event.user == user)
start_timestamp = date.replace(hour=0, minute=0, second=0, microsecond=0) start_timestamp = self.timestamp.replace(hour=0, minute=0, second=0, microsecond=0)
end_timestamp = start_timestamp + timedelta(days=1) end_timestamp = start_timestamp + timedelta(days=1)
events = events.filter(((Event.start_time >= start_timestamp) & (Event.start_time < end_timestamp)) | events = events.filter(((Event.start_time >= start_timestamp) & (Event.start_time < end_timestamp)) |

View File

@ -15,6 +15,15 @@
padding-bottom: .5em; padding-bottom: .5em;
} }
tr.month > td > a {
font-weight: normal;
color: black;
}
tr.month > td.month-name > a {
font-weight: bold;
}
tr.days > td { tr.days > td {
text-align: center; text-align: center;
border-bottom: 2px solid black; border-bottom: 2px solid black;
@ -46,7 +55,27 @@
<table class="calendar"> <table class="calendar">
<thead> <thead>
<tr class="month"> <tr class="month">
<td colspan="7">{{ calendar.month }}</td> <td>
<a href="{{ url_for('hello', date=calendar.prev_year) }}">« {{ calendar.prev_year_year }}</a>
</td>
<td>
<a href="{{ url_for('hello', date=calendar.prev_month) }}"> {{ calendar.prev_month_name }}</a>
</td>
<td colspan="3" class="month-name">
{% if not calendar.has_today %}
<a href="{{ url_for('hello', date=now_ts) }}">
{% endif %}
{{ calendar.month }}
{% if not calendar.has_today %}
</a>
{% endif %}
</td>
<td>
<a href="{{ url_for('hello', date=calendar.next_month) }}">{{ calendar.next_month_name }} </a>
</td>
<td>
<a href="{{ url_for('hello', date=calendar.next_year) }}">{{ calendar.next_year_year }} »</a>
</td>
</tr> </tr>
<tr class="days"> <tr class="days">
{% for day in calendar.day_names %} {% for day in calendar.day_names %}
@ -68,7 +97,7 @@
{%- endif %} {%- endif %}
<tr class="week"> <tr class="week">
{%- endif %} {%- endif %}
<td class="{% if day.month != calendar.timestamp.month %} other-month{% endif %}{% if day.date() == calendar.timestamp.date() %} today{% endif %}"> <td class="{% if day.month != calendar.timestamp.month %} other-month{% endif %}{% if day.date() == now.date() %} today{% endif %}">
<span class="day-num">{{ day.day }}</span> <span class="day-num">{{ day.day }}</span>
{% for event in calendar.day_events(day, user=current_user) %} {% for event in calendar.day_events(day, user=current_user) %}
<div class="event"> <div class="event">