You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

96 lines
2.8 KiB
Python

import os.path
import re
import markdown
from markdown.extensions.codehilite import CodeHiliteExtension
from django.http import HttpResponseNotFound
from django.shortcuts import render
from django.conf import settings
from django.utils.translation import ugettext as _, get_language
from django import http
from django.utils.http import is_safe_url
from django.utils.translation import (
LANGUAGE_SESSION_KEY, check_for_language,
)
from constance import config
from .common import get_price_float
md = markdown.Markdown(extensions=[
'markdown.extensions.toc',
'markdown.extensions.meta',
CodeHiliteExtension(noclasses=True),
])
def index(request):
eur = '%.2f' % get_price_float()
return render(request, 'ccvpn/index.html', dict(eur_price=eur, motd=config.MOTD))
def chat(request):
if request.user.is_authenticated:
username = request.user.username + '|cc'
else:
username = "cc?"
ctx = dict(username=username, title=_("Live Chat"))
return render(request, 'ccvpn/chat.html', ctx)
def set_lang(request):
""" django.views.i18n.set_language() with GET """
next = request.GET.get('next', request.GET.get('next'))
if not is_safe_url(url=next, allowed_hosts={request.get_host()}):
next = request.META.get('HTTP_REFERER')
if not is_safe_url(url=next, allowed_hosts={request.get_host()}):
next = '/'
response = http.HttpResponseRedirect(next)
lang_code = request.GET.get('lang', None)
if lang_code and check_for_language(lang_code):
if hasattr(request, 'session'):
request.session[LANGUAGE_SESSION_KEY] = lang_code
else:
response.set_cookie(settings.LANGUAGE_COOKIE_NAME, lang_code,
max_age=settings.LANGUAGE_COOKIE_AGE,
path=settings.LANGUAGE_COOKIE_PATH,
domain=settings.LANGUAGE_COOKIE_DOMAIN)
return response
def page(request, name):
if not re.match('^[a-z0-9_-]{1,50}$', name):
return HttpResponseNotFound()
basename = settings.PAGES_DIR + '/' + name
username = request.user.username
page_replace = {
'USERNAME': username or '[username]',
}
files = [
basename + '.' + get_language() + '.md',
basename + '.en.md',
basename + '.md',
]
for file in files:
if not os.path.isfile(file):
continue
with open(file, encoding='utf8') as fh:
page = fh.read()
for s, r in page_replace.items():
page = page.replace('{' + s + '}', r)
page = md.convert(page)
title = md.Meta.get('title', [None])[0]
ctx = dict(content=page, title=title)
return render(request, 'ccvpn/page.html', ctx)
return HttpResponseNotFound()