79 lines
2.7 KiB
Python
79 lines
2.7 KiB
Python
|
# Society Self-Service
|
||
|
# Copyright © 2018-2019 Yingtong Li (RunasSudo)
|
||
|
#
|
||
|
# This program is free software: you can redistribute it and/or modify
|
||
|
# it under the terms of the GNU Affero General Public License as published by
|
||
|
# the Free Software Foundation, either version 3 of the License, or
|
||
|
# (at your option) any later version.
|
||
|
#
|
||
|
# This program is distributed in the hope that it will be useful,
|
||
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
|
# GNU Affero General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU Affero General Public License
|
||
|
# along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||
|
|
||
|
from googleapiclient.discovery import build
|
||
|
|
||
|
from django.conf import settings
|
||
|
from django.utils import timezone
|
||
|
|
||
|
from . import models
|
||
|
|
||
|
import datetime
|
||
|
|
||
|
def get_calendar_events(calbegin, calend):
|
||
|
service = build('calendar', 'v3', developerKey=settings.GOOGLE_API_KEY)
|
||
|
events = service.events().list(
|
||
|
calendarId=settings.GOOGLE_CALENDAR_ID,
|
||
|
orderBy='startTime',
|
||
|
singleEvents=True,
|
||
|
timeMin=datetime.datetime.combine(calbegin, datetime.time(tzinfo=timezone.get_current_timezone())).isoformat(),
|
||
|
timeMax=datetime.datetime.combine(calend, datetime.time(tzinfo=timezone.get_current_timezone())).isoformat()
|
||
|
).execute()
|
||
|
for event in events['items']:
|
||
|
yield {
|
||
|
'date': datetime.datetime.strptime(event['start']['date'], '%Y-%m-%d') if 'date' in event['start'] else datetime.datetime.strptime(event['start']['dateTime'][:-3] + event['start']['dateTime'][-2:], '%Y-%m-%dT%H:%M:%S%z'),
|
||
|
'title': event['summary'],
|
||
|
'link': event['htmlLink']
|
||
|
}
|
||
|
|
||
|
def bulletin_dates(dt):
|
||
|
calbegin = dt.date() + datetime.timedelta(days=1) # Start tomorrow for calendar
|
||
|
calend = calbegin + datetime.timedelta(days=14)
|
||
|
|
||
|
bulbegin = dt.date()
|
||
|
bulend = bulbegin + datetime.timedelta(days=7)
|
||
|
|
||
|
return calbegin, calend, bulbegin, bulend
|
||
|
|
||
|
def bulletin_args(member, events, bulbegin, bulend):
|
||
|
if member is None:
|
||
|
groups = models.Group.objects.all()
|
||
|
else:
|
||
|
groups = models.Group.get_member_groups(member)
|
||
|
|
||
|
groups_data = []
|
||
|
for group in groups:
|
||
|
items = []
|
||
|
for item in group.bulletinitem_set.filter(date__gte=bulbegin).filter(date__lt=bulend).all():
|
||
|
# Check also_limit
|
||
|
if member is None or len(item.also_limit) == 0 or any(models.Group.objects.get(id=x).contains_member(member) for x in item.also_limit if models.Group.objects.filter(id=x).count() > 0):
|
||
|
items.append(item)
|
||
|
|
||
|
if len(items) > 0:
|
||
|
groups_data.append({
|
||
|
'group': group,
|
||
|
'name': group.name,
|
||
|
'bulletin_items': items
|
||
|
})
|
||
|
|
||
|
return {
|
||
|
'events': events,
|
||
|
'groups': groups_data,
|
||
|
'more': [], # TODO
|
||
|
'bulbegin': bulbegin,
|
||
|
'bulend': bulend
|
||
|
}
|