def model_run(self, model_context): from PyQt4 import QtGui from camelot.view import action_steps yield action_steps.UpdateProgress(text=_('Closing form')) validator = model_context.admin.get_validator() obj = model_context.get_object() admin = model_context.admin if obj == None: yield action_steps.CloseView() # # validate the object, and if the object is valid, simply close # the view # messages = validator.objectValidity(obj) valid = (len(messages) == 0) if valid: yield action_steps.CloseView() else: # # if the object is not valid, request the user what to do # message = action_steps.MessageBox( '\n'.join(messages), QtGui.QMessageBox.Warning, _('Invalid form'), QtGui.QMessageBox.Ok | QtGui.QMessageBox.Discard) reply = yield message if reply == QtGui.QMessageBox.Discard: yield action_steps.CloseView() if admin.is_persistent(obj): admin.refresh(obj) yield action_steps.UpdateObject(obj) else: yield action_steps.DeleteObject(obj) admin.expunge(obj)
def model_run(self, model_context): from camelot.view import action_steps buttons = QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No answer = yield action_steps.MessageBox(title=self.message_title, text=self.message_text, standard_buttons=buttons) if answer == QtWidgets.QMessageBox.Yes: yield action_steps.UpdateEditor('value', None, propagate=True)
def model_run(self, model_context): from camelot.view import action_steps ok = yield action_steps.MessageBox( text='Are you sure you want to segfault the application', standard_buttons=QtGui.QMessageBox.No | QtGui.QMessageBox.Yes) if ok == QtGui.QMessageBox.Yes: import faulthandler faulthandler._read_null()
def model_run(self, model_context): # begin change object from camelot.view import action_steps app_admin = MetaCamelotAdmin() options = NewProjectOptions() yield action_steps.UpdateProgress(text='Request information') yield action_steps.ChangeObject( options, app_admin.get_related_admin(NewProjectOptions)) # end change object yield action_steps.UpdateProgress(text='Creating new project') self.start_project(options) project_path = os.path.abspath(options.source) if options.installer: cloudlaunch_found = False try: import cloudlaunch cloudlaunch_found = True except Exception: yield action_steps.MessageBox( 'To build a Windows installer, you need to be using<br/>' \ 'the Conceptive Python SDK, please visit<br/>' \ '<a href="http://www.conceptive.be/python-sdk.html">www.conceptive.be/python-sdk.html</a><br/>' \ 'for more information' ) if cloudlaunch_found: LOGGER.debug('%s imported' % (cloudlaunch.__name__)) yield action_steps.UpdateProgress( text='Building windows installer') import distutils.core current_dir = os.getcwd() # change to the app directory for setuptools to do its job os.chdir(project_path) setup_path = os.path.join('setup.py') distribution = distutils.core.run_setup( setup_path, script_args=['egg_info', 'bdist_cloud', 'wininst_cloud']) os.chdir(current_dir) for command, _python_version, filename in distribution.dist_files: if command == 'wininst_cloud': yield action_steps.MessageBox( 'Use Inno Setup to process the file<br/>' \ '<b>%s</b><br/> to build the installer executable'% os.path.join( project_path, filename ), standard_buttons = QtWidgets.QMessageBox.Ok ) yield action_steps.MessageBox( 'All files for the new project<br/>' \ 'were created in <b>%s</b>'%project_path, standard_buttons = QtWidgets.QMessageBox.Ok ) yield action_steps.OpenFile(project_path)
def model_run(self, model_context): from ...view import action_steps from six import StringIO import cProfile import pstats if self.profile is None: yield action_steps.MessageBox('Start profiler') self.profile = cProfile.Profile() self.profile.enable() else: yield action_steps.UpdateProgress(text='Creating statistics') self.profile.disable() stream = StringIO() stats = pstats.Stats(self.profile, stream=stream) self.profile = None stats.sort_stats('cumulative') yield action_steps.UpdateProgress(text='Create report') stats.print_stats() stream.seek(0) yield action_steps.OpenStream(stream) filename = action_steps.OpenFile.create_temporary_file('.prof') stats.dump_stats(filename) yield action_steps.MessageBox( 'Profile stored in {0}'.format(filename))
def model_run(self, model_context): from camelot.view import action_steps filenames = yield action_steps.SelectFile(self.file_name_filter) storage = model_context.field_attributes['storage'] for file_name in filenames: remove = False if model_context.field_attributes.get('remove_original'): reply = yield action_steps.MessageBox( text=_('Do you want to remove the original file?'), icon=QtWidgets.QMessageBox.Warning, title=_('The file will be stored.'), standard_buttons=QtWidgets.QMessageBox.No | QtWidgets.QMessageBox.Yes) if reply == QtWidgets.QMessageBox.Yes: remove = True yield action_steps.UpdateProgress(text='Attaching file') stored_file = storage.checkin(file_name) yield action_steps.UpdateEditor('value', stored_file, propagate=True) if remove: os.remove(file_name)
def model_run(self, model_context): from camelot.view import action_steps yield action_steps.UpdateProgress(text=_('Closing form')) validator = model_context.admin.get_validator() obj = model_context.get_object() admin = model_context.admin if obj is None: yield self.step_when_valid() raise StopIteration # # validate the object, and if the object is valid, simply close # the view # messages = validator.validate_object(obj) valid = (len(messages) == 0) if valid: yield self.step_when_valid() else: # # if the object is not valid, request the user what to do # message = action_steps.MessageBox( '\n'.join(messages), QtWidgets.QMessageBox.Warning, _('Invalid form'), QtWidgets.QMessageBox.Ok | QtWidgets.QMessageBox.Discard) reply = yield message if reply == QtWidgets.QMessageBox.Discard: if admin.is_persistent(obj): admin.refresh(obj) yield action_steps.UpdateObject(obj) else: yield action_steps.DeleteObject(obj) admin.expunge(obj) # only close the form after the object has been discarded or # deleted, to avoid yielding action steps after the widget mapper # has been garbage collected yield self.step_when_valid()
def model_run(self, model_context): from camelot.view import action_steps if model_context.selection_count <= 0: raise StopIteration admin = model_context.admin if model_context.admin.get_delete_mode() == 'on_confirm': step = action_steps.MessageBox(_('Please confirm'), admin.get_delete_message(None), QtGui.QMessageBox.Yes, QtGui.QMessageBox.No) response = yield step if response == QtGui.QMessageBox.No: raise StopIteration objects_to_remove = list(model_context.get_selection()) # # it might be impossible to determine the depending objects once # the object has been removed from the collection # depending_objects = set() for o in objects_to_remove: depending_objects.update(set(admin.get_depending_objects(o))) for i, obj in enumerate(objects_to_remove): yield action_steps.UpdateProgress(i, model_context.selection_count, _('Removing')) # # We should not update depending objects that have # been deleted themselves # try: depending_objects.remove(obj) except KeyError: pass for step in self.handle_object(model_context, obj): yield step for depending_obj in depending_objects: yield action_steps.UpdateObject(depending_obj) yield action_steps.FlushSession(model_context.session)
def model_run(self, model_context): import os.path import chardet from camelot.view import action_steps from camelot.view.import_utils import (UnicodeReader, RowData, RowDataAdmin, XlsReader, ColumnMapping, ColumnMappingAdmin) file_names = yield action_steps.SelectFile() if not len(file_names): return file_name = file_names[0] yield action_steps.UpdateProgress(text=_('Reading data')) # # read the data into temporary row_data objects # if os.path.splitext(file_name)[-1] == '.xls': items = XlsReader(file_name) else: detected = chardet.detect(open(file_name).read())['encoding'] enc = detected or 'utf-8' items = UnicodeReader(open(file_name), encoding=enc) collection = [RowData(i, row_data) for i, row_data in enumerate(items)] if len(collection) < 1: raise UserException(_('No data in file')) # # select columns to import # admin = model_context.admin columns = max(row_data.columns for row_data in collection) default_fields = [ field for field, fa in admin.get_columns() if fa.get('editable', True) ] column_mapping = ColumnMapping(columns, collection, admin, default_fields) field_choices = [ (f, entity_fa['name']) for f, entity_fa in admin.get_all_fields_and_attributes().items() if entity_fa.get('editable', True) ] column_mapping_admin = ColumnMappingAdmin(columns, admin, field_choices) yield action_steps.ChangeObject(column_mapping, column_mapping_admin) # # validate the temporary data # row_data_admin = RowDataAdmin(admin, column_mapping) yield action_steps.ChangeObjects(collection, row_data_admin) # # Ask confirmation # yield action_steps.MessageBox(icon=QtGui.QMessageBox.Warning, title=_('Proceed with import'), text=_( 'Importing data cannot be undone,\n' 'are you sure you want to continue')) # # import the temporary objects into real objects # for i, row in enumerate(collection): new_entity_instance = admin.entity() for field_name, attributes in row_data_admin.get_columns(): try: from_string = attributes['from_string'] except KeyError: LOGGER.warn( 'field %s has no from_string field attribute, dont know how to import it properly' % attributes['original_field']) from_string = lambda _a: None setattr(new_entity_instance, attributes['original_field'], from_string(getattr(row, field_name))) admin.add(new_entity_instance) # in case the model is a collection proxy, the new objects should # be appended model_context._model.append(new_entity_instance) yield action_steps.UpdateProgress(i, len(collection), _('Importing data')) yield action_steps.FlushSession(model_context.session) yield action_steps.Refresh()
def model_run(self, model_context): from camelot.view import action_steps from camelot.view.action_steps.profile import EditProfiles # dummy profiles new_profile, save_profiles, load_profiles = object(), object(), object( ) selected_profile = new_profile try: while selected_profile in (None, new_profile, save_profiles, load_profiles): profiles = self.profile_store.read_profiles() profiles.sort() items = [(None, '')] + [(p, p.name) for p in profiles] font = QtGui.QFont() font.setItalic(True) items.append({ Qt.UserRole: new_profile, Qt.FontRole: font, Qt.DisplayRole: ugettext('new/edit profile'), Qt.DecorationRole: self.new_icon }) if len(profiles): items.append({ Qt.UserRole: save_profiles, Qt.FontRole: font, Qt.DisplayRole: ugettext('save profiles'), Qt.DecorationRole: self.save_icon }) items.append({ Qt.UserRole: load_profiles, Qt.FontRole: font, Qt.DisplayRole: ugettext('load profiles'), Qt.DecorationRole: self.load_icon }) select_profile = action_steps.SelectItem(items) last_profile = self.profile_store.get_last_profile() select_profile.title = ugettext('Profile Selection') if len(profiles): subtitle = ugettext('Select a stored profile:') else: subtitle = ugettext('''Load profiles from file or''' ''' create a new profile''') select_profile.subtitle = subtitle if last_profile in profiles: select_profile.value = last_profile elif len(profiles): select_profile.value = None else: select_profile.value = load_profiles selected_profile = yield select_profile if selected_profile is new_profile: edit_profile_name = '' while selected_profile is new_profile: profile_info = yield EditProfiles( profiles, current_profile=edit_profile_name, dialog_class=self.edit_dialog_class) profile = self.profile_store.read_profile( profile_info['name']) if profile is None: profile = self.profile_store.profile_class( **profile_info) else: profile.__dict__.update(profile_info) yield action_steps.UpdateProgress( text=ugettext('Verifying database settings')) engine = profile.create_engine() try: connection = engine.raw_connection() cursor = connection.cursor() cursor.close() connection.close() except Exception as e: exception_box = action_steps.MessageBox( title=ugettext( 'Could not connect to database, please check host and port' ), text= _('Verify driver, host and port or contact your system administrator' ), standard_buttons=QtWidgets.QMessageBox.Ok) exception_box.informative_text = six.text_type(e) yield exception_box edit_profile_name = profile.name if profile in profiles: profiles.remove(profile) profiles.append(profile) profiles.sort() continue self.profile_store.write_profile(profile) selected_profile = profile elif selected_profile is save_profiles: file_name = yield action_steps.SaveFile( file_name_filter=self.file_name_filter) self.profile_store.write_to_file(file_name) elif selected_profile is load_profiles: file_names = yield action_steps.SelectFile( file_name_filter=self.file_name_filter) for file_name in file_names: self.profile_store.read_from_file(file_name) except CancelRequest: # explicit handling of exit when cancel button is pressed, # to avoid the use of subgenerators in the main action yield Exit() message = ugettext(u'Use {} profile'.format(selected_profile.name)) yield action_steps.UpdateProgress(text=message) self.profile_store.set_last_profile(selected_profile) self.selected_profile = selected_profile