Titivillus/api/views.py

61 lines
1.5 KiB
Python

#!/usr/bin/env python3
"""
API endpoints.
"""
from user_messages import api
from django.http import HttpResponse
from django.shortcuts import redirect
from django.views.decorators.http import require_POST
from quest.models import Quest
def index(request):
"""An index page."""
return HttpResponse('Hello.')
@require_POST
def set_session(request):
"""
API endpoint for setting certain values in the users session.
"""
for key, value in request.POST.items():
if key not in ['hide_header', 'hide_chat']:
continue
if value == 'on':
request.session[key] = True
elif value == 'off':
request.session[key] = False
return HttpResponse('true')
@require_POST
def subscribe(request):
"""Endpoint for users to subscribe to a quest."""
if not request.user.is_authenticated:
return redirect('login:index')
quest_id = request.POST.get('quest_id')
try:
quest = Quest.objects.get(id=quest_id)
except Quest.DoesNotExist:
return redirect('quest:quest', quest_id=quest_id, page_num='0')
request.user.subscriptions.add(quest)
return redirect('quest:quest', quest_id=quest.id, page_num='0')
@require_POST
def clear_notification(request):
"""Endpoint for users to clear a message notification."""
if not request.user.is_authenticated:
return redirect('login:index')
msg_id = request.POST.get('msg_id')
messages = api.get_messages(user=request.user)
message = [m for m in messages if str(m.id) == msg_id]
if message:
message = message[0]
else:
return HttpResponse('false')
message.delete()
return HttpResponse('true')