2012-07-10 22:18:48 +00:00
|
|
|
from django.http import Http404
|
|
|
|
from django.shortcuts import render, redirect
|
2012-07-10 19:35:30 +00:00
|
|
|
from django.template import RequestContext
|
|
|
|
|
2012-07-10 22:18:48 +00:00
|
|
|
from .models import Page
|
|
|
|
from .forms import PageForm
|
2012-07-10 19:35:30 +00:00
|
|
|
|
|
|
|
|
2012-07-10 22:18:48 +00:00
|
|
|
def can_edit(user):
|
|
|
|
if user.is_staff or user.is_superuser:
|
|
|
|
return True
|
|
|
|
if user.has_perm("cms.change_page"):
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def page(request, path):
|
2012-07-10 19:35:30 +00:00
|
|
|
|
2012-07-10 22:18:48 +00:00
|
|
|
editable = can_edit(request.user)
|
|
|
|
try:
|
|
|
|
page = Page.published.get(path=path)
|
|
|
|
except Page.DoesNotExist:
|
|
|
|
if editable:
|
|
|
|
return redirect("cms_page_edit", path=path)
|
|
|
|
else:
|
|
|
|
raise Http404
|
2012-07-10 19:35:30 +00:00
|
|
|
|
2012-07-10 22:18:48 +00:00
|
|
|
return render(request, "cms/page_detail.html", {
|
2012-07-10 19:35:30 +00:00
|
|
|
"page": page,
|
2012-07-10 22:18:48 +00:00
|
|
|
"editable": editable,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
|
|
def page_edit(request, path):
|
|
|
|
|
|
|
|
if not can_edit(request.user):
|
|
|
|
raise Http404
|
|
|
|
|
|
|
|
try:
|
|
|
|
page = Page.published.get(path=path)
|
|
|
|
except Page.DoesNotExist:
|
|
|
|
page = None
|
|
|
|
|
|
|
|
if request.method == "POST":
|
|
|
|
form = PageForm(request.POST, instance=page)
|
|
|
|
if form.is_valid():
|
|
|
|
page = form.save(commit=False)
|
|
|
|
page.path = path
|
|
|
|
page.save()
|
|
|
|
return redirect(page)
|
|
|
|
else:
|
|
|
|
print form.errors
|
|
|
|
else:
|
|
|
|
form = PageForm(instance=page, initial={"path": path})
|
|
|
|
|
|
|
|
return render(request, "cms/page_edit.html", {
|
|
|
|
"path": path,
|
|
|
|
"form": form
|
|
|
|
})
|