def _add_child_pigeon(self, pindex, sire, dam, active): pigeon = None try: pigeon = pigeonparser.parser.add_empty_pigeon( pindex, enums.Sex.unknown, active, sire, dam) except ValueError: # Empty bandnumber return except database.InvalidValueError: # Pigeon does exist, update parents pigeon = pigeonparser.parser.get_pigeon(pindex) s, sy = common.get_band_from_pindex(sire) d, dy = common.get_band_from_pindex(dam) pigeon.sire = s pigeon.yearsire = sy pigeon.dam = d pigeon.yeardam = dy database.update_pigeon(pigeon.get_pindex(), { "sire": s, "yearsire": sy, "dam": d, "yeardam": dy }) if active: # Pigeon isn't visible, but user checked the "add to list" option pigeon.show = 1 database.update_pigeon(pigeon.get_pindex(), {"show": 1}) if pigeon.get_visible() and not self.maintreeview.has_pigeon(pigeon): self.maintreeview.add_pigeon(pigeon, False)
def update_pigeon(pigeon, data, status, statusdata): """ Update the pigeon @param pigeon: @param data: @param status: One of the status constants @param statusdata: """ if not data["sex"] in (enums.Sex.c**k, enums.Sex.hen, enums.Sex.unknown): raise ValueError( "Sex value has to be of type enums.Sex, but got '%r'" % data["sex"]) try: database.update_pigeon(pigeon.pindex, data) except database.InvalidValueError: if pigeon.show == 1: logger.debug("Pigeon already exists '%s'", pigeon.pindex) raise errors.PigeonAlreadyExists(pigeon.pindex) else: raise errors.PigeonAlreadyExistsHidden(pigeon.pindex) database.update_result_for_pindex(pigeon.pindex, {"pindex": data["pindex"]}) database.update_medication_for_pindex(pigeon.pindex, {"pindex": data["pindex"]}) database.update_media_for_pindex(pigeon.pindex, {"pindex": data["pindex"]}) # Remove the old thumbnail (if exists) if pigeon.get_image() and data["image"] != pigeon.get_image(): try: os.remove(thumbnail.get_path(pigeon.get_image())) except: pass old_status = pigeon.get_active() if status != old_status: # Status has changed. Remove the old status and add the new data. if old_status != enums.Status.active: database.remove_status(common.get_status_table(old_status), pigeon.pindex) if status != enums.Status.active: database.add_status(common.get_status_table(status), statusdata) else: # Status stayed the same, just update those values if status != enums.Status.active: database.update_status(common.get_status_table(status), pigeon.pindex, statusdata) # Save the data values database.add_data(database.Tables.COLOURS, data.get("colour", "")) database.add_data(database.Tables.STRAINS, data.get("strain", "")) database.add_data(database.Tables.LOFTS, data.get("loft", "")) return pigeonparser.parser.update_pigeon(data["pindex"], pigeon.pindex)
def menuremove_activate(self, widget): model, paths = self.widgets.selection.get_selected_rows() if self.widgets.selection.count_selected_rows() == 0: return elif self.widgets.selection.count_selected_rows() == 1: pigeon = self.widgets.treeview.get_selected_pigeon() pigeons = [pigeon] pigeonlabel = pigeon.get_band_string() statusbarmsg = _("Pigeon %s has been removed") % pigeonlabel show_result_option = database.pigeon_has_results(pigeon.get_pindex()) else: pigeons = [pobj for pobj in self.widgets.treeview.get_selected_pigeon()] pigeonlabel = ", ".join([pigeon.get_band_string() for pigeon in pigeons]) statusbarmsg = _("%s pigeons have been removed") % len(pigeons) show_result_option = False for pigeon in pigeons: if database.pigeon_has_results(pigeon.get_pindex()): show_result_option = True break self.widgets.labelPigeon.set_text(pigeonlabel) self.widgets.chkKeep.set_active(True) self.widgets.chkResults.set_active(False) self.widgets.chkResults.set_visible(show_result_option) answer = self.widgets.removedialog.run() if answer == 2: if self.widgets.chkKeep.get_active(): logger.debug("Remove: Hiding the pigeon(s)") for pigeon in pigeons: pigeon.show = 0 database.update_pigeon(pigeon.get_pindex(), {"show": 0}) else: remove_results = not self.widgets.chkResults.get_active() for pigeon in pigeons: corepigeon.remove_pigeon(pigeon, remove_results) # Reverse the pathlist so we can safely remove each row without # having problems with invalid paths. paths.reverse() # Block the selection changed handler during deletion. In some cases # while removing multiple rows the tree iters would get invalid in # the handler. There's no need for it to be called anyway after each # call, once at the end is enough. self.widgets.selection.handler_block_by_func(self.on_selection_changed) for path in paths: self.widgets.treeview.remove_row(path) self.widgets.selection.handler_unblock_by_func(self.on_selection_changed) self.widgets.selection.select_path(paths[-1]) self.widgets.statusbar.display_message(statusbarmsg) self.widgets.removedialog.hide()
def _edit_child(self, pigeon, child, clear=False): if child is None: return if clear: band, year = "", "" else: band, year = pigeon.get_band() if pigeon.get_sex() == enums.Sex.c**k: data = {"sire": band, "yearsire": year} else: data = {"dam": band, "yeardam": year} database.update_pigeon(child.get_pindex(), data) pigeonparser.parser.update_pigeon(child.get_pindex())
def on_addtopedigree_clicked(self, widget): result = self.widgets.resultview.get_selected() text = "%se %s %s %s." % (result["placed"], result["point"], result["out"], _("Pigeons")[0].lower()) for index, field in enumerate(self.pigeon.get_extra()): if field == "": database.update_pigeon(self.pigeon.pindex, {"extra%s" % (index + 1): text}) pigeonparser.parser.update_pigeon(self.pigeon.pindex) component.get("DetailsView").set_details(self.pigeon) break else: InfoDialog( (_("No empty space found in pedigree details."), "", ""), self._parent, None)
def operation_saved(self): """ Collect pigeon data, save it to the database and do the required steps for saving extra data. Return True to keep dialog open, False to close it """ try: data = self.get_edit_details() except errors.InvalidInputError as msg: ErrorDialog(msg.value, self.parent) return True if self._operation == enums.Action.edit: pindex = self.pigeon.get_pindex() data["pindex"] = common.get_pindex_from_band( data["band"], data["year"]) status = self.widgets.combostatus.get_active() statusdata = self._get_info_for_status(status, pindex) try: pigeon = corepigeon.update_pigeon(self.pigeon, data, status, statusdata) except errors.PigeonAlreadyExists: ErrorDialog(messages.MSG_PIGEON_EXISTS, self.parent) return False except errors.PigeonAlreadyExistsHidden: if WarningDialog(messages.MSG_SHOW_PIGEON, self.parent).run(): database.update_pigeon(pindex, {"show": 1}) pigeon = pigeonparser.parser.update_pigeon(pindex) except errors.InvalidInputError: # This is a corner case. Some status date is incorrect, but the # user choose another one. Don't bother him with this. pass elif self._operation == enums.Action.add: pindex = common.get_pindex_from_band(data["band"], data["year"]) data["pindex"] = pindex status = self.widgets.combostatus.get_active() statusdata = self._get_info_for_status(status, pindex) try: pigeon = corepigeon.add_pigeon(data, status, statusdata) except errors.PigeonAlreadyExists: ErrorDialog(messages.MSG_PIGEON_EXISTS, self.parent) return False except errors.PigeonAlreadyExistsHidden: if WarningDialog(messages.MSG_SHOW_PIGEON, self.parent).run(): database.update_pigeon(pindex, {"show": 1}) pigeon = pigeonparser.parser.update_pigeon(pindex) except errors.InvalidInputError: # See comment above pass self.set_details(pigeon) self.emit("edit-finished", pigeon, self._operation) combodata = [(self.widgets.combocolour, data["colour"]), (self.widgets.combostrain, data["strain"]), (self.widgets.comboloft, data["loft"])] for combo, value in combodata: combo.add_item(value) logger.debug("Operation '%s' finished", self._operation) return False