Beispiel #1
0
 def model_run(self, model_context):
     from camelot.view import action_steps
     for i, obj in enumerate(model_context.get_selection()):
         yield action_steps.UpdateProgress(i, model_context.selection_count,
                                           self.verbose_name)
         new_object = model_context.admin.copy(obj)
         model_context._model.append_object(new_object)
     yield action_steps.FlushSession(model_context.session)
Beispiel #2
0
 def model_run(self, model_context):
     from camelot.view import action_steps
     field_name, value_getter = yield action_steps.ChangeField(
         model_context.admin)
     yield action_steps.UpdateProgress(text=_('Replacing field'))
     if value_getter != None:
         value = value_getter()
         for obj in model_context.get_selection():
             setattr(obj, field_name, value)
         yield action_steps.FlushSession(model_context.session)
Beispiel #3
0
 def model_run(self, model_context):
     from sqlalchemy.orm import object_session
     from camelot.view import action_steps
     obj_getter = yield action_steps.SelectObject(model_context.admin)
     if obj_getter != None:
         obj_to_add = obj_getter()
         for obj in model_context.get_collection():
             if obj_to_add == obj:
                 raise StopIteration()
         model_context._model.append_object(obj_to_add, flush=False)
         yield action_steps.FlushSession(object_session(obj_to_add))
Beispiel #4
0
 def model_run(self, model_context):
     from camelot.model.authentication import get_current_authentication
     from camelot.view import action_steps
     from camelot.view.controls.editors.imageeditor import ImageEditor
     select_file = action_steps.SelectFile(
         file_name_filter=ImageEditor.filter)
     filenames = yield select_file
     for filename in filenames:
         yield action_steps.UpdateProgress(text=ugettext('Scale image'))
         image = QtGui.QImage(filename)
         image = image.scaled(self.image_size, self.image_size,
                              Qt.KeepAspectRatio)
         authentication = get_current_authentication()
         authentication.set_representation(image)
         yield action_steps.FlushSession(model_context.session)
Beispiel #5
0
 def model_run( self, model_context ):
     for person in model_context.get_selection():
         soc_number = person.social_security_number
         if soc_number:
             # assume the social sec number contains the birth date
             person.birth_date = datetime.date( int(soc_number[0:4]),
                                                int(soc_number[4:6]),
                                                int(soc_number[6:8])
                                                )
             # delete the email of the person
             for contact_mechanism in person.contact_mechanisms:
                 model_context.session.delete( contact_mechanism )
             # add a new email
             m = ('email', '*****@*****.**'%( person.first_name,
                                                 person.last_name ) )
             cm = party.ContactMechanism( mechanism = m )
             party.PartyContactMechanism( party = person,
                                         contact_mechanism = cm )                            
     # flush the session on finish and update the GUI
     yield action_steps.FlushSession( model_context.session )
Beispiel #6
0
 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)
Beispiel #7
0
 def model_run(self, model_context, new_status=None):
     """
     :param new_status: overrule the new_status defined at the class level,
         this can be used when overwwriting the `model_run` method in a
         subclass
     """
     new_status = new_status or self.new_status
     validator = model_context.admin.get_validator()
     for obj in model_context.get_selection():
         for message in validator.validate_object(obj):
             raise UserException(message)
     with model_context.session.begin():
         for obj in model_context.get_selection():
             # the number of status changes as seen in the UI
             number_of_statuses = len(obj.status)
             history_type = obj._status_history
             history = model_context.session.query(history_type)
             history = history.filter(history_type.status_for == obj)
             history = history.with_for_update(nowait=True)
             history_count = sum(1 for _h in history.yield_per(10))
             if number_of_statuses != history_count:
                 if obj not in model_context.session.new:
                     model_context.session.expire(obj)
                 yield action_steps.UpdateObject(obj)
                 raise UserException(
                     _('Concurrent status change'),
                     detail=_('Another user changed the status'),
                     resolution=_('Try again if needed'))
             if obj.current_status != new_status:
                 for step in self.before_status_change(model_context, obj):
                     yield step
                 obj.change_status(new_status)
                 for step in self.after_status_change(model_context, obj):
                     yield step
                 yield action_steps.UpdateObject(obj)
         yield action_steps.FlushSession(model_context.session)
Beispiel #8
0
    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()
Beispiel #9
0
 def model_run(self, model_context):
     for obj in model_context.get_selection():
         obj.change_status(self.new_status)
     yield action_steps.FlushSession(model_context.session)