society-self-service/sspromotions/utils.py

92 lines
3.1 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, groups, events, bulbegin, bulend):
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(any(g.id == x for g in groups) for x in item.also_limit):
items.append(item)
if len(items) > 0:
groups_data.append({
'group': group,
'name': group.name,
'bulletin_items': items
})
more = []
all_groups = models.Group.objects.all()
for group in all_groups:
if group in groups:
continue
if not group.subscribable:
continue
for item in group.bulletinitem_set.filter(date__gte=bulbegin).filter(date__lt=bulend).all():
# Check also_limit
# Allow matching across all groups, but only subscribable ones
if member is None or len(item.also_limit) == 0 or any(any(g.id == x and g.subscribable for g in all_groups) for x in item.also_limit):
more.append(item)
if len(more) >= 3:
break
return {
'member': member,
'events': events,
'groups': groups_data,
'more': more,
'bulbegin': bulbegin,
'bulend': bulend
}