def manage_user_changepw(request): """ 修改自己的账号密码 /api/manage/user/changepw """ try: #id = get_parameter(request.POST.get('id'), para_intro='用户ID', valid_check=INTEGER_NONNEGATIVE) oldpw = get_parameter(request.POST.get('oldpw'), para_intro='原密码', valid_check=PASSWORD) newpw = get_parameter(request.POST.get('newpw'), para_intro='新密码', valid_check=PASSWORD) except InvalidParaException as ipe: logger.exception(ipe) return utils_common.response(respformat(ipe.message)) # 用户不存在 this_user = User.objects.filter(is_delete=False, id=request.user.id).first() if not this_user: return utils_common.response(get_msg(USER_NOT_EXIST)) # 原密码错误 user = auth.authenticate(account=this_user.account, password=oldpw) if not user: return utils_common.response(get_msg(USER_OLD_PASSWORD_WRONG)) this_user.set_password(newpw) this_user.save() dict_resp = {"c": REQUEST_SUCCESS[0], "m": REQUEST_SUCCESS[1]} return utils_common.response(dict_resp)
def manage_user_resetpw(request): """ 重置某一个用户的账号密码 /api/manage/user/resetpw """ try: id = get_parameter(request.POST.get('id'), para_intro='用户ID', valid_check=INTEGER_NONNEGATIVE) newpw = get_parameter(request.POST.get('newpw'), para_intro='新密码', valid_check=PASSWORD) except InvalidParaException as ipe: logger.exception(ipe) return utils_common.response(respformat(ipe.message)) # 用户不存在 this_user = User.objects.filter(is_delete=False, id=id).first() if not this_user: return utils_common.response(get_msg(USER_NOT_EXIST)) this_user.set_password(newpw) this_user.save() dict_resp = {"c": REQUEST_SUCCESS[0], "m": REQUEST_SUCCESS[1]} return utils_common.response(dict_resp)
def manage_preview(request): """ 进入文章预览页面 page/manage/preview """ try: is_preview = get_parameter(request.GET.get('is_preview'), para_intro='是否预览', allow_null=True, default=FALSE, valid_check=CHOICES, choices=(TRUE, FALSE)) if is_preview == FALSE: articleid = get_parameter(request.GET.get('articleid'), para_intro='文章ID编号', valid_check=INTEGER_NONNEGATIVE) else: articleid = '' except InvalidParaException as ipe: logger.exception(ipe) return utils_common.response(respformat(ipe.message)) if is_preview == FALSE: article = SiteArticle.objects.filter(is_delete=False, id=articleid).first() else: article = SiteArticle.objects.filter(author__startswith='preview_' + str(request.user.id) + '_', is_delete=True).first() if not article: return utils_common.response(get_msg(ARTICLE_ID_INVALID)) return render(request, 'manage/manage_preview.html', {'article_id': articleid})
def manage_user_delete(request): """ 删除用户 /api/manage/user/delete """ try: id_list = get_parameter(request.POST.get('id_list'), para_intro='用户ID') except InvalidParaException as ipe: logger.exception(ipe) return utils_common.response(respformat(ipe.message)) userlist = list() for eachid in id_list.split(','): this_user = User.objects.filter(is_delete=False, id=eachid).first() if not this_user: return utils_common.response(get_msg(USER_NOT_EXIST)) #如果用户发布过文章则不允许删除 userartcount = SiteArticle.objects.filter(publish_user=this_user, is_delete=False).count() if userartcount > 0: dict_resp = {"c": USER_HAS_ARTICLE[0], "m": USER_HAS_ARTICLE[1]} return utils_common.response(dict_resp) #如果列表中包含自己侧跳过 if this_user.id != request.user.id: userlist.append(this_user) for eachuser in userlist: eachuser.delete() dict_resp = {"c": REQUEST_SUCCESS[0], "m": REQUEST_SUCCESS[1]} return utils_common.response(dict_resp)
def manage_user_add(request): """ 增加用户 /api/manage/user/add """ try: username = get_parameter(request.POST.get('username'), para_intro='登录账号', valid_check=ACCOUNT) password = get_parameter(request.POST.get('password'), para_intro='登录密码', valid_check=PASSWORD) realname = get_parameter(request.POST.get('realname'), para_intro='用户名') role = get_parameter(request.POST.get('role'), para_intro='角色', allow_null=False, valid_check=CHOICES, choices=(str(DB_USER_ROLE_ADMIN), str(DB_USER_ROLE_TEACHER), str(DB_USER_ROLE_STUDENT), str(DB_USER_ROLE_OTHER))) mobile = get_parameter(request.POST.get('mobile'), para_intro='手机号码', allow_null=True, default='') intro = get_parameter(request.POST.get('intro'), para_intro='介绍', allow_null=True, default='') is_active = get_parameter(request.POST.get('is_active'), para_intro='是否激活', allow_null=True, default=TRUE) except InvalidParaException as ipe: logger.exception(ipe) return utils_common.response(respformat(ipe.message)) # 检查用户名是否重复 if utils_common.is_duplicate_field(username, 'User', 'account'): return utils_common.response(get_msg(USER_DUPLICATE_ACCOUNT)) new_user = User() new_user.account = username new_user.set_password(password) new_user.name = realname new_user.role = int(role) new_user.mobile_phone = mobile new_user.intro = intro new_user.is_active = utils_common.str2bool(is_active) new_user.is_db_admin = False new_user.is_delete = False new_user.save() dict_resp = {"c": REQUEST_SUCCESS[0], "m": REQUEST_SUCCESS[1]} return utils_common.response(dict_resp)
def portal_search(request): """ 进入搜索页面 """ try: searchkey = get_parameter(request.GET.get('searchkey'), para_intro='查询关键字(文章标题模糊查询)') except InvalidParaException as ipe: logger.exception(ipe) return utils_common.response(respformat(ipe.message)) return render(request, 'portal/find.html', {'searchkey': searchkey})
def manage_draft(request): """ 进入文章编写页面 page/manage/draft """ try: articleid = get_parameter(request.GET.get('articleid'), allow_null=True, default=None, para_intro='文章ID编号', valid_check=INTEGER_NONNEGATIVE) except InvalidParaException as ipe: logger.exception(ipe) return utils_common.response(respformat(ipe.message)) html_parser = HTMLParser.HTMLParser() #当传入栏目key时,需要将栏目ID和NAME提供给前端进行展示 page_article_id = '' if articleid: # 编辑模式 article = SiteArticle.objects.filter(is_delete=False, id=int(articleid)).first() if not article: return utils_common.response(get_msg(ARTICLE_ID_INVALID)) page_article_id = str(articleid) editorform = CommonUeditorForm({ 'content': html_parser.unescape(html_parser.unescape(article.content)) }) else: # 新增模式 editorform = CommonUeditorForm() tmp_img_save_location = ARTICLE_IMAGE_TEMP % datetime.now().strftime( '%Y%m') tmp_file_save_location = ARTICLE_IMAGE_FILE % datetime.now().strftime( '%Y%m') tmp_video_save_location = ARTICLE_VIDEO_TEMP % datetime.now().strftime( '%Y%m') editorform.fields['content'].widget._upload_settings[ 'imagePathFormat'] = tmp_img_save_location editorform.fields['content'].widget._upload_settings[ 'filePathFormat'] = tmp_file_save_location editorform.fields['content'].widget._upload_settings[ 'videoPathFormat'] = tmp_video_save_location return render(request, 'manage/manage_draft.html', { "form": editorform, 'article_id': page_article_id })
def manage_user_edit(request): """ 修改用户 /api/manage/user/edit """ try: id = get_parameter(request.POST.get('id'), para_intro='用户ID', valid_check=INTEGER_NONNEGATIVE) username = get_parameter(request.POST.get('username'), para_intro='登录账号', valid_check=ACCOUNT) realname = get_parameter(request.POST.get('realname'), para_intro='姓名', allow_null=False) mobile = get_parameter(request.POST.get('mobile'), para_intro='手机号码', allow_null=True, default='') intro = get_parameter(request.POST.get('intro'), para_intro='介绍', allow_null=True, default='') is_active = get_parameter(request.POST.get('is_active'), para_intro='是否激活', allow_null=True, default=TRUE) except InvalidParaException as ipe: logger.exception(ipe) return utils_common.response(respformat(ipe.message)) this_user = User.objects.filter(is_delete=False, id=id).first() if not this_user: return utils_common.response(get_msg(USER_NOT_EXIST)) # 检查用户名是否重复 if username != this_user.account: if utils_common.is_duplicate_field(username, 'User', 'account'): return utils_common.response(get_msg(USER_DUPLICATE_ACCOUNT)) this_user.account = username this_user.name = realname this_user.mobile_phone = mobile this_user.intro = intro this_user.is_active = utils_common.str2bool(is_active) this_user.save() dict_resp = {"c": REQUEST_SUCCESS[0], "m": REQUEST_SUCCESS[1]} return utils_common.response(dict_resp)
def common_sys_edit(request): """ 修改某一个系统配置参数 """ try: id = get_parameter(request.POST.get('id'), para_intro='系统参数配置ID编号', valid_check=INTEGER_NONNEGATIVE) value = get_parameter(request.POST.get('value'), para_intro='参数新的配置值', allow_null=True, default='') except InvalidParaException as ipe: return utils_common.response(respformat(ipe.message)) sys_para = CommonParameter.objects.filter(is_delete=False, id=id).first() if not sys_para: return utils_common.response(get_msg(COMMON_SYS_PARA_NOT_EXIST)) sys_para.value1 = value sys_para.save() dict_resp = {"c": REQUEST_SUCCESS[0], "m": REQUEST_SUCCESS[1]} return utils_common.response(dict_resp)
def manage_user_login(request): """ 用户登录 /api/manage/user/login """ try: username = get_parameter(request.POST.get('username'), para_intro='登录账号', valid_check=ACCOUNT) password = get_parameter(request.POST.get('password'), para_intro='登录密码', valid_check=PASSWORD) except InvalidParaException as ipe: logger.exception(ipe) return utils_common.response(respformat(ipe.message)) user = auth.authenticate(account=username, password=password) if not user or user.is_delete or not user.is_active: # 用户账号密码错误或者未启用 dict_resp = get_msg(LOGIN_WRONG_ACCOUNT) elif user.role not in (DB_USER_ROLE_ADMIN, DB_USER_ROLE_TEACHER): # 只有门户管理员和教师才可以使用本系统 dict_resp = get_msg(LOGIN_NO_PRIVILEGES) else: auth.login(request, user) info = { 'username': user.account, 'realname': user.name, 'role': user.role, 'mobile': user.mobile_phone, 'intro': user.intro, } dict_resp = { "c": REQUEST_SUCCESS[0], "m": REQUEST_SUCCESS[1], "d": info } return utils_common.response(dict_resp)
def portal_content(request): """ 进入文章正文页面 """ try: articleid = get_parameter(request.GET.get('articleid'), para_intro='文章ID编号') except InvalidParaException as ipe: logger.exception(ipe) return utils_common.response(respformat(ipe.message)) # 检查文章编号是否合法 article_qs = SiteArticle.objects.filter(is_delete=False, id=articleid) if not article_qs.exists(): utils_common.response(get_msg(ARTICLE_ID_INVALID)) this_article = article_qs.first() page_goto = DEFAULT_CONTENT_PAGE ctx = {'article_id': articleid} return render(request, page_goto, ctx)
def portal_article_list(request): """ 进入某一个栏目的文章列表页面 page/hzjm/article/list """ try: column = get_parameter(request.GET.get('columnkey'), para_intro='栏目/频道/推送频道关键字') except InvalidParaException as ipe: logger.exception(ipe) return utils_common.response(respformat(ipe.message)) ctx = {'channel_key': '', 'category_key': '', 'article_id': ''} category = SiteCategory.objects.filter(is_delete=False, is_active=True, key=column, link='').first() channel = SiteChannel.objects.filter(is_delete=False, is_active=True, key=column, link='').first() push_channel = SitePushChannel.objects.filter(is_delete=False, is_active=True, key=column).first() # 如果请求的是栏目 if category: ctx['category_key'] = category.key ctx['channel_key'] = category.channel.key page_goto = category.type.page_list or DEFAULT_LIST_PAGE if category.type.is_allow_direct and category.is_support_direct: # 支持单文直达 articles_qs = SiteArticle.objects.filter(is_delete=False, category=category) if articles_qs.count() == 1: ctx['article_id'] = articles_qs.first().id page_goto = category.type.page_content or DEFAULT_CONTENT_PAGE # 如果请求的是频道 elif channel: default_cate = SiteCategory.objects.filter(channel=channel, link='', is_default=True, is_delete=False, is_active=True).first() if not default_cate: default_cate = SiteCategory.objects.filter(channel=channel, link='', is_delete=False, is_active=True).first() if not default_cate: #return utils.response(get_msg(CHANNEL_NOT_HAS_CATEGORY)) return HttpResponseRedirect(request.META['HTTP_REFERER']) ctx['category_key'] = default_cate.key ctx['channel_key'] = default_cate.channel.key page_goto = default_cate.type.page_list or DEFAULT_LIST_PAGE if default_cate.type.is_allow_direct and default_cate.is_support_direct: # 支持单文直达 articles_qs = SiteArticle.objects.filter(is_delete=False, category=default_cate) if articles_qs.count() == 1: ctx['article_id'] = articles_qs.first().id page_goto = default_cate.type.page_content or DEFAULT_CONTENT_PAGE # 如果请求的是一个推送频道 elif push_channel: ctx['channel_key'] = push_channel.key page_goto = 'hzjm/list_push.html' else: return utils_common.response(get_msg(CHANNEL_NOT_EXIST)) return render(request, page_goto, ctx)