2014-09-02 51 views
1

我想添加更多的數據到屬性CreateImageFormhandle方法。所以當它創建圖像時,它有一個自定義屬性abc如何覆蓋OpenStack horizo​​n中SelfHandlingForm中的句柄方法?

修改後的代碼可能是這樣的

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.')) 

我想這樣做,不改變現有的代碼,如覆蓋或繼承類。

回答

1

沒有覆蓋的方式沒有觸及任何代碼

ModalFormViewhorizon/forms/views.py
你可以看到form_valid方法使用form.handle(...)

所以這個handle方法是在地平線硬編碼。

至少,你要接觸一個地方覆蓋handle不直接修改CreateImageForm

# openstack_dashboard/dashboards/project/images/images/forms.py 
class YourCreateImageForm(CreateImageForm): # <== Create your form inherited from CreateImageForm! 
    def handle(self, request, data): 
     ... 
     (the whole your logic here) 
     ... 

# openstack_dashboard/dashboards/project/images/images/views.py 
class CreateView(forms.ModalFormView): 
    form_class = project_forms.YourCreateImageForm # <== touch here! 
    ... 

也許你想覆蓋它,因爲你在升級地平線的時候都害怕衝突未來。

如果你想定製的地平線,不碰任何東西,最好的辦法是:

  1. 創建您的dashborad(標籤)
  2. 添加您的面板。在面板中,您可以輕鬆繼承Project/Admin中需要的任何類,並更改所需的部分。添加邏輯,添加表單元素,添加任何東西...

最後,在你的情況下,你只是想添加一行代碼,那麼爲什麼不只是添加它?如果您升級Horizo​​n,我認爲這部分不會導致衝突。

+0

如果我添加新的儀表板和覆蓋方法,那麼它會向Horizo​​n添加新菜單,所以當用戶點擊我的菜單時,他將獲得新的功能,但如果他點擊舊菜單,不會得到它。 – Nilesh 2014-09-13 11:38:57

1

您可以使用HORIZON_CONFIG中的customization_module添加所需的任何覆蓋。只需將路徑添加到含有像這樣的替換文件:

HORIZON_CONFIG = { 
    'customization_module': 'some_path.overrides', 
} 

在您創建您可以添加一個猴子補丁覆蓋現有類與新類的overrides.py文件:

class YourCreateImageForm(CreateImageForm): 
    ... 


form_class = project_forms.YourCreateImageForm 

它基本上與上述評論中提到的想法基本相同,但您可以避免使用customization_module觸摸上游代碼。