From 93ac75da63ac1891764f3ddcb82aebe9b12a2660 Mon Sep 17 00:00:00 2001 From: Thierry Parmentelat Date: Wed, 4 Sep 2013 10:55:17 +0200 Subject: [PATCH] sliceview rewritten as a class-based view hide decorators in a single place portal/templateviews that defines 2 abstract classes from portal.templateviews import LoginRequiredView,LogoutOnManifoldExceptionView --- myslice/urls.py | 7 +- myslice/viewutils.py | 30 --- portal/sliceview.py | 504 ++++++++++++++++++++-------------------- portal/templateviews.py | 52 +++++ 4 files changed, 311 insertions(+), 282 deletions(-) create mode 100644 portal/templateviews.py diff --git a/myslice/urls.py b/myslice/urls.py index 6ab0fd9e..891eaafa 100644 --- a/myslice/urls.py +++ b/myslice/urls.py @@ -9,6 +9,8 @@ from django.conf import settings from django.template.loader import add_to_builtins add_to_builtins('insert_above.templatetags.insert_tags') +import portal.sliceview + # main entry point (set to the / URL) default_view='trash.pluginview.test_plugin_view' #default_view='portal.views.PlatformsView' @@ -43,8 +45,9 @@ urlpatterns = patterns( # # the slice view # - (r'^slice/?$', 'portal.sliceview.slice_view'), - (r'^slice/(?P[\w\.]+)/?$', 'portal.sliceview.slice_view'), + (r'^slice/?$', portal.sliceview.SliceView.as_view()), + (r'^slice/(?P[\w\.]+)/?$', portal.sliceview.SliceView.as_view()), + # # various trash views # (r'^tab/?$', 'trash.sampleviews.tab_view'), diff --git a/myslice/viewutils.py b/myslice/viewutils.py index cb1b31b9..7fbdc77f 100644 --- a/myslice/viewutils.py +++ b/myslice/viewutils.py @@ -1,9 +1,5 @@ # a set of utilities to help make the global layout consistent across views -from django.http import HttpResponseRedirect - -from manifold.manifoldresult import ManifoldException - def topmenu_items (current,request=None): has_user=request.user.is_authenticated() result=[] @@ -34,29 +30,3 @@ def the_user (request): else: return request.user.email - -# a decorator for view classes to catch manifold exceptions -# by design views should not directly exercise a manifold query -# given that these are asynchroneous, you would expect a view to just -# return a mundane skeleton -# however of course this is not always true, and if only for metadata -# that for some reason we deal with some other way, it is often a good idea -# for a view to monitor these exceptions - and to take this opportunity to -# logout people if it's a matter of expired session for example -def logout_on_manifold_exception (view_as_a_function): - def wrapped (request, *args, **kwds): - try: - return view_as_a_function(request,*args, **kwds) - except ManifoldException, manifold_result: - # xxx we need a means to display this message to user... - from django.contrib.auth import logout - logout(request) - return HttpResponseRedirect ('/') - except Exception, e: - # xxx we need to sugarcoat this error message in some error template... - print "Unexpected exception",e - import traceback - traceback.print_exc() - return HttpResponseRedirect ('/') - return wrapped - diff --git a/portal/sliceview.py b/portal/sliceview.py index a7003d74..d233c434 100644 --- a/portal/sliceview.py +++ b/portal/sliceview.py @@ -4,13 +4,14 @@ from django.template import RequestContext from django.shortcuts import render_to_response from django.contrib.auth.decorators import login_required +from portal.templateviews import LoginRequiredView,LogoutOnManifoldExceptionView + from unfold.page import Page from manifold.core.query import Query, AnalyzedQuery from manifold.manifoldresult import ManifoldException from manifold.metadata import MetaData as Metadata -# need to remove this dep. -from trash.trashutils import quickfilter_criterias -from myslice.viewutils import topmenu_items, the_user, logout_on_manifold_exception + +from myslice.viewutils import topmenu_items, the_user from plugins.raw.raw import Raw from plugins.stack.stack import Stack @@ -30,255 +31,258 @@ from plugins.messages.messages import Messages tmp_default_slice='ple.upmc.myslicedemo' debug = True -@logout_on_manifold_exception -@login_required -def slice_view (request, slicename=tmp_default_slice): - - page = Page(request) - page.expose_js_metadata() - - metadata = page.get_metadata() - resource_md = metadata.details_by_object('resource') - resource_fields = [column['name'] for column in resource_md['column']] - - user_md = metadata.details_by_object('user') - user_fields = ['user_hrn'] # [column['name'] for column in user_md['column']] - - # TODO The query to run is embedded in the URL - main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename) - main_query.select( - 'slice_hrn', - 'resource.resource_hrn', 'resource.hostname', 'resource.type', 'resource.network_hrn', - #'lease.urn', - 'user.user_hrn', - #'application.measurement_point.counter' - ) - - query_resource_all = Query.get('resource').select(resource_fields) - query_user_all = Query.get('user').select(user_fields) - - aq = AnalyzedQuery(main_query, metadata=metadata) - page.enqueue_query(main_query, analyzed_query=aq) - page.enqueue_query(query_resource_all) - page.enqueue_query(query_user_all) - - # Prepare the display according to all metadata - # (some parts will be pending, others can be triggered by users). - # - # For example slice measurements will not be requested by default... +class SliceView (LoginRequiredView, LogoutOnManifoldExceptionView): - # Create the base layout (Stack)... - main_plugin = Stack ( - page=page, - title="Slice !!view for %s"%slicename, - sons=[], - ) +# def __init__ (self, slicename=None): +# self.slicename = slicename or tmp_default_slice - # ... responsible for the slice properties... - - - main_plugin.insert ( - Raw (page=page,togglable=False, toggled=True,html="

Slice page for %s

"%slicename) - ) - - main_plugin.insert( - Raw (page=page,togglable=False, toggled=True,html='Description: TODO') - ) - - sq_plugin = Tabs ( - page=page, - title="Slice view for %s"%slicename, - togglable=False, - sons=[], - ) - - - # ... and for the relations - # XXX Let's hardcode resources for now - sq_resource = aq.subquery('resource') - sq_user = aq.subquery('user') - sq_lease = aq.subquery('lease') - sq_measurement = aq.subquery('measurement') + def get_or_logout (self,request, slicename=tmp_default_slice): - - ############################################################################ - # RESOURCES - # - # A stack inserted in the subquery tab that will hold all operations - # related to resources - # - - stack_resources = Stack( - page = page, - title = 'Resources', - sons=[], - ) - - resource_query_editor = QueryEditor( - page = page, - query = sq_resource, - ) - stack_resources.insert(resource_query_editor) - - resource_active_filters = ActiveFilters( - page = page, - query = sq_resource, - ) - stack_resources.insert(resource_active_filters) - - # -------------------------------------------------------------------------- - # Different displays = DataTables + GoogleMaps - # - tab_resource_plugins = Tabs( - page = page, - sons = [] - ) - - tab_resource_plugins.insert(Hazelnut( - page = page, - title = 'List', - domid = 'checkboxes', - # this is the query at the core of the slice list - query = sq_resource, - query_all = query_resource_all, - checkboxes = True, - datatables_options = { - # for now we turn off sorting on the checkboxes columns this way - # this of course should be automatic in hazelnut - 'aoColumns' : [None, None, None, None, {'bSortable': False}], - 'iDisplayLength' : 25, - 'bLengthChange' : True, - }, - )) - - tab_resource_plugins.insert(GoogleMaps( - page = page, - title = 'Geographic view', - domid = 'gmap', - # tab's sons preferably turn this off - togglable = False, - query = sq_resource, - query_all = query_resource_all, - checkboxes = True, - # center on Paris - latitude = 49., - longitude = 2.2, - zoom = 3, - )) - - stack_resources.insert(tab_resource_plugins) - - sq_plugin.insert(stack_resources) - - ############################################################################ - # USERS - # - - tab_users = Tabs( - page = page, - title = 'Users', - domid = 'thetabs2', - # activeid = 'checkboxes', - active_domid = 'checkboxes2', - ) - sq_plugin.insert(tab_users) - - tab_users.insert(Hazelnut( - page = page, - title = 'List', - domid = 'checkboxes2', - # tab's sons preferably turn this off - togglable = False, - # this is the query at the core of the slice list - query = sq_user, - query_all = query_user_all, - checkboxes = True, - datatables_options = { - # for now we turn off sorting on the checkboxes columns this way - # this of course should be automatic in hazelnut - 'aoColumns' : [None, None, None, None, {'bSortable': False}], - 'iDisplayLength' : 25, - 'bLengthChange' : True, - }, - )) - - tab_measurements = Tabs ( - page = page, - title = 'Measurements', - domid = 'thetabs3', - # activeid = 'checkboxes', - active_domid = 'checkboxes3', - ) - sq_plugin.insert(tab_measurements) - - tab_measurements.insert(Hazelnut( - page = page, - title = 'List', - domid = 'checkboxes3', - # tab's sons preferably turn this off - togglable = False, - # this is the query at the core of the slice list - query = sq_measurement, - checkboxes = True, - datatables_options = { - # for now we turn off sorting on the checkboxes columns this way - # this of course should be automatic in hazelnut - 'aoColumns' : [None, None, None, None, {'bSortable': False}], - 'iDisplayLength' : 25, - 'bLengthChange' : True, - }, - )) - - main_plugin.insert(sq_plugin) - - # -------------------------------------------------------------------------- - # ResourcesSelected - # - main_plugin.insert(ResourcesSelected( - page = page, - title = 'Pending operations', - query = main_query, - togglable = True, - )) - - main_plugin.insert(Messages( - page = page, - title = "Runtime messages for slice %s"%slicename, - domid = "msgs-pre", - levels = "ALL", - )) -# main_plugin.insert(Updater( -# page = page, -# title = "wont show up as non togglable by default", -# query = main_query, -# label = "Update slice", -# )) + page = Page(request) + page.expose_js_metadata() - - - # variables that will get passed to the view-unfold1.html template - template_env = {} + metadata = page.get_metadata() + resource_md = metadata.details_by_object('resource') + resource_fields = [column['name'] for column in resource_md['column']] - # define 'unfold1_main' to the template engine - the main contents - template_env [ 'unfold1_main' ] = main_plugin.render(request) - - # more general variables expected in the template - template_env [ 'title' ] = '%(slicename)s'%locals() - # the menu items on the top - template_env [ 'topmenu_items' ] = topmenu_items('Slice', request) - # so we can sho who is logged - template_env [ 'username' ] = the_user (request) - - # don't forget to run the requests - page.expose_queries () - - # xxx create another plugin with the same query and a different layout (with_datatables) - # show that it worls as expected, one single api call to backend and 2 refreshed views - - # the prelude object in page contains a summary of the requirements() for all plugins - # define {js,css}_{files,chunks} - prelude_env = page.prelude_env() - template_env.update(prelude_env) - result=render_to_response ('view-unfold1.html',template_env, - context_instance=RequestContext(request)) - return result + user_md = metadata.details_by_object('user') + user_fields = ['user_hrn'] # [column['name'] for column in user_md['column']] + + # TODO The query to run is embedded in the URL + main_query = Query.get('slice').filter_by('slice_hrn', '=', slicename) + main_query.select( + 'slice_hrn', + 'resource.resource_hrn', 'resource.hostname', 'resource.type', 'resource.network_hrn', + #'lease.urn', + 'user.user_hrn', + #'application.measurement_point.counter' + ) + + query_resource_all = Query.get('resource').select(resource_fields) + query_user_all = Query.get('user').select(user_fields) + + aq = AnalyzedQuery(main_query, metadata=metadata) + page.enqueue_query(main_query, analyzed_query=aq) + page.enqueue_query(query_resource_all) + page.enqueue_query(query_user_all) + + # Prepare the display according to all metadata + # (some parts will be pending, others can be triggered by users). + # + # For example slice measurements will not be requested by default... + + # Create the base layout (Stack)... + main_plugin = Stack ( + page=page, + title="Slice !!view for %s"%slicename, + sons=[], + ) + + # ... responsible for the slice properties... + + + main_plugin.insert ( + Raw (page=page,togglable=False, toggled=True,html="

Slice page for %s

"%slicename) + ) + + main_plugin.insert( + Raw (page=page,togglable=False, toggled=True,html='Description: TODO') + ) + + sq_plugin = Tabs ( + page=page, + title="Slice view for %s"%slicename, + togglable=False, + sons=[], + ) + + + # ... and for the relations + # XXX Let's hardcode resources for now + sq_resource = aq.subquery('resource') + sq_user = aq.subquery('user') + sq_lease = aq.subquery('lease') + sq_measurement = aq.subquery('measurement') + + + ############################################################################ + # RESOURCES + # + # A stack inserted in the subquery tab that will hold all operations + # related to resources + # + + stack_resources = Stack( + page = page, + title = 'Resources', + sons=[], + ) + + resource_query_editor = QueryEditor( + page = page, + query = sq_resource, + ) + stack_resources.insert(resource_query_editor) + + resource_active_filters = ActiveFilters( + page = page, + query = sq_resource, + ) + stack_resources.insert(resource_active_filters) + + # -------------------------------------------------------------------------- + # Different displays = DataTables + GoogleMaps + # + tab_resource_plugins = Tabs( + page = page, + sons = [] + ) + + tab_resource_plugins.insert(Hazelnut( + page = page, + title = 'List', + domid = 'checkboxes', + # this is the query at the core of the slice list + query = sq_resource, + query_all = query_resource_all, + checkboxes = True, + datatables_options = { + # for now we turn off sorting on the checkboxes columns this way + # this of course should be automatic in hazelnut + 'aoColumns' : [None, None, None, None, {'bSortable': False}], + 'iDisplayLength' : 25, + 'bLengthChange' : True, + }, + )) + + tab_resource_plugins.insert(GoogleMaps( + page = page, + title = 'Geographic view', + domid = 'gmap', + # tab's sons preferably turn this off + togglable = False, + query = sq_resource, + query_all = query_resource_all, + checkboxes = True, + # center on Paris + latitude = 49., + longitude = 2.2, + zoom = 3, + )) + + stack_resources.insert(tab_resource_plugins) + + sq_plugin.insert(stack_resources) + + ############################################################################ + # USERS + # + + tab_users = Tabs( + page = page, + title = 'Users', + domid = 'thetabs2', + # activeid = 'checkboxes', + active_domid = 'checkboxes2', + ) + sq_plugin.insert(tab_users) + + tab_users.insert(Hazelnut( + page = page, + title = 'List', + domid = 'checkboxes2', + # tab's sons preferably turn this off + togglable = False, + # this is the query at the core of the slice list + query = sq_user, + query_all = query_user_all, + checkboxes = True, + datatables_options = { + # for now we turn off sorting on the checkboxes columns this way + # this of course should be automatic in hazelnut + 'aoColumns' : [None, None, None, None, {'bSortable': False}], + 'iDisplayLength' : 25, + 'bLengthChange' : True, + }, + )) + + tab_measurements = Tabs ( + page = page, + title = 'Measurements', + domid = 'thetabs3', + # activeid = 'checkboxes', + active_domid = 'checkboxes3', + ) + sq_plugin.insert(tab_measurements) + + tab_measurements.insert(Hazelnut( + page = page, + title = 'List', + domid = 'checkboxes3', + # tab's sons preferably turn this off + togglable = False, + # this is the query at the core of the slice list + query = sq_measurement, + checkboxes = True, + datatables_options = { + # for now we turn off sorting on the checkboxes columns this way + # this of course should be automatic in hazelnut + 'aoColumns' : [None, None, None, None, {'bSortable': False}], + 'iDisplayLength' : 25, + 'bLengthChange' : True, + }, + )) + + main_plugin.insert(sq_plugin) + + # -------------------------------------------------------------------------- + # ResourcesSelected + # + main_plugin.insert(ResourcesSelected( + page = page, + title = 'Pending operations', + query = main_query, + togglable = True, + )) + + main_plugin.insert(Messages( + page = page, + title = "Runtime messages for slice %s"%slicename, + domid = "msgs-pre", + levels = "ALL", + )) + # main_plugin.insert(Updater( + # page = page, + # title = "wont show up as non togglable by default", + # query = main_query, + # label = "Update slice", + # )) + + + + # variables that will get passed to the view-unfold1.html template + template_env = {} + + # define 'unfold1_main' to the template engine - the main contents + template_env [ 'unfold1_main' ] = main_plugin.render(request) + + # more general variables expected in the template + template_env [ 'title' ] = '%(slicename)s'%locals() + # the menu items on the top + template_env [ 'topmenu_items' ] = topmenu_items('Slice', request) + # so we can sho who is logged + template_env [ 'username' ] = the_user (request) + + # don't forget to run the requests + page.expose_queries () + + # xxx create another plugin with the same query and a different layout (with_datatables) + # show that it worls as expected, one single api call to backend and 2 refreshed views + + # the prelude object in page contains a summary of the requirements() for all plugins + # define {js,css}_{files,chunks} + prelude_env = page.prelude_env() + template_env.update(prelude_env) + result=render_to_response ('view-unfold1.html',template_env, + context_instance=RequestContext(request)) + return result diff --git a/portal/templateviews.py b/portal/templateviews.py new file mode 100644 index 00000000..0d76fcd5 --- /dev/null +++ b/portal/templateviews.py @@ -0,0 +1,52 @@ +from django.contrib.auth.decorators import login_required +from django.utils.decorators import method_decorator +from django.http import HttpResponseRedirect +# for 'as_view' that we need to call in urls.py and the like +from django.views.generic.base import TemplateView + +from manifold.manifoldresult import ManifoldException + +########## the base class for views that require a login +class LoginRequiredView (TemplateView): + + @method_decorator(login_required) + def dispatch(self, *args, **kwargs): + return super(LoginRequiredView, self).dispatch(*args, **kwargs) + + +########## the base class for views that need to protect against ManifoldException +# a decorator for view classes to catch manifold exceptions +# by design views should not directly exercise a manifold query +# given that these are asynchroneous, you would expect a view to just +# return a mundane skeleton +# however of course this is not always true, +# e.g. we deal with metadata some other way, and so +# it is often a good idea for a view to monitor these exceptions +# and to take this opportunity to logout people + +def logout_on_manifold_exception (view_as_a_function): + def wrapped (request, *args, **kwds): + try: + return view_as_a_function(request,*args, **kwds) + except ManifoldException, manifold_result: + # xxx we need a means to display this message to user... + from django.contrib.auth import logout + logout(request) + return HttpResponseRedirect ('/') + except Exception, e: + # xxx we need to sugarcoat this error message in some error template... + print "Unexpected exception",e + import traceback + traceback.print_exc() + return HttpResponseRedirect ('/') + return wrapped + +# at first sight this matters only for views that require login +# however we prefer this to be explicit +# i.e. a user class has to inherit both LoginRequiredView and LogoutOnManifoldExceptionView + +class LogoutOnManifoldExceptionView (TemplateView): + + @logout_on_manifold_exception + def get (self, request, *args, **kwds): + return self.get_or_logout (request, *args, **kwds) -- 2.43.0