Remove calendar code, integrate Google Calendar

This commit is contained in:
Yingtong Li 2019-01-25 11:24:10 +11:00
parent eb01c38914
commit 4727a02914
Signed by: RunasSudo
GPG Key ID: 7234E476BF21C61A
7 changed files with 26 additions and 111 deletions

View File

@ -4,3 +4,4 @@ social-auth-app-django==2.1.0
jsonfield==2.0.2 jsonfield==2.0.2
Pillow==5.4.1 Pillow==5.4.1
Markdown==3.0.1 Markdown==3.0.1
google-api-python-client==1.7.7

View File

@ -125,6 +125,9 @@ SOCIAL_AUTH_GOOGLE_OAUTH2_KEY = None # FIXME
SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = None # FIXME SOCIAL_AUTH_GOOGLE_OAUTH2_SECRET = None # FIXME
#SOCIAL_AUTH_GOOGLE_OAUTH2_AUTH_EXTRA_ARGUMENTS = {'hd': 'monash.edu'} # To restrict to a particular domain #SOCIAL_AUTH_GOOGLE_OAUTH2_AUTH_EXTRA_ARGUMENTS = {'hd': 'monash.edu'} # To restrict to a particular domain
GOOGLE_API_KEY = None # FIXME
GOOGLE_CALENDAR_ID = None # FIXME
SOCIAL_AUTH_PIPELINE = ( SOCIAL_AUTH_PIPELINE = (
'social_core.pipeline.social_auth.social_details', 'social_core.pipeline.social_auth.social_details',
'social_core.pipeline.social_auth.social_uid', 'social_core.pipeline.social_auth.social_uid',

View File

@ -56,7 +56,7 @@
<a class="ui card" href="{{ url('promotions') }}"> <a class="ui card" href="{{ url('promotions') }}">
<div class="content"> <div class="content">
<div class="header">Promotions Hub</div> <div class="header">Promotions Hub</div>
<div class="description">Create bulletin items. Add calendar events.</div> <div class="description">Create and manage bulletin items.</div>
</div> </div>
<div class="ui bottom attached primary button"> <div class="ui bottom attached primary button">
<i class="chevron right icon" style="margin: 0 .42857143em 0 -.21428571em;"></i> <i class="chevron right icon" style="margin: 0 .42857143em 0 -.21428571em;"></i>

View File

@ -31,13 +31,6 @@
<a class="{% if request.resolver_match.url_name == 'bulletin_preview' %}active {% endif %}item" href="{{ url('bulletin_preview') }}">Preview the bulletin</a> <a class="{% if request.resolver_match.url_name == 'bulletin_preview' %}active {% endif %}item" href="{{ url('bulletin_preview') }}">Preview the bulletin</a>
</div> </div>
</div> </div>
<div class="item">
Calendar
<div class="menu">
<a class="{% if request.resolver_match.url_name == 'calendar_list' %}active {% endif %}item" href="{{ url('calendar_list') }}">Your calendar events</a>
<a class="{% if request.resolver_match.url_name == 'calendar_new' %}active {% endif %}item" href="{{ url('calendar_new') }}">Create new calendar event</a>
</div>
</div>
</div> </div>
</div> </div>

View File

@ -62,13 +62,3 @@ class BulletinItem(models.Model):
if user.email in self.author.delegates: if user.email in self.author.delegates:
return True return True
return False return False
class CalendarItem(models.Model):
group = models.ForeignKey(Group, on_delete=models.CASCADE)
also_limit = JSONField(default=[])
title = models.CharField(max_length=100)
link = models.CharField(max_length=100, null=True)
date = models.DateField()
class Meta:
ordering = ['date', 'title', 'id']

View File

@ -24,9 +24,5 @@ urlpatterns = [
path('bulletin/edit/<int:id>', views.bulletin_edit, name='bulletin_edit'), path('bulletin/edit/<int:id>', views.bulletin_edit, name='bulletin_edit'),
path('bulletin/delete/<int:id>', views.bulletin_delete, name='bulletin_delete'), path('bulletin/delete/<int:id>', views.bulletin_delete, name='bulletin_delete'),
path('bulletin/preview/', views.bulletin_preview, name='bulletin_preview'), path('bulletin/preview/', views.bulletin_preview, name='bulletin_preview'),
path('calendar/', views.calendar_list, name='calendar_list'),
path('calendar/new/', views.calendar_new, name='calendar_new'),
path('calendar/edit/<int:id>', views.calendar_edit, name='calendar_edit'),
path('calendar/delete/<int:id>', views.calendar_delete, name='calendar_delete'),
path('', views.index, name='promotions'), path('', views.index, name='promotions'),
] ]

View File

@ -14,8 +14,11 @@
# You should have received a copy of the GNU Affero General Public License # 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/>. # along with this program. If not, see <https://www.gnu.org/licenses/>.
from googleapiclient.discovery import build
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.conf import settings
from django.http import HttpResponse from django.http import HttpResponse
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
from django.urls import reverse from django.urls import reverse
@ -24,11 +27,28 @@ from django.utils import timezone
from . import models from . import models
import datetime import datetime
import json
@login_required @login_required
def index(request): def index(request):
return render(request, 'sspromotions/index.html') return render(request, 'sspromotions/index.html')
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']
}
@login_required @login_required
def bulletin_list(request): def bulletin_list(request):
items_past = [] items_past = []
@ -64,7 +84,7 @@ def bulletin_preview(request):
bulend = bulbegin + datetime.timedelta(days=7) bulend = bulbegin + datetime.timedelta(days=7)
return render(request, 'sspromotions/email/bulletin.html', { return render(request, 'sspromotions/email/bulletin.html', {
'events': models.CalendarItem.objects.filter(date__gte=calbegin).filter(date__lt=calend).all(), 'events': get_calendar_events(calbegin, calend),
'groups': models.Group.objects.all(), 'groups': models.Group.objects.all(),
'more': [], 'more': [],
'bulbegin': bulbegin, 'bulbegin': bulbegin,
@ -137,91 +157,3 @@ def bulletin_delete(request, id):
item.delete() item.delete()
return redirect(reverse('bulletin_list')) return redirect(reverse('bulletin_list'))
@login_required
def calendar_list(request):
items_past = []
items_upcoming = []
items_future = []
dtbegin = timezone.now().date() + datetime.timedelta(days=1) # Start tomorrow for calendar
dtend = dtbegin + datetime.timedelta(days=14)
for item in models.CalendarItem.objects.all():
if not item.group.can_user_access(request.user):
continue
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)
return render(request, 'sspromotions/calendar_list.html', {
'items_past': items_past,
'items_upcoming': items_upcoming,
'items_future': items_future
})
@login_required
def calendar_new(request):
if request.method == 'POST':
item = models.CalendarItem()
item.group = models.Group.objects.get(id=int(request.POST['group']))
if not item.group.can_user_access(request.user):
return HttpResponse('Unauthorized', status=401)
item.title = request.POST['title']
item.date = request.POST['date']
item.link = request.POST['link']
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('calendar_list'))
else:
return redirect(reverse('calendar_edit', kwargs={'id': item.id}))
else:
item = models.CalendarItem()
item.date = timezone.now()
return render(request, 'sspromotions/calendar_edit.html', {
'item': item,
'groups': [group for group in models.Group.objects.all() if group.can_user_access(request.user)],
'all_groups': models.Group.objects.all()
})
@login_required
def calendar_edit(request, id):
if request.method == 'POST':
item = models.CalendarItem.objects.get(id=id)
item.group = models.Group.objects.get(id=int(request.POST['group']))
if not item.group.can_user_access(request.user):
return HttpResponse('Unauthorized', status=401)
item.title = request.POST['title']
item.date = request.POST['date']
item.link = request.POST['link']
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('calendar_list'))
else:
return redirect(reverse('calendar_edit', kwargs={'id': item.id}))
else:
item = models.CalendarItem.objects.get(id=id)
if not item.group.can_user_access(request.user):
return HttpResponse('Unauthorized', status=401)
return render(request, 'sspromotions/calendar_edit.html', {
'item': item,
'groups': [group for group in models.Group.objects.all() if group.can_user_access(request.user)],
'all_groups': models.Group.objects.all()
})
@login_required
def calendar_delete(request, id):
item = models.CalendarItem.objects.get(id=id)
if not item.group.can_user_access(request.user):
return HttpResponse('Unauthorized', status=401)
item.delete()
return redirect(reverse('calendar_list'))