def comment_list(request): is_login = get_login_status(request) userid = request.session.get('user_id', None) user = User.objects.get(id=userid) my_posts = Post.objects.filter(author=user) my_comments = Comment.objects.filter(user=user) my_posts_comments = [] for post in my_posts: comments = Comment.objects.filter(post=post) for comment in comments: comment.content = striptags(comment.content) if len(comment.content) > 30: comment.content = comment.content[0:30] + "..." my_posts_comments.extend(comments) my_comments_comments = [] for my_comment in my_comments: comments = Comment.objects.filter(reply_to_comment=my_comment) for comment in comments: comment.content = striptags(comment.content) if len(comment.content) > 30: comment.content = comment.content[0:30] + "..." my_comments_comments.extend(comments) context = { "posts_comments": my_posts_comments, "comments_comments": my_comments_comments, "userid": userid, "user": user, "is_login": is_login } return render(request, 'forum/Mention.html', context)
def post_safe_delete(request, id): id = int(id) is_login = get_login_status(request) if not is_login: return redirect('/index/', locals()) userid = request.session.get('user_id', None) user = User.objects.get(id=userid) post = Post.objects.get(id=id) post.delete() context = {'is_login': is_login, 'user': user} return redirect(reverse('space', kwargs={"id": str(userid)}), context)
def blackList(request, id): id = int(id) is_login = get_login_status(request) if is_login: userid = request.session.get('user_id', None) is_login, is_owner, space_owner, user, is_Following, is_Black, black_Owner = get_space_status( request, userid, id) blackLists = BlackList.objects.filter(BlockerID=id) else: return redirect('/index/', locals()) return render(request, 'space/BlackList.html', locals())
def comment_safe_delete(request, id): id = int(id) is_login = get_login_status(request) if not is_login: return redirect('/index/', locals()) userid = request.session.get('user_id', None) user = User.objects.get(id=userid) comment = Comment.objects.get(id=id) post_id = comment.post.id comment.delete() return redirect(reverse('PostContent', kwargs={"s": str(post_id)}), locals())
def myInfo(request, id): id = int(id) is_login = get_login_status(request) if is_login: userid = request.session.get('user_id', None) is_login, is_owner, space_owner, user, is_Following, is_Black, black_Owner = get_space_status( request, userid, id) if is_Black and not user.is_admin: return redirect(reverse('space', kwargs={"id": str(id)})) return render(request, "space/myInfo.html", locals()) # return redirect(reverse('settings', kwargs={'id': id}), locals()) return render(request, "forum/index.html", locals())
def letter(request, id): id = int(id) is_login = get_login_status(request) if is_login: userid = request.session.get('user_id', None) is_login, is_owner, space_owner, user, is_Following, is_Black, black_Owner = get_space_status( request, userid, id) if is_Black and not user.is_admin: return redirect(reverse('space', kwargs={"id": str(id)})) follows = Follow.objects.filter(FollowerID=id) else: return redirect('/index/', locals()) return render(request, 'space/Letter.html', locals())
def space(request, id): # 拉黑信息会跳出两次 id = int(id) is_login = get_login_status(request) if is_login: userid = request.session.get('user_id', None) is_login, is_owner, space_owner, user, is_Following, is_Black, black_Owner = get_space_status( request, userid, id) if not is_owner and not user.is_admin and not user.is_read: messages.success(request, "您没有看新手上路帖!") return redirect('/index/', locals()) # if is_Black: # messages.success(request, "您被对方拉黑了!") # return redirect(reverse('space', kwargs={"id": str(userid)})) # return redirect(reverse('space', kwargs={"id": str(userid)}), locals()) posts = Post.objects.filter(author=id) comments = Comment.objects.filter(user=id).order_by("-created") for post in posts: post.comment_count = len(Comment.objects.filter(post=post)) # print(post.get_absolute_url()) # Comment表里没有post的title属性,故在本地进行查询 # 发现可以直接使用comment.user访问User类,那就不需要下面的东西了 ''' comments_tmp = Comment.objects.filter(user=userid) comments = [] for i in comments_tmp: post = Post.objects.get(author=i.user, id=i.post) title = post.title author = user.name tag = post.tag dic = {'title': title, 'author': author, 'tag': tag} comments.append(dic) ''' # 限定显示30个字符 for i in posts: # print(i.comment_set.all().count) i.content = striptags(i.content) if len(i.content) > 30: i.content = i.content[0:30] + "..." for i in comments: # print(i.comment_set.all().count) i.content = striptags(i.content) if len(i.content) > 30: i.content = '{}...'.format(str(i.content)[0:29]) return render(request, "space/space.html", locals()) else: return redirect('/index/', locals())
def BlogList(request, id): id = int(id) is_login = get_login_status(request) if is_login: userid = request.session.get('user_id', None) is_login, is_owner, space_owner, user, is_Following, is_Black, black_Owner = get_space_status( request, userid, id) favoritePosts = FavoritePost.objects.filter(UserID=id) for it in favoritePosts: it.PostID.content = striptags(it.PostID.content) # print(i.content) if len(it.PostID.content) > 30: it.PostID.content = it.PostID.content[0:30] + "..." else: return redirect('/index/', locals()) return render(request, 'space/BlogList.html', locals())
def post_list(request): is_login = get_login_status(request) userid = request.session.get('user_id', None) user = User.objects.get(id=userid) search = request.GET.get('search') searchPost = request.GET.get('searchPost') # 用户搜索逻辑 print(search, searchPost) if search: if searchPost == 'true': # 用 Q对象 进行联合搜索 post_list = Post.objects.filter( Q(title__icontains=search) | Q(content__icontains=search)).order_by('-views') else: # 将 search 参数重置为空 user_list = User.objects.filter(Q(name__icontains=search)) else: print('hh') search = '' if searchPost: post_list = Post.objects.all().order_by('-views') else: user_list = User.objects.all() if searchPost == 'true': # print('in search post') context = { 'posts': post_list, 'search': search, 'user': user, 'is_login': True, 'userid': userid } return render(request, 'base/PostList.html', context) else: # print('in search user') # print(user_list) context = { 'users': user_list, 'search': search, 'user': user, 'is_login': True, 'userid': userid } return render(request, 'base/UserList.html', context)
def followUser(request): is_login = get_login_status(request) if not is_login: return redirect('/index/', locals()) userid = request.session.get('user_id', None) user = User.objects.get(id=userid) follows = Follow.objects.filter(FollowerID=userid) post_list = [] for follower in follows: posts = Post.objects.filter(author=follower.FollowedID) post_list.extend(posts) for post in post_list: post.content = striptags(post.content) if len(post.content) > 100: post.content = post.content[0:100] + "..." return render(request, 'forum/FollowUser.html', locals())
def followPost(request): is_login = get_login_status(request) if not is_login: return redirect('/index/', locals()) userid = request.session.get('user_id', None) user = User.objects.get(id=userid) favorites = FavoritePost.objects.filter(UserID=userid) post_list = [] for f in favorites: posts = Post.objects.filter(id=f.PostID.id) post_list.extend(posts) for post in post_list: post.content = striptags(post.content) if len(post.content) > 100: post.content = post.content[0:100] + "..." return render(request, 'forum/FollowPost.html', locals())
def post_update(request, id): is_login = get_login_status(request) if is_login: userid = request.session.get('user_id', None) user = User.objects.get(id=userid) else: return redirect('/index/', locals()) # 获取需要修改的具体文章对象 if user.is_ban: messages.success(request, "您已经被禁言,暂时不能修改帖子") # return redirect(reverse('space', args=str(user.id)), locals()) return redirect(reverse('PostContent', kwargs={"id": str(user.id)}), locals()) post = Post.objects.get(id=id) # 判断用户是否为 POST 提交表单数据 if request.method == "POST": # 将提交的数据赋值到表单实例中 update_form = UpdatePostForm(data=request.POST) # 判断提交的数据是否满足模型的要求 if update_form.is_valid(): # 保存新写入的 title、body 数据并保存 post.title = request.POST['title'] post.content = request.POST['content'] post.section = request.POST['section'] post.save() return redirect(reverse('PostContent', kwargs={"s": str(post.id)}), locals()) # 如果数据不合法,返回错误信息 else: return HttpResponse("表单内容有误,请重新填写。") # 如果用户 GET 请求获取数据 else: # 创建表单类实例 post_form = PostForm(initial={ 'title': post.title, 'content': post.content, 'section': post.section }) # 赋值上下文,将 article 文章对象也传递进去,以便提取旧的内容 context = {'post': post, 'post_form': post_form} # 将响应返回到模板中 return render(request, 'base/post_update.html', locals())
def make_post_top(request): is_login = get_login_status(request) if not is_login: return redirect('index', locals()) userid = request.session.get('user_id', None) user = User.objects.get(id=userid) if not user.is_admin: return redirect('index', locals()) data = {"isTop": False} post_id = request.GET.get("post_id", None) post = Post.objects.get(id=post_id) if post.is_top: post.is_top = False else: post.is_top = True data["isTop"] = True post.save() return JsonResponse(data)
def all_lists(request): is_login = get_login_status(request) if not is_login: return redirect('index', locals()) userid = request.session.get('user_id', None) user = User.objects.get(id=userid) if not user.is_admin: return redirect('index', locals()) post_list = Post.objects.all() user_list = User.objects.all() context = { "posts": post_list, "users": user_list, "userid": userid, "user": user, "is_login": is_login } return render(request, 'forum/AllList.html', context)
def post_create(request): is_login = get_login_status(request) if is_login: userid = request.session.get('user_id', None) user = User.objects.get(id=userid) else: return redirect('/index/', locals()) if user.is_ban: messages.success(request, "您已经被禁言,暂时不能发帖") return redirect('/index/', locals()) if request.method == "POST": # 将提交的数据赋值到表单实例中 post_form = PostForm(data=request.POST) # 判断提交的数据是否满足模型的要求 if post_form.is_valid(): # 保存数据,但暂时不提交到数据库中 new_post = Post() new_post.title = post_form.cleaned_data.get("title", None) new_post.content = post_form.cleaned_data.get("content", None) new_post.section = post_form.cleaned_data.get("section", None) new_post.level_restriction = post_form.cleaned_data.get( "level_restriction", None) # new_post = post_form.save(commit=False) # 指定数据库中 id=1 的用户为作者 # 如果你进行过删除数据表的操作,可能会找不到id=1的用户 # 此时请重新创建用户,并传入此用户的id new_post.author = user # 将新文章保存到数据库中 user.exp += CREATE_POST_EXP # 发文章加经验 user.save() new_post.save() # 完成后返回到文章列表 return redirect('/index/', locals()) # 如果数据不合法,返回错误信息 else: return HttpResponse("表单内容有误,请重新填写。") # 如果用户请求获取数据 else: # 创建表单类实例 article_post_form = PostForm() context = {'article_post_form': article_post_form} return render(request, 'base/post_create.html', locals())
def mention(request): is_login = get_login_status(request) if is_login: userid = request.session.get('user_id', None) user = User.objects.get(id=userid) posts = Post.objects.filter(author=userid) to_post = Comment.objects.filter( post__in=posts, reply_to_comment__isnull=True).order_by("-created") my_comment = Comment.objects.filter(user=user) to_comment = Comment.objects.filter( Q(reply_to_comment__in=my_comment, reply_to__isnull=True) | Q(reply_to=user)).order_by("-created") for comment in to_post: comment.content = striptags(comment.content) if len(comment.content) > 30: comment.content = comment.content[0:30] + '...' for comment in to_comment: comment.content = striptags(comment.content) if len(comment.content) > 30: comment.content = comment.content[0:30] + '...' # Comment表里没有post的title属性,故在本地进行查询 # 发现可以直接使用comment.user访问User类,那就不需要下面的东西了 ''' comments_tmp = Comment.objects.filter(user=userid) comments = [] for i in comments_tmp: post = Post.objects.get(author=i.user, id=i.post) title = post.title author = user.name tag = post.tag dic = {'title': title, 'author': author, 'tag': tag} comments.append(dic) ''' # 限定显示100个字符 for i in posts: if len(i.content) > 100: i.content = i.content[0:100] + "..." return render(request, "forum/Mention.html", locals()) else: return redirect('/index/', locals())
def get_space_status(request, userid, ownerid): is_login = get_login_status(request) is_owner = (str(userid) == str(ownerid)) space_owner = User.objects.get(id=ownerid) user = User.objects.get(id=userid) try: BlackList.objects.get(BlockerID=ownerid, BlockedID=userid) is_Black = True except: is_Black = False try: BlackList.objects.get(BlockerID=userid, BlockedID=ownerid) black_Owner = True except: black_Owner = False try: Follow.objects.get(FollowerID=userid, FollowedID=ownerid) is_Following = True except: is_Following = False return is_login, is_owner, space_owner, user, is_Following, is_Black, black_Owner
def all_lists(request, id): id = int(id) is_login = get_login_status(request) if not is_login: return redirect('index', locals()) userid = request.session.get('user_id', None) user = User.objects.get(id=userid) is_login, is_owner, space_owner, user, is_Following, is_Black, black_Owner = get_space_status( request, userid, id) if not user.is_admin: return redirect('index', locals()) posts = Post.objects.all() for post in posts: post.content = striptags(post.content) # print(i.content) if len(post.content) > 100: post.content = post.content[0:100] + "..." users = User.objects.all() #context = {"posts": post_list, "users": user_list, "userid": userid, # "user": user, "is_login": is_login} return render(request, 'forum/AllList.html', locals())
def index(request): is_login = get_login_status(request) if is_login: userid = request.session.get('user_id', None) user = User.objects.get(id=userid) # 限定显示30个字符 top_posts = Post.objects.filter(is_top=True) hot_posts = Post.objects.filter(is_top=False).order_by("-views")[0:5] for i in hot_posts: i.content = striptags(i.content) # print(i.content) if len(i.content) > 30: i.content = i.content[0:30] + "..." for i in hot_posts: i.content = striptags(i.content) # print(i.content) if len(i.content) > 100: i.content = i.content[0:100] + "..." return render(request, "forum/index.html", locals())
def forumBoard(request, id): if id not in (1, 2, 3, 4, 5): return HttpResponse("不存在这个板块") hot_posts = Post.objects.filter(section=str(id)).order_by("-views")[0:3] normal_posts = Post.objects.filter(section=str(id)).order_by("-last_edit") is_login = get_login_status(request) if is_login: userid = request.session.get('user_id', None) user = User.objects.get(id=userid) for i in hot_posts: i.content = striptags(i.content) # print(i.content) if len(i.content) > 30: i.content = i.content[0:30] + "..." for i in normal_posts: i.content = striptags(i.content) # print(i.content) if len(i.content) > 100: i.content = i.content[0:100] + "..." return render(request, 'forum/ForumBoard.html', locals())
def index_old(request): is_login = get_login_status(request) return render(request, 'forum/index.html', locals())
def settings(request, id): # 定制化提示信息, id = int(id) is_login = get_login_status(request) if is_login: userid = request.session.get('user_id', None) is_login, is_owner, space_owner, user, is_Following, is_Black, black_Owner = get_space_status( request, userid, id) if request.method == "POST": flag = 0 if is_owner and user.is_admin: flag = -1 form = userInfo_all(request.POST, request.FILES, instance=space_owner) elif is_owner: flag = 0 form = userInfo_user(request.POST, request.FILES, instance=space_owner) elif not is_owner and user.is_admin: flag = 1 # 因为这里没有name,特殊处理 form = userInfo_admin(request.POST, request.FILES, instance=space_owner) else: return redirect(reverse('space', kwargs={"id": str(id)})) now_name = space_owner.name # 如果全部输入信息有效 if form.is_valid() and flag != 1: value = form.cleaned_data['name'] # print(space_owner.name) # print(value) if User.objects.filter(name=value) and now_name != value: space_owner.name = now_name messages.success(request, "用户名已存在") return redirect(reverse('space', kwargs={'id': id}), locals()) print(1) form.save(commit=True) messages.success(request, "修改成功") return redirect(reverse('space', kwargs={'id': id}), locals()) elif form.is_valid(): # 无name字段 form.save(commit=True) messages.success(request, "修改成功") return redirect(reverse('space', kwargs={'id': id}), locals()) else: if flag == -1: messages.success( request, "修改失败!注意年龄/经验值不得小于0, 密码长度需要在4~16之间,使用合法邮箱格式") elif flag == 0: messages.success(request, "修改失败!注意年龄不得小于0,密码长度需要在4~16之间,使用合法邮箱格式") else: messages.success(request, "修改失败!经验值不得小于0") print("error") # 失败 # 打印输入的信息 print("---", form.cleaned_data) # 得到一个字典 print("???", form.errors) # ErrorDict : {"校验错误的字段":["错误信息",]} print("!!!", form.errors.get("email")) # ErrorList ["错误信息",] g_error = form.errors.get("__all__") print("+++", g_error ) # <ul class="errorlist nonfield"><li>两次密码不一致</li></ul> return redirect(reverse('space', args=str(id)), locals()) else: if is_owner and user.is_admin: form = userInfo_all(instance=space_owner) elif is_owner: form = userInfo_user(instance=space_owner) elif not is_owner and user.is_admin: form = userInfo_admin(instance=space_owner) # # form = userInfo(instance=space_owner) # form = userInfo_all(initial={'name': space_owner.name, 'sex': space_owner.sex, 'age': space_owner.age, # 'school': space_owner.school, 'major': space_owner.major, # 'exp': space_owner.exp, 'email': space_owner.email, 'avatar': space_owner.avatar}, # instance=space_owner) # # 访问者是自己,除了经验值都能改 # if is_owner: # form.fields['exp'].widget.attrs['readonly'] = True # # form.fields['exp'].disabled = True # # 访问者不是自己但是是管理员,只能改经验值 # elif user.is_admin: # for i in form.fields.values(): # i.widget.attrs['readonly'] = True # # i.disabled = True # # i.required = False # form.fields['exp'].disabled = False # # 什么也不能改 # else: # for i in form.fields.values(): # i.widget.attrs['readonly'] = True # # i.disabled = True # # i.required = False # form.fields['avatar'].widget.attrs['disabled'] = True # form.fields['sex'].widget.attrs['disabled'] = True # # form = UserInfo(request.POST) return render(request, "space/settings.html", locals()) return render(request, "space/settings.html", locals())
def PostContent(request, s): is_login = get_login_status(request) if is_login: userid = request.session.get('user_id', None) user = User.objects.get(id=userid) else: return redirect('/index/', locals()) comment_form = CommentForm(request.POST) ls = s.split("&") if request.method == 'GET': if len(ls) > 1: return redirect("/index/", locals()) post = Post.objects.get(id=int(ls[0])) if post.id == MUST_READ_POST_ID and not user.is_read: user.is_read = True user.save() if not user.is_read and not user.is_admin: messages.success(request, "您没有看新手上路帖!") return redirect('/index/', locals()) if int(post.level_restriction) > user.level and not user.is_admin: messages.success(request, "您等级不够,无法查看此帖!") return redirect("/index/", locals()) comments = Comment.objects.filter(post=int(ls[0])) post.views += 1 post.save() try: UpAndDown.objects.get(user=user, post=post) is_thumbed_post = True except: is_thumbed_post = False # 将markdown语法渲染成html样式 comments_lv1 = [] for comment in comments: if not comment.reply_to_comment_id: # print(comment.reply_to_id) try: UpAndDown.objects.get(user=user, comment=comment) comments_lv1.append([comment, [], True]) except: comments_lv1.append([comment, [], False]) for comment_list in comments_lv1: for comment in comments: if comment.reply_to_comment_id == comment_list[0].id: try: UpAndDown.objects.get(user=user, comment=comment) comment_list[1].append([comment, True]) except: comment_list[1].append([comment, False]) # print(comments_lv1) # print(1) is_owner = (str(userid) == str(post.author.id)) try: FavoritePost.objects.get(UserID=user, PostID=post) print(1) is_favourite = True except: is_favourite = False return render(request, 'forum/PostContent.html', locals()) elif request.method == 'POST': post = Post.objects.get(id=int(ls[0])) if user.is_ban: messages.success(request, "您已被禁言,暂时不能回复") return redirect(reverse('PostContent', kwargs={"s": str(post.id)}), locals()) # 当调用 form.is_valid() 方法时,Django 自动帮我们检查表单的数据是否符合格式要求。 if comment_form.is_valid(): # commit=False 的作用是仅仅利用表单的数据生成 Comment 模型类的实例,但还不保存评论数据到数据库。 new_comment = comment_form.save(commit=False) # 将评论和被评论的文章关联起来。 new_comment.post = post new_comment.user = user if ls[1] != '0': new_comment.reply_to_id = int(ls[1]) if ls[2] != '0': new_comment.reply_to_comment_id = int(ls[2]) # 最终将评论数据保存进数据库,调用模型实例的 save 方法 user.exp += COMMENT_EXP # 发评论加经验 user.save() new_comment.save() post.last_edit = new_comment.created post.save() return redirect(reverse('PostContent', kwargs={"s": str(post.id)}), locals()) else: return HttpResponse("表单内容有误,请重新填写。")