Commit ff30435e authored by ibuler's avatar ibuler

Finish adin user add

parent 5a0b1194
# coding:utf-8
from django import forms
from .models import IDC, Asset, AssetGroup
from .models import IDC, Asset, AssetGroup, AdminUser, SystemUser
from django.utils.translation import gettext_lazy as _
......@@ -11,7 +11,7 @@ class AssetForm(forms.ModelForm):
fields = [
"ip", "other_ip", "remote_card_ip", "hostname", "port", "groups", "username", "password",
"idc", "mac_addr", "brand", "cpu", "memory", "disk", "os", "cabinet_no", "cabinet_pos",
"idc", "mac_address", "brand", "cpu", "memory", "disk", "os", "cabinet_no", "cabinet_pos",
"number", "status", "type", "env", "sn", "is_active", "comment"
]
......@@ -80,32 +80,52 @@ class IDCForm(forms.ModelForm):
}
class AdminUser(forms.ModelForm):
class AdminUserForm(forms.ModelForm):
assets = forms.ModelMultipleChoiceField(queryset=Asset.objects.all(),
label=_('Asset'),
required=False,
widget=forms.SelectMultiple(
attrs={'class': 'select2', 'data-placeholder': _('Select assets')})
)
password = forms.CharField(widget=forms.PasswordInput, max_length=100, strip=True)
password = forms.CharField(widget=forms.PasswordInput, max_length=100, min_length=8, strip=True,
help_text=_('If also set private key, use that first'), required=False)
private_key_file = forms.FileField(required=False)
def __init__(self, *args, **kwargs):
if kwargs.get('instance'):
initial = kwargs.get('initial', {})
initial['assets'] = kwargs['instance'].assets.all()
super(AdminUser, self).__init__(*args, **kwargs)
super(AdminUserForm, self).__init__(*args, **kwargs)
def _save_m2m(self):
super(AdminUser, self)._save_m2m()
super(AdminUserForm, self)._save_m2m()
assets = self.cleaned_data['assets']
self.instance.assets.clear()
self.instance.assets.add(*tuple(assets))
def save(self, commit=True):
admin_user = super(AdminUserForm, self).save(commit=commit)
password = self.cleaned_data['password']
private_key_file = self.cleaned_data['private_key_file']
if password:
admin_user.password = password
print(password)
if private_key_file:
print(private_key_file)
admin_user.private_key = private_key_file.read()
admin_user.save()
return self.instance
class Meta:
model = IDC
fields = ['name', "username", 'as_default', 'comment']
model = AdminUser
fields = ['name', 'username', 'password', 'private_key_file', 'as_default', 'comment']
widgets = {
'name': forms.TextInput(attrs={'placeholder': _('Name')}),
'network': forms.Textarea(
attrs={'placeholder': '192.168.1.0/24\n192.168.2.0/24'})
'username': forms.TextInput(attrs={'placeholder': _('Username')}),
}
help_texts = {
'name': '* required',
'username': '* required',
}
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2016-09-07 15:11
# Generated by Django 1.10 on 2016-09-08 03:02
from __future__ import unicode_literals
from django.db import migrations, models
......@@ -18,15 +18,15 @@ class Migration(migrations.Migration):
name='AdminUser',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(blank=True, max_length=128, null=True, unique=True, verbose_name='Name')),
('username', models.CharField(blank=True, max_length=16, null=True, verbose_name='Username')),
('password', models.CharField(blank=True, max_length=256, null=True, verbose_name='Password')),
('private_key', models.CharField(blank=True, max_length=4096, null=True, verbose_name='SSH private key')),
('is_default', models.BooleanField(default=True, verbose_name='As default')),
('auto_update', models.BooleanField(default=True, verbose_name='Auto update pass/key')),
('date_created', models.DateTimeField(auto_now=True, null=True)),
('create_by', models.CharField(blank=True, max_length=32, null=True, verbose_name='Created by')),
('name', models.CharField(max_length=128, unique=True, verbose_name='Name')),
('username', models.CharField(max_length=16, verbose_name='Username')),
('_password', models.CharField(blank=True, max_length=256, verbose_name='Password')),
('_private_key', models.CharField(blank=True, max_length=4096, verbose_name='SSH private key')),
('_public_key', models.CharField(blank=True, max_length=4096, verbose_name='SSH public key')),
('as_default', models.BooleanField(default=False, verbose_name='As default')),
('comment', models.TextField(blank=True, verbose_name='Comment')),
('date_created', models.DateTimeField(auto_now=True, null=True)),
('created_by', models.CharField(max_length=32, null=True, verbose_name='Created by')),
],
options={
'db_table': 'admin_user',
......@@ -142,7 +142,7 @@ class Migration(migrations.Migration):
('home', models.CharField(blank=True, max_length=64, verbose_name='Home')),
('uid', models.IntegerField(blank=True, verbose_name='Uid')),
('date_created', models.DateTimeField(auto_now=True, null=True)),
('create_by', models.CharField(blank=True, max_length=32, verbose_name='Created by')),
('created_by', models.CharField(blank=True, max_length=32, verbose_name='Created by')),
('comment', models.CharField(blank=True, max_length=128, verbose_name='Comment')),
],
options={
......
......@@ -68,15 +68,15 @@ class AssetExtend(models.Model):
class AdminUser(models.Model):
name = models.CharField(max_length=128, unique=True, null=True, blank=True, verbose_name=_('Name'))
username = models.CharField(max_length=16, null=True, blank=True, verbose_name=_('Username'))
_password = models.CharField(max_length=256, null=True, blank=True, verbose_name=_('Password'))
_private_key = models.CharField(max_length=4096, null=True, blank=True, verbose_name=_('SSH private key'))
_public_key = models.CharField(max_length=4096, null=True, blank=True, verbose_name=_('SSH public key'))
as_default = models.BooleanField(default=True, verbose_name=_('As default'))
name = models.CharField(max_length=128, unique=True, verbose_name=_('Name'))
username = models.CharField(max_length=16, verbose_name=_('Username'))
_password = models.CharField(max_length=256, blank=True, verbose_name=_('Password'))
_private_key = models.CharField(max_length=4096, blank=True, verbose_name=_('SSH private key'))
_public_key = models.CharField(max_length=4096, blank=True, verbose_name=_('SSH public key'))
as_default = models.BooleanField(default=False, verbose_name=_('As default'))
comment = models.TextField(blank=True, verbose_name=_('Comment'))
date_created = models.DateTimeField(auto_now=True, null=True, blank=True)
created_by = models.CharField(max_length=32, null=True, blank=True, verbose_name=_('Created by'))
date_created = models.DateTimeField(auto_now=True, null=True)
created_by = models.CharField(max_length=32, null=True, verbose_name=_('Created by'))
def __unicode__(self):
return self.name
......@@ -110,7 +110,7 @@ class AdminUser(models.Model):
@classmethod
def generate_fake(cls, count=100):
from random import seed, choice
from random import seed
import forgery_py
from django.db import IntegrityError
......@@ -204,10 +204,11 @@ class Asset(models.Model):
groups = models.ManyToManyField(AssetGroup, related_name='assets', verbose_name=_('Asset groups'))
username = models.CharField(max_length=16, null=True, blank=True, verbose_name=_('Admin user'))
password = models.CharField(max_length=256, null=True, blank=True, verbose_name=_("Admin password"))
admin_user = models.ForeignKey(AdminUser, null=True, on_delete=models.SET_NULL, verbose_name=_("Admin user"))
admin_user = models.ForeignKey(AdminUser, null=True, related_name='assets',
on_delete=models.SET_NULL, verbose_name=_("Admin user"))
system_user = models.ManyToManyField(SystemUser, blank=True, verbose_name=_("System User"))
idc = models.ForeignKey(IDC, null=True, related_name='assets', on_delete=models.SET_NULL, verbose_name=_('IDC'))
mac_addr = models.CharField(max_length=20, null=True, blank=True, verbose_name=_("Mac address"))
mac_address = models.CharField(max_length=20, null=True, blank=True, verbose_name=_("Mac address"))
brand = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('Brand'))
cpu = models.CharField(max_length=64, null=True, blank=True, verbose_name=_('CPU'))
memory = models.CharField(max_length=128, null=True, blank=True, verbose_name=_('Memory'))
......
{% extends 'base.html' %}
{% load i18n %}
{% load static %}
{% load bootstrap %}
{% 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>{% trans 'Create asset group' %}</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">
<form enctype="multipart/form-data" method="post" class="form-horizontal" action="" >
{% csrf_token %}
{{ form.name|bootstrap_horizontal }}
{{ form.username|bootstrap_horizontal }}
{{ form.password|bootstrap_horizontal }}
{{ form.private_key_file|bootstrap_horizontal }}
<div class="form-group">
<label for="{{ form.as_default.id_for_label }}" class="col-sm-2 control-label">{% trans 'As default' %}</label>
<div class="col-sm-8">
{{ form.as_default}}
</div>
</div>
{{ form.assets|bootstrap_horizontal }}
{{ form.comment|bootstrap_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>
$(document).ready(function () {
$('.select2').select2();
})
</script>
{% endblock %}
\ No newline at end of file
......@@ -2,19 +2,16 @@
from __future__ import absolute_import, unicode_literals
from django.utils.translation import ugettext as _
from django.shortcuts import get_object_or_404
from django.views.generic import TemplateView, ListView
from django.urls import reverse_lazy
from django.views.generic.edit import CreateView, DeleteView, FormView, UpdateView
from django.conf import settings
from django.db.models import Q
from django.views.generic import TemplateView, ListView
from django.views.generic.edit import CreateView, DeleteView, FormView, UpdateView
from django.urls import reverse_lazy
from django.contrib.messages.views import SuccessMessageMixin
from django.views.generic.detail import DetailView, SingleObjectMixin
from .models import Asset, AssetGroup, IDC, AssetExtend, AdminUser, SystemUser
from .forms import AssetForm, AssetGroupForm, IDCForm
from .forms import AssetForm, AssetGroupForm, IDCForm, AdminUserForm
from .hands import AdminUserRequiredMixin
......@@ -230,8 +227,26 @@ class AdminUserListView(AdminUserRequiredMixin, ListView):
return self.queryset
class AdminUserCreateView(AdminUserRequiredMixin, CreateView):
pass
class AdminUserCreateView(AdminUserRequiredMixin, SuccessMessageMixin, CreateView):
model = AdminUser
form_class = AdminUserForm
template_name = 'assets/admin_user_create.html'
success_url = reverse_lazy('assets:admin-user-list')
success_message = _('Create admin user <a href="%s">%s</a> successfully.')
def get_context_data(self, **kwargs):
context = {
'app': 'assets',
'action': 'Create admin user'
}
kwargs.update(context)
return super(AdminUserCreateView, self).get_context_data(**kwargs)
def get_success_message(self, cleaned_data):
return self.success_message % (
reverse_lazy('assets:admin-user-detail', kwargs={'pk': self.object.pk}),
self.object.name,
)
class AdminUserUpdateView(AdminUserRequiredMixin, UpdateView):
......
This source diff could not be displayed because it is too large. You can view the blob instead.
......@@ -98,7 +98,7 @@ class UserCreateView(AdminUserRequiredMixin, SuccessMessageMixin, CreateView):
form_class = UserCreateForm
template_name = 'users/user_create.html'
success_url = reverse_lazy('users:user-list')
success_message = _('Create user <a href="%s">%s</a> success.')
success_message = _('Create user <a href="%s">%s</a> successfully.')
def get_context_data(self, **kwargs):
context = super(UserCreateView, self).get_context_data(**kwargs)
......
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