def article(request): if C.checkLoginAdmin(request.session.get('uInfo', False)) == False: return HttpResponseRedirect('/signin/') else: context = {} searchCondition = '' keyword = cgi.escape(request.GET.get('word', '')) if C.isset(keyword): searchCondition += " WHERE article.title LIKE '%%" + keyword + "%%' " sql = 'SELECT article_id, title, username AS author, name AS category FROM "' + Meta.db_table + '_article" AS article ' sql += 'LEFT JOIN "' + Meta.db_table + '_relation" AS relation ON article.article_id=relation.aid ' sql += 'LEFT JOIN "' + Meta.db_table + '_category" AS category ON relation.cid=category.category_id ' sql += 'LEFT JOIN "' + Meta.db_table + '_user" AS usertab ON article.author=usertab.user_id ' sql += searchCondition + 'ORDER BY article_id DESC' articleList = list(Article.objects.raw(sql)) paginator = Paginator(articleList, 10) page = int(request.GET.get('page', 1)) try: pagebar = paginator.page(page) except PageNotAnInteger: pagebar = paginator.page(1) except EmptyPage: pagebar = paginator.page(paginator.num_pages) context = { 'pagebar' : pagebar } return render(request, manageThemeDir + 'article.html', context)
def detail(request): aid = cgi.escape(request.GET.get('aid')) url = request.get_host() detail = [] if C.isset(aid): detail = Article.objects.get(article_id=aid) detail.author = User.objects.get(user_id=detail.author).username detail.content = detail.content.replace('\t', '').replace('\n', '').replace(' ', '') navList = C.getNavList() userInfo = request.session.get('uInfo', '') commentHtml = commentTree(commentList, 0, False) commentList = Comment.objects.filter(article_id=aid) del commentList upArticle = Article.objects.get(article_id=aid) upArticle.look_count = upArticle.look_count+1 upArticle.save() context = { 'userinfo' : userInfo, 'detail' : detail, 'navList' : navList, 'url' : url, 'aid' : aid, 'commentHtml' : commentHtml, 'themeHeader' : C.getThemePath() + '/Public/header.html', 'themeFooter' : C.getThemePath() + '/Public/footer.html' } return render(request, C.getThemePath() + 'detail.html', context)
def comment_post(request): if request.method == 'POST': userId = 0 aid = int(request.POST.get('aid', 0)) upArticle = Article.objects.get(article_id=aid) form = CommentForm(request.POST) if form.is_valid(): if C.isset(request.session.get('uInfo')): userId = request.session['uInfo']['user_id'] status = Comment.objects.create( article_id = aid, status = 'waiting' user_id = int(userId), ip = getClientIp(request), pid = int(request.POST.get('pid', 0)), agent = request.META.get('HTTP_USER_AGENT', ''), avatar = cgi.escape(request.POST.get('avatar', '')), comment = cgi.escape(request.POST.get('comment', '')), nickname = cgi.escape(request.POST.get('nickname', '')) ) if status: upArticle.comment_count = upArticle.comment_count+1 upArticle.save()
def index(request): url = request.get_host() cid = request.GET.get('cid') if C.isset(cid) == 0: cid = 0 navList = C.getNavList() if request.method == 'POST': articleList = Article.objects.filter(title__icontains=cgi.escape(request.POST.get('word'))).order_by('-article_id') elif cid == 0: articleList = Article.objects.all().order_by('-article_id') else: sql = 'SELECT * FROM "' + Meta.db_table + '_article" AS article LEFT JOIN "' + Meta.db_table sql += '_relation" AS relation ON article.article_id=relation.aid WHERE relation.cid=' + cgi.escape(cid) #sql += " AND article.title LIKE '%%" + request.POST.get('word') + "%%'" sql += ' ORDER BY article_id DESC' articleList = Article.objects.raw(sql) articleList = list(articleList) paginator = Paginator(articleList, 5) page = int(request.GET.get('page', 1)) try: pagebar = paginator.page(page) except PageNotAnInteger: pagebar = paginator.page(1) except EmptyPage: pagebar = paginator.page(paginator.num_pages) categoryList = C.getCategoryList() userInfo = request.session.get('uInfo', '') contentDateList = Article.objects.order_by('created').values('created').distinct() context = { 'url' : url, 'pagebar' : pagebar, 'navList' : navList, 'userinfo' : userInfo, 'cid' : int(cid), 'articleList' : articleList, 'categoryList' : categoryList, 'webInfo' : C.getWebInfo(), 'contentDateList' : contentDateList, 'themeHeader' : C.getThemePath() + '/Public/header.html', 'themeFooter' : C.getThemePath() + '/Public/footer.html' } return render(request, C.getThemePath() + 'index.html', context)
def article_edit(request): if C.checkLoginAdmin(request.session.get('uInfo', False)) == False: return HttpResponseRedirect('/signin/') else: context = {} url = request.get_host() userInfo = request.session.get('uInfo', False) aid = cgi.escape(request.GET.get('aid', 0)) if request.method == 'POST': updateArticle = Article.objects.get(article_id=aid) createTime = cgi.escape(request.POST.get('create_date')) + ' ' + cgi.escape(request.POST.get('create_time')) updateArticle.create_time = createTime updateArticle.update_time = C.getCurrTime() updateArticle.author = int(userInfo['user_id']) updateArticle.article_pic = request.POST.get('article_pic', '') updateArticle.title = cgi.escape(request.POST.get('title', '')) updateArticle.content = cgi.escape(request.POST.get('content', '')) updateArticle.save() Relation.objects.filter(aid=aid).delete() categoryIdList = cgi.escape(request.REQUEST.getlist('category')) for item in categoryIdList: Relation.objects.create(aid=aid, cid=item) return HttpResponse('修改文章成功') detail = [] if C.isset(aid): detail = Article.objects.get(article_id=aid) detail.create_date = str(detail.create_time)[0:10] detail.create_time = str(detail.create_time)[10:16] detail.author = User.objects.get(user_id=detail.author).username detail.content = detail.content.replace('\t', '').replace('\n', '').replace(' ', '') categoryList = C.getCategoryList() activeCategory = Relation.objects.filter(aid=aid).all() attachmentList = Attachment.objects.all().order_by('-attrch_id') context = { 'detail' : detail, 'categoryList' : categoryList, 'activeCategory' : activeCategory, 'attachmentList' : attachmentList } return render(request, manageThemeDir + 'article_edit.html', context)