Commit 4701cfb4 by Kálmán Viktor

dashboard: add validator conn command

parent db31f971
...@@ -54,7 +54,7 @@ from .models import Profile, GroupProfile ...@@ -54,7 +54,7 @@ from .models import Profile, GroupProfile
from circle.settings.base import LANGUAGES, MAX_NODE_RAM from circle.settings.base import LANGUAGES, MAX_NODE_RAM
from django.utils.translation import string_concat from django.utils.translation import string_concat
from .virtvalidator import domain_validator from .validators import domain_validator
from dashboard.models import ConnectCommand from dashboard.models import ConnectCommand
...@@ -1064,19 +1064,14 @@ class ConnectCommandForm(forms.ModelForm): ...@@ -1064,19 +1064,14 @@ class ConnectCommandForm(forms.ModelForm):
fields = ('name', 'access_method', 'template') fields = ('name', 'access_method', 'template')
model = ConnectCommand model = ConnectCommand
@property
def helper(self):
helper = FormHelper()
helper.add_input(Submit("submit", _("Save")))
return helper
def __init__(self, *args, **kwargs): def __init__(self, *args, **kwargs):
self.user = kwargs.pop("user", None) self.user = kwargs.pop("user")
super(ConnectCommandForm, self).__init__(*args, **kwargs) super(ConnectCommandForm, self).__init__(*args, **kwargs)
def clean(self): def clean(self):
if self.user: if self.user:
self.instance.user = self.user self.instance.user = self.user
return super(ConnectCommandForm, self).clean() return super(ConnectCommandForm, self).clean()
......
...@@ -49,6 +49,7 @@ from vm.tasks.agent_tasks import add_keys, del_keys ...@@ -49,6 +49,7 @@ from vm.tasks.agent_tasks import add_keys, del_keys
from vm.models.instance import ACCESS_METHODS from vm.models.instance import ACCESS_METHODS
from .store_api import Store, NoStoreException, NotOkException from .store_api import Store, NoStoreException, NotOkException
from .validators import connect_command_template_validator
logger = getLogger(__name__) logger = getLogger(__name__)
...@@ -113,10 +114,8 @@ class ConnectCommand(Model): ...@@ -113,10 +114,8 @@ class ConnectCommand(Model):
help_text=_('Template for connection command string. ' help_text=_('Template for connection command string. '
'Available parameters are: ' 'Available parameters are: '
'username, password, ' 'username, password, '
'host, port, app. Example: sshpass ' 'host, port.'),
'-p %(password)s ssh -o ' validators=[connect_command_template_validator])
'StrictHostKeyChecking=no %(username)s@'
'%(host)s -p %(port)s'))
def __unicode__(self): def __unicode__(self):
return self.template return self.template
......
...@@ -14,7 +14,28 @@ ...@@ -14,7 +14,28 @@
<h3 class="no-margin"><i class="fa fa-code"></i> {% trans "Create new command template" %}</h3> <h3 class="no-margin"><i class="fa fa-code"></i> {% trans "Create new command template" %}</h3>
</div> </div>
<div class="panel-body"> <div class="panel-body">
{% crispy form %} <form method="POST">
{% csrf_token %}
{{ form.name|as_crispy_field }}
{{ form.access_method|as_crispy_field }}
{{ form.template|as_crispy_field }}
<p class="text-muted">
{% trans "Examples" %}
</p>
<p>
<strong>SSH:</strong>
<span class="text-muted">
sshpass -p %(password)s ssh -o StrictHostKeyChecking=no cloud@%(host)s -p %(port)d
</span>
</p>
<p>
<strong>RDP:</strong>
<span class="text-muted">
rdesktop %(host)s:%(port)d -u cloud -p %(password)s
</span>
</p>
<input type="submit" class="btn btn-primary" value="{% trans "Save" %}">
</form>
</div> </div>
</div> </div>
</div> </div>
......
{% extends "dashboard/base.html" %} {% extends "dashboard/base.html" %}
{% load i18n %} {% load i18n %}
{% load sizefieldtags %}
{% load crispy_forms_tags %} {% load crispy_forms_tags %}
{% block title-page %}{% trans "Edit command template" %}{% endblock %} {% block title-page %}{% trans "Edit command template" %}{% endblock %}
...@@ -15,7 +14,28 @@ ...@@ -15,7 +14,28 @@
<h3 class="no-margin"><i class="fa fa-code"></i> {% trans "Edit command template" %}</h3> <h3 class="no-margin"><i class="fa fa-code"></i> {% trans "Edit command template" %}</h3>
</div> </div>
<div class="panel-body"> <div class="panel-body">
{% crispy form %} <form method="POST">
{% csrf_token %}
{{ form.name|as_crispy_field }}
{{ form.access_method|as_crispy_field }}
{{ form.template|as_crispy_field }}
<p class="text-muted">
{% trans "Examples" %}
</p>
<p>
<strong>SSH:</strong>
<span class="text-muted">
sshpass -p %(password)s ssh -o StrictHostKeyChecking=no cloud@%(host)s -p %(port)d
</span>
</p>
<p>
<strong>RDP:</strong>
<span class="text-muted">
rdesktop %(host)s:%(port)d -u cloud -p %(password)s
</span>
</p>
<input type="submit" class="btn btn-primary" value="{% trans "Save" %}">
</form>
</div> </div>
</div> </div>
</div> </div>
......
...@@ -125,7 +125,7 @@ ...@@ -125,7 +125,7 @@
</div> </div>
{% empty %} {% empty %}
<div class="input-group" id="dashboard-vm-details-connect-command"> <div class="input-group dashboard-vm-details-connect-command">
<span class="input-group-addon input-tags">{% trans "Command" %}</span> <span class="input-group-addon input-tags">{% trans "Command" %}</span>
<input type="text" spellcheck="false" value="{% trans "Connection is not possible." %}" <input type="text" spellcheck="false" value="{% trans "Connection is not possible." %}"
id="vm-details-connection-string" class="form-control input-tags" /> id="vm-details-connection-string" class="form-control input-tags" />
......
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.utils.translation import ugettext_lazy as _
from lxml import etree as ET from lxml import etree as ET
import logging import logging
...@@ -29,3 +31,15 @@ def domain_validator(value): ...@@ -29,3 +31,15 @@ def domain_validator(value):
relaxng.assertValid(parsed_xml) relaxng.assertValid(parsed_xml)
except Exception as e: except Exception as e:
raise ValidationError(e.message) raise ValidationError(e.message)
def connect_command_template_validator(value):
try:
value % {
'username': "uname",
'password': "pw",
'host': "111.111.111.111",
'port': 12345,
}
except (KeyError, TypeError, ValueError):
raise ValidationError(_("Invalid template string."))
...@@ -3341,6 +3341,11 @@ class ConnectCommandDetail(LoginRequiredMixin, SuccessMessageMixin, ...@@ -3341,6 +3341,11 @@ class ConnectCommandDetail(LoginRequiredMixin, SuccessMessageMixin,
raise PermissionDenied() raise PermissionDenied()
return super(ConnectCommandDetail, self).post(request, args, kwargs) return super(ConnectCommandDetail, self).post(request, args, kwargs)
def get_form_kwargs(self):
kwargs = super(ConnectCommandDetail, self).get_form_kwargs()
kwargs['user'] = self.request.user
return kwargs
class ConnectCommandDelete(LoginRequiredMixin, DeleteView): class ConnectCommandDelete(LoginRequiredMixin, DeleteView):
model = ConnectCommand model = ConnectCommand
......
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