2012-10-18 06:43:00 +00:00
|
|
|
from django.conf import settings
|
|
|
|
from django.db import transaction
|
|
|
|
from django.http import Http404, HttpResponse
|
|
|
|
from django.shortcuts import render, redirect, get_object_or_404
|
|
|
|
from django.views import static
|
2012-07-10 19:35:30 +00:00
|
|
|
|
2012-12-20 06:48:19 +00:00
|
|
|
from django.contrib.auth.decorators import login_required
|
2012-10-18 06:43:00 +00:00
|
|
|
from .models import Page, File
|
|
|
|
from .forms import PageForm, FileUploadForm
|
2012-07-10 19:35:30 +00:00
|
|
|
|
|
|
|
|
2012-12-20 06:48:19 +00:00
|
|
|
def can_edit(page, user):
|
|
|
|
if page and page.is_community:
|
2012-07-10 22:18:48 +00:00
|
|
|
return True
|
2012-12-20 06:48:19 +00:00
|
|
|
else:
|
|
|
|
return user.has_perm("cms.change_page")
|
2012-07-10 22:18:48 +00:00
|
|
|
|
|
|
|
|
2012-10-18 06:43:00 +00:00
|
|
|
def can_upload(user):
|
|
|
|
if user.is_staff or user.is_superuser:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2012-07-10 22:18:48 +00:00
|
|
|
def page(request, path):
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-07-10 22:18:48 +00:00
|
|
|
try:
|
|
|
|
page = Page.published.get(path=path)
|
|
|
|
except Page.DoesNotExist:
|
2012-12-20 06:48:19 +00:00
|
|
|
page = None
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-12-20 06:48:19 +00:00
|
|
|
editable = can_edit(page, request.user)
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-12-20 06:48:19 +00:00
|
|
|
if page is None:
|
2012-07-10 22:18:48 +00:00
|
|
|
if editable:
|
|
|
|
return redirect("cms_page_edit", path=path)
|
|
|
|
else:
|
|
|
|
raise Http404
|
2014-07-30 18:19:26 +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,
|
|
|
|
})
|
|
|
|
|
|
|
|
|
2012-12-20 06:48:19 +00:00
|
|
|
@login_required
|
2012-07-10 22:18:48 +00:00
|
|
|
def page_edit(request, path):
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-07-10 22:18:48 +00:00
|
|
|
try:
|
|
|
|
page = Page.published.get(path=path)
|
|
|
|
except Page.DoesNotExist:
|
|
|
|
page = None
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-12-20 06:48:19 +00:00
|
|
|
if not can_edit(page, request.user):
|
|
|
|
raise Http404
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-07-10 22:18:48 +00:00
|
|
|
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})
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-07-10 22:18:48 +00:00
|
|
|
return render(request, "cms/page_edit.html", {
|
|
|
|
"path": path,
|
|
|
|
"form": form
|
|
|
|
})
|
2012-10-18 06:43:00 +00:00
|
|
|
|
|
|
|
|
|
|
|
def file_index(request):
|
|
|
|
if not can_upload(request.user):
|
|
|
|
raise Http404
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-10-18 06:43:00 +00:00
|
|
|
ctx = {
|
|
|
|
"files": File.objects.all(),
|
|
|
|
}
|
|
|
|
return render(request, "cms/file_index.html", ctx)
|
|
|
|
|
|
|
|
|
|
|
|
def file_create(request):
|
|
|
|
if not can_upload(request.user):
|
|
|
|
raise Http404
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-10-18 06:43:00 +00:00
|
|
|
if request.method == "POST":
|
|
|
|
form = FileUploadForm(request.POST, request.FILES)
|
|
|
|
if form.is_valid():
|
|
|
|
with transaction.commit_on_success():
|
|
|
|
kwargs = {
|
|
|
|
"file": form.cleaned_data["file"],
|
|
|
|
}
|
|
|
|
File.objects.create(**kwargs)
|
|
|
|
return redirect("file_index")
|
|
|
|
else:
|
|
|
|
form = FileUploadForm()
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-10-18 06:43:00 +00:00
|
|
|
ctx = {
|
|
|
|
"form": form,
|
|
|
|
}
|
|
|
|
return render(request, "cms/file_create.html", ctx)
|
|
|
|
|
|
|
|
|
|
|
|
def file_download(request, pk, *args):
|
|
|
|
file = get_object_or_404(File, pk=pk)
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-10-18 06:43:00 +00:00
|
|
|
if getattr(settings, "USE_X_ACCEL_REDIRECT", False):
|
|
|
|
response = HttpResponse()
|
|
|
|
response["X-Accel-Redirect"] = file.file.url
|
|
|
|
# delete content-type to allow Gondor to determine the filetype and
|
|
|
|
# we definitely don't want Django's default :-)
|
|
|
|
del response["content-type"]
|
|
|
|
else:
|
|
|
|
response = static.serve(request, file.file.name, document_root=settings.MEDIA_ROOT)
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-10-18 06:43:00 +00:00
|
|
|
return response
|
|
|
|
|
|
|
|
|
|
|
|
def file_delete(request, pk):
|
|
|
|
if not can_upload(request.user):
|
|
|
|
raise Http404
|
2014-07-30 18:19:26 +00:00
|
|
|
|
2012-10-18 06:43:00 +00:00
|
|
|
file = get_object_or_404(File, pk=pk)
|
|
|
|
if request.method == "POST":
|
|
|
|
file.delete()
|
|
|
|
# @@@ message
|
|
|
|
return redirect("file_index")
|