def clear_caches_view(request): """ 清除全部缓存 """ try: cache.delete_pattern("tmp_*") return http_response(request, statuscode=ERRORCODE.SUCCESS) except Exception as exp: return http_response(request, statuscode=ERRORCODE.FAILED, msg=exp)
def get_comments_view(request): """ 获取评论列表 """ form = GetCommentsForm(request.GET) if not form.is_valid(): return http_response(request, statuscode=ERRORCODE.PARAM_ERROR) page_num = form.cleaned_data.get('page_num') or 1 page_size = form.cleaned_data.get('page_size') or 10 target = form.cleaned_data.get('target') md = markdown.Markdown(extensions=[ 'markdown.extensions.extra', 'markdown.extensions.codehilite', 'markdown.extensions.toc', ]) comment_list = [] comments = get_cache_comments(target) comments, total = paginate(comments, page_num, page_size) for item in comments: comment_list.append({ "comment_id": item.id, "anchor": item.anchor, "comment": md.convert(item.content), "avatar": item.user.avatar, "website": item.user.website, "nickname": item.user.nickname, "blogger": True if item.user.blogger else False, "created_time": item.created_time.strftime("%Y-%m-%d %H:%M"), "reply_to": True if item.reply_to else False, "reply_to_user": item.reply_to.nickname if item.reply_to else '', "parent_anchor": item.parent.anchor if item.parent else '', "parent_comment": md.convert(item.parent.content) if item.parent else '' }) return http_response(request, statuscode=ERRORCODE.SUCCESS, context={ "comment_list": comment_list, "total": total, "page_num": page_num, "page_size": page_size, })
def del_ownmessage_view(request): """ 删除主人寄语 """ item_ids = request.POST.getlist('item_ids') if not item_ids: return http_response(request, statuscode=ERRORCODE.PARAM_ERROR, msg='参数错误') try: OwnerMessage.objects.filter(id__in=item_ids).delete() return http_response(request, statuscode=ERRORCODE.SUCCESS) except Exception as e: return http_response(request, statuscode=ERRORCODE.FAILED, msg='删除失败: %s' % e)
def del_ownmessage_view(request): """ 删除主人寄语 """ item_ids = request.POST.getlist('item_ids') if not item_ids: return http_response(request, statuscode=ERRORCODE.PARAM_ERROR, msg=u'参数错误') try: OwnerMessage.objects.filter(id__in=item_ids).delete() return http_response(request, statuscode=ERRORCODE.SUCCESS) except Exception as e: return http_response(request, statuscode=ERRORCODE.FAILED, msg=u'删除失败: %s' % e)
def del_classification_view(request): """ 删除文章分类 """ item_ids = request.POST.getlist('item_ids') if not item_ids: return http_response(request, statuscode=ERRORCODE.PARAM_ERROR, msg=u'参数错误') try: Classification.objects.filter(id__in=item_ids).delete() return http_response(request, statuscode=ERRORCODE.SUCCESS) except Exception as e: return http_response(request, statuscode=ERRORCODE.FAILED, msg=u'删除失败: %s' % e)
def del_author_view(request): """ 删除作者 """ item_ids = request.POST.getlist('item_ids') if not item_ids: return http_response(request, statuscode=ERRORCODE.PARAM_ERROR, msg=u'参数错误') try: Author.objects.filter(id__in=item_ids).delete() return http_response(request, statuscode=ERRORCODE.SUCCESS) except Exception as e: return http_response(request, statuscode=ERRORCODE.FAILED, msg=u'删除失败: %s' % e)
def del_music_view(request): """ 删除背景音乐 """ item_ids = request.POST.getlist('item_ids') if not item_ids: return http_response(request, statuscode=ERRORCODE.PARAM_ERROR, msg='参数错误') try: Music.objects.filter(id__in=item_ids).delete() cache.delete_pattern('tmp_musics') # 清除缓存 return http_response(request, statuscode=ERRORCODE.SUCCESS) except Exception as e: return http_response(request, statuscode=ERRORCODE.FAILED, msg='删除失败: %s' % e)
def blog_del_view(request): """ 删除博客 """ item_ids = request.POST.getlist('item_ids') if not item_ids: return http_response(request, statuscode=ERRORCODE.PARAM_ERROR, msg='参数错误') try: Article.objects.filter(id__in=item_ids).delete() cache.delete_pattern("tmp_articles") # 清除缓存 cache.delete_pattern("tmp_archive") # 清除缓存 return http_response(request, statuscode=ERRORCODE.SUCCESS) except Exception as e: return http_response(request, statuscode=ERRORCODE.FAILED, msg='删除失败: %s' % e)
def delete_comment_view(request): item_ids = request.POST.getlist('item_ids') if not item_ids: return http_response(request, statuscode=ERRORCODE.PARAM_ERROR, msg=u'参数错误') try: Comments.objects.filter(id__in=item_ids).delete() cache.delete_pattern('/*') # 清除缓存 return http_response(request, statuscode=ERRORCODE.SUCCESS) except Exception as e: return http_response(request, statuscode=ERRORCODE.FAILED, msg=u'删除失败: %s' % e)
def del_carousel_view(request): """ 删除轮播图片 """ item_ids = request.POST.getlist('item_ids') if not item_ids: return http_response(request, statuscode=ERRORCODE.PARAM_ERROR, msg='参数错误') try: CarouselImg.objects.filter(id__in=item_ids).delete() cache.delete_pattern('tmp_carouse_imgs') # 清除缓存 cache.delete_pattern('tmp_ads_imgs') return http_response(request, statuscode=ERRORCODE.SUCCESS) except Exception as e: return http_response(request, statuscode=ERRORCODE.FAILED, msg='删除失败: %s' % e)
def blog_list(request): form = BlogListForm(request.GET) form.is_valid() query = Q(status=BlogStatus.PUBLISHED) title = form.cleaned_data.get("title") page_num = form.cleaned_data.get('page_num') page_size = form.cleaned_data.get('page_size') if title: query &= (Q(title__icontains=title) | Q(classification__name=title) | Q(tags__name=title)) blogs = Article.objects.select_related().filter(query).order_by("-id") blogs, total = paginate(blogs, page_num, page_size) blog_list = [] for blog in blogs: blog_list.append({ "blog_id": blog.id, "title": blog.title, "author": blog.author.name, "classification": blog.classification.name, "content": blog.content, "publish_time": blog.publish_time.strftime('%Y-%m-%d'), "count": blog.count }) return http_response(request, statuscode=ERRORCODE.SUCCESS, context={ "blog_list": blog_list, "total": total, "page_num": page_num, "page_size": page_size, })
def blog_update_status_view(request): """ 发布或撤回博客 """ form = UpdateBlogStatusForm(request.POST) if not form.is_valid(): return http_response(request, statuscode=ERRORCODE.PARAM_ERROR) blog_id = form.cleaned_data.get('blog_id') status = form.cleaned_data.get('status') try: if Article.objects.filter(id=blog_id).update(status=status) <= 0: return http_response(request, statuscode=ERRORCODE.NOT_FOUND) cache.delete_pattern("tmp_articles") # 清除缓存 cache.delete_pattern("tmp_archive") # 清除缓存 return http_response(request, statuscode=ERRORCODE.SUCCESS) except Exception as e: return http_response(request, statuscode=ERRORCODE.FAILED, msg='失败: %s' % e)
def get_banners(request): banners = CarouselImg.objects.order_by('id') banner_list = [] for item in banners: banner_list.append({"img_url": item.path, "link_url": item.link}) context = {"banner_list": banner_list} return http_response(request, statuscode=ERRORCODE.SUCCESS, context=context)
def get_banners(request): banners = CarouselImg.objects.order_by('id') banner_list = [] for item in banners: banner_list.append({ "img_url": item.path, "link_url": item.link }) context = { "banner_list": banner_list } return http_response(request, statuscode=ERRORCODE.SUCCESS, context=context)
def get_comments_view(request): form = GetCommentsForm(request.GET) if not form.is_valid(): return http_response(request, statuscode=ERRORCODE.PARAM_ERROR) page_num = form.cleaned_data.get('page_num') or 1 page_size = form.cleaned_data.get('page_size') or 10 target = form.cleaned_data.get('target') comment_list = [] comments = Comments.objects.select_related().filter(target=target).order_by('-id') comments, total = paginate(comments, page_num, page_size) for item in comments: comment_list.append({ "anchor": item.anchor }) return http_response(request, statuscode=ERRORCODE.SUCCESS, context={ "comments": comment_list, "total": total, "page_num": page_num, "page_size": page_size, })
def add_comments_view(request): """ 添加评论 """ form = CommentForm(request.POST) if not form.is_valid(): return http_response(request, statuscode=ERRORCODE.PARAM_ERROR) nickname = form.cleaned_data.get('nickname') email = form.cleaned_data.get('email') website = form.cleaned_data.get('website') content = form.cleaned_data.get('content') target = form.cleaned_data.get('target') parent_comment_id = form.cleaned_data.get('parent_comment_id') try: user, created = Visitor.objects.update_or_create( nickname=nickname, email=email, defaults={ "nickname": nickname, "email": email, "website": website, "avatar": gravatar_url(email) }) ip_address = get_clientip(request) country, province, city = get_location_by_ip(ip_address) anchor = "".join([ random.choice("abcdefghijklmnopqrstuvwxyz1234567890") for i in xrange(16) ]) comment_data = { "user_id": user.id, "content": content, "target": target, "ip_address": ip_address, "country": country, "province": province, "city": city, "anchor": anchor, } # 二级回复 if parent_comment_id: parent_comment = Comments.objects.select_related().filter( pk=parent_comment_id).first() reply_to = parent_comment.user if parent_comment else None comment_data.update({ "parent_id": parent_comment_id, "reply_to": reply_to }) mail_body = MailTemplate.notify_parent_user.format( parent_user=parent_comment.user.nickname, parent_comment=parent_comment.content, target_url=DOMAIN_NAME + parent_comment.target, anchor='#' + parent_comment.anchor) send_email_task.delay(reply_to.email, mail_body) Comments.objects.create(**comment_data) messages.success(request, u'评论成功') cache.delete_pattern(target) # 清除缓存 if not parent_comment_id and not user.blogger: mail_body = MailTemplate.notify_blogger.format( nickname=nickname, comment=content, target_url=DOMAIN_NAME + target, anchor='#' + anchor) send_email_task.delay(BLOGGER_EMAIL, mail_body) return http_response(request, statuscode=ERRORCODE.SUCCESS) except Exception as exp: return http_response(request, statuscode=ERRORCODE.FAILED, msg=exp)
def clear_caches_view(request): try: cache.delete_pattern("tmp_*") return http_response(request, statuscode=ERRORCODE.SUCCESS) except Exception as exp: return http_response(request, statuscode=ERRORCODE.FAILED, msg=exp)