[TASK] Remove session based project_id juggling.
This commit is contained in:
parent
ddfcdecb66
commit
3a42642019
|
@ -60,7 +60,6 @@ MIDDLEWARE_CLASSES = (
|
||||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||||
'django.contrib.messages.middleware.MessageMiddleware',
|
'django.contrib.messages.middleware.MessageMiddleware',
|
||||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||||
'frontend.proxy.EnforceActiveProjectProxy',
|
|
||||||
)
|
)
|
||||||
|
|
||||||
ROOT_URLCONF = 'citavi_mapper.urls'
|
ROOT_URLCONF = 'citavi_mapper.urls'
|
||||||
|
|
|
@ -6,15 +6,12 @@ from django.contrib import admin
|
||||||
admin.autodiscover()
|
admin.autodiscover()
|
||||||
|
|
||||||
from frontend.views import login_wrap, logout_wrap
|
from frontend.views import login_wrap, logout_wrap
|
||||||
from frontend.views import IndexView, ProjectOverView, ProjectView
|
from frontend.views import IndexView, ProjectOverView, UpdateProjectView
|
||||||
from frontend.views import enterProject, leaveProject
|
|
||||||
|
|
||||||
urlpatterns = patterns('',
|
urlpatterns = patterns('',
|
||||||
url(r'^$', IndexView.as_view(), name='frontend-index'),
|
url(r'^$', IndexView.as_view(), name='frontend-index'),
|
||||||
url(r'^projects/$', ProjectOverView.as_view(), name='frontend-projects'),
|
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+)/update$', UpdateProjectView.as_view(), name='frontend-project-update'),
|
||||||
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'^login/$', login_wrap, name='frontend-login'),
|
url(r'^login/$', login_wrap, name='frontend-login'),
|
||||||
url(r'^logout/$', logout_wrap, name='frontend-logout'),
|
url(r'^logout/$', logout_wrap, name='frontend-logout'),
|
||||||
url(r'^admin/', include(admin.site.urls)),
|
url(r'^admin/', include(admin.site.urls)),
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
# -*- coding: utf-8 -*-
|
# -*- coding: utf-8 -*-
|
||||||
|
|
||||||
from django.contrib import admin
|
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)
|
|
@ -2,6 +2,7 @@
|
||||||
|
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
|
||||||
|
|
||||||
class Project(models.Model):
|
class Project(models.Model):
|
||||||
name = models.CharField(max_length=255)
|
name = models.CharField(max_length=255)
|
||||||
description = models.TextField()
|
description = models.TextField()
|
||||||
|
@ -12,5 +13,25 @@ class Project(models.Model):
|
||||||
if self.associated_filename:
|
if self.associated_filename:
|
||||||
repr += " (" + self.associated_filename + ")"
|
repr += " (" + self.associated_filename + ")"
|
||||||
else:
|
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
|
return repr
|
|
@ -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
|
|
||||||
"""
|
|
||||||
|
|
|
@ -4,14 +4,14 @@
|
||||||
<meta charset="utf-8">
|
<meta charset="utf-8">
|
||||||
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
<meta http-equiv="X-UA-Compatible" content="IE=edge">
|
||||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
<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>
|
<title>{% block title %}{{page.name}} :: {{page.title}}{% endblock %}</title>
|
||||||
{% load staticfiles compressed %}
|
{% load staticfiles compressed %}
|
||||||
<!-- jQuery -->
|
<!-- jQuery -->
|
||||||
<script type="application/javascript" src="{% static 'jquery/jquery-2.1.1.min.js' %}" charset="utf-8"></script>
|
<script type="application/javascript" src="{% static 'jquery/jquery-2.1.1.min.js' %}" charset="utf-8"></script>
|
||||||
<!-- Bootstrap -->
|
<!-- Bootstrap -->
|
||||||
{% compressed_css 'bootstrap' %}
|
{% compressed_css 'bootstrap' %}
|
||||||
{% compressed_js 'bootstrap' %}
|
{% compressed_js 'bootstrap' %}
|
||||||
<!-- /Bootstrap -->
|
<!-- /Bootstrap -->
|
||||||
{% compressed_css 'frontend' %}
|
{% compressed_css 'frontend' %}
|
||||||
<!-- Additional -->
|
<!-- Additional -->
|
||||||
|
@ -25,7 +25,6 @@
|
||||||
<ul class="nav navbar-nav">
|
<ul class="nav navbar-nav">
|
||||||
{% block navbar-header %}
|
{% block navbar-header %}
|
||||||
<li><a href="/">Index</a></li>
|
<li><a href="/">Index</a></li>
|
||||||
<li><a href="/register">Register</a></li>
|
|
||||||
<li>
|
<li>
|
||||||
{% if user.is_anonymous %}
|
{% if user.is_anonymous %}
|
||||||
<a href="/login">Login</a>
|
<a href="/login">Login</a>
|
||||||
|
@ -33,7 +32,7 @@
|
||||||
<a href="/logout">Logout</a>
|
<a href="/logout">Logout</a>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
</li>
|
</li>
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
</ul>
|
</ul>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -3,11 +3,12 @@
|
||||||
{% load crispy_forms_tags %}
|
{% load crispy_forms_tags %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
{% block navbar-header %}
|
{% 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 %}
|
{% endblock %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h3>Update Project "{{project.name}}"</h3>
|
<p><strong>Name:</strong> {{project.name}}</h3>
|
||||||
<p>{{project.description}}</p>
|
<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>
|
<p>{% crispy form %}</p>
|
||||||
|
|
||||||
{% endblock %}
|
{% endblock %}
|
|
@ -3,9 +3,9 @@
|
||||||
{% load crispy_forms_tags %}
|
{% load crispy_forms_tags %}
|
||||||
{% endblock %}
|
{% endblock %}
|
||||||
{% block content %}
|
{% block content %}
|
||||||
<h3>Create a new Project</h3>
|
<h3>Create a new project</h3>
|
||||||
<p>{% crispy form %}</p>
|
<p>{% crispy form %}</p>
|
||||||
<h3>Existing Projects</h3>
|
<h3>Existing projects</h3>
|
||||||
{% if not projects %}
|
{% if not projects %}
|
||||||
<p><strong>No projects to find here. Maybe go ahead and create one?</strong></p>
|
<p><strong>No projects to find here. Maybe go ahead and create one?</strong></p>
|
||||||
{% else %}
|
{% else %}
|
||||||
|
@ -15,6 +15,7 @@
|
||||||
<th>ID</th>
|
<th>ID</th>
|
||||||
<th>Name</th>
|
<th>Name</th>
|
||||||
<th>Description</th>
|
<th>Description</th>
|
||||||
|
<th>Citavi file</th>
|
||||||
<th>Features</th>
|
<th>Features</th>
|
||||||
</tr>
|
</tr>
|
||||||
</thead>
|
</thead>
|
||||||
|
@ -24,10 +25,11 @@
|
||||||
<td>{{project.id}}</td>
|
<td>{{project.id}}</td>
|
||||||
<td>{{project.name}}</td>
|
<td>{{project.name}}</td>
|
||||||
<td>{{project.description}}</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>
|
</tr>
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
</tbody>
|
</tbody>
|
||||||
</table>
|
</table>
|
||||||
{% endif %}
|
{% endif %}
|
||||||
{% endblock %}
|
{% endblock %}
|
|
@ -89,27 +89,6 @@ class ProtectedUpdateView(LoggedInMixin, MyUpdateView):
|
||||||
pass
|
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
|
# Actual Views
|
||||||
class IndexView(ProtectedTemplateView):
|
class IndexView(ProtectedTemplateView):
|
||||||
template_name = 'index.html'
|
template_name = 'index.html'
|
||||||
|
@ -122,45 +101,44 @@ class ProjectOverView(ProtectedFormView):
|
||||||
success_url = '/projects'
|
success_url = '/projects'
|
||||||
|
|
||||||
def get_context_data(self, **kwargs):
|
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)
|
return super(ProjectOverView, self).get_context_data(**kwargs)
|
||||||
|
|
||||||
def form_valid(self, form):
|
def form_valid(self, form):
|
||||||
form.save()
|
form.save()
|
||||||
return super(ProjectOverView, self).form_valid(form)
|
return super(ProjectOverView, self).form_valid(form)
|
||||||
|
|
||||||
class ProjectView(ProtectedFormView, SingleObjectMixin):
|
class UpdateProjectView(ProtectedFormView, SingleObjectMixin):
|
||||||
template_name = 'project.html'
|
template_name = 'project.html'
|
||||||
page_title = 'Project View'
|
page_title = 'Update project'
|
||||||
form_class = FileUploadForm
|
form_class = FileUploadForm
|
||||||
|
|
||||||
success_url = '/projects/'
|
success_url = '/projects/'
|
||||||
|
|
||||||
def get(self, request, *args, **kwargs):
|
def get(self, request, *args, **kwargs):
|
||||||
project_id = kwargs[u'project_id']
|
self.project_id = kwargs[u'project_id']
|
||||||
self.object = Project.objects.get(pk=project_id)
|
self.object = Project.objects.get(pk=self.project_id)
|
||||||
return super(ProjectView, self).get(request, *args, **kwargs)
|
return super(UpdateProjectView, self).get(request, *args, **kwargs)
|
||||||
|
|
||||||
def post(self, request, *args, **kwargs):
|
def post(self, request, *args, **kwargs):
|
||||||
project_id = kwargs[u'project_id']
|
self.project_id = kwargs[u'project_id']
|
||||||
self.success_url = self.success_url + project_id
|
self.success_url = self.success_url + self.project_id + '/update'
|
||||||
return super(ProjectView, self).post(request, *args, **kwargs)
|
return super(UpdateProjectView, self).post(request, *args, **kwargs)
|
||||||
|
|
||||||
""" This form_valid handles the file uploads and triggers a citavi schema import and things. """
|
""" 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):
|
||||||
project_id = self.request.session[u'project_id']
|
original_file = form.files[u'file']
|
||||||
file = form.files[u'file']
|
original_filename = str(original_file)
|
||||||
relative_path = default_storage.save('tmp/project_import_' + project_id + '.ctt4', ContentFile(file.read()))
|
relative_path = default_storage.save('tmp/project_' + self.project_id + '.ctt4', ContentFile(original_file.read()))
|
||||||
path = os.path.join(settings.MEDIA_ROOT, relative_path)
|
path = os.path.join(settings.MEDIA_ROOT, relative_path)
|
||||||
print "Path: " + path
|
|
||||||
|
|
||||||
#Now things will happen
|
#TODO: Actually validate that we get an SQLite3 file!
|
||||||
from service.sqlitehelper import import_sqlite
|
|
||||||
import_sqlite(project_id=project_id, sqlite_file=path)
|
|
||||||
|
|
||||||
# Maybe it is a good idea to keep the file
|
# Store this in the project model
|
||||||
# default_storage.delete(relative_path)
|
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)
|
||||||
|
|
||||||
|
|
|
@ -8,7 +8,7 @@ def import_sqlite(project_id, sqlite_file):
|
||||||
print project_id
|
print project_id
|
||||||
print sqlite_file
|
print sqlite_file
|
||||||
print "Importing data..."
|
print "Importing data..."
|
||||||
|
|
||||||
from sqlalchemy import *
|
from sqlalchemy import *
|
||||||
from sqlalchemy.schema import CreateSchema
|
from sqlalchemy.schema import CreateSchema
|
||||||
from sqlalchemy.ext.automap import automap_base
|
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)
|
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:
|
for table in sqlite_meta.sorted_tables:
|
||||||
table_name = str(table)
|
table_name = str(table)
|
||||||
columns = table.columns.items()
|
columns = table.columns.items()
|
||||||
|
print "class " + table_name + "(Base):"
|
||||||
|
print " __tablename__ = '" + table_name + "'"
|
||||||
|
print
|
||||||
for column_tuple in columns:
|
for column_tuple in columns:
|
||||||
column_name = column_tuple[0]
|
column_name = column_tuple[0]
|
||||||
actual_sqlalchemy_column = column_tuple[1]
|
actual_sqlalchemy_column = column_tuple[1]
|
||||||
column_type = str(vars(actual_sqlalchemy_column)['type'])
|
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
|
print
|
||||||
|
|
||||||
# Shove it up into postgresql's ... you know.
|
# Shove it up into postgresql's ... you know.
|
||||||
psql_meta.create_all(psql_engine)
|
psql_meta.create_all(psql_engine)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue