def front_reset_pwd(request): if request.method == 'GET': return render(request, 'front_reset_pwd.html') else: username = request.user form = ResetpwdForm(request.POST, user=username) if form.is_vaild(): oldpwd = form.cleaned_data.get('oldpwd') newpwd = form.cleaned_data.get('newpwd') user = FrontUserModel.objects.filter(username=username, password=oldpwd).first() if user: is_vaild = user.check_password() if is_vaild: user.set_password(newpwd) user.save() return myjson.json_result() else: return myjson.json_params_error(message=u'密码验证错误!') else: user = user.create(username=username, password=newpwd) return myjson.json_result() else: return render(request, 'front_reset_pwd.html', {'error': form.get_error()})
def cms_top_article(request): '''置顶和取消置顶 1. 如果文章已置顶则无需再置顶,未置顶则置顶。 2. 如果选择的是置顶(不置顶),而数据库中存的也是置顶(不置顶), 则不需操作,抛出异常 如果选择的是置顶,而数据库中存的是不置顶,则删除该字段 如果选择的是不置顶,而数据库中存的是置顶,则创建改字段 3. 修改TopModel之后也要更改ArticleModel的top字段 ''' form = TopArticleForm(request.POST) if form.is_valid(): article_id = form.cleaned_data.get('article_id') # 本文章选择的是置顶还是不置顶 is_top = form.cleaned_data.get('is_top') article_model = ArticleModel.objects.filter(pk=article_id).first() if article_model: top_model = article_model.top if is_top: if top_model: return myjson.json_params_error(message=u'该文章已经置顶!') else: top_model = TopModel() top_model.save() article_model.top = top_model article_model.save(update_fields=['top']) return myjson.json_result() else: if top_model: top_model.delete() return myjson.json_result() else: return myjson.json_params_error(message=u'该文章没有置顶!') else: message = form.errors return myjson.json_params_error(message)
def front_article_star(request): '''点赞和取消点赞,同置顶 1. 如果文章已点赞则无需再点赞,未点赞则点赞。 2. 如果选择的是点赞(不点赞),而数据库中存的也是点赞(不点赞), 则不需操作,抛出异常 如果选择的是点赞,而数据库中存的是不点赞,则删除该字段 如果选择的是不点赞,而数据库中存的是点赞,则创建改字段 3. 修改TopModel之后也要更改ArticleModel的top字段 ''' form = ArticleStarForm(request.POST) if form.is_valid(): article_id = form.cleaned_data.get('article_id') # 本文章选择的是置顶还是不置顶 is_star = form.cleaned_data.get('is_star') article_model = ArticleModel.objects.filter(pk=article_id).first() star_model = ArticleStarModel.objects.filter(pk=article_id).first() if article_model: if is_star: if star_model: return myjson.json_params_error(message=u'该文章已经置顶!') else: star_model = ArticleStarModel() star_model.author = request.user.username star_model.article = article_model star_model.save() return myjson.json_result() else: if star_model: star_model.delete() return myjson.json_result() else: return myjson.json_params_error(message=u'该文章没有置顶!') else: return myjson.json_params_error(message=form.get_error())
def cms_reset_pwd(request): if request.method == 'GET': return render(request, 'cms_reset_pwd.html') else: username = request.user ## 使用cleaned_password方法 # form = ResetpwdForm(request.POST,username=username) # return render(request,'cms_reset_pwd.html',{'error':form.errors}) # 使用save_password方法 form = ResetpwdForm(request.POST, user=username) if form.is_vaild(): oldpwd = form.cleaned_data.get('oldpwd') user = authenticate(username=username, password=oldpwd) if user: is_vaild = form.save_password() if is_vaild: return myjson.json_result() else: return myjson.json_params_error(message=u'密码验证错误!') else: user = user.create(username=username, password=newpwd) return myjson.json_result() else: return render(request, 'cms_reset_pwd.html', {'error': form.get_error()})
def top_article(request): # method(GET,POST) # 参数 # wiki form = TopArticleForm(request.POST) if form.is_valid(): uid = form.cleaned_data.get('uid') articleModel = ArticleModel.objects.filter(pk=uid).first() if articleModel: if not articleModel.thumbnail: return myjson.json_params_error(message=u'有缩略图的文章才能置顶') topModel = articleModel.top if not topModel: topModel = TopModel() # 如果存在,也save一下,这样就会更新operate_time了 topModel.save() articleModel.top = topModel articleModel.save(update_fields=['top']) return myjson.json_result() else: return myjson.json_params_error(message=u'该文章不存在') else: return form.get_error_response()
def update_profile(request): # 更新简介函数 """ 1. 更新用户名(username) 2. 更新头像(avatar) """ print "request.POST过来的数据 =", request.POST print "request.POST过来的数据username ="******"username") print "request.POST过来的数据avater =", request.POST.get("avatar") # 如果没有就是空值 form = UpdateProfileForm(request.POST) if form.is_valid(): avatar = form.cleaned_data.get('avatar', None) username = form.cleaned_data.get('username', None) print "form中的数据username ="******"form中的数据avater =", avatar # 1.更新用户名 user = User.objects.get(username=username) print "修改的User ="******"": cmsuser.avatar = avatar else: pass cmsuser.save() return myjson.json_result() else: return form.get_error_response()
def article_list(request, category_id=0, page=1): categoryId = int(category_id) currentPage = int(page) start = (currentPage - 1) * settings.NUM_PAGE end = start + settings.NUM_PAGE articles = ArticleModel.objects.all() topArticle = None # 如果是处在所有文章界面,那么首先需要获取三篇置顶文章 # 再获取其他的文章,应该排除置顶的文章 if categoryId > 0: articles = articles.filter(category__pk=categoryId).order_by('-release_time') else: topArticle = articles.filter(top__isnull=False).order_by('-top__create_time') articles = articles.filter(top__isnull=True).order_by('-release_time') articles = list(articles.values()[start:end]) # model_to_dict(articleModel) ---> 将模型转换成字典 context = { 'articles': articles, 'c_page': currentPage } if request.is_ajax(): return myjson.json_result(data=context) else: context['categorys'] = CategoryModel.objects.all() context['top_articles'] = topArticle context['c_category'] = CategoryModel.objects.filter(pk=category_id).first() return render(request, 'front_article_list.html', context)
def front_reset_email(request): if request.method == 'GET': return render(request, 'front_reset_email.html') else: # 如果邮箱在数据库存在,则无需修改 # 否则才允许修改新密码 form = ResetEmailForm(request.POST) if form.is_valid(): email = form.cleaned_data.get('email', None) user = request.user if email: if request.email == email: return myjson.json_params_error(u'新邮箱与老邮箱一致,无需修改!') else: if send_mail(receivers=email): user.email = email user.save(update_fields=['email']) return myjson.json_result(u'该邮箱验证成功!') else: return myjson.json_server_error() else: return myjson.json_params_error(message=u'必须输入邮箱!') else: return render(request, 'front_reset_email.html', {'error': form.get_error()})
def cms_settings(request): # 1.默认User表里是没有avatar,所以用到了CMSUser表 # 2.CMSUser表和User表建立了一对一 # 3.我们先查找CMSUser里的用户id # 4.如果用户存在则存储avatar,否则新建该用户并储存 if request.method == 'GET': return render(request, 'cms_settings.html') else: form = SettingsForm(request.POST) if form.is_valid(): username = form.cleaned_data.get('username', None) avatar = form.cleaned_data.get('avatar', None) # 存储username # user = User.objects.all().first() user = request.user user.username = username user.save() # 存储avatar cms_user = CmsUserModel.objects.filter(user__pk=user.pk) if cms_user: cms_user.avatar = avatar else: CmsUserModel(user=user, avatar=avatar) cms_user.save() return myjson.json_result() else: return render(request, 'cms_settings.html', {'error': form.get_error()})
def article_list(request, category_id=0, page=1): categoryId = int(category_id) currentPage = int(page) start = (currentPage - 1) * settings.NUM_PAGE end = start + settings.NUM_PAGE categorys = CategoryModel.objects.all() articles = ArticleModel.objects.all() top_articles = None # 若url中存在id 则筛选出相应文章 if categoryId > 0: articles = articles.filter( category__pk=categoryId).order_by('public_date') else: top_articles = articles.filter( top__isnull=False).order_by('-top__Alter_time') articles = articles.filter(top__isnull=True).order_by('public_date') # 需要将其转化为数组 articles = list(articles.values()[start:end]) context = {'articles': articles, 'c_page': currentPage} if request.is_ajax(): return myjson.json_result(data=context) else: context['categorys'] = categorys context['c_category'] = CategoryModel.objects.filter( pk=categoryId).first() context['top_articles'] = top_articles return render(request, 'front_article_list.html', context)
def sms_captcha(request): telephone = request.GET.get('telephone') # 获取用户名,用于发送短信验证码显示用户名 if not telephone: return myjson.json_params_error(message=u'必须指定手机号码!') if cache.get(telephone): return myjson.json_params_error(message=u'验证码已发送,请1分钟后重复发送!') # if not username: # return myjson.json_params_error(message=u'必须输入用户名!') # 阿里大于APP_KEY及APP_SECRET app_key = settings.APP_KEY app_secret = settings.APP_SECRET req = top.setDefaultAppInfo(app_key, app_secret) req = toprequest.api.AlibabaAliqinFcSmsNumSendRequest() req.extend = "" req.sms_type = 'normal' # 签名名称 req.sms_free_sign_name = settings.SIGN_NAME # 随即生成字符串 captcha = Captcha.gene_text() # 设置短信的模板 req.sms_param = "{code:%s}" % captcha # req.sms_param = "{username:%s,code:%s}" % (username, captcha) req.rec_num = telephone.decode('utf-8').encode('ascii') req.sms_template_code = settings.TEMPLATE_CODE try: resp = req.getResponse() cache.set(telephone, captcha, 120) return myjson.json_result() except Exception, e: print e return myjson.json_server_error()
def front_settings(request): if request.method == 'GET': return render(request, 'front_settings.html') else: form = SettingsForm(request.POST) if form.is_valid(): username = form.cleaned_data.get('username', None) realname = form.cleaned_data.get('realname', None) qq = form.cleaned_data.get('qq', None) avatar = form.cleaned_data.get('avatar', None) signature = form.cleaned_data.get('signature', None) gender = form.cleaned_data.get('gender', None) # 用户登录之后只要获取当前用户的信息,然后就可以直接传入数据 user_model = request.user # 只有用户名是必填项,其他如果没有可以不填,所以需要判断,而性别是有默认选项 user_model.username = username user_model.gender = gender if realname: user_model.realname = realname if qq: user_model.qq = qq if avatar: user_model.avatar = avatar if signature: user_model.signature = signature user_model.save() return myjson.json_result() else: return render(request, 'cms_reset_email.html', {'error': form.get_error()})
def update_profile(request): # 1.更新头像 # 2.更新用户名 form = UpdateFrontProfileForm(request.POST) if form.is_valid(): front_username = form.cleaned_data.get('front_username', None) portrait = form.cleaned_data.get('portrait', None) email = form.cleaned_data.get('email') print email user = FrontUserModel.objects.filter(email=email).first() if user: user.username = front_username user.portrait = portrait user.save() # # 获取FrontUserModel中所有用户信息 # users = FrontUserModel.objects.all() # user = users.filter(email="*****@*****.**").first() # frontUser = FrontUser.objects.filter(user__pk=user.pk).first() # 如果FrontUser表中已存在数据与user对应 直接更新 # if frontUser: # frontUser.portrait = portrait # 否则,先新建一个frontuser模型 再保存 # else: # frontUser = FrontUser(portrait=portrait,user=user) # frontUser.save() # return JsonResponse({'code':200}) return myjson.json_result() else: print '------------------------fial' # return JsonResponse({'error':form.errors}) return myjson.json_params_error(message=u'表单验证错误!')
def test(request): for x in xrange(0, 100): title = u'博客标题%s' % x category = CategoryModel.objects.all().first() content_html = u'博客内容%s' % x articleModel = ArticleModel(title=title, content_html=content_html, category=category) articleModel.save() return myjson.json_result()
def cms_remove_comment(request): comment_id = request.POST.get('comment_id', 1) if not comment_id: return myjson.json_params_error() comment_model = CommentModel.objects.filter(comment_id).first() if comment_model: comment_model.is_removed = True comment_model.save() return myjson.json_result()
def front_edit_article(request): ''' 它的逻辑和add_article相似,但作者不会变,只需获取文章的id 我们会用到一篇已存在的article里的的所有数据,包括tags 但文章里的tags会通过article.tags多对多的方式获取,而不再从tagmodel获取 因为可能会修改tags,我们仍需要传入所有的tags 因此我们需要将tags添加到article里,而为了方便使用,也为了添加tags 我们会使用到model_to_dict这个函数,将数据库里的数据提取出来并转化成dict类型 ''' if request.method == 'GET': article_id = request.GET.get('article_id', None) if article_id: article_model = ArticleModel.objects.filter(pk=article_id).first() article_dict = model_to_dict(article_model) tag_model = article_model.tags.all() article_dict['tags'] = [tag_model.id for tags in tag_model] context = { 'categorys': CategoryModel.objects.all(), 'tags': TagModel.objects.all(), 'article': article_dict } return render(request, 'front_add_article.html', context=context) else: return myjson.json_params_error(u'没有该id!') else: form = EditArticleForm(request.POST) if form.is_valid(): tag_ids = request.POST.getlist('tags[]') article_id = form.cleaned_data.get('article_id') title = form.cleaned_data.get('title') thumbnail = form.cleaned_data.get('thumbnail') content = form.cleaned_data.get('content') category_id = form.cleaned_data.get('category') article_model = ArticleModel.objects.filter(pk=article_id).first() category = CategoryModel.objects.filter(pk=category_id).first() # 修改和创建的方式不一样 if article_model: article_model.title = title article_model.thumbnail = thumbnail article_model.content = content article_model.category = category article_model.save() # 文章与标签的多对多的保存 # 如果选择了标签,则将选择的标签以此存入,否则不用存 if tag_ids: for tag_id in tag_ids: tag_model = TagModel.objects.filter(pk=tag_id).first() if tag_model: article_model.tags.add(tag_model) return myjson.json_result() else: return render(request, 'front_edit_article.html', {'error': form.get_error()})
def test(request): for x in xrange(21, 40): title = u'代码%s' % x # 查找id为1的 category = CategoryModel.objects.get(id=3) content_html = u'代码 %s' % x articleModel = ArticleModel(title=title, content_html=content_html, category=category) articleModel.save() return myjson.json_result()
def edit_blog(request, pk=''): if request.method == 'GET': article = ArticleModel.objects.filter(pk=pk).first() # 通过moel_to_dict 转换为 dict articleDict = model_to_dict(article) # 取dict中key['tags']的值 设为列表 articleDict['tags'] = [] # print(type(articleDict)) --dict # print(articleDict) --QuerySet # 将其转化为id并存入articleDict中 for tagModel in article.tags.all(): articleDict['tags'].append(tagModel.id) # print(articleDict) context = { 'article': article, # 保存标签,另起一个Dict存放 'Dict': articleDict, 'categorys': CategoryModel.objects.all(), 'tags': TagModel.objects.all(), } return render(request, 'cms_edit_article.html', context) else: form = UpdateArticleForm(request.POST) if form.is_valid(): title = form.cleaned_data.get('title') category = form.cleaned_data.get('category') desc = form.cleaned_data.get('desc') thumbnail = form.cleaned_data.get('thumbnail') content_html = form.cleaned_data.get('content_html') tags = request.POST.getlist('tags[]') uid = form.cleaned_data.get('uid') articleModel = ArticleModel.objects.filter(pk=uid).first() if articleModel: # 更新文章各项数据 # articleModel.uid = uid articleModel.title = title articleModel.desc = desc articleModel.thumbnail = thumbnail articleModel.content_html = content_html articleModel.category = CategoryModel.objects.filter( pk=category).first() articleModel.save() if tags: for tag in tags: tagModel = TagModel.objects.filter(pk=tag).first() articleModel.tags.add(tagModel) return myjson.json_result() else: return form.get_error_response()
def delete_blog(request): form = DeleteArticleForm(request.POST) if form.is_valid(): uid = form.cleaned_data.get('uid') articleModel = ArticleModel.objects.filter(pk=uid).first() if articleModel: articleModel.delete() return myjson.json_result() else: return myjson.json_params_error(message=u'该文章不存在!') else: return form.get_error_response()
def front_delete_comment(request): comment_id = request.GET.get('comment_id', None) if comment_id: comment_model = CommentModel.objects.filter(pk=comment_id).first() if comment_model: comment_model.is_removed = True comment_model.save() return myjson.json_result() else: return myjson.json_params_error(message=u'该评论不存在!') else: return myjson.json_params_error(u'没有该id!')
def cms_delete_article(request): form = DeleteArticleForm(request.POST) if form.is_valid(): article_id = form.cleaned_data.get('article_id') article_model = ArticleModel.objects.filter(pk=article_id).first() if article_model: article_model.delete() return myjson.json_result() else: return myjson.json_params_error(message=u'该文章不存在或已被删除') else: # return myjson.json_params_error(message=form.errors) return redirect(reverse('article_list'))
def edit_category(request): form = EditCategoryForm(request.POST) if form.is_valid(): categoryId = form.cleaned_data.get('category_id') name = form.cleaned_data.get('name') categoryModel = CategoryModel.objects.filter(pk=categoryId).first() if categoryModel: categoryModel.name = name categoryModel.save(update_fields=['name']) return myjson.json_result() else: return myjson.json_params_error(message=u'该分类不存在') else: return form.get_error_response()
def add_category(request): form = AddCategoryForm(request.POST) if form.is_valid(): categoryname = form.cleaned_data.get('categoryname', None) resultCategory = CategoryModel.objects.filter(name=categoryname).first() if not resultCategory: categoryModel = CategoryModel(name=categoryname) categoryModel.save() return myjson.json_result(data={'id': categoryModel.id, 'name': categoryModel.name}) else: # return JsonResponse({'error':u'不能設置同名的分類!','code':403}) return myjson.json_params_error(message=u'不能設置同名的分類!') else: # return JsonResponse({'error':form.errors.as_json,'code':'403'}); return myjson.json_params_error(message=u'表单验证失败')
def add_tag(request): form = AddTagForm(request.POST) if form.is_valid(): tagname = form.cleaned_data.get('tagname') # 先判断一下数据库中是否已经存在同名的标签 resultTag = TagModel.objects.filter(name=tagname).first() if not resultTag: # 如果没有,说明可以添加 tagModel = TagModel(name=tagname) tagModel.save() return myjson.json_result(data={'id': tagModel.id, 'name': tagModel.name}) else: return myjson.json_params_error(message=u'不能设置同名标签!') else: return form.get_error_response()
def untop_article(request): form = TopArticleForm(request.POST) if form.is_valid(): uid = form.cleaned_data.get('uid') articleModel = ArticleModel.objects.filter(pk=uid).first() if articleModel: if articleModel.top: topModel = articleModel.top topModel.delete() return myjson.json_result() else: return myjson.json_params_error(message=u'本文章还未置顶呢') else: return myjson.json_params_error(message=u'该文章不存在') else: return form.get_error_response()
def front_article_list(request, page=1, sort=1, category_id=0): ''' 通过ajax的方式加载页面,首页默认以置顶的方式,每次加载都先排除掉置顶的 ''' context = ArticleModelHelper.article_list(page, sort, category_id) articles = context['articles'] if category_id > 1: top_article = articles.filter( top__isnull=False).order_by('-top__create_time') articles = articles.filter(top__isnull=True) if request.is_ajax(): return myjson.json_result(context) else: context['top_articles'] = top_article return render(request, 'front_article_list.html', context=context)
def cms_add_tag(request): '''如果tag已经存在,则不允许在添加,否则添加 ''' form = AddTagForm(request.POST) if form.is_valid(): tag_name = form.cleaned_data.get('tag_name') tag_model = TagModel.objects.filter(name=tag_name).first() if tag_model: return myjson.json_params_error(u'该标签已存在,请重新设置!') else: tagModel = TagModel(name=tag_name) tagModel.save() return myjson.json_result() else: message = form.errors return myjson.json_params_error(message)
def cms_add_category(request): '''如果tag已经存在,则不允许在添加,否则添加 ''' form = AddCategoryForm(request.POST) if form.is_valid(): category_name = form.cleaned_data.get('category_name') category_model = CategoryModel.objects.filter( name=category_name).first() if category_model: return myjson.json_params_error(u'该分类已存在,请重新设置!') else: categoryModel = categoryModel(name=category_name) categoryModel.save() return myjson.json_result() else: message = form.errors return myjson.json_params_error(message)
def delete_category(request): form = CategoryForm(request.POST) if form.is_valid(): categoryId = form.cleaned_data.get('category_id') categoryModel = CategoryModel.objects.filter(pk=categoryId).first() if categoryModel: # 首先拿到该分类下的文章数量 articleCount = categoryModel.articlemodel_set.all().count() # 如果文章数量大于0,就不让删除 if articleCount > 0: return myjson.json_params_error(message=u'该分类下还存在文章,不能删除!') else: categoryModel.delete() return myjson.json_result() else: return myjson.json_params_error(message=u'该分类不存在') else: return form.get_error_response()
def add_tag(requset): form = AddTagForm(requset.POST) if form.is_valid(): tagname = form.cleaned_data.get('tagname', None) resultTag = TagModel.objects.filter(name=tagname).first() if not resultTag: tagModel = TagModel(name=tagname) tagModel.save() # return JsonResponse(data={'id':tagModel.id,'name':tagModel.name,'code':200}) return myjson.json_result(data={ 'id': tagModel.id, 'name': tagModel.name }) else: # return JsonResponse({'error':u'该标签已存在!','code':400}) return myjson.json_params_error(message=u'该标签已存在!') else: # return JsonResponse({'error':form.errors.as_json,'code':'403'}) return myjson.json_params_error(message=u'表单验证错误!')