Plan create a new app: terminal

pull/530/head
ibuler 8 years ago
parent 0446f449e9
commit 9960a6cd21

@ -54,7 +54,7 @@ INSTALLED_APPS = [
'users.apps.UsersConfig', 'users.apps.UsersConfig',
'assets.apps.AssetsConfig', 'assets.apps.AssetsConfig',
'perms.apps.PermsConfig', 'perms.apps.PermsConfig',
# 'terminal.apps.TerminalConfig', 'terminal.apps.TerminalConfig',
'ops.apps.OpsConfig', 'ops.apps.OpsConfig',
'audits.apps.AuditsConfig', 'audits.apps.AuditsConfig',
'common.apps.CommonConfig', 'common.apps.CommonConfig',

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

@ -0,0 +1,7 @@
from __future__ import unicode_literals
from django.apps import AppConfig
class TerminalConfig(AppConfig):
name = 'terminal'

@ -0,0 +1,5 @@
from __future__ import unicode_literals
from django.db import models
# Create your models here.

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

@ -0,0 +1,3 @@
from django.shortcuts import render
# Create your views here.

@ -21,11 +21,13 @@ logger = get_logger(__name__)
class UserDetailApi(generics.RetrieveUpdateDestroyAPIView): class UserDetailApi(generics.RetrieveUpdateDestroyAPIView):
queryset = User.objects.all() queryset = User.objects.all()
serializer_class = UserDetailSerializer serializer_class = UserDetailSerializer
permission_classes = (IsSuperUser,)
class UserAndGroupEditApi(generics.RetrieveUpdateAPIView): class UserAndGroupEditApi(generics.RetrieveUpdateAPIView):
queryset = User.objects.all() queryset = User.objects.all()
serializer_class = UserAndGroupSerializer serializer_class = UserAndGroupSerializer
permission_classes = (IsSuperUser,)
class UserResetPasswordApi(generics.UpdateAPIView): class UserResetPasswordApi(generics.UpdateAPIView):
@ -109,3 +111,23 @@ class DeleteUserFromGroupApi(generics.DestroyAPIView):
user_id = kwargs.get('uid') user_id = kwargs.get('uid')
user = get_object_or_404(User, id=user_id) user = get_object_or_404(User, id=user_id)
instance.users.remove(user) instance.users.remove(user)
class AppUserRegisterApi(generics.CreateAPIView):
"""App send a post request to register a app user
request params contains `username_signed`, You can unsign it,
username = unsign(username_signed), if you get the username,
It's present it's a valid request, or return (401, Invalid request),
then your should check if the user exist or not. If exist,
return (200, register success), If not, you should be save it, and
notice admin user, The user default is not active before admin user
unblock it.
Save fields:
username:
name: name + request.ip
email: username + '@app.org'
role: App
"""
pass

@ -69,7 +69,6 @@ class User(AbstractUser):
ROLE_CHOICES = ( ROLE_CHOICES = (
('Admin', _('Administrator')), ('Admin', _('Administrator')),
('User', _('User')), ('User', _('User')),
('App', _('Application')),
) )
username = models.CharField(max_length=20, unique=True, verbose_name=_('Username')) username = models.CharField(max_length=20, unique=True, verbose_name=_('Username'))
@ -149,15 +148,6 @@ class User(AbstractUser):
else: else:
self.role = 'User' self.role = 'User'
is_admin = is_superuser
@property
def is_app_user(self):
if self.role == 'App':
return True
else:
return False
@property @property
def is_staff(self): def is_staff(self):
if self.is_authenticated and self.is_valid: if self.is_authenticated and self.is_valid:
@ -188,7 +178,6 @@ class User(AbstractUser):
token = Token.objects.get(user=self) token = Token.objects.get(user=self)
except Token.DoesNotExist: except Token.DoesNotExist:
token = Token.objects.create(user=self) token = Token.objects.create(user=self)
return token.key return token.key
def refresh_private_token(self): def refresh_private_token(self):

@ -5,23 +5,23 @@ from django.utils.translation import ugettext_lazy as _
from rest_framework import serializers from rest_framework import serializers
from rest_framework_bulk import BulkListSerializer, BulkSerializerMixin from rest_framework_bulk import BulkListSerializer, BulkSerializerMixin
from common.utils import unsign
from .models import User, UserGroup from .models import User, UserGroup
class UserDetailSerializer(serializers.ModelSerializer): class UserDetailSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = User model = User
fields = ['avatar', 'wechat', 'phone', 'enable_otp', 'comment', 'is_active', 'name'] fields = ['avatar', 'wechat', 'phone', 'enable_otp', 'comment', 'is_active', 'name']
class UserPKUpdateSerializer(serializers.ModelSerializer): class UserPKUpdateSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = User model = User
fields = ['id', '_public_key'] fields = ['id', '_public_key']
def validate__public_key(self, value): @staticmethod
def validate__public_key(value):
from sshpubkeys import SSHKey from sshpubkeys import SSHKey
from sshpubkeys.exceptions import InvalidKeyException from sshpubkeys.exceptions import InvalidKeyException
ssh = SSHKey(value) ssh = SSHKey(value)
@ -45,7 +45,6 @@ class UserAndGroupSerializer(serializers.ModelSerializer):
class GroupDetailSerializer(serializers.ModelSerializer): class GroupDetailSerializer(serializers.ModelSerializer):
class Meta: class Meta:
model = UserGroup model = UserGroup
fields = ['id', 'name', 'comment', 'date_created', 'created_by', 'users'] fields = ['id', 'name', 'comment', 'date_created', 'created_by', 'users']
@ -63,16 +62,17 @@ class UserBulkUpdateSerializer(BulkSerializerMixin, serializers.ModelSerializer)
'enable_otp', 'comment', 'groups', 'get_role_display', 'enable_otp', 'comment', 'groups', 'get_role_display',
'group_display', 'active_display'] 'group_display', 'active_display']
def get_group_display(self, obj): @staticmethod
def get_group_display(obj):
return " ".join([group.name for group in obj.groups.all()]) return " ".join([group.name for group in obj.groups.all()])
def get_active_display(self, obj): @staticmethod
# TODO: user ative state def get_active_display(obj):
# TODO: user active state
return not (obj.is_expired and obj.is_active) return not (obj.is_expired and obj.is_active)
class GroupBulkUpdateSerializer(BulkSerializerMixin, serializers.ModelSerializer): class GroupBulkUpdateSerializer(BulkSerializerMixin, serializers.ModelSerializer):
user_amount = serializers.SerializerMethodField() user_amount = serializers.SerializerMethodField()
class Meta: class Meta:
@ -80,5 +80,18 @@ class GroupBulkUpdateSerializer(BulkSerializerMixin, serializers.ModelSerializer
list_serializer_class = BulkListSerializer list_serializer_class = BulkListSerializer
fields = ['id', 'name', 'comment', 'user_amount'] fields = ['id', 'name', 'comment', 'user_amount']
def get_user_amount(self, obj): @staticmethod
def get_user_amount(obj):
return obj.users.count() return obj.users.count()
class AppUserRegisterSerializer(serializers.Serializer):
username = serializers.CharField(max_length=20)
def create(self, validated_data):
sign = validated_data('username', '')
username = unsign(sign)
pass
def update(self, instance, validated_data):
pass

Loading…
Cancel
Save