def get_user_by_username(self, user_name): key = self.get_user_name_key(user_name) user_instance = self.handle.get(key) if not user_instance: user_instance = BusinessUser.get_object(**{'phone': user_name}) if isinstance(user_instance, Exception): return user_instance self.set_data_to_cache(key, user_instance) return user_instance
def get_user_by_id(self, user_id): key = self.get_user_id_key(user_id) user_instance = self.handle.get(key) if not user_instance: user_instance = BusinessUser.get_object(**{'pk': user_id}) if isinstance(user_instance, Exception): return user_instance self.set_user_to_cache(key, user_instance) return user_instance
def post(self, request, *args, **kwargs): user = BusinessUser.get_user_detail(request) if isinstance(user, Exception): return Response({'Error': user.args}, status=status.HTTP_400_BAD_REQUEST) serializer = UserDetailSerializer(data=user) if serializer.is_valid(): return Response(serializer.data, status=status.HTTP_201_CREATED) return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def get_user_by_id(self, request, user_id=None): if not request.user.is_admin: user_id = request.user.id key = self.get_user_id_key(user_id) user_instance = self.handle.get(key) if not user_instance: user_instance = BusinessUser.get_object(**{'pk': user_id}) if isinstance(user_instance, Exception): return user_instance self.set_data_to_cache(key, user_instance) return user_instance
def put(self, request, *args, **kwargs): """ 修改密码 """ form = ChangePasswordForm(request.data) if not form.is_valid(): return Response(form.errors, status=status.HTTP_400_BAD_REQUEST) obj = BusinessUser.get_object(**{'pk': request.user.id}) serializer = UserSerializer(obj) try: serializer.update_password(obj, form.cleaned_data) except Exception as e: return Response({'Error': e.args}, status=status.HTTP_400_BAD_REQUEST) serializer_response = UserInstanceSerializer(obj) return Response(serializer_response.data, status=status.HTTP_206_PARTIAL_CONTENT)
def filter_perfect_details(cls, request, **kwargs): instances = cls.filter_objects(**kwargs) if isinstance(instances, Exception): return instances users_dict = {} details = [] for ins in instances: if not request.user.is_admin: ins.bank_card_number = ins.security_card_number detail = model_to_dict(ins) user = users_dict.get(ins.user_id) if not user: user = BusinessUser.get_object(pk=ins.user_id) users_dict[ins.user_id] = user detail['chinese_people_id'] = user.chinese_people_id details.append(detail) return details
def get_user_object(self, username): return BusinessUser.get_object(phone=username)
def get_objects_list(self, request, **kwargs): return BusinessUser.get_objects_list(request, **kwargs)
def get_user_info(user_id): return BusinessUser.get_object(pk=user_id)