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

Commit

Permalink
adding links to Family and Genus (from Species). #259.
Browse files Browse the repository at this point in the history
cleaning up session ownership and responsibility to close, relates to #253.
  • Loading branch information
mfrasca committed Jan 7, 2016
1 parent cc4ff5d commit 6a2770e
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 29 deletions.
85 changes: 76 additions & 9 deletions bauble/plugins/plants/infoboxes.glade
Original file line number Diff line number Diff line change
Expand Up @@ -265,11 +265,13 @@
<object class="GtkEventBox" id="g_eventbox1">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="visible_window">False</property>
<child>
<object class="GtkLabel" id="gen_fam_data">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xalign">0</property>
<property name="xpad">10</property>
<property name="label">--</property>
</object>
</child>
Expand Down Expand Up @@ -468,19 +470,84 @@
<property name="can_focus">False</property>
<property name="spacing">5</property>
<child>
<object class="GtkLabel" id="sp_name_data">
<object class="GtkVBox" id="sp_taxonomy_box">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
<property name="xalign">0</property>
<property name="ypad">5</property>
<property name="label">--</property>
<property name="use_markup">True</property>
<property name="selectable">True</property>
<child>
<object class="GtkHBox" id="sp_binomial_hbox">
<property name="visible">True</property>
<property name="can_focus">False</property>
<child>
<object class="GtkEventBox" id="sp_gen_ebx">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="visible_window">False</property>
<child>
<object class="GtkLabel" id="sp_gen_data">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xalign">0</property>
<property name="label">-</property>
<property name="use_markup">True</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkLabel" id="sp_epithet_data">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="events">GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK | GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK</property>
<property name="xalign">0</property>
<property name="ypad">5</property>
<property name="label">-</property>
<property name="use_markup">True</property>
<property name="selectable">True</property>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
<child>
<object class="GtkEventBox" id="sp_fam_ebx">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="visible_window">False</property>
<child>
<object class="GtkLabel" id="sp_fam_data">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="xalign">0</property>
<property name="xpad">10</property>
<property name="label">-</property>
<property name="use_markup">True</property>
</object>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">True</property>
<property name="position">1</property>
</packing>
</child>
</object>
<packing>
<property name="expand">False</property>
<property name="fill">False</property>
<property name="expand">True</property>
<property name="fill">True</property>
<property name="position">0</property>
</packing>
</child>
Expand Down
38 changes: 23 additions & 15 deletions bauble/plugins/plants/species.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
import os
import traceback

from sqlalchemy.orm.session import object_session

import bauble
import bauble.paths as paths
import bauble.db as db
Expand Down Expand Up @@ -69,8 +71,8 @@ def remove_callback(values):
The callback function to remove a species from the species context menu.
"""
from bauble.plugins.garden.accession import Accession
session = db.Session()
species = values[0]
session = object_session(species)
if isinstance(species, VernacularName):
species = species.species
nacc = session.query(Accession).filter_by(species_id=species.id).count()
Expand All @@ -92,8 +94,6 @@ def remove_callback(values):
msg = _('Could not delete.\n\n%s') % utils.xml_safe(e)
utils.message_details_dialog(msg, traceback.format_exc(),
type=gtk.MESSAGE_ERROR)
finally:
session.close()
return True


Expand Down Expand Up @@ -227,9 +227,8 @@ def update(self, row):
# remove old labels
syn_box.foreach(syn_box.remove)
logger.debug(row.synonyms)
from sqlalchemy.orm.session import object_session
self.session = object_session(row)
syn = self.session.query(SpeciesSynonym).filter(
session = object_session(row)
syn = session.query(SpeciesSynonym).filter(
SpeciesSynonym.synonym_id == row.id).first()
accepted = syn and syn.species
logger.debug("species %s is synonym of %s and has synonyms %s" %
Expand Down Expand Up @@ -285,7 +284,7 @@ def __init__(self, widgets):
general_box = self.widgets.sp_general_box
self.widgets.remove_parent(general_box)
self.vbox.pack_start(general_box)
self.widgets.sp_name_data.set_line_wrap(True)
self.widgets.sp_epithet_data.set_line_wrap(True)

# make the check buttons read only
def on_enter(button, *args):
Expand Down Expand Up @@ -314,13 +313,23 @@ def update(self, row):
:param row: the row to get the values from
'''
self.current_obj = row
# TODO: how do we put the genus in a seperate label so it
# can be clickable but still respect the text wrap to wrap
# around and indent from the genus name instead of from the
# species name
session = db.Session()
self.widget_set_value('sp_name_data', '<big>%s</big>' %
row.markup(True), markup=True)
session = object_session(row)
# link function
on_label_clicked = lambda l, e, x: select_in_search_results(x)
# Link to family
self.widget_set_value('sp_fam_data', '<small>(%s)</small>' %
row.genus.family.family, markup=True)
utils.make_label_clickable(
self.widgets.sp_fam_data, on_label_clicked, row.genus.family)
# link to genus
self.widget_set_value('sp_gen_data', '<big><i>%s</i></big>' %
row.genus.genus, markup=True)
utils.make_label_clickable(
self.widgets.sp_gen_data, on_label_clicked, row.genus)
# epithet (full binomial but missing genus)
self.widget_set_value('sp_epithet_data', '<big>%s</big>' %
row.markup(authors=True, genus=False),
markup=True)

awards = ''
if row.awards:
Expand Down Expand Up @@ -374,7 +383,6 @@ def update(self, row):
filter_by(id=row.id).distinct().count()
self.widget_set_value('sp_nplants_data', '%s in %s accessions'
% (nplants, nacc_in_plants))
session.close()


class SpeciesInfoBox(InfoBox):
Expand Down
19 changes: 14 additions & 5 deletions bauble/plugins/plants/species_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -379,20 +379,20 @@ def distribution_str(self):
dist = ['%s' % d for d in self.distribution]
return unicode(', ').join(sorted(dist))

def markup(self, authors=False):
def markup(self, authors=False, genus=True):
'''
returns this object as a string with markup
:param authors: flag to toggle whethe the author names should be
included
'''
return Species.str(self, authors, True)
return Species.str(self, authors, markup=True, genus=genus)

# in PlantPlugins.init() we set this to 'x' for win32
hybrid_char = utils.utf8(u'\u2a09') # U+2A09

@staticmethod
def str(species, authors=False, markup=False, remove_zws=False):
def str(species, authors=False, markup=False, remove_zws=False, genus=True):
'''
returns a string for species
Expand All @@ -405,7 +405,10 @@ def str(species, authors=False, markup=False, remove_zws=False):
# TODO: this method will raise an error if the session is none
# since it won't be able to look up the genus....we could
# probably try to query the genus directly with the genus_id
genus = str(species.genus)
if genus is True:
genus = str(species.genus)
else:
genus = ''
if species.sp:
sp = u'\u200b' + species.sp # prepend with zero_width_space
else:
Expand Down Expand Up @@ -485,6 +488,9 @@ def accepted(self):
'Name that should be used if name of self should be rejected'
from sqlalchemy.orm.session import object_session
session = object_session(self)
if not session:
logger.warn('species:accepted - object not in session')
return None
syn = session.query(SpeciesSynonym).filter(
SpeciesSynonym.synonym_id == self.id).first()
accepted = syn and syn.species
Expand All @@ -499,7 +505,10 @@ def accepted(self, value):
return
# remove any previous `accepted` link
from sqlalchemy.orm.session import object_session
session = object_session(self) or db.Session()
session = object_session(self)
if not session:
logger.warn('species:accepted.setter - object not in session')
return
session.query(SpeciesSynonym).filter(
SpeciesSynonym.synonym_id == self.id).delete()
session.commit()
Expand Down

0 comments on commit 6a2770e

Please sign in to comment.