Commit 6bdf8445 authored by LE GAC Renaud's avatar LE GAC Renaud
Browse files

Complete running version with the new schema for the lists.

parent 70227736
""" Controllers handling list
"""
from gluon.dal import smart_query
from gluon.dal import Field, smart_query
from harvest_tools import get_id
from list_postprocessing import *
from list_tools import (category_article,
......@@ -11,6 +11,7 @@ from list_tools import (category_article,
category_talk,
Diplomas,
Publications,
get_sections,
get_selector)
from plugin_dbui import decode_field
......@@ -33,154 +34,78 @@ def index():
# retrieve the list data
list = db.lists[id_list]
# get the basic query for a diplomas or publications
# taking into account foreign keys and user requirements
tool = Diplomas(db)
tool.decode_request()
q_diplomas = tool.query()
tool = Publications(db)
tool.decode_request()
q_publications = tool.query()
# get the user requirements
# download the list as a file with a given format
tool = Publications(db)
iframe = tool.download()
if iframe:
return iframe
# retrieve the sections belonging to the list
# the section are ordered by position
# section with a position equal to 0 or negative are ignored
query = (db.sections.id_lists == id_list) & (db.sections.position > 0)
sections = db(query).select(orderby=db.sections.position)
if not sections:
return MSG_NO_SECTIONS % list.list
# build the query for each section.
# It handles categories associated to the section and section conditions
for section in sections:
q_section = q_publications
if section.tablename == 'diplomas':
q_section = q_diplomas
if section.categories:
q_cat = None
codes = section.categories.replace(' ','').split(',')
for code in codes:
q = db.categories.code == code
if q_cat:
q_cat = (q_cat) | (q)
else:
q_cat = q
q_section = (q_section) & (q_cat)
if section.conditions:
q_conditions = smart_query(db[section.tablename], section.conditions)
q_section = (q_section) & (q_conditions)
section.query = q_section
# retrieve the sections belonging to the list
# and add parameter query and orderby
sections = get_sections(db, list)
# build the order by directive associated to the section
# It handle directive for the list and for the section
for section in sections:
orderby = []
for i in range(1,5):
fieldname = 'split_%i' % i
if list[fieldname] == 'category':
continue
if list[fieldname]:
field = db[section.tablename][list[fieldname]]
if list[fieldname] == 'id_projects':
field = db.projects.project
if list[fieldname] == 'id_teams':
field = db.teams.team
if list[fieldname] == 'year' and list['dir_year'] == 'DESC':
field = ~field
orderby.append(field)
if section.sort_field:
field = db[section.tablename][section.sort_field]
if section.dir == 'DESC':
field = ~field
orderby.append(field)
section.orderby = tuple(orderby)
# get the splitting parameters defined at the list level
split_fields, split_tables = [], []
for i in range(1, 5):
fieldname = list['split_%i' % i]
if not fieldname:
break
if fieldname == 'id_projects':
fld, tbl = 'project', 'projects'
elif fieldname == 'id_teams':
fld, tbl = 'team', 'teams'
else:
fld, tbl = fieldname, section.tablename
split_fields.append(fld)
split_tables.append(tbl)
# retrieve the publications for each section
# organize them according to splits requirements, for example
# category
# category/team
# category/team/year → list
# organise them according to the level requirements, for example
# category articles
# category/team articles/lhcb
# category/team/year articles/lhcb/2012 → list
#
# format each record according to user requirements
paths_keys = []
paths_data = {}
for section in sections:
# retrieve the database field defining the levels of the section
# they will be used to build the path associated to each record
# This information is fully defined by the orderby directive
level_keys = []
for field in section.orderby:
if not isinstance(field, Field):
field = field.first
level_keys.append((field.tablename, field.name))
# retrieve the rendered associated to the section
renderer = db.renderers[section.id_renderers]
# retrieve the record
for record in db(section.query).select(orderby=section.orderby):
paths = []
for i in range(len(split_fields)):
fieldname = split_fields[i]
tablename = split_tables[i]
# build the list of paths -- similar to a list of directory
# example ['articles', 'articles/2012']
path = None
for i in range(len(level_keys)):
fieldname = level_keys[i][1]
tablename = level_keys[i][0]
if fieldname == 'category':
node = section.section
else:
node = record[tablename][fieldname]
node = record[tablename][fieldname]
node = str(node)
if paths:
p = "%s/%s" % (paths[-1], node)
if path:
path = "%s/%s" % (path, node)
else:
p = node
paths.append(p)
path = node
for p in paths:
if p not in paths_keys:
paths_keys.append(p)
last = paths[-1]
if last not in paths_data:
paths_data[last] = []
value = section.template.format(**record)
if path not in paths_keys:
paths_keys.append(path)
# fill the last path with its record
if path not in paths_data:
paths_data[path] = []
if section.postprocessing:
funcs = section.postprocessing.replace(' ','').split(',')
# format the record according to user requirement
value = renderer.template.format(**record)
# run post processing to polish/clean the value
if renderer.postprocessing:
funcs = renderer.postprocessing.replace(' ','').split(',')
for func in funcs:
if func in globals():
value = globals()[func](value, section.template, record)
value = globals()[func](value, renderer.template, record)
paths_data[last].append(value)
paths_data[path].append(value)
return dict(list=list,
paths_keys=paths_keys,
......
......@@ -5,6 +5,7 @@
"""
from gluon import current
from gluon.dal import smart_query
from gluon.html import IFRAME, URL
from plugin_dbui import decode_field
......@@ -50,6 +51,91 @@ def category_talk(db):
return db.categories.code == 'COM'
def get_sections(db, list):
"""Helper function returning a list of section.
Build the query and orderby directive for each section taking into
account list and user directive. The corresponding keyword are added
to the section storage.
"""
# get the basic query for a diplomas or publications
# taking into account foreign keys and user requirements
tool = Diplomas(db)
tool.decode_request()
q_diplomas = tool.query()
tool = Publications(db)
tool.decode_request()
q_publications = tool.query()
# build the list of sections embedded in the list
sections = []
for el in list.sections.split(','):
el = el.strip()
rows = db(db.sections.section == el).select()
# go to the next section if nothing is found in the database
if not rows:
continue
section = rows.first()
sections.append(section)
# build the query
# the basic query depends on the database table
q_section = q_publications
if section.tablename == 'diplomas':
q_section = q_diplomas
# add to the query the category codes processed by the section
if section.category_codes:
q_cat = None
codes = section.category_codes.replace(' ','').split(',')
for code in codes:
q = db.categories.code == code
if q_cat:
q_cat = (q_cat) | (q)
else:
q_cat = q
q_section = (q_section) & (q_cat)
# Add to the query the section conditions
if section.conditions:
q_conditions = smart_query(db[section.tablename], section.conditions)
q_section = (q_section) & (q_conditions)
section.query = q_section
# build the order by directive taking into account
# directive from the list and from the section
orderby = []
for i in range(1,5):
level = 'section_level_%i' % i
if not list[level]:
break
tablename = list[level]
fieldname = list['axis_granularity_%s' % list[level]]
direction = list["axis_direction_%s" % list[level]]
if tablename == 'time':
tablename = section.tablename
field = db[tablename][fieldname]
if direction == "DESC":
field = ~field
orderby.append(field)
section.orderby = tuple(orderby)
return sections
def get_selector(*fields):
"""Helper function returning a tuple with the selector value
corresponding to the list arguments fields.
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment