forked from gergely/calendar-social
Add the previous/next year/month links to the month view
This commit is contained in:
@@ -1,9 +1,19 @@
|
||||
from datetime import datetime, timedelta
|
||||
from functools import wraps
|
||||
|
||||
from flask_babelex import lazy_gettext as _
|
||||
|
||||
from . import CalendarSystem
|
||||
|
||||
|
||||
def to_timestamp(func):
|
||||
@wraps(func)
|
||||
def decorator(*args, **kwargs):
|
||||
return func(*args, **kwargs).timestamp()
|
||||
|
||||
return decorator
|
||||
|
||||
|
||||
class GregorianCalendar(CalendarSystem):
|
||||
__name__ = _('Gregorian')
|
||||
|
||||
@@ -12,6 +22,21 @@ class GregorianCalendar(CalendarSystem):
|
||||
START_DAY = 0
|
||||
END_DAY = 7
|
||||
|
||||
month_names = (
|
||||
_('January'),
|
||||
_('February'),
|
||||
_('March'),
|
||||
_('April'),
|
||||
_('May'),
|
||||
_('June'),
|
||||
_('July'),
|
||||
_('August'),
|
||||
_('September'),
|
||||
_('October'),
|
||||
_('November'),
|
||||
_('December'),
|
||||
)
|
||||
|
||||
day_names = (
|
||||
_('Monday'),
|
||||
_('Tuesday'),
|
||||
@@ -47,6 +72,76 @@ class GregorianCalendar(CalendarSystem):
|
||||
|
||||
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):
|
||||
from ..models import Event
|
||||
|
||||
@@ -55,7 +150,7 @@ class GregorianCalendar(CalendarSystem):
|
||||
if 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)
|
||||
|
||||
events = events.filter(((Event.start_time >= start_timestamp) & (Event.start_time < end_timestamp)) |
|
||||
|
Reference in New Issue
Block a user