[TASK] Add very basic file handling. Upload processing still TODO.

This commit is contained in:
Jan Philipp Timme 2014-09-01 15:30:57 +02:00
parent 988771a219
commit 73dbacb74b
2 changed files with 18 additions and 2 deletions

View File

@ -5,6 +5,8 @@ from django.http import HttpResponse, HttpResponseRedirect
class EnforceActiveProjectProxy(): class EnforceActiveProjectProxy():
def do_debug_output(self, request, *args, **kwargs): def do_debug_output(self, request, *args, **kwargs):
if True:
return
print "\n########## <session> ##########" print "\n########## <session> ##########"
print ',\n'.join("%s: %s" % item for item in request.session.items()) print ',\n'.join("%s: %s" % item for item in request.session.items())
print "########## </session> ##########" print "########## </session> ##########"
@ -22,8 +24,8 @@ class EnforceActiveProjectProxy():
# kwargs project_id AND path project_id have to match! # kwargs project_id AND path project_id have to match!
print request.path print request.path
try: try:
project_id_arg = args[2][u'project_id'] project_id_arg = args[2][u'project_id']
if request.session[u'project_id'] != None: if request.session[u'project_id'] != None:
print "Active project ID: " + request.session[u'project_id'] print "Active project ID: " + request.session[u'project_id']
if project_id_arg != request.session[u'project_id']: if project_id_arg != request.session[u'project_id']:
return HttpResponse("<proxy> You tried to work on a project with ID " + project_id_arg + ", but your active project id is " + request.session['project_id'] + ".") return HttpResponse("<proxy> You tried to work on a project with ID " + project_id_arg + ", but your active project id is " + request.session['project_id'] + ".")

View File

@ -1,5 +1,11 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# File upload related imports
import os
from django.core.files.storage import default_storage
from django.core.files.base import ContentFile
from django.conf import settings
from django.http import HttpResponse, HttpResponseRedirect from django.http import HttpResponse, HttpResponseRedirect
from django.views.generic import TemplateView, FormView, CreateView, UpdateView from django.views.generic import TemplateView, FormView, CreateView, UpdateView
from django.views.generic.detail import SingleObjectMixin from django.views.generic.detail import SingleObjectMixin
@ -140,12 +146,20 @@ class ProjectView(ProtectedFormView, SingleObjectMixin):
self.success_url = self.success_url + project_id self.success_url = self.success_url + project_id
return super(ProjectView, self).post(request, *args, **kwargs) return super(ProjectView, self).post(request, *args, **kwargs)
""" This form_valid handles the file uploads and triggers a citavi schema import and things. """
def form_valid(self, form, *args, **kwargs): def form_valid(self, form, *args, **kwargs):
print("FORM:") print("FORM:")
attrs = vars(form) attrs = vars(form)
print ', '.join("%s: %s" % item for item in attrs.items()) print ', '.join("%s: %s" % item for item in attrs.items())
file = form.files[u'file']
relative_path = default_storage.save('tmp/citavi_import.sqlite', ContentFile(file.read()))
path = os.path.join(settings.MEDIA_ROOT, relative_path)
print "Path: " + path
#TODO: Use file to do things here.
default_storage.delete('tmp/citavi_import.sqlite')
return super(ProjectView, self).form_valid(form) return super(ProjectView, self).form_valid(form)