Gnumed.timelinelib.editors.container

Package Gnumed :: Package timelinelib :: Package editors :: Module container |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.editors.container

      1  # Copyright (C) 2009, 2010, 2011  Rickard Lindberg, Roger Lindberg
      2  #
      3  # This file is part of Timeline.
      4  #
      5  # Timeline is free software: you can redistribute it and/or modify
      6  # it under the terms of the GNU General Public License as published by
      7  # the Free Software Foundation, either version 3 of the License, or
      8  # (at your option) any later version.
      9  #
     10  # Timeline is distributed in the hope that it will be useful,
     11  # but WITHOUT ANY WARRANTY; without even the implied warranty of
     12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13  # GNU General Public License for more details.
     14  #
     15  # You should have received a copy of the GNU General Public License
     16  # along with Timeline.  If not, see <http://www.gnu.org/licenses/>.
     17
     18
     19  from timelinelib.db.objects import Container
     20  from timelinelib.repositories.dbwrapper import DbWrapperEventRepository
     21
     22
     23 -class ContainerEditor(object):
     24      """
     25      This controller is responsible for two things:
     26        1. creating a new Container event
     27        2. updating properties of an existing Container event
     28      When creating a new Container event the result is NOT stored in the
     29      timeline database. This happens later when the first event added to the
     30      container is saved to the database.
     31      The reason for this behavior is that we don't want to have empty Conatiners
     32      in the database.
     33      When updating the properties of an existing Container event the changes
     34      are stored in the timeline database.
     35      """
     36 -    def __init__(self, view, db, container):
     37          self._set_initial_values_to_member_variables(view, db, container)
     38          self._set_view_initial_values()
     39
     40 -    def _set_initial_values_to_member_variables(self, view, db, container):
     41          self.view = view
     42          self.db = db
     43          self.container = container
     44          self.container_exists = (self.container != None)
     45          if self.container_exists:
     46              self.name = self.container.text
     47              self.category = self.container.category
     48          else:
     49              self.name = ""
     50              self.category = None
     51
     52 -    def _set_view_initial_values(self):
     53          self.view.set_name(self.name)
     54          self.view.set_category(self.category)
     55
     56      #
     57      # Dialog API
     58      #
     59 -    def save(self):
     60          self.name = self.view.get_name()
     61          self.category = self.view.get_category()
     62          try:
     63              self._verify_name()
     64              if self.container_exists:
     65                  self._update_container()
     66              else:
     67                  self._create_container()
     68              self.view.close()
     69          except ValueError:
     70              pass
     71
     72 -    def get_container(self):
     73          return self.container
     74
     75      #
     76      # Internals
     77      #
     78 -    def _verify_name(self):
     79          name_is_invalid = (self.name == "")
     80          if name_is_invalid:
     81              msg = _("Field '%s' can't be empty.") % _("Name")
     82              self.view.display_invalid_name(msg)
     83              raise ValueError()
     84
     85 -    def _update_container(self):
     86          self.container.update_properties(self.name, self.category)
     87          self._save_to_db()
     88
     89 -    def _save_to_db(self):
     90          try:
     91              DbWrapperEventRepository(self.db).save(self.container)
     92          except Exception, ex:
     93              self.view.display_db_exception(ex)
     94              raise ex
     95
     96 -    def _create_container(self):
     97          time_type = self.db.get_time_type()
     98          start = time_type.now()
     99          end = start
    100          self.container = Container(time_type, start, end, self.name,
    101                                     self.category)
    102