[TASK] Remove session based project_id juggling.

This commit is contained in:
Jan Philipp Timme 2014-09-15 16:32:23 +02:00
parent ddfcdecb66
commit 3a42642019
10 changed files with 71 additions and 116 deletions

View File

@ -60,7 +60,6 @@ MIDDLEWARE_CLASSES = (
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'frontend.proxy.EnforceActiveProjectProxy',
)
ROOT_URLCONF = 'citavi_mapper.urls'

View File

@ -6,15 +6,12 @@ from django.contrib import admin
admin.autodiscover()
from frontend.views import login_wrap, logout_wrap
from frontend.views import IndexView, ProjectOverView, ProjectView
from frontend.views import enterProject, leaveProject
from frontend.views import IndexView, ProjectOverView, UpdateProjectView
urlpatterns = patterns('',
url(r'^$', IndexView.as_view(), name='frontend-index'),
url(r'^projects/$', ProjectOverView.as_view(), name='frontend-projects'),
url(r'^projects/(?P<project_id>\d+)/$', ProjectView.as_view(), name='frontend-project-detail'),
url(r'^projects/(?P<project_id>\d+)/enter/$', enterProject, name='frontend-enter-project-detail'),
url(r'^projects/(?P<project_id>\d+)/leave/$', leaveProject, name='frontend-leave-project-detail'),
url(r'^projects/(?P<project_id>\d+)/update$', UpdateProjectView.as_view(), name='frontend-project-update'),
url(r'^login/$', login_wrap, name='frontend-login'),
url(r'^logout/$', logout_wrap, name='frontend-logout'),
url(r'^admin/', include(admin.site.urls)),

View File

@ -1,6 +1,8 @@
# -*- coding: utf-8 -*-
from django.contrib import admin
from frontend.models import Project
from frontend.models import Project, PersonGlobalIdentity, CitaviProjectIdentity
admin.site.register(Project)
admin.site.register(Project)
admin.site.register(PersonGlobalIdentity)
admin.site.register(CitaviProjectIdentity)

View File

@ -2,6 +2,7 @@
from django.db import models
class Project(models.Model):
name = models.CharField(max_length=255)
description = models.TextField()
@ -12,5 +13,25 @@ class Project(models.Model):
if self.associated_filename:
repr += " (" + self.associated_filename + ")"
else:
repr += " (Empty)"
repr += " (empty)"
return repr
class PersonGlobalIdentity(models.Model):
type = models.CharField(max_length=255)
#TODO: Extend this for further stuff - maybe vivo external url or something?
def __unicode__(self):
repr = "<PersonGlobalIdentity ID=" + str(self.id) + ", type=" + self.type + ">"
return repr
class CitaviProjectIdentity(models.Model):
global_identity = models.ForeignKey(PersonGlobalIdentity)
project_id = models.ForeignKey(Project)
citavi_uuid = models.CharField(max_length=255)
preferred_id = models.BooleanField()
def __unicode__(self):
repr = "<CitaviProjectIdentity project_id=" + str(self.project_id) + ", citavi_uuid=" + self.citavi_uuid + ", global_identity=" + str(self.global_identity) + ", preferred=" + str(self.preferred_id) + ">"
return repr

View File

@ -1,50 +0,0 @@
# -*- coding: utf-8 -*-
from django.http import HttpResponse, HttpResponseRedirect
class EnforceActiveProjectProxy():
def do_debug_output(self, request, *args, **kwargs):
if True:
return
print "\n########## <session> ##########"
print ',\n'.join("%s: %s" % item for item in request.session.items())
print "########## </session> ##########"
print "\n########## <request> ##########"
print ',\n'.join("%s: %s" % item for item in vars(request).items())
print "########## </request> ##########"
print "\n########## <args> ##########"
print args
print "########## </args> ##########"
print "\n########## <kwargs> ##########"
print kwargs
print "########## </kwargs> ##########\n"
def do_project_id_check(self, request, *args, **kwargs):
# kwargs project_id AND path project_id have to match!
print request.path
try:
project_id_arg = args[2][u'project_id']
if request.session[u'project_id'] != None:
print "Active project ID: " + 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'] + ".")
except KeyError:
print "No project attribute set."
return None
def process_view(self, request, *args, **kwargs):
print "\n########## <EnforceActiveProjectProxy:process_view> ##########"
self.do_debug_output(request, *args, **kwargs)
print "########## </EnforceActiveProjectProxy:process_view> ##########\n"
return self.do_project_id_check(request, *args, **kwargs)
""" def process_request(self, request, *args, **kwargs):
return None
print "\n########## <EnforceActiveProjectProxy:process_request> ##########"
self.do_debug_output(request, *args, **kwargs)
print "########## </EnforceActiveProjectProxy:process_request> ##########\n"
return None
"""

View File

@ -4,14 +4,14 @@
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="description" content="Merging Citavi projects and mapping existing datasets.">
<meta name="description" content="Mapping citavi datasets against more datasets.">
<title>{% block title %}{{page.name}} :: {{page.title}}{% endblock %}</title>
{% load staticfiles compressed %}
<!-- jQuery -->
<script type="application/javascript" src="{% static 'jquery/jquery-2.1.1.min.js' %}" charset="utf-8"></script>
<!-- Bootstrap -->
{% compressed_css 'bootstrap' %}
{% compressed_js 'bootstrap' %}
{% compressed_js 'bootstrap' %}
<!-- /Bootstrap -->
{% compressed_css 'frontend' %}
<!-- Additional -->
@ -25,7 +25,6 @@
<ul class="nav navbar-nav">
{% block navbar-header %}
<li><a href="/">Index</a></li>
<li><a href="/register">Register</a></li>
<li>
{% if user.is_anonymous %}
<a href="/login">Login</a>
@ -33,7 +32,7 @@
<a href="/logout">Logout</a>
{% endif %}
</li>
{% endblock %}
{% endblock %}
</ul>
</div>
</div>

View File

@ -3,11 +3,12 @@
{% load crispy_forms_tags %}
{% endblock %}
{% block navbar-header %}
<li><a href="{% url 'frontend-leave-project-detail' request.session.project_id %}">Leave Project</a></li>
<li><a href="{% url 'frontend-projects' %}">Back to projects</a></li>
{% endblock %}
{% block content %}
<h3>Update Project "{{project.name}}"</h3>
<p>{{project.description}}</p>
<p><strong>Name:</strong> {{project.name}}</h3>
<p><strong>Description:</strong> {{project.description}}</h3>
<p><strong>Current uploaded database filename:</strong> {{project.associated_filename}}</p>
<h3>Upload the new database version</h3>
<p>{% crispy form %}</p>
{% endblock %}

View File

@ -3,9 +3,9 @@
{% load crispy_forms_tags %}
{% endblock %}
{% block content %}
<h3>Create a new Project</h3>
<h3>Create a new project</h3>
<p>{% crispy form %}</p>
<h3>Existing Projects</h3>
<h3>Existing projects</h3>
{% if not projects %}
<p><strong>No projects to find here. Maybe go ahead and create one?</strong></p>
{% else %}
@ -15,6 +15,7 @@
<th>ID</th>
<th>Name</th>
<th>Description</th>
<th>Citavi file</th>
<th>Features</th>
</tr>
</thead>
@ -24,10 +25,11 @@
<td>{{project.id}}</td>
<td>{{project.name}}</td>
<td>{{project.description}}</td>
<td><a href="{% url 'frontend-enter-project-detail' project.id %}">Enter project</a></td>
<td>{{project.associated_filename}}</td>
<td><a href="{% url 'frontend-project-update' project.id %}">Update project</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</table>
{% endif %}
{% endblock %}

View File

@ -89,27 +89,6 @@ class ProtectedUpdateView(LoggedInMixin, MyUpdateView):
pass
# Helper view methods
def enterProject(request, project_id=None):
try:
if request.session['project_id'] != project_id:
return HttpResponse("<enterProject> Please leave your current project - Project ID " + request.session['project_id'] + " is still active.")
except KeyError:
pass
request.session['project_id'] = project_id
return HttpResponseRedirect('/projects/' + project_id + '/')
def leaveProject(request, project_id=None):
try:
if request.session['project_id'] == project_id:
del request.session['project_id']
else:
return HttpResponse("<leaveProject> You tried to leave project with ID " + project_id + ", but your active project id is " + request.session['project_id'] + ".")
except KeyError:
pass
return HttpResponseRedirect('/projects/')
# Actual Views
class IndexView(ProtectedTemplateView):
template_name = 'index.html'
@ -122,45 +101,44 @@ class ProjectOverView(ProtectedFormView):
success_url = '/projects'
def get_context_data(self, **kwargs):
kwargs[u'projects'] = Project.objects.all()
kwargs[u'projects'] = Project.objects.order_by('id')
return super(ProjectOverView, self).get_context_data(**kwargs)
def form_valid(self, form):
form.save()
return super(ProjectOverView, self).form_valid(form)
class ProjectView(ProtectedFormView, SingleObjectMixin):
class UpdateProjectView(ProtectedFormView, SingleObjectMixin):
template_name = 'project.html'
page_title = 'Project View'
page_title = 'Update project'
form_class = FileUploadForm
success_url = '/projects/'
def get(self, request, *args, **kwargs):
project_id = kwargs[u'project_id']
self.object = Project.objects.get(pk=project_id)
return super(ProjectView, self).get(request, *args, **kwargs)
self.project_id = kwargs[u'project_id']
self.object = Project.objects.get(pk=self.project_id)
return super(UpdateProjectView, self).get(request, *args, **kwargs)
def post(self, request, *args, **kwargs):
project_id = kwargs[u'project_id']
self.success_url = self.success_url + project_id
return super(ProjectView, self).post(request, *args, **kwargs)
self.project_id = kwargs[u'project_id']
self.success_url = self.success_url + self.project_id + '/update'
return super(UpdateProjectView, 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):
project_id = self.request.session[u'project_id']
file = form.files[u'file']
relative_path = default_storage.save('tmp/project_import_' + project_id + '.ctt4', ContentFile(file.read()))
original_file = form.files[u'file']
original_filename = str(original_file)
relative_path = default_storage.save('tmp/project_' + self.project_id + '.ctt4', ContentFile(original_file.read()))
path = os.path.join(settings.MEDIA_ROOT, relative_path)
print "Path: " + path
#Now things will happen
from service.sqlitehelper import import_sqlite
import_sqlite(project_id=project_id, sqlite_file=path)
#TODO: Actually validate that we get an SQLite3 file!
# Maybe it is a good idea to keep the file
# default_storage.delete(relative_path)
# Store this in the project model
project = Project.objects.get(id=self.project_id)
project.associated_filename = original_filename
project.save()
return super(ProjectView, self).form_valid(form)
return super(UpdateProjectView, self).form_valid(form)

View File

@ -8,7 +8,7 @@ def import_sqlite(project_id, sqlite_file):
print project_id
print sqlite_file
print "Importing data..."
from sqlalchemy import *
from sqlalchemy.schema import CreateSchema
from sqlalchemy.ext.automap import automap_base
@ -32,18 +32,24 @@ def import_sqlite(project_id, sqlite_file):
sqlite_autobase.prepare(sqlite_engine, reflect=True)
"""
# Now that sh- super stuff is reflected and i can go on, using the Table instances.
# This small snippet prints python class code for sqlalchemy reflected classes
for table in sqlite_meta.sorted_tables:
table_name = str(table)
columns = table.columns.items()
print "class " + table_name + "(Base):"
print " __tablename__ = '" + table_name + "'"
print
for column_tuple in columns:
column_name = column_tuple[0]
actual_sqlalchemy_column = column_tuple[1]
column_type = str(vars(actual_sqlalchemy_column)['type'])
print table_name + "." + column_name + ': ' + column_type
column_type = str(vars(actual_sqlalchemy_column)['type'])
#print table_name + "." + column_name + ': ' + column_type
print " " + column_name + " = Column(" + column_type + ")"
print
print
print
# Shove it up into postgresql's ... you know.
psql_meta.create_all(psql_engine)