2014-07-26 13:00:21 +02:00
# -*- coding: utf-8 -*-
2014-08-29 13:55:43 +02:00
from django . http import HttpResponse , HttpResponseRedirect
2014-07-28 15:13:55 +02:00
from django . views . generic import TemplateView , FormView , CreateView , UpdateView
2014-08-11 16:01:03 +02:00
from django . views . generic . detail import SingleObjectMixin
2014-07-27 23:34:23 +02:00
from django . contrib . auth . decorators import login_required
from django . utils . decorators import method_decorator
from django . contrib . auth . views import login , logout
2014-07-26 13:00:21 +02:00
2014-08-11 16:01:03 +02:00
from frontend . forms import NewProjectForm , FileUploadForm
from frontend . models import Project
2014-07-28 15:13:55 +02:00
2014-07-27 23:34:23 +02:00
FRONTEND_PAGE_NAME = ' Citavi Mapper '
2014-07-28 15:13:55 +02:00
# Login wrapper functions
2014-07-27 23:34:23 +02:00
def login_wrap ( * args , * * kwargs ) :
kwargs [ ' extra_context ' ] = {
' page ' : {
' name ' : FRONTEND_PAGE_NAME ,
' title ' : ' Login '
2014-08-29 13:55:43 +02:00
}
2014-07-27 23:34:23 +02:00
}
return login ( * args , * * kwargs )
def logout_wrap ( * args , * * kwargs ) :
kwargs [ ' extra_context ' ] = {
' page ' : {
' name ' : FRONTEND_PAGE_NAME ,
' title ' : ' Logout '
}
}
return logout ( * args , * * kwargs )
2014-07-28 15:13:55 +02:00
# My base classes for views
2014-07-29 14:48:09 +02:00
class MyViewMixin ( object ) :
2014-07-27 23:34:23 +02:00
template_name = ' base.html '
page_name = FRONTEND_PAGE_NAME
page_title = ' BASE '
def get_page_data ( self ) :
return {
u ' name ' : self . page_name ,
u ' title ' : self . page_title
}
2014-07-26 13:00:21 +02:00
def get_context_data ( self , * * kwargs ) :
2014-07-27 23:34:23 +02:00
kwargs [ u ' page ' ] = self . get_page_data ( )
2014-07-29 14:48:09 +02:00
return super ( MyViewMixin , self ) . get_context_data ( * * kwargs )
2014-07-28 15:13:55 +02:00
2014-07-29 14:48:09 +02:00
class MyTemplateView ( MyViewMixin , TemplateView ) :
pass
class MyFormView ( MyViewMixin , FormView ) :
pass
2014-07-28 15:13:55 +02:00
2014-07-29 14:48:09 +02:00
class MyCreateView ( MyViewMixin , CreateView ) :
pass
class MyUpdateView ( MyViewMixin , UpdateView ) :
pass
2014-07-28 15:13:55 +02:00
2014-07-27 23:34:23 +02:00
2014-07-29 14:48:09 +02:00
# Mixin to protect pages
2014-07-27 23:34:23 +02:00
class LoggedInMixin ( object ) :
@method_decorator ( login_required )
def dispatch ( self , * args , * * kwargs ) :
return super ( LoggedInMixin , self ) . dispatch ( * args , * * kwargs )
2014-07-29 14:48:09 +02:00
# My protected view classes
2014-07-28 15:13:55 +02:00
class ProtectedTemplateView ( LoggedInMixin , MyTemplateView ) :
pass
class ProtectedFormView ( LoggedInMixin , MyFormView ) :
2014-07-27 23:34:23 +02:00
pass
2014-07-29 14:48:09 +02:00
class ProtectedCreateView ( LoggedInMixin , MyCreateView ) :
pass
2014-07-28 15:13:55 +02:00
2014-07-29 14:48:09 +02:00
class ProtectedUpdateView ( LoggedInMixin , MyUpdateView ) :
pass
2014-08-29 13:55:43 +02:00
# Helper view methods
def enterProject ( request , project_id = None ) :
try :
if request . session [ ' project_id ' ] != project_id :
2014-08-29 15:52:09 +02:00
return HttpResponse ( " <enterProject> Please leave your current project - Project ID " + request . session [ ' project_id ' ] + " is still active. " )
2014-08-29 13:55:43 +02:00
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 :
2014-08-29 15:52:09 +02:00
return HttpResponse ( " <leaveProject> You tried to leave project with ID " + project_id + " , but your active project id is " + request . session [ ' project_id ' ] + " . " )
2014-08-29 13:55:43 +02:00
except KeyError :
pass
return HttpResponseRedirect ( ' /projects/ ' )
2014-07-29 14:48:09 +02:00
# Actual Views
2014-07-28 15:13:55 +02:00
class IndexView ( ProtectedTemplateView ) :
2014-07-27 23:34:23 +02:00
template_name = ' index.html '
page_title = ' Index '
2014-08-11 16:01:03 +02:00
class ProjectOverView ( ProtectedFormView ) :
2014-08-07 14:47:59 +02:00
template_name = ' projects.html '
page_title = ' Projects '
form_class = NewProjectForm
success_url = ' /projects '
2014-07-28 15:13:55 +02:00
2014-08-11 16:01:03 +02:00
def get_context_data ( self , * * kwargs ) :
kwargs [ u ' projects ' ] = Project . objects . all ( )
return super ( ProjectOverView , self ) . get_context_data ( * * kwargs )
2014-07-28 15:13:55 +02:00
def form_valid ( self , form ) :
2014-08-11 16:01:03 +02:00
form . save ( )
return super ( ProjectOverView , self ) . form_valid ( form )
class ProjectView ( ProtectedFormView , SingleObjectMixin ) :
template_name = ' project.html '
2014-08-29 13:55:43 +02:00
page_title = ' Project View '
2014-08-11 16:01:03 +02:00
form_class = FileUploadForm
2014-08-29 13:55:43 +02:00
success_url = ' /projects/ '
2014-08-29 15:52:09 +02:00
2014-08-29 13:55:43 +02:00
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 )
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 )
def form_valid ( self , form , * args , * * kwargs ) :
print ( " FORM: " )
attrs = vars ( form )
print ' , ' . join ( " %s : %s " % item for item in attrs . items ( ) )
return super ( ProjectView , self ) . form_valid ( form )