def updatePwd(request): response = Response.ResponseObj() user_id = request.GET.get('user_id') user_objs = models.userprofile.objects.filter(id=user_id) if user_objs: user_obj = user_objs[0] oldPwd = request.POST.get('oldPwd') newPwd = request.POST.get('newPwd') if newPwd and oldPwd: oldPwd = account.str_encrypt(oldPwd) # 加密 密码 if oldPwd == user_obj.password: newPwd = account.str_encrypt(newPwd) token = account.get_token(newPwd + str(int(time.time()) * 1000)) # token user_objs.update(password=newPwd, token=token) response.code = 200 response.msg = '修改成功' else: response.code = 301 response.msg = '旧密码验证错误' else: if not oldPwd: response.msg = '请输入旧密码' else: response.msg = '请输入新密码' response.code = 301 else: response.code = 500 response.msg = '非法请求' return JsonResponse(response.__dict__)
def inner(request, *args, **kwargs): rand_str = request.GET.get('rand_str') timestamp = request.GET.get('timestamp', '') user_id = request.GET.get('user_id') # print('user_id -->', user_id) # print('timestamp -->', timestamp) # print('rand_str -->', rand_str) objs = table_obj.objects.filter(id=user_id) if objs: obj = objs[0] print('str_encrypt(timestamp + obj.token) -->', str_encrypt(timestamp + obj.token)) # print('rand_str -->', rand_str) if str_encrypt(timestamp + obj.token) == rand_str: flag = True else: flag = False else: flag = False if not flag: response = Response.ResponseObj() response.code = 400 response.msg = "token异常" return JsonResponse(response.__dict__) return func(request, *args, **kwargs)
def celery_regularly_update_articles(request): response = Response.ResponseObj() objs = models.article.objects.filter(original_link__isnull=False) for obj in objs: original_link = obj.original_link data = get_content(original_link, get_content=1) obj.content = data.get('content') obj.save() return JsonResponse(response.__dict__)
def error_send_msg(request): response = Response.ResponseObj() # msg = request.GET.get('msg') # if msg: # url = 'http://zhugeleida.zhugeyingxiao.com/tianyan/api/outside_calls_send_msg?msg={}&external=1'.format(msg) # # url = 'http://127.0.0.1:8008/tianyan/api/outside_calls_send_msg?msg={}&external=1'.format(msg) # requests.get(url) # # response.code = 200 # response.msg = '已发送错误消息' url = request.GET.get('url') return JsonResponse(response.__dict__)
def qiniu_oper(request, oper_type): response = Response.ResponseObj() if oper_type == 'get_token': SecretKey = 'wVig2MgDzTmN_YqnL-hxVd6ErnFhrWYgoATFhccu' AccessKey = 'a1CqK8BZm94zbDoOrIyDlD7_w7O8PqJdBHK-cOzz' q = qiniu.Auth(AccessKey, SecretKey) bucket_name = 'bjhzkq_tianyan' token = q.upload_token(bucket_name) # 可以指定key 图片名称 response.code = 200 response.msg = '生成成功' response.data = {'token': token} return JsonResponse(response.__dict__)
def get_yuemei_case(request): response = Response.ResponseObj() objs = models.yuemei.objects.filter(is_use=0).order_by('create_date') if objs: obj = objs[0] obj.is_use = 1 obj.save() response.code = 200 response.data = obj.url else: response.code = 301 # url = request.GET.get('url') # objs = models.yuemei.objects.filter(url=url) # if not objs: # models.yuemei.objects.create(url=url) return JsonResponse(response.__dict__)
def login(request): response = Response.ResponseObj() username = request.POST.get('username') password = request.POST.get('password') # 查询数据库 userprofile_objs = models.userprofile.objects.filter( username=username, # md5加密 密码 password=account.str_encrypt(password), status=1) if userprofile_objs: # 如果有数据 查询第一条对象 userprofile_obj = userprofile_objs[0] token = userprofile_obj.token # 如果没有token 则生成 token if not userprofile_obj.token: token = account.get_token(account.str_encrypt(password)) userprofile_obj.token = token else: public.record_user_last_login_time(user_id=userprofile_obj.id) response.code = 200 response.msg = '登录成功' time.time() response.data = { 'token': token, 'user_id': userprofile_obj.id, # 'set_avator': userprofile_obj.set_avator, 'username': userprofile_obj.username, 'role_id': userprofile_obj.role_id, } userprofile_obj.last_login_date = datetime.datetime.now() userprofile_obj.save() else: response.code = 401 response.msg = "账号或密码错误" return JsonResponse(response.__dict__)
def role_oper(request, oper_type, o_id): response = Response.ResponseObj() if request.method == "POST": user_id = request.GET.get('user_id') # 添加角色 if oper_type == "add": form_data = { 'oper_user_id': request.GET.get('user_id'), # 操作人ID 'name': request.POST.get('name'), # 角色名称 'permissionsList': request.POST.get('permissionsList'), # 角色权限 } forms_obj = AddForm(form_data) if forms_obj.is_valid(): obj = models.role.objects.create( **{ 'name': forms_obj.cleaned_data.get('name'), 'oper_user_id': forms_obj.cleaned_data.get( 'oper_user_id'), }) permissionsList = forms_obj.cleaned_data.get('permissionsList') obj.permissions = permissionsList obj.save() response.code = 200 response.msg = "添加成功" response.data = {'testCase': obj.id} else: response.code = 301 response.msg = json.loads(forms_obj.errors.as_json()) # 修改角色 elif oper_type == "update": form_data = { 'o_id': o_id, 'name': request.POST.get('name'), # 角色名称 'oper_user_id': request.GET.get('user_id'), # 操作人ID 'permissionsList': request.POST.get('permissionsList'), # 角色权限 } forms_obj = UpdateForm(form_data) if forms_obj.is_valid(): o_id = forms_obj.cleaned_data['o_id'] name = forms_obj.cleaned_data['name'] # 角色名称 oper_user_id = forms_obj.cleaned_data['oper_user_id'] # 操作人ID permissionsList = forms_obj.cleaned_data[ 'permissionsList'] # 角色权限 objs = models.role.objects.filter(id=o_id) if objs: objs.update( name=name, oper_user_id=oper_user_id, ) objs[0].permissions = permissionsList response.code = 200 response.msg = "修改成功" else: response.code = 303 response.msg = '不存在的数据' else: response.code = 301 response.msg = json.loads(forms_obj.errors.as_json()) # 删除角色 elif oper_type == "delete": objs = models.role.objects.filter(id=o_id) if objs: obj = objs[0] userObj = models.userprofile.objects.get(id=user_id) if userObj.role_id == obj.id: response.code = 301 response.msg = '不能删除自己角色' else: if models.userprofile.objects.filter(role_id=o_id): response.code = 304 response.msg = '含有子级数据,请先删除或转移子级数据' else: objs.delete() response.code = 200 response.msg = "删除成功" else: response.code = 302 response.msg = '删除ID不存在' else: if oper_type == "get_rules": objs = models.role.objects.filter(id=o_id) if objs: obj = objs[0] rules_list = [ i['name'] for i in obj.permissions.values('name') ] response.data = {'rules_list': rules_list} response.code = 200 response.msg = "查询成功" else: response.code = 402 response.msg = "请求异常" return JsonResponse(response.__dict__)
def role(request): response = Response.ResponseObj() if request.method == "GET": forms_obj = SelectForm(request.GET) if forms_obj.is_valid(): current_page = forms_obj.cleaned_data['current_page'] length = forms_obj.cleaned_data['length'] print('forms_obj.cleaned_data -->', forms_obj.cleaned_data) order = request.GET.get('order', '-create_date') user_id = request.GET.get('user_id') userObjs = models.userprofile.objects.filter(id=user_id) field_dict = { 'id': '', 'name': '__contains', 'create_date': '', 'oper_user__username': '******', } q = conditionCom(request, field_dict) print('q -->', q) objs = models.role.objects.filter(q).order_by(order) count = objs.count() if length != 0: start_line = (current_page - 1) * length stop_line = start_line + length objs = objs[start_line:stop_line] ret_data = [] for obj in objs: permissionsList = [] if obj.permissions: permissionsList = [ i['id'] for i in obj.permissions.values('id') ] # if len(permissionsList) > 0: # permissionsData = init_data(selected_list=permissionsList) if obj.oper_user: oper_user_id = obj.oper_user_id oper_user_username = obj.oper_user.username else: oper_user_username = '' oper_user_id = '' ret_data.append({ 'id': obj.id, 'name': obj.name, # 角色名称 'permissionsData': permissionsList, # 角色权限 'oper_user_id': oper_user_id, # 操作人ID 'oper_user__username': oper_user_username, # 操作人 'create_date': obj.create_date.strftime('%Y-%m-%d %H:%M:%S'), }) response.code = 200 response.msg = '查询成功' response.data = { 'ret_data': ret_data, 'data_count': count, } else: response.code = 402 response.msg = "请求异常" response.data = json.loads(forms_obj.errors.as_json()) return JsonResponse(response.__dict__)
def permissions(request): response = Response.ResponseObj() if request.method == "GET": forms_obj = SelectForm(request.GET) if forms_obj.is_valid(): current_page = forms_obj.cleaned_data['current_page'] length = forms_obj.cleaned_data['length'] print('forms_obj.cleaned_data -->', forms_obj.cleaned_data) order = request.GET.get('order', '-create_date') field_dict = { 'id': '', 'name': '__contains', 'create_date': '', 'oper_user__username': '******', 'pid_id': '__isnull' } q = conditionCom(request, field_dict) print('q -->', q) objs = models.permissions.objects.select_related('pid').filter( q).order_by(order) count = objs.count() if length != 0: start_line = (current_page - 1) * length stop_line = start_line + length objs = objs[start_line:stop_line] ret_data = [] for obj in objs: # 如果有oper_user字段 等于本身名字 if obj.oper_user: oper_user_id = obj.oper_user_id oper_user_username = obj.oper_user.username else: oper_user_username = '' oper_user_id = '' pid_name = '' if obj.pid: pid_name = obj.pid.name ret_data.append({ 'id': obj.id, 'name': obj.name, # 权限名字 'title': obj.title, # 权限路径 'pid_id': obj.pid_id, # 权限父级ID 'pid_name': pid_name, # 权限父级名称 'oper_user_id': oper_user_id, # 操作人ID 'oper_user__username': oper_user_username, # 操作人 'create_date': obj.create_date.strftime('%Y-%m-%d %H:%M:%S'), }) # 查询成功 返回200 状态码 response.code = 200 response.msg = '查询成功' response.data = { 'ret_data': ret_data, 'data_count': count, } response.note = { 'ret_data': { 'id': '权限ID', 'name': '权限名字', 'title': '权限路径', 'pid_id': '权限父级ID', 'pid_name': '权限父级名称', 'oper_user_id': '操作人ID', 'oper_user__username': '******', 'create_date': '创建时间', }, 'data_count': '数据总数', } else: response.code = 301 response.data = json.loads(forms_obj.errors.as_json()) else: response.code = 402 response.msg = "请求异常" return JsonResponse(response.__dict__)
def permissions_oper(request, oper_type, o_id): response = Response.ResponseObj() user_id = request.GET.get('user_id') if request.method == "POST": # 创建权限 if oper_type == "add": form_data = { 'oper_user_id': request.GET.get('user_id'), # 操作人ID 'name': request.POST.get('name'), # 权限名称 'title': request.POST.get('title'), # 权限路径 'pid_id': request.POST.get('pid_id'), # 权限父级ID } forms_obj = AddForm(form_data) if forms_obj.is_valid(): models.permissions.objects.create(**forms_obj.cleaned_data) response.code = 200 response.msg = "添加成功" else: response.code = 301 response.msg = json.loads(forms_obj.errors.as_json()) # 修改权限 elif oper_type == "update": form_data = { 'o_id': o_id, 'name': request.POST.get('name'), # 权限名称 'title': request.POST.get('title'), # 权限路径 'pid_id': request.POST.get('pid_id'), # 权限父级ID 'oper_user_id': request.GET.get('user_id'), # 操作人ID } forms_obj = UpdateForm(form_data) if forms_obj.is_valid(): o_id = forms_obj.cleaned_data['o_id'] name = forms_obj.cleaned_data['name'] # 权限名称 title = forms_obj.cleaned_data['title'] # 权限路由 pid_id = forms_obj.cleaned_data['pid_id'] # 权限父级ID oper_user_id = forms_obj.cleaned_data['oper_user_id'] # 操作人ID models.permissions.objects.filter(id=o_id).update( name=name, title=title, pid_id=pid_id, oper_user_id=oper_user_id, ) response.code = 200 response.msg = "修改成功" else: response.code = 301 response.msg = json.loads(forms_obj.errors.as_json()) # 删除权限 elif oper_type == "delete": objs = models.permissions.objects.filter(id=o_id) if objs: obj = objs[0] if models.permissions.objects.filter( pid_id=obj.id).count() > 0: response.code = 304 response.msg = "含有子级数据,请先删除或转移子级数据" else: objs.delete() response.code = 200 response.msg = "删除成功" else: response.code = 302 response.msg = '删除ID不存在' else: # 获取tree数据成功 if oper_type == "get_tree_data": response.code = 200 response.msg = "获取tree数据成功" response.data = {'ret_data': init_data()} # 获取该用户所有权限 elif oper_type == 'get_permissions': user_obj = models.userprofile.objects.filter(id=user_id) if user_obj: role_obj = models.role.objects.filter(id=user_obj[0].role_id) data_list = [] for i in role_obj[0].permissions.all(): data_list.append({ 'id': i.id, 'name': i.name, 'title': i.title, }) response.code = 200 response.msg = '查询成功' response.data = data_list else: response.code = 301 response.msg = '非法用户' else: response.code = 402 response.msg = "请求异常" return JsonResponse(response.__dict__)
def classfiy(request): response = Response.ResponseObj() if request.method == "GET": forms_obj = SelectForm(request.GET) if forms_obj.is_valid(): current_page = forms_obj.cleaned_data['current_page'] length = forms_obj.cleaned_data['length'] order = request.GET.get('order', '-create_date') user_id = request.GET.get('user_id') field_dict = { 'id': '', 'level': '', 'classify_name': '__contains', 'parent_class_id': '', 'oper_user_id': '', } q = conditionCom(request, field_dict) print('q -->', q) objs = models.classfiy.objects.filter(q, oper_user__isnull=False) count = objs.count() if length != 0: start_line = (current_page - 1) * length stop_line = start_line + length objs = objs[start_line:stop_line] # 返回的数据 ret_data = [] for obj in objs: parent_id = '' parent_name = '' class_list = [] if obj.parent_class: parent_id = obj.parent_class_id parent_name = obj.parent_class.classify_name class_list = query_classification_supervisor( parent_id, class_list) ret_data.append({ 'id': obj.id, 'name': obj.classify_name, # 分类名称 'parent_id': parent_id, # 父级ID 'parent_name': parent_name, # 父级分类名称 'level': obj.level, # 父级分类名称 'oper_user_id': obj.oper_user_id, # 操作人ID 'oper_user__username': obj.oper_user.username, # 操作人 'class_list': class_list, # 所有分类 'create_date': obj.create_date.strftime('%Y-%m-%d %H:%M:%S'), }) # 查询成功 返回200 状态码 response.code = 200 response.msg = '查询成功' response.data = { 'ret_data': ret_data, 'data_count': count, } else: response.code = 402 response.msg = "请求异常" response.data = json.loads(forms_obj.errors.as_json()) return JsonResponse(response.__dict__)
def settlement_rules_oper(request, oper_type, o_id): response = Response.ResponseObj() form_data = { 'o_id': o_id, 'oper_user_id': request.GET.get('user_id'), # 操作人ID 'reading_num': request.POST.get('reading_num'), # 阅读数量 'reading_time': request.POST.get('reading_time'), # 阅读时长 'the_amount_of': request.POST.get('the_amount_of'), # 金额 } forms_obj = PermissionForm(form_data) if forms_obj.is_valid(): success_output_msg("操作结算规则-验证通过") if request.method == "POST": # 添加结算规则 if oper_type == "add": forms_obj = AddForm(form_data) if forms_obj.is_valid(): success_output_msg("结算规则添加-验证通过") obj = models.settlement_rules.objects.create( **forms_obj.cleaned_data) response.code = 200 response.msg = "添加成功" response.data = {'testCase': obj.id} else: error_output_msg("结算规则添加-验证不通过") response.code = 301 response.msg = json.loads(forms_obj.errors.as_json()) # 修改结算规则 elif oper_type == "update": forms_obj = UpdateForm(form_data) if forms_obj.is_valid(): success_output_msg("修改结算规则-验证通过") o_id, obj = forms_obj.cleaned_data.get('o_id') obj.update( **{ 'reading_num': forms_obj.cleaned_data.get('reading_num'), 'reading_time': forms_obj.cleaned_data.get('reading_time'), 'the_amount_of': forms_obj.cleaned_data.get('the_amount_of'), 'oper_user_id': forms_obj.cleaned_data.get('oper_user_id'), }) response.code = 200 response.msg = "修改成功" else: error_output_msg("修改结算规则-验证不通过") response.code = 301 response.msg = json.loads(forms_obj.errors.as_json()) else: response.code = 402 response.msg = "请求异常" else: error_output_msg("操作结算规则-验证不通过") response.code = 301 response.msg = json.loads(forms_obj.errors.as_json()) return JsonResponse(response.__dict__)
def article_oper(request, oper_type, o_id): response = Response.ResponseObj() user_id = request.GET.get('user_id') if request.method == "POST": form_data = { 'o_id': o_id, 'belongToUser_id': request.GET.get('user_id'), # 操作人ID 'title': request.POST.get('title'), # 文章标题 'summary': request.POST.get('summary'), # 文章摘要 'article_cover': request.POST.get('article_cover'), # 文章封面图片 'content': request.POST.get('content'), # 文章内容 'article_word_count': request.POST.get('article_word_count', 0), # 文章字数 'edit_name': request.POST.get('edit_name'), # 编辑别名 'article_source': request.POST.get('article_source'), # 文章来源 'classfiy_id': request.POST.get('classfiy_id'), # 类别 'toward_whether': request.POST.get('toward_whether', 0) # 是否对外公开 } # 添加文章 if oper_type == "add": forms_obj = AddForm(form_data) if forms_obj.is_valid(): obj = models.article.objects.create(**forms_obj.cleaned_data) response.code = 200 response.msg = "添加成功" else: response.code = 301 response.msg = json.loads(forms_obj.errors.as_json()) # 修改文章 elif oper_type == "update": forms_obj = UpdateForm(form_data) if forms_obj.is_valid(): o_id, obj = forms_obj.cleaned_data.get('o_id') obj.update( **{ 'toward_whether': forms_obj.cleaned_data.get('toward_whether'), 'title': forms_obj.cleaned_data.get('title'), 'summary': forms_obj.cleaned_data.get('summary'), 'content': forms_obj.cleaned_data.get('content'), 'article_source': forms_obj.cleaned_data.get('article_source'), # 文章来源 'article_cover': forms_obj.cleaned_data.get('article_cover'), # 文章缩略图 'edit_name': forms_obj.cleaned_data.get('edit_name'), # 编辑别名 'article_word_count': forms_obj.cleaned_data.get( 'article_word_count'), # 文章字数 'classfiy_id': forms_obj.cleaned_data.get('classfiy_id'), # 文章字数 }) response.code = 200 response.msg = '修改成功' else: response.code = 301 response.msg = json.loads(forms_obj.errors.as_json()) # 删除文章 elif oper_type == "delete": models.article.objects.get(id=o_id).delete() response.code = 200 response.msg = '删除成功' # forms_obj = DeleteForm(form_data) # if forms_obj.is_valid(): # o_id, obj = forms_obj.cleaned_data.get('o_id') # obj[0].is_delete = 1 # obj[0].save() # response.code = 200 # response.msg = '删除成功' # # else: # response.code = 301 # response.msg = json.loads(forms_obj.errors.as_json()) # 转载文章添加 elif oper_type == 'add_reposts': edit_name = request.POST.get('edit_name') # 别名 reprint_link = request.POST.get('reprint_link') classfiy_id = request.POST.get('classfiy_id') # 类别 toward_whether = request.POST.get('toward_whether', 0) # 是否对外公开 form_data = { 'reprint_link': reprint_link, 'classfiy_id': classfiy_id, 'edit_name': edit_name, 'toward_whether': toward_whether } response.code = 301 forms_obj = AddRepostsForm(form_data) if forms_obj.is_valid(): if reprint_link and 'http' in reprint_link: ret = requests.get(reprint_link) status_code = ret.status_code # 请求状态 if status_code == 200: data = get_content(reprint_link) title = data.get('title') num = get_article_word_count( data.get('content')) # 获取文章字数 article_objs = models.article.objects.filter( title=title, is_delete=0) data['article_source'] = 2 data['belongToUser_id'] = user_id data['classfiy_id'] = classfiy_id data['edit_name'] = edit_name # 编辑别名 data['article_word_count'] = num # 文章字数 if not article_objs: models.article.objects.create(**data) response.code = 200 msg = '添加成功' else: response.code = 200 article_objs.update(**data) msg = '覆盖成功' else: msg = '请求链接异常' else: if not reprint_link: msg = '请输入链接' else: msg = '链接错误' else: msg = json.loads(forms_obj.errors.as_json()) response.msg = msg # 修改是否对公 elif oper_type == 'update_closed_public': models.article.objects.filter(id=o_id).update( toward_whether=form_data.get('toward_whether')) response.code = 200 response.msg = '修改成功' else: # 测试 if oper_type == 'test': obj = models.article.objects.get(id=o_id) get_article_word_count(obj.content) else: response.code = 402 response.msg = "请求异常" return JsonResponse(response.__dict__)
def user(request): response = Response.ResponseObj() forms_obj = SelectForm(request.GET) if forms_obj.is_valid(): current_page = forms_obj.cleaned_data['current_page'] length = forms_obj.cleaned_data['length'] order = request.GET.get('order', '-create_date') field_dict = { 'id': '', 'status': '', 'role_id': '', 'create_date': '', 'oper_user__username': '******', } q = conditionCom(request, field_dict) objs = models.userprofile.objects.select_related('role').filter(q).order_by(order) count = objs.count() if length != 0: start_line = (current_page - 1) * length stop_line = start_line + length objs = objs[start_line: stop_line] # 返回的数据 ret_data = [] now = datetime.date.today() for obj in objs: last_login_time = '' if obj.last_login_time: last_login_time = obj.last_login_time.strftime('%Y-%m-%d %H:%M:%S') # 将查询出来的数据 加入列表 ret_data.append({ 'id': obj.id, 'username': obj.username, # 用户名称 'role_id': obj.role_id, # 角色ID 'role__name': obj.role.name, # 角色名称 'oper_user__username':obj.oper_user.username, # 操作人 'oper_user_id': obj.oper_user_id, # 操作人ID 'status': obj.status, # 用户状态ID 'get_status_display': obj.get_status_display(), # 用户状态 (审核, 未审核) # 'set_avator': obj.set_avator, # 头像 'phone': obj.phone, # 电话 'last_login_time': last_login_time, # 最后一次登录时间 'create_time': obj.create_date.strftime('%Y-%m-%d %H:%M:%S'), }) # 查询成功 返回200 状态码 response.code = 200 response.msg = '查询成功' response.data = { 'ret_data': ret_data, 'data_count': count, 'status':models.userprofile.status_choices } else: response.code = 301 response.msg = json.loads(forms_obj.errors.as_json()) return JsonResponse(response.__dict__)
def user_oper(request, oper_type, o_id): response = Response.ResponseObj() user_id = request.GET.get('user_id') if request.method == "POST": # 获取需要修改的信息 form_data = { 'o_id': o_id, 'username': request.POST.get('username'), # 用户名 'role_id': request.POST.get('role_id'), # 角色ID 'oper_user_id': request.GET.get('user_id'), # 操作人 # 'set_avator': request.POST.get('set_avator'), # 头像 'phone': request.POST.get('phone'), # 电话 'password': request.POST.get('password'), # 密码 } # 添加用户 if oper_type == "add": forms_obj = AddForm(form_data) if forms_obj.is_valid(): models.userprofile.objects.create(**forms_obj.cleaned_data) response.code = 200 response.msg = "添加成功" else: response.code = 301 response.msg = json.loads(forms_obj.errors.as_json()) # 修改用户 elif oper_type == "update": forms_obj = UpdateForm(form_data) if forms_obj.is_valid(): o_id = forms_obj.cleaned_data['o_id'] username = forms_obj.cleaned_data['username'] # 用户名 role_id = forms_obj.cleaned_data['role_id'] # 角色ID oper_user_id = forms_obj.cleaned_data['oper_user_id'] # 操作人 # set_avator = forms_obj.cleaned_data['set_avator'] # 头像 phone = forms_obj.cleaned_data['phone'] # 电话 objs = models.userprofile.objects.filter( id=o_id ) # 更新 数据 if objs: objs.update( username=username, role_id=role_id, oper_user_id=oper_user_id, phone=phone, # set_avator=set_avator ) response.code = 200 response.msg = "修改成功" else: response.code = 303 response.msg = '修改ID不存在' else: response.code = 301 response.msg = json.loads(forms_obj.errors.as_json()) # 删除 用户 elif oper_type == "delete": if o_id == user_id: response.code = 301 response.msg = '不能删除自己' else: objs = models.userprofile.objects.get(id=o_id) if objs: objs.delete() response.code = 200 response.msg = "删除成功" else: response.code = 302 response.msg = '删除ID不存在' response.data = {} else: # 用户审核 if oper_type == 'user_audit': objs = models.userprofile.objects.filter(id=o_id) if objs: if int(objs[0].status) == 1: objs[0].status = 2 else: objs[0].status = 1 objs[0].save() response.code = 200 response.msg = '修改成功' else: response.code = 301 response.msg = '无此用户' # 获取用户信息 elif oper_type == 'get_user_info': obj = models.userprofile.objects.get(id=user_id) response.code = 200 response.msg = '查询个人信息成功' response.data = { # 'set_avator': obj.set_avator, 'username': obj.username, 'role_id': obj.role_id, } else: response.code = 402 response.msg = "请求异常" return JsonResponse(response.__dict__)
def external_query_article(request, oper_type): response = Response.ResponseObj() if request.method == "GET": # 查询公开文章 if oper_type == 'article': forms_obj = SelectForm(request.GET) if forms_obj.is_valid(): current_page = forms_obj.cleaned_data['current_page'] length = forms_obj.cleaned_data['length'] order = request.GET.get('order', '-create_date') field_dict = { 'id': '', 'title': '__contains', 'create_date': '__contains', 'article_source': '', # 'oper_user__username': '******', } q = conditionCom(request, field_dict) objs = models.article.objects.filter( q, toward_whether=1).order_by(order).exclude(is_delete=1) count = objs.count() if length != 0: start_line = (current_page - 1) * length stop_line = start_line + length objs = objs[start_line:stop_line] ret_data = [] id = request.GET.get('id') for obj in objs: classfiy_id = obj.classfiy_id classfiy_name = obj.classfiy.classify_name result_data = { 'id': obj.id, 'title': obj.title, # 文章标题 'summary': obj.summary, # 文章摘要 'article_cover': obj.article_cover, # 文章封面图 'edit_name': obj.edit_name, # 作者别名 'article_source_id': obj.article_source, # 文章来源ID 'article_source': obj.get_article_source_display(), # 文章来源 # 'stop_upload': obj.stop_upload, # 是否停止发布 'classfiy_id': classfiy_id, # 分类ID 'classfiy_name': classfiy_name, # 分类名称 'article_word_count': obj.article_word_count, # 文章字数 'create_date': obj.create_date.strftime( '%Y-%m-%d %H:%M:%S'), # 文章创建时间 } if id: result_data['content'] = obj.content # 文章内容 class_list = [] class_list = query_classification_supervisor( classfiy_id, class_list) result_data['classfiy_list'] = class_list # 分类所有等级 ret_data.append(result_data) article_source = [] for i in models.article.article_source_choices: article_source.append({'id': i[0], 'name': i[1]}) # 查询成功 返回200 状态码 response.code = 200 response.msg = '查询成功' response.data = { 'ret_data': ret_data, 'data_count': count, 'article_source': article_source # 文章来源 } else: response.code = 301 response.data = json.loads(forms_obj.errors.as_json()) else: response.code = 402 response.msg = "请求异常" else: response.code = 402 response.msg = "请求异常" return JsonResponse(response.__dict__)
def classfiy_oper(request, oper_type, o_id): response = Response.ResponseObj() if request.method == "POST": user_id = request.GET.get('user_id') form_data = { 'o_id': o_id, # 操作ID 'oper_user_id': user_id, # 操作人ID 'classify_name': request.POST.get('classify_name'), # 分类名称 'parent_class': request.POST.get('parent_class'), # 父级分类 ID } # 添加分类 if oper_type == "add": # 创建 form验证 实例(参数默认转成字典) forms_obj = AddForm(form_data) if forms_obj.is_valid(): form_clean_data = forms_obj.cleaned_data oper_user_id = form_clean_data.get('oper_user_id') classify_name = form_clean_data.get('classify_name') parent_class = '' num = 1 if form_clean_data.get('parent_class'): parent_class, num = form_clean_data.get('parent_class') objs = models.classfiy.objects.filter( level=num, classify_name=classify_name) if objs: response.code = 301 response.msg = '{num}级分类已存在该名称'.format(num=num) else: models.classfiy.objects.create( oper_user_id=oper_user_id, classify_name=classify_name, parent_class_id=parent_class, level=num) response.code = 200 response.msg = "添加成功" else: response.code = 301 response.msg = json.loads(forms_obj.errors.as_json()) # 修改分类 elif oper_type == "update": forms_obj = UpdateForm(form_data) if forms_obj.is_valid(): form_clean_data = forms_obj.cleaned_data o_id, objs = form_clean_data.get('o_id') oper_user_id = form_clean_data.get('oper_user_id') classify_name = form_clean_data.get('classify_name') parent_class = '' classifiy_level = 1 if form_clean_data.get('parent_class'): parent_class, classifiy_level = form_clean_data.get( 'parent_class') objs.update(oper_user_id=oper_user_id, classify_name=classify_name, parent_class_id=parent_class, level=classifiy_level) response.code = 200 response.msg = '修改成功' else: response.code = 301 response.msg = json.loads(forms_obj.errors.as_json()) # 删除 elif oper_type == "delete": # 删除 ID objs = models.classfiy.objects.filter(id=o_id) if objs: child_objs = models.classfiy.objects.filter( parent_class_id=o_id) if not child_objs: if models.article.objects.filter(classfiy_id=o_id): response.code = 301 response.msg = '该分类下存在文章' else: objs.delete() response.code = 200 response.msg = '删除成功' else: response.code = 301 response.msg = '含有子级分类' else: response.code = 301 response.msg = '删除ID不存在' else: # 查询分类树状图 if oper_type == 'get_tree': result_data = public.GroupTree() response.code = 200 response.msg = '查询成功' response.data = {'ret_data': result_data} # 分类等级查询 elif oper_type == 'classification_level_query': objs = models.classfiy.objects.values('level').annotate( Count('id')) ret_data = [] for obj in objs: ret_data.append(obj.get('level')) response.code = 200 response.msg = '查询成功' response.data = {'ret_data': ret_data} else: response.code = 402 response.msg = "请求异常" return JsonResponse(response.__dict__)