def set_password(self, request, pk=None): perms = UserInfoView.get_permission_from_role(request) user = UserProfile.objects.get(id=pk) if 'admin' in perms or 'user_all' in perms or request.user.is_superuser: new_password1 = request.data['new_password1'] new_password2 = request.data['new_password2'] if new_password1 == new_password2: user.set_password(new_password2) user.save() return MisResponse('密码修改成功!') else: return MisResponse('新密码两次输入不一致!', status=status.HTTP_400_BAD_REQUEST) else: old_password = request.data['old_password'] if check_password(old_password, user.password): new_password1 = request.data['new_password1'] new_password2 = request.data['new_password2'] if new_password1 == new_password2: user.set_password(new_password2) user.save() return MisResponse('密码修改成功!') else: return MisResponse('新密码两次输入不一致!', status=status.HTTP_400_BAD_REQUEST) else: return MisResponse('旧密码错误!', status=status.HTTP_400_BAD_REQUEST)
def post(self, request, *args, **kwargs): username = request.data.get('username') password = request.data.get('password') user = authenticate(username=username, password=password) if user: payload = jwt_payload_handler(user) return MisResponse({'token': jwt.encode(payload, SECRET_KEY)}, status=OK) else: return MisResponse('用户名或密码错误!', status=BAD)
def get(self, request): if request.user.id is not None: perms = self.get_permission_from_role(request) data = { 'id': request.user.id, 'label': request.user.label, 'email': request.user.email, 'is_active': request.user.is_active, 'roles': perms } return MisResponse(data, status=OK) else: return MisResponse('请登录后访问!', status=FORBIDDEN)
def create(self, request, *args, **kwargs): request.data['num'] = build_order_num("A") request.data['recorder'] = request.user.id 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 MisResponse(serializer.data, status=CREATED, headers=headers)
def create(self, request, *args, **kwargs): # 创建用户默认添加密码 self.generate_name(request) request.data['password'] = make_password('123456') 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 MisResponse(serializer.data, status=CREATED, headers=headers)
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 MisResponse(serializer.data)
def get(self, request, format=None): locations = Location.objects.all() serializer = LocationSerializer(locations, many=True) tree_dict = {} tree_data = [] p_list = [] for item in serializer.data: new_item = { 'id': item['id'], 'name': item['name'], 'pid': item['pid'] } tree_dict[item['id']] = new_item for i in tree_dict: if tree_dict[i]['pid'] is not None and tree_dict[ tree_dict[i]['pid']]['pid'] is None: pid = tree_dict[i]['pid'] tree_data.append({ 'id': tree_dict[i]['id'], 'label': "{}{}".format(tree_dict[pid]['name'], tree_dict[i]['name']) }) p_list.append(tree_dict[i]['pid']) for i in tree_dict: if tree_dict[i]['pid'] is None and tree_dict[i]['id'] not in p_list: tree_data.append({ 'id': tree_dict[i]['id'], 'label': "{}".format(tree_dict[i]['name']) }) # tree_data 的长度 n = len(tree_data) # 外层循环确定比较的轮数,x 是下标,tree_data[x] 在外层循环中代表 tree_data 中所有元素 for x in range(n - 1): # 内层循环开始比较 for y in range(x + 1, n): # tree_data[x] 在 for y 循环中是代表特定的元素,tree_data[y] 代表任意一个 tree_data 一个元素 if tree_data[x]['id'] > tree_data[y]['id']: # 让 tree_data[x] 和 tree_data 列表中每一个元素比较,找出小的 tree_data[x], tree_data[y] = tree_data[y], tree_data[x] return MisResponse(tree_data)
def get(self, request, format=None): locations = Location.objects.all() serializer = LocationUserTreeSerializer(locations, many=True) tree_dict = {} tree_data = [] for item in serializer.data: new_item = { 'id': 'o' + str(item['id']), 'label': item['label'], 'pid': item['pid'], 'children': item['children'] } tree_dict[item['id']] = new_item for i in tree_dict: if tree_dict[i]['pid']: pid = tree_dict[i]['pid'] parent = tree_dict[pid] parent['children'].append(tree_dict[i]) else: tree_data.append(tree_dict[i]) return MisResponse(tree_data)
def list(self, request, *args, **kwargs): queryset = self.filter_queryset(self.get_queryset()) page = self.paginate_queryset(queryset) serializer = self.get_serializer(queryset, many=True) tree_dict = {} tree_data = [] try: for item in serializer.data: tree_dict[item['id']] = item for i in tree_dict: if tree_dict[i]['pid']: pid = tree_dict[i]['pid'] parent = tree_dict[pid] parent.setdefault('children', []).append(tree_dict[i]) else: tree_data.append(tree_dict[i]) results = tree_data except KeyError: results = serializer.data if page is not None: return self.get_paginated_response(results) return MisResponse(results)
def get(self, request, format=None): locations = Location.objects.all() serializer = LocationSerializer(locations, many=True) tree_dict = {} tree_data = [] for item in serializer.data: new_item = { 'id': item['id'], 'name': item['name'], 'pid': item['pid'] } tree_dict[item['id']] = new_item for i in tree_dict: if tree_dict[i]['pid'] and tree_dict[tree_dict[i]['pid']]['pid']: pid = tree_dict[i]['pid'] p_pid = tree_dict[pid]['pid'] tree_data.append({ 'id': tree_dict[i]['id'], 'label': "{}{} {}".format(tree_dict[p_pid]['name'], tree_dict[pid]['name'], tree_dict[i]['name']) }) elif tree_dict[i]['pid']: pid = tree_dict[i]['pid'] tree_data.append({ 'id': tree_dict[i]['id'], 'label': "{}{}".format(tree_dict[pid]['name'], tree_dict[i]['name']) }) else: tree_data.append({ 'id': tree_dict[i]['id'], 'label': tree_dict[i]['name'] }) return MisResponse(tree_data)
def destroy(self, request, *args, **kwargs): # 删除用户时删除其他表关联的用户 instance = self.get_object() self.perform_destroy(instance) return MisResponse(status=NO_CONTENT)
def get(self, request): if request.user.id is not None: menu_data = self.get_all_menus(request) return MisResponse(menu_data, status=OK) else: return MisResponse('请登录后访问!', status=FORBIDDEN)
def get(self, request, format=None): departments = Permit.objects.all() serializer = PermitUserTreeSerializer(departments, many=True) return MisResponse(serializer.data)
def get(self, request, format=None): intercom = Intercom.objects.all() serializer = IntercomSerializer(intercom, many=True) return MisResponse(serializer.data)