Commit a1875afa authored by legac's avatar legac
Browse files

Crude running version including a controller and a view.

parent bd5e0a55
""" Controllers for buildind list
""" Controllers handling list
"""
from gluon.dal import smart_query
from harvest_tools import get_id
from list_tools import (category_article,
category_preprint,
......@@ -8,7 +9,8 @@ from list_tools import (category_article,
category_report,
category_talk,
Diplomas,
Publications)
Publications,
get_selector)
from plugin_dbui import decode_field
......@@ -18,17 +20,123 @@ def index():
"""
# get the list identifier
listname = None
for key in request.vars:
t = decode_field(key)
if 'list' in t:
listname = request.vars[key]
break
if not listname:
(id_list,) = get_selector("id_lists")
# protection
if not id_list:
return "Please select a list...."
# retrieve the list data
list = db.lists[id_list]
# get the basic query for a diplomas or publications
# taking into account the user wishes
tool = Diplomas(db)
tool.decode_request()
q_diplomas = tool.query()
tool = Publications(db)
tool.decode_request()
q_publications = tool.query()
# retrieve the sections belonging to the list ordered by position
sections = []
query = db.lists_have_sections.id_lists == id_list
for row in db(query).select(orderby=db.lists_have_sections.position):
id_section = row.id_sections
sections.append(db.sections[id_section])
# 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
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
# 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,4):
fieldname = 'split_section_%i' % i
direction = 'dir_%i' % i
if list[fieldname]:
field = db[section.tablename][list[fieldname]]
if list[direction] == '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 = [list.split_section_1, list.split_section_2, list.split_section_3]
split_tables = [section.tablename]*3
for i in range(3):
if split_fields[i] == 'id_projects':
split_fields[i] = 'project'
split_tables[i] = 'projects'
elif split_fields[i] == 'id_teams':
split_fields[i] = 'team'
split_tables[i] = 'teams'
# retrieve the publications for each section
# split them according to user requirements
# format each record according to user requirements
paths_keys = []
paths_data = {}
for section in sections:
for record in db(section.query).select(orderby=section.orderby):
paths = [section.section]
for i in range(3):
fieldname = split_fields[i]
tablename = split_tables[i]
if fieldname:
p = "%s/%s" % (paths[-1], record[tablename][fieldname])
paths.append(p)
for el in paths:
if el not in paths_keys:
paths_keys.append(el)
last = paths[-1]
if last not in paths_data:
paths_data[last] = []
paths_data[last].append(record)
return 'List section %s' % listname
return dict(list=list, paths_keys=paths_keys, paths_data=paths_data)
def basic():
......
......@@ -73,7 +73,7 @@ IS_FLOAT_IN_RANGE(0., 1.0)
#
#-------------------------------------------------------------------------------
dirs = ['ASC', 'DESC']
splits = ['', 'id_projects', 'id_teams', 'years']
splits = ['', 'id_projects', 'id_teams', 'year']
db.define_table("lists",
Field("list", "string", notnull=True),
......@@ -88,13 +88,28 @@ db.define_table("lists",
Field("note", "text"),
migrate="lists.table")
tp_categories = \
T("List of publication category separated by a comma: ACL, ACLN. "
"Only the ublications with the given catgories will be displayed in the section.")
tp_section = \
T("Name of the section. it will be used as a section header in the list.")
tp_sort_field = \
T("Name of a database field. The field has to belong to the table tabelanme. "
"The publications of this section will be sort according to this field")
tp_tablename = \
T("Name of the database table containing the publications display in this section.")
db.define_table("sections",
Field("section", "string", notnull=True),
Field("tablename", "string", notnull=True),
Field("section", "string", notnull=True, comment=tp_section),
Field("tablename", "string", notnull=True, comment=tp_tablename),
Field("template", "text", notnull=True),
Field("categories", "text", notnull=True),
Field("categories", "text", notnull=True, comment=tp_categories),
Field("conditions", "text"),
Field("sort_field", "string"),
Field("sort_field", "string", comment=tp_sort_field),
Field("dir", "string", default='ASC', requires=IS_IN_SET(dirs)),
Field("note", "text"),
migrate="sections.table")
......@@ -104,11 +119,11 @@ db.sections.tablename.requires = IS_IN_SET(['diplomas', 'publications'])
db.define_table("lists_have_sections",
Field("id_lists", db.lists, label='List', notnull=True),
Field("id_sections", db.sections, label='Section', notnull=True),
Field("position", "integer", notnull=True),
Field("position", "integer", notnull=True, requires=IS_INT_IN_RANGE(1, 999)),
migrate="lists_have_sections.table")
db.lists_have_sections.id_lists.requires = \
IS_IN_DB(db, 'lists.id', 'lists.list')
db.lists_have_sections.id_sections.requires = \
IS_IN_DB(db, 'sections.id', 'sections.title')
IS_IN_DB(db, 'sections.id', 'sections.section')
......@@ -83,9 +83,13 @@ virtdb.define_table('list_selector_2',
Field('id_teams', db.teams, label='Team'),
Field('id_projects', db.projects, label='Project'),
Field('author', 'string'),
Field('list', 'string'),
Field('id_lists', db.lists, label="List"),
Field('format', 'string', default='html'))
virtdb.list_selector_2.id_lists.requires = IS_IN_DB(db,
'lists.id',
'lists.list')
virtdb.list_selector_2.id_projects.requires = IS_IN_DB(db,
'projects.id',
'projects.project')
......@@ -94,9 +98,6 @@ virtdb.list_selector_2.id_teams.requires = IS_IN_DB(db,
'teams.id',
'teams.team')
lists = [el.list for el in db().select(db.lists.list, orderby=db.lists.list)]
virtdb.list_selector_2.list.requires = IS_IN_SET(lists)
virtdb.list_selector_2.format.requires = IS_IN_SET(['html', 'tex', 'pdf'])
#
......
......@@ -3,7 +3,7 @@
"""
#
# merge fields for the harvest_selector forms
# merge fields for the harvest selector
#
fieldsModifier = dbui.FieldsModifier('harvester_selector')
fieldsModifier.configure_field('year_start', flex=1)
......@@ -27,7 +27,7 @@ fieldsModifier.configure_field('dir_3', flex=1)
fieldsModifier.merge_fields('split_section_3', 'dir_3', fieldLabel=T('Split section #3'))
#
# merge fields for the list_selector forms
# merge fields for the list selector
#
fieldsModifier = dbui.FieldsModifier('list_selector')
fieldsModifier.configure_field('year_start', flex=1)
......@@ -35,7 +35,7 @@ fieldsModifier.configure_field('year_end', flex=1)
fieldsModifier.merge_fields('year_start', 'year_end', fieldLabel=T('Year'))
#
# merge fields for the metric_selector forms
# merge fields for the metric selector
#
fieldsModifier = dbui.FieldsModifier('metric_selector')
fieldsModifier.configure_field('year_start', flex=1)
......@@ -54,3 +54,11 @@ fieldsModifier.configure_field('publication_date',
fieldsModifier.configure_field('year',
maxValue=datetime.now().year)
#
# Merge fields for the section forms
#
fieldsModifier = dbui.FieldsModifier('sections')
fieldsModifier.configure_field('sort_field', flex=2)
fieldsModifier.configure_field('dir', flex=1)
fieldsModifier.merge_fields('sort_field', 'dir', fieldLabel=T('Sort field'))
......@@ -50,6 +50,28 @@ def category_talk(db):
return db.categories.code == 'COM'
def get_selector(*fields):
"""Helper function returning a tuple with the selector value
corresponding to the list arguments fields.
They are search in request.vars
Note that the field is equal to None when not found in vars.
"""
di = {}
for el in fields:
di[el] = None
# keep in mind that the field are encode ad TablenameFieldName
# where tablename is the name of the selector
for key in current.request.vars:
t = decode_field(key)
if t[1] in fields:
di[t[1]] = current.request.vars[key]
return (di[el] for el in fields)
class ListTool(object):
"""Basic tool to build a publication list.
......@@ -177,10 +199,12 @@ class Diplomas(ListTool):
"""
db = self.db
# publication list query resolving foreign key
# query resolving foreign keys
query = db.diplomas.id_levels == db.levels.id
query = (query) & (db.diplomas.id_projects == db.projects.id)
query = (query) & (db.diplomas.id_teams == db.teams.id)
# add list selector constraints
# add selector constraints
if self.id_project:
query = (query) & (db.diplomas.id_projects == self.id_project)
......@@ -209,13 +233,15 @@ class Publications(ListTool):
"""
db = self.db
# publication list query resolving foreign key
# query resolving foreign keys
query = db.publications.id_collaborations == db.collaborations.id
query = (query) & (db.publications.id_categories == db.categories.id)
query = (query) & (db.publications.id_countries == db.countries.id)
query = (query) & (db.publications.id_projects == db.projects.id)
query = (query) & (db.publications.id_publishers == db.publishers.id)
# add list selector constraints
query = (query) & (db.publications.id_teams == db.teams.id)
# add selector constraints
if self.id_project:
query = (query) & (db.publications.id_projects == self.id_project)
......@@ -233,4 +259,4 @@ class Publications(ListTool):
query = (query) & (cnd)
return query
\ No newline at end of file
{{
for key in paths_keys:
margin = 3 + 3*key.count('/')
response.write(P(key.split('/')[-1], _class='my-p', _style="{margin-left:%s%%;" % margin))
if key not in paths_data:
continue
li = []
for record in paths_data[key]:
print type(record), 'publications' in record
print record.keys()
s = "{publications.title}, {collaborations.collaboration} et al.".format(**record)
# s = record.publications.title
response.write(s)
pass
pass
}}
\ No newline at end of file
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