2016-04-05 19:38:09 +01:00
|
|
|
import os.path
|
2016-04-05 22:37:37 +01:00
|
|
|
from django.views.generic import FormView
|
2016-04-05 19:38:09 +01:00
|
|
|
from django.conf import settings
|
|
|
|
from django.http import HttpResponse, Http404
|
|
|
|
from django.template.loader import get_template
|
2016-04-07 21:53:44 +01:00
|
|
|
from .utils import get_context, parse_content, get_title_from_markdown, swap_page
|
2016-04-05 22:37:37 +01:00
|
|
|
from project.common.forms import ContactForm
|
2016-04-05 19:38:09 +01:00
|
|
|
|
|
|
|
|
|
|
|
def page_view(request, path):
|
|
|
|
template = None
|
2016-04-07 21:56:16 +01:00
|
|
|
if path.endswith('/'): # Strip trailing slash
|
2016-04-05 22:37:37 +01:00
|
|
|
path = path[:-1]
|
|
|
|
|
2016-04-07 21:53:44 +01:00
|
|
|
path = swap_page(path)
|
|
|
|
|
2016-04-05 19:38:09 +01:00
|
|
|
if os.path.isdir(os.path.join(settings.BASE_DIR, 'templates', path)):
|
|
|
|
path = os.path.join(path, 'index')
|
|
|
|
for extension in ['md', 'html']:
|
|
|
|
try:
|
|
|
|
template = get_template("{}.{}".format(path, extension))
|
|
|
|
break
|
|
|
|
except:
|
|
|
|
pass
|
|
|
|
if not template:
|
|
|
|
raise Http404
|
|
|
|
context = get_context(path)
|
|
|
|
parsed_content = parse_content(template.render(context, request), extension)
|
|
|
|
if extension == 'md':
|
|
|
|
template = get_template('markdown_content.html')
|
|
|
|
context['markdown_content'] = parsed_content
|
|
|
|
context['page_title'] = get_title_from_markdown(parsed_content)
|
2016-04-07 22:00:58 +01:00
|
|
|
context['html_title'] = context['page_title']
|
2016-04-05 19:38:09 +01:00
|
|
|
parsed_content = template.render(context, request)
|
|
|
|
return HttpResponse(parsed_content)
|
|
|
|
|
|
|
|
|
2016-04-05 22:07:16 +01:00
|
|
|
class AboutView(FormView):
|
|
|
|
template_name = 'about/index.html'
|
|
|
|
success_url = '/about/?sent'
|
|
|
|
form_class = ContactForm
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
context = dict(super().get_context_data(**kwargs), **get_context('/about'))
|
|
|
|
context['sent'] = 'sent' not in self.request.GET
|
|
|
|
return context
|