Ask Your Question
2

how to override handle method in SelfHandlingForm in OpenStack horizon?

asked 2014-09-01 23:10:05 -0500

lafada gravatar image

updated 2014-09-05 18:08:28 -0500

smaffulli gravatar image

I want to add some more data to properties of CreateImageForm in handle method. So when it create image, it has one custom property abc.

After changes code might be like this

def handle(self, request, data):
        # Glance does not really do anything with container_format at the
        # moment. It requires it is set to the same disk_format for the three
        # Amazon image types, otherwise it just treats them as 'bare.' As such
        # we will just set that to be that here instead of bothering the user
        # with asking them for information we can already determine.
        if data['disk_format'] in ('ami', 'aki', 'ari',):
            container_format = data['disk_format']
        else:
            container_format = 'bare'

        meta = {'is_public': data['is_public'],
                'protected': data['protected'],
                'disk_format': data['disk_format'],
                'container_format': container_format,
                'min_disk': (data['minimum_disk'] or 0),
                'min_ram': (data['minimum_ram'] or 0),
                'name': data['name'],
                'properties': {}}

        if data['description']:
            meta['properties']['description'] = data['description']
        if data['architecture']:
            meta['properties']['architecture'] = data['architecture']

        ###################################
        # My changes 
        ###################################
        meta['properties']['abc'] = 'def'

        if (settings.HORIZON_IMAGES_ALLOW_UPLOAD and
                policy.check((("image", "upload_image"),), request) and
                data.get('image_file', None)):
            meta['data'] = self.files['image_file']
        else:
            meta['copy_from'] = data['copy_from']

        try:
            image = api.glance.image_create(request, **meta)
            messages.success(request,
                _('Your image %s has been queued for creation.') %
                data['name'])
            return image
        except Exception:
            exceptions.handle(request, _('Unable to create new image.'))

I want to do this without changing existing code, like overriding or inheriting class.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2014-12-18 23:16:44 -0500

teju gravatar image

I believe you can extend the CreateImageForm class as in below code:

http://paste.openstack.org/show/153022/

and you can include the new class in views.py as below:

form_class = MyCreateImageForm

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Get to know Ask OpenStack

Resources for moderators

Question Tools

1 follower

Stats

Asked: 2014-09-01 23:10:05 -0500

Seen: 1,515 times

Last updated: Sep 01 '14