Gnumed.timelinelib.wxgui.dialogs.setcategory.controller

Package Gnumed :: Package timelinelib :: Package wxgui :: Package dialogs :: Package setcategory :: Module controller |
[frames] | no frames]

# Source Code for Module Gnumed.timelinelib.wxgui.dialogs.setcategory.controller

     1  # Copyright (C) 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017, 2018  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.wxgui.framework import Controller
    20
    21
    22 -class SetCategoryDialogController(Controller):
    23
    24 -    def on_init(self, db, selected_event_ids):
    25          self._db = db
    26          self._selected_event_ids = selected_event_ids
    27          self.view.PopulateCategories()
    28          self._set_title()
    29
    30 -    def on_ok_clicked(self, event):
    31          category = self.view.GetSelectedCategory()
    32          if not self._category_is_given(category) and self._selected_event_ids == []:
    33              self.view.DisplayErrorMessage(_("You must select a category!"))
    34          else:
    35              self._save_category_in_events(category)
    36              self.view.EndModalOk()
    37
    38 -    def _set_title(self):
    39          if self._selected_event_ids == []:
    40              self.view.SetTitle(_("Set Category on events without category"))
    41          else:
    42              self.view.SetTitle(_("Set Category on selected events"))
    43
    44 -    def _category_is_given(self, category):
    45          return category is not None
    46
    47 -    def _save_category_in_events(self, category):
    48          with self._db.transaction("Set category"):
    49              if self._selected_event_ids == []:
    50                  self._save_category_in_events_for_events_without_category(category)
    51              else:
    52                  self._save_category_in_events_for_selected_events(category)
    53
    54 -    def _save_category_in_events_for_selected_events(self, category):
    55          for event_id in self._selected_event_ids:
    56              event = self._db.find_event_with_id(event_id)
    57              event.set_category(category)
    58              event.save()
    59
    60 -    def _save_category_in_events_for_events_without_category(self, category):
    61          for event in self._db.get_all_events():
    62              if event.get_category() is None:
    63                  event.set_category(category)
    64                  event.save()
    65