From 3b5bb2e712718348e295f49ba2b27c48f20f8b65 Mon Sep 17 00:00:00 2001 From: legac <renaud.legac@free.fr> Date: Sat, 6 Oct 2012 11:58:32 +0200 Subject: [PATCH] Improved the files organization of the model. --- models/access.py | 7 + models/db.py | 45 +++---- models/defaults.py | 4 - models/{dummy.py => virtdb.py} | 8 +- models/widgets.py | 227 --------------------------------- models/widgets_fields.py | 24 ++++ models/widgets_forms.py | 67 ++++++++++ models/widgets_grids.py | 94 ++++++++++++++ models/widgets_stores.py | 9 ++ models/widgets_viewport.py | 43 +++++++ 10 files changed, 263 insertions(+), 265 deletions(-) create mode 100644 models/access.py rename models/{dummy.py => virtdb.py} (83%) delete mode 100755 models/widgets.py create mode 100755 models/widgets_fields.py create mode 100755 models/widgets_forms.py create mode 100755 models/widgets_grids.py create mode 100755 models/widgets_stores.py create mode 100755 models/widgets_viewport.py diff --git a/models/access.py b/models/access.py new file mode 100644 index 00000000..cc58f794 --- /dev/null +++ b/models/access.py @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- +""" access + + setup the connection to the databases + +""" +db = DAL('sqlite://storage.sqlite', migrate=True) diff --git a/models/db.py b/models/db.py index 4d4bb567..6aaf29a9 100644 --- a/models/db.py +++ b/models/db.py @@ -2,13 +2,14 @@ """ db Defined the database tables for your applications. - Initialized the dbui plugins """ import locale from datetime import datetime from gluon.tools import PluginManager +dbui = local_import('plugin_dbui') + #------------------------------------------------------------------------------- # # plugin configuration parameters @@ -26,8 +27,6 @@ plugins.dbui.app_script_dir = 'static/scripts' # relative directory static/scri #------------------------------------------------------------------------------- # # set the languages for the application -# The language has also to be defined in the internationalization service, -# locale, since it is use latter on to order list in alphabetic order. # #------------------------------------------------------------------------------- T.set_current_languages('en', 'en-gb', 'en-us') # mother tongue @@ -38,10 +37,12 @@ locale.setlocale(locale.LC_ALL, "fr_FR.UTF-8") #------------------------------------------------------------------------------- # -# connection to the database +# set constants # #------------------------------------------------------------------------------- -db = DAL('sqlite://storage.sqlite') # if not, use SQLite or other DB +undef = T(dbui.UNDEF) +undef_id = dbui.UNDEF_ID +year = datetime.now().year #------------------------------------------------------------------------------- # @@ -98,13 +99,13 @@ tp_ratio = \ "Parameter for fuzzy string search." db.define_table("harvesters", - Field("id_teams", db.teams, default=1, label='Team'), + Field("id_teams", db.teams, default=undef_id, label='Team'), Field("controller", "string", notnull=True, comment=tp_controller), Field("host", "string", notnull=True, default='cdsweb.cern.ch', comment=tp_host), Field("collections", "string", comment=tp_collections), Field("max_records", "integer", notnull=True, default=200, comment=tp_max_records), Field("ratio", "double", notnull=True, default=1.0, comment=tp_ratio), - Field("id_categories", db.categories, default=1, label='Category', comment=tp_category)) + Field("id_categories", db.categories, default=undef_id, label='Category', comment=tp_category)) db.harvesters.controller.requires = \ IS_IN_SET(['articles', 'proceedings', 'reports', 'talks']) @@ -124,22 +125,6 @@ IS_FLOAT_IN_RANGE(0., 1.0) # Publications table # #------------------------------------------------------------------------------- -# -# Regular expression defining authors, authors_cppm and speaker -# -#match_authors = \ -#IS_MATCH(r'(((, )?([A-Z]\.(-[A-Z]\.)?)( [A-Z][a-z]+)+( et al.)?)+)') -# -#match_authors_cppm = \ -#IS_MATCH(r'((, )?([A-Z]\.(-[A-Z]\.)?)( [A-Z][a-z]+)+)+') - -#match_report_numbers = \ -#IS_MATCH(r'') - -#match_speaker = IS_MATCH(r'[A-Z]\.(-[A-Z]\.)?( [A-Z][a-z]+)+') - -year = datetime.now().year - tp_authors = \ """List of authors separated by commma: J. Doe, P.-Y. Smith For large collaboration the first author followed by et al.: J. Doe et al.""" @@ -157,8 +142,8 @@ tp_speaker = \ db.define_table("publications", Field("title", "text", notnull=True), Field("authors", "string", notnull=True, comment=tp_authors), - Field("id_collaborations", db.collaborations, default=1, label="Collaboration"), - Field("id_publishers", db.publishers, default=1, label="Publisher"), + Field("id_collaborations", db.collaborations, default=undef_id, label="Collaboration"), + Field("id_publishers", db.publishers, default=undef_id, label="Publisher"), Field("year", "integer", notnull=True, default=year, requires=IS_INT_IN_RANGE(1900, year+1)), Field("doi", "string"), Field("volume", "integer", requires=IS_EMPTY_OR(IS_INT_IN_RANGE(1, None))), @@ -170,14 +155,14 @@ db.define_table("publications", Field("conference_start", "date", label='Start date'), Field("conference_end", "date", label='End date'), Field("conference_town", "string", label='Town'), - Field("id_countries", db.countries, default=1, label='Country'), + Field("id_countries", db.countries, default=undef_id, label='Country'), Field("conference_speaker", "string", label='Speaker', comment=tp_speaker), Field("report_numbers", "string", comment=tp_report_numbers), - Field("id_reports", db.reports, default=1, label="Report type"), + Field("id_reports", db.reports, default=undef_id, label="Report type"), Field("authors_cppm", "text", notnull=True, comment=tp_authors_cppm), - Field("id_teams", db.teams, default=1, label='Team'), - Field("id_projects", db.projects, default=1, label='Projects'), - Field("id_categories", db.categories, default=1, label='AERES')) + Field("id_teams", db.teams, default=undef_id, label='Team'), + Field("id_projects", db.projects, default=undef_id, label='Projects'), + Field("id_categories", db.categories, default=undef_id, label='AERES')) db.publications.id_categories.requires = \ IS_IN_DB(db, 'categories.id', 'categories.code') diff --git a/models/defaults.py b/models/defaults.py index c871dfd9..edd43ea4 100644 --- a/models/defaults.py +++ b/models/defaults.py @@ -4,10 +4,6 @@ Insert defaults values in the tables """ -dbui = local_import('plugin_dbui') -undef = T(dbui.UNDEF) -undef_id = dbui.UNDEF_ID - if not db(db.categories.id).count(): db.categories.insert(id=undef_id, code=undef, definition=undef) diff --git a/models/dummy.py b/models/virtdb.py similarity index 83% rename from models/dummy.py rename to models/virtdb.py index 04f786f7..0a8fe01c 100644 --- a/models/dummy.py +++ b/models/virtdb.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- -""" dummy +""" virtdb - Defined tables in a dummy database. + Defined tables in a virtual database. They are used to build forms in order to generate url, .... taking benefit of the web2py validators @@ -9,12 +9,12 @@ """ -dummy = DAL(None) +virtdb = DAL(None) # the report node with selector tp_string = "tool tip for the string" -dummy.define_table("foo1", +virtdb.define_table("foo1", Field('my_string', 'string', comment=tp_string), Field('my_int', 'integer', requires=IS_INT_IN_RANGE(0, 100)), Field('my_date', 'date'), diff --git a/models/widgets.py b/models/widgets.py deleted file mode 100755 index c00aadfb..00000000 --- a/models/widgets.py +++ /dev/null @@ -1,227 +0,0 @@ -# -*- coding: utf-8 -*- -""" widgets - - Customize forms and grids widgets - -""" -from datetime import datetime -dbui = local_import('plugin_dbui') - -# -# add plugin to all grids and to all forms -# -dbui.configure_grids(db, plugins=['pGridRowEditorConfirmDelete', - 'pGridRowEditorContextMenu', - 'pGridRowEditorDblClick', - 'pGridExport']) - -dbui.configure_forms(db, plugins=['pFormToolTip']) - -# -# Modify publications fields -# -fieldsModifier = dbui.FieldsModifier('publications') - -# -# Setup date format and default values -# -fieldsModifier.configure_field('conference_start', - format='Y-m-d') - -fieldsModifier.configure_field('conference_end', - format='Y-m-d') - -fieldsModifier.configure_field('year', - maxValue=datetime.now().year) - -# -# Create composite fields for the publication form -# -fieldsModifier.merge_fields('first_page', - 'last_page', - fieldLabel=T('Pages'), - defaults={'flex': 1}) - -fieldsModifier.merge_fields('conference_start', - 'conference_end', - fieldLabel=T('Dates'), - defaults={'flex': 1}) - -# -# Create fieldSet for the publication form -# -formModifier = dbui.FormModifier('publications') -formModifier.merge_fields('title', - 'authors', - 'id_collaborations', - 'id_publishers', - 'year', - 'doi', - 'volume', - 'first_page', - 'e_print', - title=T('General'), - flex=1) - -formModifier.merge_fields('conference_title', - 'conference_url', - 'conference_start', - 'conference_town', - 'id_countries', - 'conference_speaker', - dbui.Spacer(height=26*3), - title=T('Conference'), - flex=1) - -formModifier.merge_fields('report_numbers', - 'id_reports', - dbui.Spacer(height=220), - title=T('Report'), - flex=1) - -formModifier.merge_fields('authors_cppm', - 'id_teams', - 'id_projects', - 'id_categories', - dbui.Spacer(height=26*5), - title='CPPM', - flex=1) - - -# -# Organize fieldSet within a TabPanel embedded in the publication form -# -formModifier.set_mapper(dbui.map_tabpanel) - -# -# Polish the look and feel of the publication form -# NOTE: the width/height define the size of the window -# when running the form from the grid. The defaults defines the height -# of the fieldSet allowing a fine tuning of the form layout. -# -formModifier.configure(buttonAlign='right', - labelWidth=100, - labelAlign='right', - width=400, - height=380, - defaults={'height': 320}) - -# -# fine tune categories grid -# -gridModifier = dbui.GridModifier('categories') -gridModifier.configure_column('code', width=10) -gridModifier.set_rownumbering() - -filters = [('code', 'contains', T('bla bla...'))] - -gridModifier.set_filters(*filters, - plugins=['pFormToolTip'], - width=300) - -# -# Define a column template for the publications grid -# -tpl = ['<b>{PublicationsTitle}</b><br>', - '{PublicationsAuthors}', - '<tpl if="PublicationsId_collaborations > 1">, {CollaborationsCollaboration}</tpl>', - '<tpl if="PublicationsDoi">, {PublicationsDoi}</tpl>', - '<tpl if="PublicationsId_publishers > 1">, {PublishersAbbreviation}', - '<tpl if="PublicationsVolume"> {PublicationsVolume}</tpl>', - '<tpl if="PublicationsYear"> ({PublicationsYear}) </tpl>', - '<tpl if="PublicationsFirst_page"> {PublicationsFirst_page}</tpl>', - '<tpl if="PublicationsLast_page">-{PublicationsLast_page}</tpl>', - '</tpl>', - '<tpl if="PublicationsConference_title"><br><br>{PublicationsConference_title}', - '<tpl if="PublicationsConference_town"><br>{PublicationsConference_town}</tpl>', - '<tpl if="CountriesCountry">, {CountriesCountry}</tpl>', - '<tpl if="PublicationsConference_start">, {PublicationsConference_start}</tpl>', - '<tpl if="PublicationsConference_end"> - {PublicationsConference_end}</tpl>', - '<tpl if="PublicationsConference_url"><br>{PublicationsConference_url}</tpl>', - '<br>%s: {PublicationsConference_speaker}' % T('Speaker'), - '</tpl>', - '<tpl if="PublicationsReport_numbers"><br><br>', - '<tpl if="PublicationsId_reports > 1">{ReportsType}, </tpl>', - '{PublicationsReport_numbers}', - '</tpl>' - ] - -gridModifier = dbui.GridModifier('publications') -gridModifier.append_plugins('pGridPaging', 'pGridExport', 'pGridMathJax') -gridModifier.merge_columns('title', - 'authors', - 'id_collaborations', - 'id_publishers', - 'doi', - 'volume', - 'first_page', - 'last_page', - 'conference_title', - 'conference_url', - 'conference_start', - 'conference_end', - 'conference_town', - 'id_countries', - 'conference_speaker', - 'report_numbers', - 'id_reports', - autohide=True, - header=T('Publication'), - position=0, - tpl=tpl, - width=700) - -gridModifier.hide_columns('authors_cppm', - 'e_print') - -gridModifier.set_rownumbering() - -# -# Setup a filter panel for the publication grid -# -filters = [('year', '==', T('select publication for a given year')), - ('id_teams', '==', T('select publications for a given team')), - ('id_projects', '==', T('select publications for a given project')), - ('id_categories', '==', T('select publications with a given category code')), - ('authors_cppm', 'contains', T('select publications for a given CPPM author')), - ('countries.country', 'contains', T('blab blab ....'))] - -gridModifier.set_filters(*filters, - plugins=['pFormToolTip'], - width=300) - -# -# order store for technical table -# -storeModifier = dbui.StoreModifier('teams') -storeModifier.orderby(~db.teams.team) - -# -# The navigation tree -# -formNode = dbui.Node(T('Forms')) -configurator = lambda tablename: dbui.to_formPanel(db[tablename]) -formNode.add_children(db.tables, func=configurator) - -gridNode = dbui.Node(T('Tables')) -configurator = lambda tablename: dbui.to_gridPanel(db[tablename]) -gridNode.add_children(db.tables, func=configurator) - -reportNode = dbui.Node(T('Reports')) -node = dbui.Panel(html="salut ma poule") -reportNode.add_child(T('report_1'), node) - -fields = dbui.to_fields(dummy.foo1) -selector = dbui.FieldSet(items=fields, plugins=['pFormToolTip'], - title=T('Select')) -node = dbui.PanelWithUrlSelector(baseUrl=URL('reports', 'report_2'), - isMathJax=dbui.is_mathjax(), - panelCfg=dbui.Panel(), - selectorCfg=selector, - selectorCollapsible=False) -reportNode.add_child(T('report_2'), node) - -viewportModifier = dbui.ViewportModifier() -viewportModifier.append_plugins('pViewportLogin') -viewportModifier.configure(logged=True) -viewportModifier.add_node(formNode, gridNode, reportNode) diff --git a/models/widgets_fields.py b/models/widgets_fields.py new file mode 100755 index 00000000..4331bdc3 --- /dev/null +++ b/models/widgets_fields.py @@ -0,0 +1,24 @@ +# -*- coding: utf-8 -*- +""" widgets fields + +""" +# +# Modify publications fields +# +fieldsModifier = dbui.FieldsModifier('publications') +fieldsModifier.configure_field('conference_start', format='Y-m-d') +fieldsModifier.configure_field('conference_end', format='Y-m-d') +fieldsModifier.configure_field('year', maxValue=datetime.now().year) + +# +# Merge fields for the publication form +# +fieldsModifier.merge_fields('first_page', + 'last_page', + fieldLabel=T('Pages'), + defaults={'flex': 1}) + +fieldsModifier.merge_fields('conference_start', + 'conference_end', + fieldLabel=T('Dates'), + defaults={'flex': 1}) \ No newline at end of file diff --git a/models/widgets_forms.py b/models/widgets_forms.py new file mode 100755 index 00000000..a8062f73 --- /dev/null +++ b/models/widgets_forms.py @@ -0,0 +1,67 @@ +# -*- coding: utf-8 -*- +""" widgets forms + +""" +# +# add plugin to all forms +# +dbui.configure_forms(db, plugins=['pFormToolTip']) + +# +# Create fieldSet for the publication form +# +formModifier = dbui.FormModifier('publications') +formModifier.merge_fields('title', + 'authors', + 'id_collaborations', + 'id_publishers', + 'year', + 'doi', + 'volume', + 'first_page', + 'e_print', + title=T('General'), + flex=1) + +formModifier.merge_fields('conference_title', + 'conference_url', + 'conference_start', + 'conference_town', + 'id_countries', + 'conference_speaker', + dbui.Spacer(height=26*3), + title=T('Conference'), + flex=1) + +formModifier.merge_fields('report_numbers', + 'id_reports', + dbui.Spacer(height=220), + title=T('Report'), + flex=1) + +formModifier.merge_fields('authors_cppm', + 'id_teams', + 'id_projects', + 'id_categories', + dbui.Spacer(height=26*5), + title='CPPM', + flex=1) + + +# +# Organize fieldSet within a TabPanel embedded in the publication form +# +formModifier.set_mapper(dbui.map_tabpanel) + +# +# Polish the look and feel of the publication form +# NOTE: the width/height define the size of the window +# when running the form from the grid. The defaults defines the height +# of the fieldSet allowing a fine tuning of the form layout. +# +formModifier.configure(buttonAlign='right', + labelWidth=100, + labelAlign='right', + width=400, + height=380, + defaults={'height': 320}) diff --git a/models/widgets_grids.py b/models/widgets_grids.py new file mode 100755 index 00000000..8f6836b3 --- /dev/null +++ b/models/widgets_grids.py @@ -0,0 +1,94 @@ +# -*- coding: utf-8 -*- +""" widgets grids + +""" +# +# add plugin to all grids +# +dbui.configure_grids(db, plugins=['pGridRowEditorConfirmDelete', + 'pGridRowEditorContextMenu', + 'pGridRowEditorDblClick', + 'pGridExport']) +# +# Polish categories grid +# +gridModifier = dbui.GridModifier('categories') +gridModifier.configure_column('code', width=10) +gridModifier.set_rownumbering() + +filters = [('code', 'contains', T('bla bla...'))] + +gridModifier.set_filters(*filters, + plugins=['pFormToolTip'], + width=300) + +# +# Define a column template for the publications grid +# +tpl = ['<b>{PublicationsTitle}</b><br>', + '{PublicationsAuthors}', + '<tpl if="PublicationsId_collaborations > 1">, {CollaborationsCollaboration}</tpl>', + '<tpl if="PublicationsDoi">, {PublicationsDoi}</tpl>', + '<tpl if="PublicationsId_publishers > 1">, {PublishersAbbreviation}', + '<tpl if="PublicationsVolume"> {PublicationsVolume}</tpl>', + '<tpl if="PublicationsYear"> ({PublicationsYear}) </tpl>', + '<tpl if="PublicationsFirst_page"> {PublicationsFirst_page}</tpl>', + '<tpl if="PublicationsLast_page">-{PublicationsLast_page}</tpl>', + '</tpl>', + '<tpl if="PublicationsConference_title"><br><br>{PublicationsConference_title}', + '<tpl if="PublicationsConference_town"><br>{PublicationsConference_town}</tpl>', + '<tpl if="CountriesCountry">, {CountriesCountry}</tpl>', + '<tpl if="PublicationsConference_start">, {PublicationsConference_start}</tpl>', + '<tpl if="PublicationsConference_end"> - {PublicationsConference_end}</tpl>', + '<tpl if="PublicationsConference_url"><br>{PublicationsConference_url}</tpl>', + '<br>%s: {PublicationsConference_speaker}' % T('Speaker'), + '</tpl>', + '<tpl if="PublicationsReport_numbers"><br><br>', + '<tpl if="PublicationsId_reports > 1">{ReportsType}, </tpl>', + '{PublicationsReport_numbers}', + '</tpl>' + ] + +gridModifier = dbui.GridModifier('publications') +gridModifier.append_plugins('pGridPaging', 'pGridExport', 'pGridMathJax') +gridModifier.merge_columns('title', + 'authors', + 'id_collaborations', + 'id_publishers', + 'doi', + 'volume', + 'first_page', + 'last_page', + 'conference_title', + 'conference_url', + 'conference_start', + 'conference_end', + 'conference_town', + 'id_countries', + 'conference_speaker', + 'report_numbers', + 'id_reports', + autohide=True, + header=T('Publication'), + position=0, + tpl=tpl, + width=700) + +gridModifier.hide_columns('authors_cppm', + 'e_print') + +gridModifier.set_rownumbering() + +# +# Setup a filter panel for the publication grid +# +filters = [('year', '==', T('select publication for a given year')), + ('id_teams', '==', T('select publications for a given team')), + ('id_projects', '==', T('select publications for a given project')), + ('id_categories', '==', T('select publications with a given category code')), + ('authors_cppm', 'contains', T('select publications for a given CPPM author')), + ('countries.country', 'contains', T('blab blab ....'))] + +gridModifier.set_filters(*filters, + plugins=['pFormToolTip'], + width=300) diff --git a/models/widgets_stores.py b/models/widgets_stores.py new file mode 100755 index 00000000..bf523e24 --- /dev/null +++ b/models/widgets_stores.py @@ -0,0 +1,9 @@ +# -*- coding: utf-8 -*- +""" widgets stores + +""" +# +# order store contents for technical tables +# +storeModifier = dbui.StoreModifier('teams') +storeModifier.orderby(~db.teams.team) diff --git a/models/widgets_viewport.py b/models/widgets_viewport.py new file mode 100755 index 00000000..36a6ad14 --- /dev/null +++ b/models/widgets_viewport.py @@ -0,0 +1,43 @@ +# -*- coding: utf-8 -*- +""" widgets viewport + +""" +# +# The form node +# +formNode = dbui.Node(T('Forms')) +configurator = lambda tablename: dbui.to_formPanel(db[tablename]) +formNode.add_children(db.tables, func=configurator) + +# +# the grid node +# +gridNode = dbui.Node(T('Tables')) +configurator = lambda tablename: dbui.to_gridPanel(db[tablename]) +gridNode.add_children(db.tables, func=configurator) + +# +# the report node +# +reportNode = dbui.Node(T('Reports')) +node = dbui.Panel(html="salut ma poule") +reportNode.add_child(T('report_1'), node) + +fields = dbui.to_fields(virtdb.foo1) +selector = dbui.FieldSet(items=fields, plugins=['pFormToolTip'], + title=T('Select')) +node = dbui.PanelWithUrlSelector(baseUrl=URL('reports', 'report_2'), + isMathJax=dbui.is_mathjax(), + panelCfg=dbui.Panel(), + selectorCfg=selector, + selectorCollapsible=False) +reportNode.add_child(T('report_2'), node) + +# +# The viewport with its navigation tree +# +print "hello", reportNode +viewportModifier = dbui.ViewportModifier() +viewportModifier.append_plugins('pViewportLogin') +viewportModifier.configure(logged=True) +viewportModifier.add_node(formNode, gridNode, reportNode) -- GitLab