Commit 5c8fa224 by Karsa Zoltán István

forms

parent 439fc600
......@@ -1551,6 +1551,12 @@ class CIDataForm(forms.ModelForm):
helper.form_show_labels = False
return helper
def clean(self):
cleaned_data = super(forms.ModelForm, self).clean()
Instance.validate_meta_data(cleaned_data['ci_meta_data'])
Instance.validate_user_data(cleaned_data['ci_user_data'])
return cleaned_data
class GroupPermissionForm(forms.ModelForm):
permissions = forms.ModelMultipleChoiceField(
......
......@@ -38,6 +38,7 @@ from django.utils import timezone
from django.utils.translation import ugettext_lazy as _, ugettext_noop
from passlib.hash import sha512_crypt
import yaml
from model_utils import Choices
from model_utils.managers import QueryManager
......@@ -47,6 +48,7 @@ from taggit.managers import TaggableManager
from django.db import models
from acl.models import AclBase
from django import forms
from common.models import (
activitycontextimpl, create_readable, HumanReadableException,
)
......@@ -370,11 +372,31 @@ class Instance(AclBase, VirtualMachineDescModel, StatusModel, OperatedMixin,
data = data.replace('{{password}}', passwd)
return data
@classmethod
def validate_user_data(cls, user_data):
data = user_data.replace('{{user}}', 'user')
data = data.replace('{{password}}', 'passwd')
try:
yaml.dump(yaml.load(data, Loader=yaml.Loader))
except yaml.YAMLError as exc:
raise forms.ValidationError(exc.problem_mark)
return True
@property
def get_meta_data(self):
data = str(self.ci_meta_data).replace('{{hostname}}', self.short_hostname)
return data
@classmethod
def validate_meta_data(cls, meta_data):
data = meta_data.replace('{{hostname}}', 'hostname')
try:
yaml.dump(yaml.load(data, Loader=yaml.Loader))
except yaml.YAMLError as exc:
raise forms.ValidationError(exc.problem_mark)
return True
def _update_status(self):
"""Set the proper status of the instance to Instance.status.
"""
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment