added set_session, made hide_header work properly

This commit is contained in:
iou1name 2018-08-10 13:06:33 -04:00
parent 0f141f3525
commit abecc430a9
11 changed files with 53 additions and 8 deletions

View File

@ -1,36 +1,39 @@
<!DOCTYPE html> <!DOCTYPE html>
<html> <html>
<head> <head>
<title>{% block title %}{% endblock %} - Titivillus</title> <title>{% block title %}{% endblock %} - Titivillus</title>
<link rel="stylesheet" type="text/css" href="{{ static('base.css') }}"> <link rel="stylesheet" type="text/css" href="{{ static('base.css') }}">
<script> <script>
function toggleHeaderCookie(state) { function toggleHeaderCookie(state) {
let xhr = new XMLHttpRequest(); let xhr = new XMLHttpRequest();
xhr.open('GET', '{# url_for(".set_session", hide_header="on") #}', true); xhr.open('POST', '{{ url("set_session:index") }}', true);
xhr.send(); xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
xhr.setRequestHeader('X-CSRFToken', '{{ csrf_token }}');
xhr.send('hide_header=' + state);
} }
function toggleHeader() { function toggleHeader() {
if (document.getElementById('header').style.display == 'initial') { if (document.getElementById('header').style.display == 'initial') {
document.getElementById('header').style.display = 'none'; document.getElementById('header').style.display = 'none';
document.getElementById('headerHidden').style.display = 'initial'; document.getElementById('headerHidden').style.display = 'initial';
//toggleHeaderCookie('on'); toggleHeaderCookie('on');
} }
else { else {
document.getElementById('header').style.display = 'initial'; document.getElementById('header').style.display = 'initial';
document.getElementById('headerHidden').style.display = 'none'; document.getElementById('headerHidden').style.display = 'none';
//toggleHeaderCookie('off'); toggleHeaderCookie('off');
} }
} }
</script> </script>
{% block head %}{% endblock %} {% block head %}{% endblock %}
</head> </head>
<body> <body>
<ul id="header" class="header" style="{# if session.get("hide_header") == True %}display:none;{% else %}display:initial;{% endif #}"> <ul id="header" class="header" style="{% if request.session.get("hide_header") == True %}display:none;{% else %}display:initial;{% endif %}">
<li><a onclick="toggleHeader();" href="javascript:void(0);"></a></li> <li><a onclick="toggleHeader();" href="javascript:void(0);"></a></li>
<li><a href="{{ url('homepage:index') }}">Home</a></li> <li><a href="{{ url('homepage:index') }}">Home</a></li>
{% block header %}{% endblock %} {% block header %}{% endblock %}
</ul> </ul>
<ul id="headerHidden" class="header" style="{# if session.get("hide_header") == True %}display:initial;{% else #}display:none;{# endif #}"> <ul id="headerHidden" class="header" style="{% if request.session.get("hide_header") == True %}display:initial;{% else %}display:none;{% endif %}">
<li><a onclick="toggleHeader();" href="javascript:void(0);"></a></li> <li><a onclick="toggleHeader();" href="javascript:void(0);"></a></li>
</ul> </ul>
<div id="content">{% block content %}{% endblock %} </div> <div id="content">{% block content %}{% endblock %} </div>

0
set_session/__init__.py Normal file
View File

3
set_session/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

5
set_session/apps.py Normal file
View File

@ -0,0 +1,5 @@
from django.apps import AppConfig
class SetSessionConfig(AppConfig):
name = 'set_session'

View File

3
set_session/models.py Normal file
View File

@ -0,0 +1,3 @@
from django.db import models
# Create your models here.

3
set_session/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

12
set_session/urls.py Normal file
View File

@ -0,0 +1,12 @@
#!/usr/bin/env python3
"""
set_session app URL configuration.
"""
from django.urls import path
from . import views
app_name = 'set_session'
urlpatterns = [
path('', views.index, name='index'),
]

15
set_session/views.py Normal file
View File

@ -0,0 +1,15 @@
#!/usr/bin/env python3
"""
/set_session app views.
"""
from django.http import HttpResponse
def index(request):
"""
A simple API endpoint for setting certain values in the users session.
"""
if request.POST.get('hide_header') == 'on':
request.session['hide_header'] = True
elif request.POST.get('hide_header') == 'off':
request.session['hide_header'] = False
return HttpResponse('true')

View File

@ -10,4 +10,5 @@ urlpatterns = [
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
path('quest/', include('quest.urls')), path('quest/', include('quest.urls')),
path('create_quest/', include('create_quest.urls')), path('create_quest/', include('create_quest.urls')),
path('set_session/', include('set_session.urls')),
] ]