def _execute_with_errorhandling(self, method, *params): try: method(*params) except UnsupportedFileFormat as e: message = Message(ERROR_MESSAGE, messagetype='error', message=_("File format '%s' is not supported!" % e.file_format)) self.message_broker.send_message(message) return False except UnsupportedFileResolution as e: message = Message( ERROR_MESSAGE, messagetype='error', message=_( "File resolution {0:d} x {1:d} is not supported!".format( int(e.x_resolution), int(e.y_resolution)))) self.message_broker.send_message(message) return False except Exception as e: try: message_text = e.strerror except AttributeError: message_text = _("Unknown error while adding file") message = Message(ERROR_MESSAGE, messagetype='error', message=message_text) self.message_broker.send_message(message) return False return True
def test_set_document_by_message(self): document = self.document_service.get_by_id(8) self.message_broker.send_message(Message(REQ_GOTO_FIRST_DOCUMENT)) self.assertEqual(self.document_window_presenter.view.entity.id, 1) self.message_broker.send_message( Message(REQ_SET_DOCUMENT, document=document)) self.assertEqual(self.document_window_presenter.view.entity.id, 8)
def receive_message(self, message): if message == REQ_SET_EVENT: self.message_broker.send_message( Message(CONF_EVENT_CHANGED, event=message.event)) if message == REQ_SET_DOCUMENT: self.message_broker.send_message( Message(CONF_DOCUMENT_CHANGED, document=message.document)) if message == CONF_EVENT_CHANGED: self.message_label.set("Current event: %s" % message.event) if message == CONF_DOCUMENT_CHANGED: self.message_label.set("Current document: %s" % message.document) if message == ERROR_MESSAGE: self.message_label.set(message.message)
def reference_event(self): if self.view.current_event is not None and self.view.current_event.id is None: self.message_broker.send_message(Message(REQ_SAVE_CURRENT_EVENT)) assert (self.view.current_event.id is not None) reference_event = self.view.reference_event if reference_event == None: return if self.view.current_document.id is None: self.message_broker.send_message( Message(REQ_SAVE_CURRENT_DOCUMENT)) assert (self.view.current_document.id is not None) self.reference_service.link_document_to_event( self.view.current_document, reference_event) self._load_document_event_references(self.view.current_document)
def test_receive_message_IV(self): self.message_broker = self.injector.get( guiinjectorkeys.MESSAGE_BROKER_KEY) message = Message(CONF_DOCUMENT_CHANGED, document=self.document_dao.get_by_id(1)) self.message_broker.send_message(message) self.assertEqual(1, self.view.current_document.id) message = Message(CONF_DOCUMENT_CHANGED, document=None) self.message_broker.send_message(message) self.assertTrue(self.view.current_document is None)
def test_delete_iii(self): message = Message(CONF_DOCUMENT_CHANGED, document=self.document_dao.get_by_id(8)) self.message_broker.send_message(message) self.view.selected_item = None self.assertTrue(os.path.isfile("%s" % self.env.file_paths[8])) self.assertTrue(os.path.isfile("%s" % self.env.file_paths[9])) self.assertTrue(os.path.isfile("%s" % self.env.file_paths[10])) self.assertFalse(os.path.isfile("%s.deleted" % self.env.file_paths[8])) self.assertFalse(os.path.isfile("%s.deleted" % self.env.file_paths[9])) self.assertFalse(os.path.isfile("%s.deleted" % self.env.file_paths[10])) self.presenter.remove_file() self.assertTrue(os.path.isfile("%s" % self.env.file_paths[8])) self.assertTrue(os.path.isfile("%s" % self.env.file_paths[9])) self.assertTrue(os.path.isfile("%s" % self.env.file_paths[10])) self.assertFalse(os.path.isfile("%s.deleted" % self.env.file_paths[8])) self.assertFalse(os.path.isfile("%s.deleted" % self.env.file_paths[9])) self.assertFalse(os.path.isfile("%s.deleted" % self.env.file_paths[10])) self.assertEqual(3, len(self.view.items))
def test_receive_message(self): message = Message(CONF_DOCUMENT_CHANGED, document=self.document_dao.get_last()) self.message_broker.send_message(message) self.assertEqual(14, self.view.current_document.id) self.assertEqual(1, len(self.view.items))
def test_receive_message_edge_case(self): message = Message(CONF_DOCUMENT_CHANGED, document=None) self.presenter.receive_message(message) self.assertEqual(self.presenter.view.current_document, None) self.assertEqual(len(self.presenter.view.items), 0)
def add_button(self, master): message = Message(REQ_SET_DOCUMENT, document=Document(1)) load_references_button = Button( master, text='Change document', command=lambda m=message: self.message_broker.send_message(m)) load_references_button.pack(side=TOP)
def test_receive_message_1(self, event_id, number_of_references): ''' Assert that the event types are set on event changes. ''' message = Message(CONF_EVENT_CHANGED, event=self.event_service.get_by_id(event_id)) self.message_broker.send_message(message) self.assertEqual(number_of_references, len(self.view.items))
def receive_message(self, message): if message == REQ_SAVE_CURRENT_EVENT: entity = self._save() self.message_broker.send_message( Message(CONF_EVENT_CHANGED, event=entity)) if message == REQ_SET_EVENT: self._change_entity(message.event) if message == REQ_GOTO_FIRST_EVENT: self.goto_first()
def test_receive_message_III(self): message = Message(CONF_DOCUMENT_CHANGED, document=None) self.message_broker = self.injector.get( guiinjectorkeys.MESSAGE_BROKER_KEY) self.message_broker.send_message(message) self.assertEqual(0, len(self.view.items))
def add_new_cross_reference(self): new_cross_reference_event = self.view.new_cross_reference_event if new_cross_reference_event != None: if self.view.current_event.id is None: self.message_broker.send_message( Message(REQ_SAVE_CURRENT_EVENT)) assert (not self.view.current_event.id is None) self.event_service.add_cross_reference(self.view.current_event, new_cross_reference_event) self._update_crossreferences()
def set_current_event(self, event_id): if event_id is not None: event = self.event_dao.get_by_id(event_id) else: event = Event() event.daterange = AlexDateRange(AlexDate(1936), None) message = Message(CONF_EVENT_CHANGED, event=event) self.message_broker.send_message(message)
def add_button(self, master): message = Message(ERROR_MESSAGE, messagetype='error', message='An error has occurred') button = AlexButton( master, text='Send error message', command=lambda m=message: self.message_broker.send_message(m)) button.pack(side=TOP)
def test_receive_message_I(self): event = self.event_dao.get_by_id(1940000001) message = Message(CONF_EVENT_CHANGED, event=event) self.message_broker = self.injector.get( guiinjectorkeys.MESSAGE_BROKER_KEY) self.message_broker.send_message(message) self.assertEqual(self.view.current_event.id, event.id)
def add_event_type_reference(self): new_event_type = self.view.new_event_type if new_event_type is None or new_event_type in self.view.items: return if self.view.current_event.id is None: self.message_broker.send_message(Message(REQ_SAVE_CURRENT_EVENT)) assert (not self.view.current_event.id is None) self.event_service.add_event_type(self.view.current_event, new_event_type) self._load_event_types(self.view.current_event)
def testSavingII(self): self.document_window_presenter.goto_first() self.view.entity.description = "Totally new description" self.view._entity_has_changed = True self.message_broker.send_message(Message(REQ_SAVE_CURRENT_DOCUMENT)) entity = self.document_service.get_by_id(1) self.assertMessage(CONF_DOCUMENT_CHANGED) self.assertEqual(entity.description, "Totally new description")
def receive_message(self, message): if message == REQ_SAVE_CURRENT_DOCUMENT: entity = self._save() self.view.entity = entity self.message_broker.send_message( Message(CONF_DOCUMENT_CHANGED, document=entity)) if message == REQ_SET_DOCUMENT: self._change_entity(message.document) if message == REQ_GOTO_FIRST_DOCUMENT: self.goto_first()
def set_current_document(self, document_id): if document_id is not None: document = self.document_dao.get_by_id(document_id) else: document = Document() document.document_type = DocumentType(1) message = Message(CONF_DOCUMENT_CHANGED, document=document) self.message_broker.send_message(message)
def test_show_file(self): message = Message(CONF_DOCUMENT_CHANGED, document=self.document_dao.get_last()) self.message_broker.send_message(message) self.view.selected_item = self.view.items[0] self.view.show_file = None self.presenter.show_file() self.assertEqual(self.env.file_paths[14], self.view.show_file)
def test_add_event_type(self, event, number_of_references): message = Message(CONF_EVENT_CHANGED, event=event) self.message_broker.send_message(message) self.view.new_event_type = self.event_type_dao.get_by_id( EventTypeIdentifier(3, 2)) self.presenter.add_event_type_reference() self.assertEqual(number_of_references, len(self.event_service.get_event_types(event)))
def show_file(self): if not self.view.selected_item: return try: self.view.show_file = self.document_service.get_file_for_file_info( self.view.selected_item) except DocumentFileNotFound as exception: self.message_broker.send_message( Message(ERROR_MESSAGE, message=_("Document %s not found" % exception.document_file_info), messagetype='error'))
def add_file(self): file = self.view.new_file if file is None: return if self.view.current_document == None: return if self.view.current_document.id is None: self.message_broker.send_message( Message(REQ_SAVE_CURRENT_DOCUMENT)) assert (self.view.current_document.id is not None) if self._execute_with_errorhandling( self.document_service.add_document_file, self.view.current_document, file): self._load_file_infos(self.view.current_document)
def _load_systematic_items(self): if self.view.current_document == None: self.view.items = [] else: try: self.view.items = self.systematic_service.\ fetch_systematic_entries_for_document(self.view.current_document) except NoSuchNodeException as exception: self.message_broker.send_message( Message(ERROR_MESSAGE, messagetype='error', message=_('No systematic point %s' % exception.identifier))) self.view.items = []
def reference_document(self): if self.view.current_document is not None and self.view.current_document.id is None: self.message_broker.send_message( Message(REQ_SAVE_CURRENT_DOCUMENT)) assert (not self.view.current_document.id is None) document_id = self.view.new_document_id if document_id == None: return try: document = self.document_service.get_by_id(document_id) except NoSuchEntityException: self.message_broker.send_message( Message(ERROR_MESSAGE, messagetype='error', message='No such document')) return if self.view.current_event.id is None: self.message_broker.send_message(Message(REQ_SAVE_CURRENT_EVENT)) assert (not self.view.current_event.id is None) self.reference_service.link_document_to_event(document, self.view.current_event) self._load_event_document_references(self.view.current_event)
def test_remove_event_type(self): event = self.event_service.get_by_id(1940000001) event_type = self.event_type_dao.get_by_id(EventTypeIdentifier(5, 2)) message = Message(CONF_EVENT_CHANGED, event=event) self.message_broker.send_message(message) self.view.selected_item = event_type self.assertEqual(2, len(self.event_service.get_event_types(event))) self.presenter.remove_event_type_reference() self.assertEqual(1, len(self.event_service.get_event_types(event)))
def setup_for_replacement(self): # Init view message = Message(CONF_DOCUMENT_CHANGED, document=self.document_dao.get_by_id(8)) self.message_broker.send_message(message) # Assert before self.assertEqual(3, len(self.view.items)) self.assertEqual(10, self.view.items[1].id) self.assertEqual(400, self.view.items[1].resolution) self.assertEqual('tif', self.view.items[1].filetype) self.assertTrue(os.path.isfile("%s" % self.env.file_paths[9])) self.assertFalse(os.path.isfile("%s.deleted" % self.env.file_paths[9]))
def generate_pdf(self): ''' Generates the pdf as a temporary file. The caller needs to delete this file. ''' file = NamedTemporaryFile(mode="wb", suffix="pdf", delete=False) try: file.write(self.file_provider.get_pdf(self.view.current_document)) file.close() self.view.pdf_file = file.name except DocumentFileNotFound as exception: self.message_broker.send_message( Message(ERROR_MESSAGE, message=_("Document %s not found" % exception.document_file_info), messagetype='error'))
def test_remove_event_type_2(self): ''' Test with no type selected. ''' event = self.event_service.get_by_id(1940000001) message = Message(CONF_EVENT_CHANGED, event=event) self.message_broker.send_message(message) self.view.selected_item = None self.assertEqual(2, len(self.event_service.get_event_types(event))) self.presenter.remove_event_type_reference() self.assertEqual(2, len(self.event_service.get_event_types(event)))