videopdf/item/views.py

117 lines
4 KiB
Python
Raw Normal View History

2013-02-12 07:09:52 +00:00
# -*- coding: utf-8 -*-
# vi:si:et:sw=4:sts=4:ts=4
# GPL 2013
from __future__ import division, with_statement
import os
2013-02-12 07:36:07 +00:00
import json
2013-02-12 07:09:52 +00:00
from django.shortcuts import render_to_response, get_object_or_404, get_list_or_404
from django.template import RequestContext
from django.http import HttpResponse, HttpResponseRedirect, HttpResponseNotFound, Http404
from django.views.decorators.http import condition
from django import forms
from django.db.models import F
from ox.django.http import HttpFileResponse
from ox.django.shortcuts import render_to_json_response
import models
class ChunkForm(forms.Form):
chunk = forms.FileField()
done = forms.IntegerField(required=False)
def chunk(request, slug):
if request.method == 'POST':
item = get_object_or_404(models.Item, slug=slug)
form = ChunkForm(request.POST, request.FILES)
canEdit = True
if form.is_valid() and canEdit:
f = form.cleaned_data['chunk']
response = {
'resultUrl': request.build_absolute_uri(item.get_absolute_url())
}
if item.filename:
name = item.filename
else:
name = f.name
if not item.save_chunk(f.read(), name):
response['result'] = 'failed'
elif form.cleaned_data['done']:
item.done = True
item.save()
response['done'] = 1
response['result'] = 1
else:
response['result'] = 1
return render_to_json_response(response)
response = {
'result': -1,
'fileUrl': '/'
}
return render_to_json_response(response)
def add(request):
if request.method == 'POST':
if request.POST.get('firefogg', False):
if not request.session.session_key:
request.session.save()
request.session.modified = True
file = models.Item()
file.filename = request.POST.get('name', '')
file.uploader = request.session.session_key
file.save()
response = {
'result': 1,
'maxRetry': 10,
'uploadUrl': request.build_absolute_uri("%s/chunk" % file.get_absolute_url())
}
return render_to_json_response(response)
# Save any files that were uploaded (ignoring empty form fields)
if 'file' in request.FILES:
new_file = request.FILES['file']
file = models.Item(filename=new_file.name)
file.done = True
file.file.save(new_file.name, new_file)
os.chmod(file.file.path, 0644)
file.save()
if request.POST.get('api', False):
return HttpResponse(request.build_absolute_uri(file.get_absolute_url()), content_type="text/plain")
return HttpResponseRedirect(file.get_absolute_url())
#no upload
return HttpResponseRedirect('/')
def index(request):
context = RequestContext(request, {
})
return render_to_response('index.html', context)
def item(request, slug, edit=False):
item = get_object_or_404(models.Item, slug=slug)
2013-02-12 07:36:07 +00:00
edit = edit and item.editable(request.user)
2013-02-12 07:09:52 +00:00
context = RequestContext(request, {
'item': item,
2013-02-12 07:36:07 +00:00
'edit': json.dumps(edit)
2013-02-12 07:09:52 +00:00
})
return render_to_response('item.html', context)
def pdf(request, slug, filename):
item = get_object_or_404(models.Item, slug=slug)
if filename == 'download':
filename = item.filename
filename = filename.encode('utf-8')
content_type = None
return HttpFileResponse(item.file.path.encode('utf-8'),
content_type=content_type, filename=filename)
def save(request):
data = json.loads(request.POST['data'])
item = get_object_or_404(models.Item, slug=data['id'])
response = json_response({})
if item.editable(request.user):
item.overlay = data['overlay']
item.save()
else:
response = json_response(status=403, text='permission denied')
return render_to_json_response(response)