calendar-social/calsocial/calendar_system/gregorian.py

65 lines
1.6 KiB
Python
Raw Normal View History

2018-06-25 07:01:13 +00:00
from datetime import datetime, timedelta
from flask_babel import lazy_gettext as _
from . import CalendarSystem
class GregorianCalendar(CalendarSystem):
__name__ = _('Gregorian')
__has_months__ = True
START_DAY = 0
END_DAY = 7
day_names = (
_('Monday'),
_('Tuesday'),
_('Wednesday'),
_('Thursday'),
_('Friday'),
_('Saturday'),
_('Sunday'),
)
def __init__(self, timestamp):
self.timestamp = datetime.utcfromtimestamp(timestamp)
@property
def month(self):
return self.timestamp.strftime("%B, %Y")
@property
def days(self):
day_list = []
start_day = self.timestamp.replace(day=1)
while start_day.weekday() > self.START_DAY:
start_day -= timedelta(days=1)
day_list.append(start_day)
current_day = start_day
while current_day.weekday() < self.END_DAY and current_day.month <= self.timestamp.month:
current_day += timedelta(days=1)
day_list.append(current_day)
return day_list
2018-06-29 14:14:35 +00:00
def day_events(self, date, user=None):
from ..models import Event
2018-06-29 14:14:35 +00:00
events = Event.query
if user:
events = events.filter(Event.user == user)
start_timestamp = date.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)) |
((Event.end_time >= start_timestamp) & (Event.end_time < end_timestamp)))
return events