Commit 4688ff2d authored by BaiJiangJie's avatar BaiJiangJie

[Feature] 应用管理: Database 添加页面View

parent c99a2b7f
from .remote_app import *
from .database import *
# coding: utf-8
#
from django import forms
from django.utils.translation import ugettext as _
from orgs.mixins import OrgModelForm
from ..models import Database
__all__ = ['DatabaseCreateUpdateForm']
class DatabaseCreateUpdateForm(OrgModelForm):
password = forms.CharField(
widget=forms.PasswordInput, max_length=128,
strip=True, required=False, label=_("Password"),
)
class Meta:
model = Database
fields = [
'name', 'login_mode', 'type', 'host', 'port', 'user', 'password',
'database', 'comment'
]
help_texts = {
'login_mode': _('If you choose manual login mode, you do not '
'need to fill in the user and password.'),
}
......@@ -13,8 +13,9 @@ class DatabaseSerializer(BulkOrgResourceModelSerializer):
model = Database
list_serializer_class = AdaptedBulkListSerializer
fields = [
'id', 'name', 'login_mode', 'host', 'port', 'user', 'password',
'database', 'comment', 'created_by', 'date_created', 'date_updated',
'id', 'name', 'login_mode', 'type', 'host', 'port', 'user',
'password', 'database', 'comment', 'created_by', 'date_created',
'date_updated',
]
read_only_fields = [
......
{% extends 'base.html' %}
{% load i18n %}
{% load static %}
{% load bootstrap3 %}
{% block custom_head_css_js %}
<link href="{% static 'css/plugins/select2/select2.min.css' %}" rel="stylesheet">
<script src="{% static 'js/plugins/select2/select2.full.min.js' %}"></script>
{% endblock %}
{% block content %}
<div class="wrapper wrapper-content animated fadeInRight">
<div class="row">
<div class="col-sm-12">
<div class="ibox float-e-margins">
<div class="ibox-title">
<h5>{{ action }}</h5>
<div class="ibox-tools">
<a class="collapse-link">
<i class="fa fa-chevron-up"></i>
</a>
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
<i class="fa fa-wrench"></i>
</a>
<a class="close-link">
<i class="fa fa-times"></i>
</a>
</div>
</div>
<div class="ibox-content">
{% if form.non_field_errors %}
<div class="alert alert-danger">
{{ form.non_field_errors }}
</div>
{% endif %}
<form enctype="multipart/form-data" method="post" class="form-horizontal" action="" >
{% csrf_token %}
<h3>{% trans 'Basic' %}</h3>
{% bootstrap_field form.name layout="horizontal" %}
{% bootstrap_field form.login_mode layout="horizontal" %}
<h3>{% trans 'Database' %}</h3>
{% bootstrap_field form.type layout="horizontal" %}
{% bootstrap_field form.host layout="horizontal" %}
{% bootstrap_field form.port layout="horizontal" %}
{% bootstrap_field form.user layout="horizontal" %}
{% bootstrap_field form.password layout="horizontal" %}
{% bootstrap_field form.database layout="horizontal" %}
<h3>{% trans 'Other' %}</h3>
{% bootstrap_field form.comment layout="horizontal" %}
<div class="form-group">
<div class="col-sm-4 col-sm-offset-2">
<button class="btn btn-white" type="reset">{% trans 'Reset' %}</button>
<button id="submit_button" class="btn btn-primary" type="submit">{% trans 'Submit' %}</button>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block custom_foot_js %}
<script>
var login_mode_id = '#' + '{{ form.login_mode.id_for_label }}';
var password_id = '#' + '{{ form.password.id_for_label }}';
function fieldDisplay(){
var login_mode = $(login_mode_id).val();
if (login_mode === 'manual'){
$(password_id).closest('.form-group').addClass('hidden');
}
else if(login_mode === 'auto'){
$(password_id).closest('.form-group').removeClass('hidden');
}
}
$(document).ready(function () {
$('.select2').select2();
})
.on('change', login_mode_id, function(){
fieldDisplay();
})
.on("submit", "form", function (evt) {
evt.preventDefault();
var the_url = '{% url 'api-applications:database-list' %}';
var redirect_to = '{% url "applications:database-list" %}';
var method = "POST";
{% if type == "update" %}
the_url = '{% url 'api-applications:database-detail' pk=object.id %}';
redirect_to = '{% url "applications:database-list" %}';
method = "PUT";
{% endif %}
var form = $("form");
var data = form.serializeObject();
var props = {
url: the_url,
data: data,
method: method,
form: form,
redirect_to: redirect_to
};
formSubmit(props);
})
</script>
{% endblock %}
{% extends 'base.html' %}
{% load static %}
{% load i18n %}
{% block custom_head_css_js %}
<link href="{% static 'css/plugins/select2/select2.min.css' %}" rel="stylesheet">
<script src="{% static 'js/plugins/select2/select2.full.min.js' %}"></script>
{% endblock %}
{% block content %}
<div class="wrapper wrapper-content animated fadeInRight">
<div class="row">
<div class="col-sm-12">
<div class="ibox float-e-margins">
<div class="panel-options">
<ul class="nav nav-tabs">
<li class="active">
<a href="{% url 'applications:database-detail' pk=database.id %}" class="text-center"><i class="fa fa-laptop"></i> {% trans 'Detail' %} </a>
</li>
<li class="pull-right">
<a class="btn btn-outline btn-default" href="{% url 'applications:database-update' pk=database.id %}"><i class="fa fa-edit"></i>{% trans 'Update' %}</a>
</li>
<li class="pull-right">
<a class="btn btn-outline btn-danger btn-delete">
<i class="fa fa-trash-o"></i>{% trans 'Delete' %}
</a>
</li>
</ul>
</div>
<div class="tab-content">
<div class="col-sm-8" style="padding-left: 0;">
<div class="ibox float-e-margins">
<div class="ibox-title">
<span class="label"><b>{{ database.name }}</b></span>
<div class="ibox-tools">
<a class="collapse-link">
<i class="fa fa-chevron-up"></i>
</a>
<a class="dropdown-toggle" data-toggle="dropdown" href="#">
<i class="fa fa-wrench"></i>
</a>
<ul class="dropdown-menu dropdown-user">
</ul>
<a class="close-link">
<i class="fa fa-times"></i>
</a>
</div>
</div>
<div class="ibox-content">
<table class="table">
<tbody>
<tr class="no-borders-tr">
<td>{% trans 'Name' %}:</td>
<td><b>{{ database.name }}</b></td>
</tr>
<tr>
<td>{% trans 'Login mode' %}:</td>
<td><b>{{ database.login_mode }}</b></td>
</tr>
<tr>
<td>{% trans 'Type' %}:</td>
<td><b>{{ database.type }}</b></td>
</tr>
<tr>
<td>{% trans 'Host' %}:</td>
<td><b>{{ database.host }}</b></td>
</tr>
<tr>
<td>{% trans 'User' %}:</td>
<td><b>{{ database.user }}</b></td>
</tr>
<tr>
<td>{% trans 'Database' %}:</td>
<td><b>{{ database.database }}</b></td>
</tr>
<tr>
<td>{% trans 'Created by' %}:</td>
<td><b>{{ database.created_by }}</b></td>
</tr>
<tr>
<td>{% trans 'Date created' %}:</td>
<td><b>{{ database.date_created }}</b></td>
</tr>
<tr>
<td>{% trans 'Date updated' %}:</td>
<td><b>{{ database.date_updated }}</b></td>
</tr>
<tr>
<td>{% trans 'Comment' %}:</td>
<td><b>{{ database.comment }}</b></td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block custom_foot_js %}
<script>
$(document).ready(function () {
})
.on('click', '.btn-delete', function () {
var $this = $(this);
var name = "{{ database.name }}";
var rid = "{{ database.id }}";
var the_url = '{% url "api-applications:database-detail" pk=DEFAULT_PK %}'.replace('{{ DEFAULT_PK }}', rid);
var redirect_url = "{% url 'applications:database-list' %}";
objectDelete($this, name, the_url, redirect_url);
})
</script>
{% endblock %}
{% extends '_base_list.html' %}
{% load i18n static %}
{% block table_search %}{% endblock %}
{% block table_container %}
<div class="uc pull-left m-r-5">
<a href="{% url 'applications:database-create' %}" class="btn btn-sm btn-primary"> {% trans "Create Database" %} </a>
</div>
<table class="table table-striped table-bordered table-hover " id="database_list_table" >
<thead>
<tr>
<th class="text-center">
<input type="checkbox" id="check_all" class="ipt_check_all" >
</th>
<th class="text-center">{% trans 'Name' %}</th>
<th class="text-center">{% trans 'Login mode' %}</th>
<th class="text-center">{% trans 'Type' %}</th>
<th class="text-center">{% trans 'Host' %}</th>
<th class="text-center">{% trans 'Port' %}</th>
<th class="text-center">{% trans 'User' %}</th>
<th class="text-center">{% trans 'Database' %}</th>
<th class="text-center">{% trans 'Comment' %}</th>
<th class="text-center">{% trans 'Action' %}</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
{% endblock %}
{% block content_bottom_left %}{% endblock %}
{% block custom_foot_js %}
<script>
function initTable() {
var options = {
ele: $('#database_list_table'),
columnDefs: [
{targets: 1, createdCell: function (td, cellData, rowData) {
cellData = htmlEscape(cellData);
{% url 'applications:database-detail' pk=DEFAULT_PK as the_url %}
var detail_btn = '<a href="{{ the_url }}">' + cellData + '</a>';
$(td).html(detail_btn.replace('{{ DEFAULT_PK }}', rowData.id));
}},
{targets: 9, createdCell: function (td, cellData, rowData) {
var update_btn = '<a href="{% url "applications:database-update" pk=DEFAULT_PK %}" class="btn btn-xs btn-info">{% trans "Update" %}</a>'.replace("{{ DEFAULT_PK }}", cellData);
var del_btn = '<a class="btn btn-xs btn-danger m-l-xs btn-delete" data-rid="{{ DEFAULT_PK }}">{% trans "Delete" %}</a>'.replace('{{ DEFAULT_PK }}', cellData);
$(td).html(update_btn + del_btn)
}}
],
ajax_url: '{% url "api-applications:database-list" %}',
columns: [
{data: "id"},
{data: "name"},
{data: "login_mode"},
{data: "type"},
{data: "host"},
{data: "port"},
{data: "user"},
{data: "database"},
{data: "comment"},
{data: "id"}
],
op_html: $('#actions').html()
};
jumpserver.initServerSideDataTable(options);
}
$(document).ready(function(){
initTable();
})
.on('click', '.btn-delete', function () {
var $this = $(this);
var $data_table = $('#database_list_table').DataTable();
var name = $(this).closest("tr").find(":nth-child(2)").children('a').html();
var rid = $this.data('rid');
var the_url = '{% url "api-applications:database-detail" pk=DEFAULT_PK %}'.replace('{{ DEFAULT_PK }}', rid);
objectDelete($this, name, the_url);
setTimeout( function () {
$data_table.ajax.reload();
}, 3000);
});
</script>
{% endblock %}
......@@ -10,7 +10,15 @@ urlpatterns = [
path('remote-app/create/', views.RemoteAppCreateView.as_view(), name='remote-app-create'),
path('remote-app/<uuid:pk>/update/', views.RemoteAppUpdateView.as_view(), name='remote-app-update'),
path('remote-app/<uuid:pk>/', views.RemoteAppDetailView.as_view(), name='remote-app-detail'),
# User RemoteApp view
path('user-remote-app/', views.UserRemoteAppListView.as_view(), name='user-remote-app-list')
# User RemoteApp
path('user-remote-app/', views.UserRemoteAppListView.as_view(), name='user-remote-app-list'),
# Database
path('database/', views.DatabaseListView.as_view(), name='database-list'),
path('database/create/', views.DatabaseCreateView.as_view(), name='database-create'),
path('database/<uuid:pk>/update/', views.DatabaseUpdateView.as_view(), name='database-update'),
path('database/<uuid:pk>/', views.DatabaseDetailView.as_view(), name='database-detail'),
# User Database
path('user-database/', views.UserDatabaseListView.as_view(), name='user-database-list'),
]
from .remote_app import *
from .database import *
# coding: utf-8
#
from django.utils.translation import ugettext as _
from django.views.generic import TemplateView
from django.views.generic.edit import CreateView, UpdateView
from django.views.generic.detail import DetailView
from django.contrib.messages.views import SuccessMessageMixin
from django.urls import reverse_lazy
from common.permissions import PermissionsMixin, IsOrgAdmin, IsValidUser
from common.const import create_success_msg, update_success_msg
from .. import forms
from ..models import Database
__all__ = [
'DatabaseListView', 'DatabaseCreateView', 'DatabaseUpdateView',
'DatabaseDetailView', 'UserDatabaseListView'
]
class DatabaseListView(PermissionsMixin, TemplateView):
template_name = 'applications/database_list.html'
permission_classes = [IsOrgAdmin]
def get_context_data(self, **kwargs):
context = {
'app': _('Applications'),
'action': _('Database list'),
}
kwargs.update(context)
return super().get_context_data(**kwargs)
class DatabaseCreateView(PermissionsMixin, SuccessMessageMixin, CreateView):
template_name = 'applications/database_create_update.html'
permission_classes = [IsOrgAdmin]
model = Database
form_class = forms.DatabaseCreateUpdateForm
success_url = reverse_lazy('applications:database-list')
def get_context_data(self, **kwargs):
context = {
'app': _('Applications'),
'action': _('Create Database'),
'type': 'create',
}
kwargs.update(context)
return super().get_context_data(**kwargs)
def get_success_message(self, cleaned_data):
return create_success_msg % ({'name': cleaned_data['name']})
class DatabaseUpdateView(PermissionsMixin, SuccessMessageMixin, UpdateView):
template_name = 'applications/database_create_update.html'
permission_classes = [IsOrgAdmin]
model = Database
form_class = forms.DatabaseCreateUpdateForm
success_url = reverse_lazy('applications:database-list')
def get_context_data(self, **kwargs):
context = {
'app': _('Applications'),
'action': _('Update Database'),
'type': 'update',
}
kwargs.update(context)
return super().get_context_data(**kwargs)
def get_success_message(self, cleaned_data):
return update_success_msg % ({'name': cleaned_data['name']})
class DatabaseDetailView(PermissionsMixin, DetailView):
template_name = 'applications/database_detail.html'
permission_classes = [IsOrgAdmin]
model = Database
context_object_name = 'database'
def get_context_data(self, **kwargs):
context = {
'app': _('Applications'),
'action': _('Database detail'),
}
kwargs.update(context)
return super().get_context_data(**kwargs)
class UserDatabaseListView(PermissionsMixin, TemplateView):
template_name = 'applications/user_database_list.html'
permission_classes = [IsValidUser]
def get_context_data(self, **kwargs):
context = {
'action': _('My database'),
}
kwargs.update(context)
return super().get_context_data(**kwargs)
......@@ -34,6 +34,7 @@
</a>
<ul class="nav nav-second-level">
<li id="remote-app"><a href="{% url 'applications:remote-app-list' %}">{% trans 'RemoteApp' %}</a></li>
<li id="database"><a href="{% url 'applications:database-list' %}">{% trans 'Database' %}</a></li>
</ul>
</li>
{% endif %}
......
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 to comment