Skip to content
This repository has been archived by the owner on Mar 22, 2018. It is now read-only.

Commit

Permalink
move markup functions to classes. close #258.
Browse files Browse the repository at this point in the history
will make a huge amount of tests fail, I will fix them next time.
add some more information when it comes to species (authorship and accepted name if status is synonym).
  • Loading branch information
mfrasca committed Jan 5, 2016
1 parent 89ab8ce commit e3a4574
Show file tree
Hide file tree
Showing 12 changed files with 101 additions and 122 deletions.
5 changes: 5 additions & 0 deletions bauble/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@ def __init__(cls, classname, bases, dict_):
cls.__mapper_args__ = {'extension': HistoryExtension()}
if 'top_level_count' not in dict_:
cls.top_level_count = lambda x: {classname: 1}
if 'search_view_markup_pair' not in dict_:
cls.search_view_markup_pair = lambda x: (
utils.xml_safe(str(x)),
'(%s)' % type(x).__name__)

super(MapperBase, cls).__init__(classname, bases, dict_)


Expand Down
22 changes: 7 additions & 15 deletions bauble/plugins/garden/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,15 @@
from bauble.view import SearchView
from bauble.plugins.garden.accession import AccessionEditor, \
Accession, AccessionInfoBox, AccessionNote, \
acc_context_menu, acc_markup_func
acc_context_menu
from bauble.plugins.garden.location import LocationEditor, \
Location, LocationInfoBox, loc_context_menu, loc_markup_func
Location, LocationInfoBox, loc_context_menu
from bauble.plugins.garden.plant import PlantEditor, PlantNote, \
Plant, PlantSearch, PlantInfoBox, plant_context_menu, plant_markup_func, \
Plant, PlantSearch, PlantInfoBox, plant_context_menu, \
plant_delimiter_key, default_plant_delimiter
from bauble.plugins.garden.source import \
Source, SourceDetail, SourceDetailInfoBox, source_detail_context_menu, \
Collection, collection_context_menu, coll_markup_func
Collection, collection_context_menu
from bauble.plugins.garden.institution import (
Institution, InstitutionCommand, InstitutionTool, start_institution_editor)

Expand All @@ -52,8 +52,6 @@
# - cultivation table
# - conservation table

from bauble import prefs


class GardenPlugin(pluginmgr.Plugin):

Expand All @@ -75,23 +73,20 @@ def init(cls):
SearchView.row_meta[Accession].set(
children=partial(db.natsort, "plants"),
infobox=AccessionInfoBox,
context_menu=acc_context_menu,
markup_func=acc_markup_func)
context_menu=acc_context_menu)

mapper_search.add_meta(('location', 'loc'), Location, ['name', 'code'])
SearchView.row_meta[Location].set(
children=partial(db.natsort, 'plants'),
infobox=LocationInfoBox,
context_menu=loc_context_menu,
markup_func=loc_markup_func)
context_menu=loc_context_menu)

mapper_search.add_meta(('plant', 'plants'), Plant, ['code'])
search.add_strategy(PlantSearch) # special search value strategy
#search.add_strategy(SpeciesSearch) # special search value strategy
SearchView.row_meta[Plant].set(
infobox=PlantInfoBox,
context_menu=plant_context_menu,
markup_func=plant_markup_func)
context_menu=plant_context_menu)

mapper_search.add_meta(('contact', 'contacts', 'person', 'org',
'source'), SourceDetail, ['name'])
Expand All @@ -102,11 +97,9 @@ def sd_kids(detail):
join(SourceDetail).options(eagerload('species')).\
filter(SourceDetail.id == detail.id).all()
return results
sd_markup_func = lambda c: utils.xml_safe(c)
SearchView.row_meta[SourceDetail].set(
children=sd_kids,
infobox=SourceDetailInfoBox,
markup_func=sd_markup_func,
context_menu=source_detail_context_menu)

mapper_search.add_meta(('collection', 'col', 'coll'),
Expand All @@ -116,7 +109,6 @@ def sd_kids(detail):
SearchView.row_meta[Collection].set(
children=coll_kids,
infobox=AccessionInfoBox,
markup_func=coll_markup_func,
context_menu=collection_context_menu)

# done here b/c the Species table is not part of this plugin
Expand Down
28 changes: 13 additions & 15 deletions bauble/plugins/garden/accession.py
Original file line number Diff line number Diff line change
Expand Up @@ -237,21 +237,6 @@ def remove_callback(accessions):
acc_context_menu = [edit_action, add_plant_action, remove_action]


def acc_markup_func(acc):
"""provide the two lines describing object for SearchView row.
it is a global function because it's invoked from a global environment.
see issue #258
"""
first, second = utils.xml_safe(unicode(acc)), acc.species_str(markup=True)
suffix = _("%(1)s plant groups in %(2)s location(s)") % {
'1': len(set(acc.plants)),
'2': len(set(p.location for p in acc.plants))}
suffix = ('<span foreground="#555555" size="small" '
'weight="light"> - %s</span>') % suffix
return first + suffix, second


# TODO: accession should have a one-to-many relationship on verifications
#ver_level = StringCol(length=2, default=None) # verification level
#ver_name = StringCol(length=50, default=None) # verifier's name
Expand Down Expand Up @@ -664,6 +649,19 @@ def validate_stripping(self, key, value):
intended2_location = relation(
'Location', primaryjoin='Accession.intended2_location_id==Location.id')

def search_view_markup_pair(self):
"""provide the two lines describing object for SearchView row.
"""
first, second = (utils.xml_safe(unicode(self)),
self.species_str(markup=True))
suffix = _("%(1)s plant groups in %(2)s location(s)") % {
'1': len(set(self.plants)),
'2': len(set(p.location for p in self.plants))}
suffix = ('<span foreground="#555555" size="small" '
'weight="light"> - %s</span>') % suffix
return first + suffix, second

@property
def pictures(self):
import operator
Expand Down
17 changes: 9 additions & 8 deletions bauble/plugins/garden/location.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,14 +92,6 @@ def remove_callback(locations):
loc_context_menu = [edit_action, add_plant_action, remove_action]


def loc_markup_func(location):
if location.description is not None:
return utils.xml_safe(str(location)), \
utils.xml_safe(str(location.description))
else:
return utils.xml_safe(str(location))


class Location(db.Base, db.Serializable):
"""
:Table name: location
Expand All @@ -125,6 +117,15 @@ class Location(db.Base, db.Serializable):
# relations
plants = relation('Plant', backref=backref('location', uselist=False))

def search_view_markup_pair(self):
'''provide the two lines describing object for SearchView row.
'''
if self.description is not None:
return (utils.xml_safe(str(self)),
utils.xml_safe(str(self.description)))
else:
return utils.xml_safe(str(self))

@validates('code', 'name')
def validate_stripping(self, key, value):
if value is None:
Expand Down
33 changes: 15 additions & 18 deletions bauble/plugins/garden/plant.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,6 @@ def remove_callback(plants):
edit_action, branch_action, remove_action, ]


def plant_markup_func(plant):
'''provide the two lines describing object for SearchView row.
see issue #258
'''
sp_str = plant.accession.species_str(markup=True)
dead_color = "#9900ff"
if plant.quantity <= 0:
dead_markup = '<span foreground="%s">%s</span>' % \
(dead_color, utils.xml_safe(plant))
return dead_markup, sp_str
else:
located_counted = ('%s <span foreground="#555555" size="small" '
'weight="light">- %s alive in %s</span>') % (
utils.xml_safe(plant), plant.quantity, plant.location)
return located_counted, sp_str


def get_next_code(acc):
"""
Return the next available plant code for an accession.
Expand Down Expand Up @@ -491,6 +473,21 @@ def validate_stripping(self, key, value):

_delimiter = None

def search_view_markup_pair(self):
'''provide the two lines describing object for SearchView row.
'''
sp_str = self.accession.species_str(markup=True)
dead_color = "#9900ff"
if self.quantity <= 0:
dead_markup = '<span foreground="%s">%s</span>' % \
(dead_color, utils.xml_safe(self))
return dead_markup, sp_str
else:
located_counted = ('%s <span foreground="#555555" size="small" '
'weight="light">- %s alive in %s</span>') % (
utils.xml_safe(self), self.quantity, self.location)
return located_counted, sp_str

@classmethod
def get_delimiter(cls, refresh=False):
"""
Expand Down
16 changes: 9 additions & 7 deletions bauble/plugins/garden/source.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,13 +48,6 @@
from types import StringTypes


def coll_markup_func(coll):
acc = coll.source.accession
safe = utils.xml_safe
return '%s - <small>%s</small>' % \
(safe(acc), safe(acc.species_str())), safe(coll)


def collection_edit_callback(coll):
from bauble.plugins.garden.accession import edit_callback
# TODO: set the tab to the source tab on the accessio neditor
Expand Down Expand Up @@ -253,6 +246,15 @@ class Collection(db.Base):

source_id = Column(Integer, ForeignKey('source.id'), unique=True)

def search_view_markup_pair(self):
'''provide the two lines describing object for SearchView row.
'''
acc = self.source.accession
safe = utils.xml_safe
return (
'%s - <small>%s</small>' % (safe(acc), safe(acc.species_str())),
safe(self))

def __str__(self):
return _('Collection at %s') % (self.locale or repr(self))

Expand Down
19 changes: 7 additions & 12 deletions bauble/plugins/plants/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,17 @@
import bauble.pluginmgr as pluginmgr
from bauble.plugins.plants.family import (
Familia, Family, FamilyInfoBox, FamilyEditor,
family_context_menu, family_markup_func)
family_context_menu)
from bauble.plugins.plants.genus import (
Genus, GenusEditor, GenusInfoBox,
genus_context_menu, genus_markup_func,
genus_context_menu,
)
from bauble.plugins.plants.species import (
Species, SpeciesEditorMenuItem, SpeciesInfoBox,
species_markup_func,
species_context_menu, add_accession_action,
SynonymSearch, SpeciesDistribution,
VernacularName, VernacularNameInfoBox,
vernname_context_menu, vernname_markup_func,
vernname_context_menu,
)
from bauble.plugins.plants.geography import (
Geography, get_species_in_geography)
Expand Down Expand Up @@ -326,14 +325,12 @@ def init(cls):
mapper_search.add_meta(('family', 'fam'), Family, ['family'])
SearchView.row_meta[Family].set(children="genera",
infobox=FamilyInfoBox,
context_menu=family_context_menu,
markup_func=family_markup_func)
context_menu=family_context_menu)

mapper_search.add_meta(('genus', 'gen'), Genus, ['genus'])
SearchView.row_meta[Genus].set(children="species",
infobox=GenusInfoBox,
context_menu=genus_context_menu,
markup_func=genus_markup_func)
context_menu=genus_context_menu)

from functools import partial
search.add_strategy(SynonymSearch)
Expand All @@ -343,16 +340,14 @@ def init(cls):
SearchView.row_meta[Species].set(
children=partial(db.natsort, 'accessions'),
infobox=SpeciesInfoBox,
context_menu=species_context_menu,
markup_func=species_markup_func)
context_menu=species_context_menu)

mapper_search.add_meta(('vernacular', 'vern', 'common'),
VernacularName, ['name'])
SearchView.row_meta[VernacularName].set(
children=partial(db.natsort, 'species.accessions'),
infobox=VernacularNameInfoBox,
context_menu=vernname_context_menu,
markup_func=vernname_markup_func)
context_menu=vernname_context_menu)

mapper_search.add_meta(('geography', 'geo'), Geography, ['name'])
SearchView.row_meta[Geography].set(children=get_species_in_geography)
Expand Down
8 changes: 0 additions & 8 deletions bauble/plugins/plants/family.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,6 @@ def remove_callback(families):
family_context_menu = [edit_action, add_species_action, remove_action]


def family_markup_func(family):
"""
return a string or object with __str__ method to use to markup
text in the results view
"""
return family


#
# Family
#
Expand Down
12 changes: 5 additions & 7 deletions bauble/plugins/plants/genus.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,13 +120,6 @@ def remove_callback(genera):
genus_context_menu = [edit_action, add_species_action, remove_action]


def genus_markup_func(genus):
'''
'''
# TODO: the genus should be italicized for markup
return utils.xml_safe(genus), utils.xml_safe(genus.family)


class Genus(db.Base, db.Serializable):
"""
:Table name: genus
Expand Down Expand Up @@ -170,6 +163,11 @@ class Genus(db.Base, db.Serializable):
rank = 'genus'
link_keys = ['accepted']

def search_view_markup_pair(self):
'''provide the two lines describing object for SearchView row.
'''
return utils.xml_safe(self), utils.xml_safe(self.family)

@property
def cites(self):
'''the cites status of this taxon, or None
Expand Down
24 changes: 1 addition & 23 deletions bauble/plugins/plants/species.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,29 +122,6 @@ def add_accession_callback(values):
vernname_context_menu = [edit_action]


def species_markup_func(species):
'''
'''
# TODO: add (syn) after species name if there are species synonyms that
# refer to the id of this plant
try:
if len(species.vernacular_names) > 0:
substring = '%s -- %s' % \
(species.genus.family,
', '.join([str(v) for v in species.vernacular_names]))
else:
substring = '%s' % species.genus.family
return species.markup(authors=False), substring
except:
return u'...', u'...'


def vernname_markup_func(vernname):
'''
'''
return str(vernname), vernname.species.markup(authors=False)


from bauble.view import InfoBox, InfoBoxPage, InfoExpander, \
select_in_search_results

Expand Down Expand Up @@ -272,6 +249,7 @@ def update(self, row):
syn_box.pack_start(box, expand=False, fill=False)
self.show_all()
self.set_sensitive(True)
self.set_expanded(True)
elif len(row.synonyms) == 0:
self.set_sensitive(False)
self.set_expanded(False)
Expand Down

0 comments on commit e3a4574

Please sign in to comment.