def post(self, request, *args, **kwargs): # username可能携带的不止是用户名,可能还是用户的其它唯一标识 手机号 邮箱 print(request.GET) username = request.data.get('username', None) password = request.data.get('password', None) if username is None or password is None: return APIResponseResult.APIResponse(-1, '用户名或密码不能为空!') user = User.objects.filter(username=username).first() if user is None: return APIResponseResult.APIResponse(-2, '用户名或密码输入有误') # 获得用户后,校验密码并签发token if not user.check_password(password): return APIResponseResult.APIResponse(-3, '密码错误') # 更新最后一次登录时间 user.last_login = datetime.now() user.save() jwt_payload_handler = api_settings.JWT_PAYLOAD_HANDLER jwt_encode_handler = api_settings.JWT_ENCODE_HANDLER payload = jwt_payload_handler(user) token = jwt_encode_handler(payload) return APIResponseResult.APIResponse(0, 'ok', results={ 'username': user.username, 'access_token': token })
def resetEnabled(self, request, *args, **kwargs): nid = request.data.get('nid', None) if nid is None: return APIResponseResult.APIResponse(-1, '请求发生错误,请稍后再试!') periodictask = PeriodicTask.objects.filter(id=nid).first() if periodictask is None: return APIResponseResult.APIResponse(-2, '请求数据不存在,请稍后再试!') periodictask.enabled = False if periodictask.enabled else True periodictask.save() return APIResponseResult.APIResponse( 0, "已启用" if periodictask.enabled else "已禁用")
def resetPassWord(self, request, *args, **kwargs): oldPassword = request.data.get("oldPassword", None) user_id = request.user["user_id"] currentUser = User.objects.filter(id=user_id).first() if not currentUser.check_password(oldPassword): return APIResponseResult.APIResponse(-1, '当前密码输入错误') password = request.data.get("password", None) repassword = request.data.get("repassword", None) if password != repassword: return APIResponseResult.APIResponse(-2, '新密码和确认新密码输入不一致') currentUser.password = make_password(password) currentUser.save() return APIResponseResult.APIResponse(0, '修改成功')
def list(self, request, *args, **kwargs): if type(request.user) == dict: username = request.user["username"] else: username = request.user.username user_id = request.user["user_id"] currentUser = User.objects.filter(username=username).first() obj, created = models.userInfo.objects.update_or_create( defaults={ "user": currentUser, 'creator': currentUser, 'editor': currentUser }, user=currentUser) results = {} results.update({"id": obj.id}) results.update({"nickName": obj.nickName}) results.update({"sex": obj.sex}) results.update({"avatar": obj.avatar.name}) # 需要构建文件服务器 results.update({"phone": obj.phone}) results.update({"email": obj.email}) results.update({"desc": obj.desc}) results.update({"roles": [{}]}) # 拥有的权限 results.update({"username": username}) return APIResponseResult.APIResponse( 0, 'success', results=results, http_status=status.HTTP_200_OK, )
def destroy(self, request, *args, **kwargs): instance = self.get_object() self.perform_destroy(instance) return APIResponseResult.APIResponse( 0, 'success', http_status=status.HTTP_200_OK, )
def retrieve(self, request, *args, **kwargs): instance = self.get_object() serializer = self.get_serializer(instance) return APIResponseResult.APIResponse( 0, 'success', results=serializer.data, http_status=status.HTTP_200_OK, )
def list(self, request, *args, **kwargs): websiteinfo = models.webSiteSet.objects.all().values().order_by( '-id')[0] return APIResponseResult.APIResponse( 0, 'success', results=websiteinfo, http_status=status.HTTP_200_OK, )
def uploadAvatar(self, request, *args, **kwargs): files = request.FILES.getlist('images', []) if len(files): user_id = request.user["user_id"] uinfo, ctime = models.userInfo.objects.update_or_create( defaults={"avatar": files[0]}, user_id=user_id) return APIResponseResult.APIResponse( 0, 'success', results={"avatar": uinfo.avatar.name}, http_status=status.HTTP_200_OK, ) return APIResponseResult.APIResponse( -1, '上传失败', http_status=status.HTTP_500_INTERNAL_SERVER_ERROR, )
def create(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) self.perform_create(serializer) headers = self.get_success_headers(serializer.data) return APIResponseResult.APIResponse(0, 'success', results=serializer.data, http_status=status.HTTP_200_OK, headers=headers)
def get(self, request, *args, **kwargs): return APIResponseResult.APIResponse(0, 'ok', results={ "username": request.user["username"], "sex": "男", "role": 1, })
def list(self, request, *args, **kwargs): queryset = self.filter_queryset(self.get_queryset()) page = self.paginate_queryset(queryset) if page is not None: serializer = self.get_serializer(page, many=True) return APIResponseResult.APIResponse( 0, 'success', results=serializer.data, http_status=status.HTTP_200_OK, **{"count": len(queryset)}) # return self.get_paginated_response(serializer.data) serializer = self.get_serializer(queryset, many=True) return APIResponseResult.APIResponse( 0, 'success', results=serializer.data, http_status=status.HTTP_200_OK, )
def parent_menu(self, request, *args, **kwargs): firstmenus = models.Menu.objects.order_by('parent', 'sort') menus = [] for item in firstmenus: title_list = [item.title] p = item.parent while p: title_list.insert(0, p.title) p = p.parent menus.append({"value": item.id, "title": '/'.join(title_list)}) return APIResponseResult.APIResponse(0, 'ok', results=menus)
def left_menu(self, request, *args, **kwargs): # 获得用户权限 user_id = request.user["user_id"] tree = [] currentUser = User.objects.filter(id=user_id).first() if currentUser.is_superuser: firstmenus = models.Menu.objects.filter( parent=None).order_by('sort') else: user_permission_id = [] group_permission_id = [] current_user_group = Group.objects.filter( user__username=currentUser) print("current_group_set", current_user_group) current_user_permissions = Permission.objects.filter( user__username=currentUser) print("current_user_permissions", current_user_permissions) print("get_user_permissions>", currentUser.get_user_permissions()) print("get_group_permissions>", currentUser.get_group_permissions()) for up in current_user_group: group_permission_id.append(up.id) for gp in current_user_permissions: user_permission_id.append(gp.id) print("user_permission_id", user_permission_id) print("group_permission_id", group_permission_id) # 查询可以操作的菜单 firstmenus = models.Menu.objects.filter( Q(group__id__in=group_permission_id) | Q(permission__id__in=user_permission_id), parent=None).distinct().order_by('sort') # print(menus.query) for menu in firstmenus: menu_data = { "name": menu.name, "title": menu.title, "icon": menu.icon, "jump": menu.url } childs = models.Menu.objects.filter(parent=menu).order_by('sort') if childs: menu_data["list"] = get_child_menu(childs) tree.append(menu_data) # tree = [x for x in tree if x["list"] != []] return APIResponseResult.APIResponse(0, 'success', results=tree)
def update(self, request, *args, **kwargs): partial = kwargs.pop('partial', False) instance = self.get_object() serializer = self.get_serializer(instance, data=request.data, partial=partial) serializer.is_valid(raise_exception=True) self.perform_update(serializer) if getattr(instance, '_prefetched_objects_cache', None): # If 'prefetch_related' has been applied to a queryset, we need to # forcibly invalidate the prefetch cache on the instance. instance._prefetched_objects_cache = {} return APIResponseResult.APIResponse( 0, 'success', results=serializer.data, http_status=status.HTTP_200_OK, )
def multiple_delete(self, request, *args, **kwargs): delete_id = request.data.get("deleteid", "") list_ids = list(filter(None, delete_id.split(','))) list_ids = [int(x) for x in list_ids if x.split()] self.queryset.model.objects.filter(id__in=list_ids).delete() return APIResponseResult.APIResponse(0, "删除成功", results=list_ids)