From 3a42642019492029b7a94beb467577a788a71144 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Mon, 15 Sep 2014 16:32:23 +0200 Subject: [PATCH] [TASK] Remove session based project_id juggling. --- citavi_mapper/settings.py | 1 - citavi_mapper/urls.py | 7 +--- frontend/admin.py | 6 ++- frontend/models.py | 23 +++++++++++- frontend/proxy.py | 50 ------------------------- frontend/templates/layout/base.html | 7 ++-- frontend/templates/project.html | 9 +++-- frontend/templates/projects.html | 10 +++-- frontend/views.py | 58 +++++++++-------------------- service/sqlitehelper.py | 16 +++++--- 10 files changed, 71 insertions(+), 116 deletions(-) delete mode 100644 frontend/proxy.py diff --git a/citavi_mapper/settings.py b/citavi_mapper/settings.py index 7f5d679..bdd8a96 100644 --- a/citavi_mapper/settings.py +++ b/citavi_mapper/settings.py @@ -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' diff --git a/citavi_mapper/urls.py b/citavi_mapper/urls.py index df025bb..2f221ed 100644 --- a/citavi_mapper/urls.py +++ b/citavi_mapper/urls.py @@ -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\d+)/$', ProjectView.as_view(), name='frontend-project-detail'), - url(r'^projects/(?P\d+)/enter/$', enterProject, name='frontend-enter-project-detail'), - url(r'^projects/(?P\d+)/leave/$', leaveProject, name='frontend-leave-project-detail'), + url(r'^projects/(?P\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)), diff --git a/frontend/admin.py b/frontend/admin.py index 2bf7803..cd2872c 100644 --- a/frontend/admin.py +++ b/frontend/admin.py @@ -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) \ No newline at end of file +admin.site.register(Project) +admin.site.register(PersonGlobalIdentity) +admin.site.register(CitaviProjectIdentity) \ No newline at end of file diff --git a/frontend/models.py b/frontend/models.py index a2d09a0..05cc5f9 100644 --- a/frontend/models.py +++ b/frontend/models.py @@ -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 = "" + 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 = "" return repr \ No newline at end of file diff --git a/frontend/proxy.py b/frontend/proxy.py deleted file mode 100644 index f1b7a6e..0000000 --- a/frontend/proxy.py +++ /dev/null @@ -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########## ##########" - print ',\n'.join("%s: %s" % item for item in request.session.items()) - print "########## ##########" - print "\n########## ##########" - print ',\n'.join("%s: %s" % item for item in vars(request).items()) - print "########## ##########" - print "\n########## ##########" - print args - print "########## ##########" - print "\n########## ##########" - print kwargs - print "########## ##########\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(" 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########## ##########" - self.do_debug_output(request, *args, **kwargs) - print "########## ##########\n" - return self.do_project_id_check(request, *args, **kwargs) - - -""" def process_request(self, request, *args, **kwargs): - return None - print "\n########## ##########" - self.do_debug_output(request, *args, **kwargs) - print "########## ##########\n" - return None -""" - diff --git a/frontend/templates/layout/base.html b/frontend/templates/layout/base.html index 615e65d..f93cebc 100644 --- a/frontend/templates/layout/base.html +++ b/frontend/templates/layout/base.html @@ -4,14 +4,14 @@ - + {% block title %}{{page.name}} :: {{page.title}}{% endblock %} {% load staticfiles compressed %} {% compressed_css 'bootstrap' %} - {% compressed_js 'bootstrap' %} + {% compressed_js 'bootstrap' %} {% compressed_css 'frontend' %} @@ -25,7 +25,6 @@ diff --git a/frontend/templates/project.html b/frontend/templates/project.html index a30c372..081f8bc 100644 --- a/frontend/templates/project.html +++ b/frontend/templates/project.html @@ -3,11 +3,12 @@ {% load crispy_forms_tags %} {% endblock %} {% block navbar-header %} -
  • Leave Project
  • +
  • Back to projects
  • {% endblock %} {% block content %} -

    Update Project "{{project.name}}"

    -

    {{project.description}}

    +

    Name: {{project.name}} +

    Description: {{project.description}} +

    Current uploaded database filename: {{project.associated_filename}}

    +

    Upload the new database version

    {% crispy form %}

    - {% endblock %} \ No newline at end of file diff --git a/frontend/templates/projects.html b/frontend/templates/projects.html index 57353a9..d36b961 100644 --- a/frontend/templates/projects.html +++ b/frontend/templates/projects.html @@ -3,9 +3,9 @@ {% load crispy_forms_tags %} {% endblock %} {% block content %} -

    Create a new Project

    +

    Create a new project

    {% crispy form %}

    -

    Existing Projects

    +

    Existing projects

    {% if not projects %}

    No projects to find here. Maybe go ahead and create one?

    {% else %} @@ -15,6 +15,7 @@ ID Name Description + Citavi file Features @@ -24,10 +25,11 @@ {{project.id}} {{project.name}} {{project.description}} - Enter project + {{project.associated_filename}} + Update project {% endfor %} - + {% endif %} {% endblock %} \ No newline at end of file diff --git a/frontend/views.py b/frontend/views.py index a0016b1..64e3a59 100644 --- a/frontend/views.py +++ b/frontend/views.py @@ -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(" 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(" 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) \ No newline at end of file diff --git a/service/sqlitehelper.py b/service/sqlitehelper.py index c59421e..ad7481a 100644 --- a/service/sqlitehelper.py +++ b/service/sqlitehelper.py @@ -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)