Skip to content

Commit

Permalink
Merge pull request YoLoveLife#107 from YoLoveLife/restful
Browse files Browse the repository at this point in the history
2.0 and docs
  • Loading branch information
YoLoveLife authored Jul 16, 2018
2 parents 7d220e6 + 2079423 commit 3df29d2
Show file tree
Hide file tree
Showing 44 changed files with 1,273 additions and 854 deletions.
34 changes: 17 additions & 17 deletions apps/authority/api/permission.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
# -*- coding:utf-8 -*-
from .. import models,serializers
from rest_framework import generics
from rest_framework.permissions import IsAuthenticated
from django.contrib.auth.models import Permission
from authority.permission import permission as PermissionPermission

__all__ = [
"PermissionListAPI"
]


class PermissionListAPI(generics.ListAPIView):
module = Permission
serializer_class = serializers.PermissionSerializer
queryset = Permission.objects.filter(codename__istartswith="yo_")
permission_classes = [PermissionPermission.PermissionListRequiredMixin,IsAuthenticated]
# -*- coding:utf-8 -*-
from .. import models,serializers
from rest_framework import generics
from rest_framework.permissions import IsAuthenticated
from django.contrib.auth.models import Permission
from authority.permission import permission as PermissionPermission

__all__ = [
"PermissionListAPI"
]


class PermissionListAPI(generics.ListAPIView):
module = Permission
serializer_class = serializers.PermissionSerializer
queryset = Permission.objects.filter(codename__istartswith="yo_")
permission_classes = [PermissionPermission.PermissionListRequiredMixin,IsAuthenticated]
273 changes: 137 additions & 136 deletions apps/authority/api/user.py
Original file line number Diff line number Diff line change
@@ -1,137 +1,138 @@
# -*- coding:utf-8 -*-
from .. import models,serializers,filter
from rest_framework import generics
from rest_framework.permissions import IsAuthenticated,AllowAny
from rest_framework.views import Response,status
from rest_framework.views import APIView
from rest_framework.pagination import PageNumberPagination
from rest_framework_jwt.views import ObtainJSONWebToken
from deveops.api import WebTokenAuthentication
from authority.permission import user as UserPermission
from deveops.utils import aes
from django.conf import settings
import pyotp
import os
from qrcode import QRCode,constants
import base64

__all__ = [
"UserLoginAPI", "UserInfoAPI", "UserListAPI",
"UserOpsListAPI", "UserUpdateAPI", "UserDeleteAPI",
"UserListByPageAPI", 'UserPagination', 'UserOpsListByPageAPI',
'UserQRCodeAPI', 'UserCreateAPI',
]


class UserLoginAPI(ObtainJSONWebToken):
def post(self, request, *args, **kwargs):
response = super(UserLoginAPI,self).post(request,*args,**kwargs)
return response


class UserInfoAPI(WebTokenAuthentication, APIView):
permission_classes = [IsAuthenticated]

def get(self, request, *args, **kwargs):
dist = {}
dist['username'] = request.user.username
dist['name'] = request.user.full_name
if request.user.is_superuser == True:
dist['isadmin'] = True
else:
dist['isadmin'] = 'None'

return Response(dist, status=status.HTTP_201_CREATED)


class UserPagination(PageNumberPagination):
page_size = 10


class UserListAPI(WebTokenAuthentication, generics.ListAPIView):
module = models.ExtendUser
serializer_class = serializers.UserSerializer
queryset = models.ExtendUser.objects.all()
permission_classes = [UserPermission.UserListRequiredMixin,IsAuthenticated]
filter_class = filter.UserFilter


class UserListByPageAPI(WebTokenAuthentication, generics.ListAPIView):
module = models.ExtendUser
serializer_class = serializers.UserSerializer
queryset = models.ExtendUser.objects.all()
permission_classes = [UserPermission.UserListRequiredMixin,IsAuthenticated]
pagination_class = UserPagination
filter_class = filter.UserFilter


class UserOpsListAPI(WebTokenAuthentication, generics.ListAPIView):
module = models.ExtendUser
serializer_class = serializers.UserSerializer
queryset = models.ExtendUser.objects.filter(groups__name__contains='运维')
permission_classes = [UserPermission.UserOpsListRequiredMixin, IsAuthenticated]


class UserOpsListByPageAPI(WebTokenAuthentication, generics.ListAPIView):
module = models.ExtendUser
serializer_class = serializers.UserSerializer
queryset = models.ExtendUser.objects.filter(groups__name__contains='运维')
permission_classes = [UserPermission.UserOpsListRequiredMixin, IsAuthenticated]
pagination_class = UserPagination


class UserCreateAPI(WebTokenAuthentication, generics.CreateAPIView):
module = models.ExtendUser
serializer_class = serializers.UserSerializer
queryset = models.ExtendUser.objects.all()
permission_classes = [UserPermission.UserCreateRequiredMixin, IsAuthenticated]


class UserUpdateAPI(WebTokenAuthentication,generics.UpdateAPIView):
module = models.ExtendUser
serializer_class = serializers.UserSerializer
queryset = models.ExtendUser.objects.all()
permission_classes = [UserPermission.UserUpdateRequiredMixin, IsAuthenticated]


class UserDeleteAPI(WebTokenAuthentication,generics.DestroyAPIView):
module = models.ExtendUser
serializer_class = serializers.UserSerializer
queryset = models.ExtendUser.objects.all()
permission_classes = [UserPermission.UserDeleteRequiredMixin,IsAuthenticated]


def get_qrcode(user):
file_name = str(aes.encrypt(user.qrcode),encoding='utf-8')
file = settings.QCODE_ROOT+'/'+file_name+'.png'
if not os.path.exists(file):
data = pyotp.totp.TOTP(user.qrcode).provisioning_uri('=v=', issuer_name="devEops")
qr = QRCode(
version=1,
error_correction=constants.ERROR_CORRECT_L,
box_size=6,
border=4,)
try:
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image()
img.save(file)
return '/media/qrcode/'+ file_name +'.png'
except Exception as e:
return '/media/qrcode/'+ file_name +'.png'
else:
return '/media/qrcode/' + file_name + '.png'


class UserQRCodeAPI(WebTokenAuthentication, APIView):
permission_classes = [IsAuthenticated,]

def get(self, request, *args, **kwargs):
user = request.user
if user.have_qrcode is False:
dist = {"url":get_qrcode(request.user)}
user.have_qrcode = True
user.save()
return Response(dist, status=status.HTTP_201_CREATED)
else:
# -*- coding:utf-8 -*-
from .. import models,serializers,filter
from rest_framework import generics
from rest_framework.permissions import IsAuthenticated,AllowAny
from rest_framework.views import Response,status
from rest_framework.views import APIView
from rest_framework.pagination import PageNumberPagination
from rest_framework_jwt.views import ObtainJSONWebToken
from deveops.api import WebTokenAuthentication
from authority.permission import user as UserPermission
from deveops.utils import aes
from django.conf import settings
import pyotp
import os
from qrcode import QRCode,constants
__all__ = [
"UserLoginAPI", "UserInfoAPI", "UserListAPI",
"UserOpsListAPI", "UserUpdateAPI", "UserDeleteAPI",
"UserListByPageAPI", 'UserPagination', 'UserOpsListByPageAPI',
'UserQRCodeAPI', 'UserCreateAPI',
]


class UserLoginAPI(ObtainJSONWebToken):
def __init__(self):
super(UserLoginAPI, self).__init__()

def post(self, request, *args, **kwargs):
response = super(UserLoginAPI,self).post(request,*args,**kwargs)
return response


class UserInfoAPI(WebTokenAuthentication, APIView):
permission_classes = [IsAuthenticated]

def get(self, request, *args, **kwargs):
dist = {}
dist['username'] = request.user.username
dist['name'] = request.user.full_name
if request.user.is_superuser == True:
dist['isadmin'] = True
else:
dist['isadmin'] = 'None'

return Response(dist, status=status.HTTP_201_CREATED)


class UserPagination(PageNumberPagination):
page_size = 10


class UserListAPI(WebTokenAuthentication, generics.ListAPIView):
module = models.ExtendUser
serializer_class = serializers.UserSerializer
queryset = models.ExtendUser.objects.all()
permission_classes = [UserPermission.UserListRequiredMixin,IsAuthenticated]
filter_class = filter.UserFilter


class UserListByPageAPI(WebTokenAuthentication, generics.ListAPIView):
module = models.ExtendUser
serializer_class = serializers.UserSerializer
queryset = models.ExtendUser.objects.all()
permission_classes = [UserPermission.UserListRequiredMixin,IsAuthenticated]
pagination_class = UserPagination
filter_class = filter.UserFilter


class UserOpsListAPI(WebTokenAuthentication, generics.ListAPIView):
module = models.ExtendUser
serializer_class = serializers.UserSerializer
queryset = models.ExtendUser.objects.filter(groups__name__contains='运维')
permission_classes = [UserPermission.UserOpsListRequiredMixin, IsAuthenticated]


class UserOpsListByPageAPI(WebTokenAuthentication, generics.ListAPIView):
module = models.ExtendUser
serializer_class = serializers.UserSerializer
queryset = models.ExtendUser.objects.filter(groups__name__contains='运维')
permission_classes = [UserPermission.UserOpsListRequiredMixin, IsAuthenticated]
pagination_class = UserPagination


class UserCreateAPI(WebTokenAuthentication, generics.CreateAPIView):
module = models.ExtendUser
serializer_class = serializers.UserSerializer
queryset = models.ExtendUser.objects.all()
permission_classes = [UserPermission.UserCreateRequiredMixin, IsAuthenticated]


class UserUpdateAPI(WebTokenAuthentication,generics.UpdateAPIView):
module = models.ExtendUser
serializer_class = serializers.UserSerializer
queryset = models.ExtendUser.objects.all()
permission_classes = [UserPermission.UserUpdateRequiredMixin, IsAuthenticated]


class UserDeleteAPI(WebTokenAuthentication,generics.DestroyAPIView):
module = models.ExtendUser
serializer_class = serializers.UserSerializer
queryset = models.ExtendUser.objects.all()
permission_classes = [UserPermission.UserDeleteRequiredMixin,IsAuthenticated]


def get_qrcode(user):
file_name = str(aes.encrypt(user.qrcode),encoding='utf-8')
file = settings.QCODE_ROOT+'/'+file_name+'.png'
if not os.path.exists(file):
data = pyotp.totp.TOTP(user.qrcode).provisioning_uri(user.username, issuer_name="devEops")
qr = QRCode(
version=1,
error_correction=constants.ERROR_CORRECT_L,
box_size=6,
border=4,)
try:
qr.add_data(data)
qr.make(fit=True)
img = qr.make_image()
img.save(file)
return '/media/qrcode/'+ file_name +'.png'
except Exception as e:
return '/media/qrcode/'+ file_name +'.png'
else:
return '/media/qrcode/' + file_name + '.png'


class UserQRCodeAPI(WebTokenAuthentication, APIView):
permission_classes = [IsAuthenticated,]

def get(self, request, *args, **kwargs):
user = request.user
if user.have_qrcode is False:
dist = {"url":get_qrcode(request.user)}
user.have_qrcode = True
user.save()
return Response(dist, status=status.HTTP_201_CREATED)
else:
return Response({'detail': '当前用户已经扫描过QRCode 如有需要请联系管理员'}, status=status.HTTP_406_NOT_ACCEPTABLE)
11 changes: 6 additions & 5 deletions apps/dashboard/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# -*- coding:utf-8 -*-
# !/usr/bin/env python
# Time 18-5-22
# Author Yo
# Email [email protected]
# -*- coding:utf-8 -*-
# !/usr/bin/env python
# Time 18-5-22
# Author Yo
# Email [email protected]
from __future__ import absolute_import
11 changes: 7 additions & 4 deletions apps/dashboard/api/expired.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
"ExpiredPagination",
"DashboardExpiredECSAPI",
"DashboardExpiredRDSAPI",
"DashboardExpiredKVStoreAPI",
"DashboardExpiredMongoDBAPI",
]


Expand All @@ -27,29 +29,30 @@ class ExpiredPagination(PageNumberPagination):
class DashboardExpiredECSAPI(WebTokenAuthentication,generics.ListAPIView):
module = models.ExpiredAliyunECS
permission_classes = [ExpirePermission.ExpiredListRequiredMixin, IsAuthenticated]
queryset = models.ExpiredAliyunECS.objects.all()
queryset = models.ExpiredAliyunECS.objects.all().order_by('expired')
serializer_class = serializers.DashboardExpiredAliyunECSSerializer
pagination_class = ExpiredPagination


class DashboardExpiredRDSAPI(WebTokenAuthentication,generics.ListAPIView):
module = models.ExpiredAliyunRDS
permission_classes = [ExpirePermission.ExpiredListRequiredMixin, IsAuthenticated]
queryset = models.ExpiredAliyunRDS.objects.all()
queryset = models.ExpiredAliyunRDS.objects.all().order_by('expired')
serializer_class = serializers.DashboardExpiredAliyunRDSSerializer
pagination_class = ExpiredPagination


class DashboardExpiredKVStoreAPI(WebTokenAuthentication, generics.ListAPIView):
module = models.ExpiredAliyunKVStore
permission_classes = [ExpirePermission.ExpiredListRequiredMixin, IsAuthenticated]
queryset = models.ExpiredAliyunKVStore.objects.all()
queryset = models.ExpiredAliyunKVStore.objects.all().order_by('expired')
serializer_class = serializers.DashboardExpiredAliyunKVStoreSerializer
pagination_class = ExpiredPagination


class DashboardExpiredMongoDBAPI(WebTokenAuthentication, generics.ListAPIView):
module = models.ExpiredAliyunMongoDB
permission_classes = [ExpirePermission.ExpiredListRequiredMixin, IsAuthenticated]
queryset = models.ExpiredAliyunMongoDB.objects.all()
queryset = models.ExpiredAliyunMongoDB.objects.all().order_by('expired')
serializer_class = serializers.DashboardExpiredAliyunMongoDBSerializer
pagination_class = ExpiredPagination
2 changes: 1 addition & 1 deletion apps/dashboard/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Meta:

class DashboardExpiredAliyunKVStoreSerializer(serializers.ModelSerializer):
class Meta:
model = models.ExpiredAliyunMongoDB
model = models.ExpiredAliyunKVStore
fields = (
'id', 'status', 'recognition_id', 'expired', 'instancename', 'version', 'connect_domain'
)
Expand Down
Loading

0 comments on commit 3df29d2

Please sign in to comment.