2018-12-07 15:53:46 +11:00
|
|
|
# Society Self-Service
|
2019-01-11 15:45:11 +11:00
|
|
|
# Copyright © 2018-2019 Yingtong Li (RunasSudo)
|
2018-12-07 15:53:46 +11:00
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
2019-01-25 11:24:10 +11:00
|
|
|
from googleapiclient.discovery import build
|
|
|
|
|
2018-12-07 15:53:46 +11:00
|
|
|
from django.contrib.auth.decorators import login_required
|
|
|
|
|
2019-01-25 11:24:10 +11:00
|
|
|
from django.conf import settings
|
2018-12-07 15:53:46 +11:00
|
|
|
from django.http import HttpResponse
|
|
|
|
from django.shortcuts import render, redirect
|
|
|
|
from django.urls import reverse
|
|
|
|
from django.utils import timezone
|
|
|
|
|
|
|
|
from . import models
|
|
|
|
|
2019-01-11 15:45:11 +11:00
|
|
|
import datetime
|
2019-01-25 11:24:10 +11:00
|
|
|
import json
|
2019-01-11 15:45:11 +11:00
|
|
|
|
2018-12-07 15:53:46 +11:00
|
|
|
@login_required
|
|
|
|
def index(request):
|
|
|
|
return render(request, 'sspromotions/index.html')
|
|
|
|
|
2019-01-25 11:24:10 +11:00
|
|
|
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']
|
|
|
|
}
|
|
|
|
|
2018-12-07 15:53:46 +11:00
|
|
|
@login_required
|
|
|
|
def bulletin_list(request):
|
|
|
|
items_past = []
|
|
|
|
items_upcoming = []
|
|
|
|
items_future = []
|
|
|
|
|
2019-01-11 15:45:11 +11:00
|
|
|
dtbegin = timezone.now().date()
|
|
|
|
dtend = dtbegin + datetime.timedelta(days=7)
|
|
|
|
|
2018-12-07 15:53:46 +11:00
|
|
|
for item in models.BulletinItem.objects.all():
|
2019-01-25 10:30:55 +11:00
|
|
|
if not item.can_user_access(request.user):
|
2018-12-07 15:53:46 +11:00
|
|
|
continue
|
|
|
|
|
2019-01-11 15:45:11 +11:00
|
|
|
if item.date >= dtbegin and item.date < dtend:
|
|
|
|
items_upcoming.append(item)
|
|
|
|
elif item.date >= dtend:
|
|
|
|
items_future.append(item)
|
|
|
|
else:
|
|
|
|
items_past.append(item)
|
2018-12-07 15:53:46 +11:00
|
|
|
|
|
|
|
return render(request, 'sspromotions/bulletin_list.html', {
|
|
|
|
'items_past': items_past,
|
|
|
|
'items_upcoming': items_upcoming,
|
|
|
|
'items_future': items_future
|
|
|
|
})
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def bulletin_preview(request):
|
2019-01-12 15:26:22 +11:00
|
|
|
calbegin = timezone.now().date() + datetime.timedelta(days=1) # Start tomorrow for calendar
|
|
|
|
calend = calbegin + datetime.timedelta(days=14)
|
|
|
|
|
|
|
|
bulbegin = timezone.now().date()
|
|
|
|
bulend = bulbegin + datetime.timedelta(days=7)
|
2019-01-11 15:45:11 +11:00
|
|
|
|
2018-12-07 15:53:46 +11:00
|
|
|
return render(request, 'sspromotions/email/bulletin.html', {
|
2019-01-25 11:24:10 +11:00
|
|
|
'events': get_calendar_events(calbegin, calend),
|
2019-01-11 15:45:11 +11:00
|
|
|
'groups': models.Group.objects.all(),
|
|
|
|
'more': [],
|
2019-01-12 15:26:22 +11:00
|
|
|
'bulbegin': bulbegin,
|
|
|
|
'bulend': bulend
|
2018-12-07 15:53:46 +11:00
|
|
|
})
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def bulletin_new(request):
|
|
|
|
if request.method == 'POST':
|
|
|
|
item = models.BulletinItem()
|
2019-01-25 10:30:55 +11:00
|
|
|
item.author = request.user
|
2018-12-07 15:53:46 +11:00
|
|
|
item.group = models.Group.objects.get(id=int(request.POST['group']))
|
|
|
|
item.title = request.POST['title']
|
|
|
|
item.date = request.POST['date']
|
|
|
|
item.content = request.POST['content']
|
|
|
|
item.link = request.POST['link']
|
|
|
|
if request.FILES:
|
|
|
|
item.image = request.FILES['image']
|
|
|
|
item.also_limit = [int(k[11:]) for k, v in request.POST.items() if k.startswith('also_limit_') and v]
|
|
|
|
item.save()
|
|
|
|
|
|
|
|
if request.POST['submit'] == 'Save':
|
|
|
|
return redirect(reverse('bulletin_list'))
|
|
|
|
else:
|
|
|
|
return redirect(reverse('bulletin_edit', kwargs={'id': item.id}))
|
|
|
|
else:
|
|
|
|
item = models.BulletinItem()
|
2019-01-25 10:30:55 +11:00
|
|
|
item.author = request.user
|
2019-01-12 15:38:02 +11:00
|
|
|
item.date = timezone.now().date()
|
|
|
|
item.date += datetime.timedelta(days=(6 - item.date.weekday() + 7) % 7) # Next Sunday (6 = Sunday)
|
2018-12-07 15:53:46 +11:00
|
|
|
return render(request, 'sspromotions/bulletin_edit.html', {
|
|
|
|
'item': item,
|
2019-01-25 10:30:55 +11:00
|
|
|
'groups': models.Group.objects.all()
|
2018-12-07 15:53:46 +11:00
|
|
|
})
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def bulletin_edit(request, id):
|
|
|
|
if request.method == 'POST':
|
|
|
|
item = models.BulletinItem.objects.get(id=id)
|
|
|
|
item.group = models.Group.objects.get(id=int(request.POST['group']))
|
2019-01-25 10:30:55 +11:00
|
|
|
if not item.can_user_access(request.user):
|
2018-12-07 15:53:46 +11:00
|
|
|
return HttpResponse('Unauthorized', status=401)
|
|
|
|
item.title = request.POST['title']
|
|
|
|
item.date = request.POST['date']
|
|
|
|
item.content = request.POST['content']
|
|
|
|
item.link = request.POST['link']
|
|
|
|
if request.FILES:
|
|
|
|
item.image = request.FILES['image']
|
|
|
|
item.also_limit = [int(k[11:]) for k, v in request.POST.items() if k.startswith('also_limit_') and v]
|
|
|
|
item.save()
|
|
|
|
|
|
|
|
if request.POST['submit'] == 'Save':
|
|
|
|
return redirect(reverse('bulletin_list'))
|
|
|
|
else:
|
|
|
|
return redirect(reverse('bulletin_edit', kwargs={'id': item.id}))
|
|
|
|
else:
|
|
|
|
item = models.BulletinItem.objects.get(id=id)
|
2019-01-25 10:30:55 +11:00
|
|
|
if not item.can_user_access(request.user):
|
2018-12-07 15:53:46 +11:00
|
|
|
return HttpResponse('Unauthorized', status=401)
|
|
|
|
return render(request, 'sspromotions/bulletin_edit.html', {
|
|
|
|
'item': item,
|
2019-01-25 10:30:55 +11:00
|
|
|
'groups': models.Group.objects.all()
|
2018-12-07 15:53:46 +11:00
|
|
|
})
|
|
|
|
|
|
|
|
@login_required
|
|
|
|
def bulletin_delete(request, id):
|
|
|
|
item = models.BulletinItem.objects.get(id=id)
|
2019-01-25 10:30:55 +11:00
|
|
|
if not item.can_user_access(request.user):
|
2018-12-07 15:53:46 +11:00
|
|
|
return HttpResponse('Unauthorized', status=401)
|
|
|
|
item.delete()
|
|
|
|
|
|
|
|
return redirect(reverse('bulletin_list'))
|