def get(self, request, pk, mode): print(pk, mode) if mode == 'add': # pk값은 상관없음. form = forms.PostForm() return render(request, 'blog/edit.html', {'form': form}) elif mode == 'list': # pk값 상관없음. username = request.session.get('username', '') user = User.objects.get(username=username) data = models.Post.objects.all().filter(author=user) context = {'data': data, 'username': username} return render(request, 'blog/list.html', context) elif mode == 'detail': p = get_object_or_404(Post, pk=pk) form = forms.PostForm(instance=p) return render(request, 'blog/detail.html', {'form': form}) elif mode == 'edit': post = get_object_or_404(models.Post, pk=pk) form = forms.PostForm(instance=post) elif mode == 'delete': post = get_object_or_404(models.Post, pk=pk) post.delete() return redirect('/0/list') else: return HttpResponse('mode를 잘 입력하세용') return render(request, "blog/edit.html", {"form": form})
def get(self, request, pk): if pk == 0 : form = forms.PostForm() else : post = get_object_or_404(models.Post, pk=pk) form = forms.PostForm(instance=post) return render(request, "blog/edit.html", {"form":form})
def post2db(request): if request.method == 'POST': post_form = forms.PostForm(request.POST) if post_form.is_valid(): message = "您的訊息已儲存,要等管理者啟用後才看得到喔。" post_form.save() else: message = '如要張貼訊息,則每一個欄位都要填...' else: post_form = forms.PostForm() message = '如要張貼訊息,則每一個欄位都要填...' return render(request, 'post2db.html', locals())
def get(self, request, pk, mode): if mode == 'add': form = forms.PostForm() elif mode == 'list': username = request.session["username"] user = User.objects.get(username=username) data = models.Post.objects.all().filter(author=user) context = {"data": data, "username": username} return render(request, "blog/list.html", context) elif mode == 'detail': p = get_object_or_404(models.Post, pk=pk) return render(request, "blog/detail.html", {"d": p}) elif mode == "edit": post = get_object_or_404(models.Post, pk=pk) form = forms.PostForm(instance=post) else: post = get_object_or_404(models.Post, pk=pk) form = forms.PostForm(instance=post) return render(request, "blog/edit.html", {"form": form})
def post(self, request, pk): username = request.session["username"] user = User.objects.get(username=username) if pk == 0: form = forms.PostForm(request.POST) else: post = get_object_or_404(Post, pk=pk) form = forms.PostForm(request.POST, instance=post) if form.is_valid(): post = form.save(commit=False) if pk == 0: post.author = user post.save() else : post.publish() return redirect("list") return render(request, "blog/edit.html", {"form": form})
def post_new(request): if request.method == "POST": form = forms.PostForm(request.POST) if form.is_valid(): post = form.save(commit=False) post.author = request.user post.published_date = timezone.now() post.save() return redirect("post_detail", pk=post.pk) else: form = PostForm() return render(request, "blog/post_edit.html", {"form": form})
def post(self, request, pk, mode): username = request.session["username"] user = User.objects.get(username=username) if pk == 0: form = forms.PostForm(request.POST) else: post = get_object_or_404(models.Post, pk=pk) form = forms.PostForm(request.POST, instance=post) #모듈화를 하면, 간단해지지만, 처음 보는 사람이 쓰기가 어렵다. if form.is_valid(): #세이브를 하기 위해 세이브를 호출한것이 아니라, form객체로부터 model 데이터를 얻기 위해서이다. post = form.save(commit=False) if pk == 0: post.author = user post.save() else: post.publish() return redirect("list") return render(request, "blog/edit.html", {"form": form})
def feed_detail(request): if request.session.get('id', False) is False: return redirect('login') if request.session['id'] > -1 and request.session['type'] == "Player": player_in_session_name = request.session['username'] player_in_session_id = request.session['id'] if request.method == 'POST': print("In Post Form") postform = forms.PostForm(request.POST) print(request.POST['post']) print(postform.errors) if postform.is_valid(): print("Post form is valid") text = postform.cleaned_data['post'] topics = request.POST.getlist('Topics', False) img = request.FILES.get('post_img', False) print(text, topics, img) insert_post(text, topics, img, player_in_session_id) return redirect('feed_detail') else: print("Post form is not valid") return redirect('feed_detail') with connection.cursor() as cursor: query = ''' SELECT USERNAME, IMAGE, FULLNAME FROM USERS WHERE USER_ID = %s ''' cursor.execute(query, [player_in_session_id]) player_info = cursor.fetchone() query = ''' SELECT COUNT(FOLLOWER_ID) FROM PLAYER_FOLLOW WHERE FOLLOWEE_ID = %s ''' cursor.execute(query, [player_in_session_id]) followers = cursor.fetchone() query = ''' SELECT COUNT(FOLLOWEE_ID) FROM PLAYER_FOLLOW WHERE FOLLOWER_ID = %s ''' cursor.execute(query, [player_in_session_id]) followees = cursor.fetchone() query = ''' SELECT T.NAME, T.LOGO, T.TOPIC_ID FROM TOPIC T, TOPIC_FOLLOW TF WHERE TF.FOLLOWER_ID = %s AND T.TOPIC_ID = TF.TOPIC_ID ''' cursor.execute(query, [player_in_session_id]) followed_topics = cursor.fetchall() cursor.execute('SELECT * FROM TOPIC') topics = cursor.fetchall() query = ''' SELECT U.USERNAME, U.IMAGE, U.USER_ID FROM PLAYER_FOLLOW PF, USERS U WHERE PF.FOLLOWER_ID = U.USER_ID AND PF.FOLLOWEE_ID = %s ''' cursor.execute(query, [player_in_session_id]) follower_info = cursor.fetchall() if followers[0] == 0: follower_info = [("-", "-")] query = ''' SELECT U.USERNAME, U.IMAGE, U.USER_ID FROM PLAYER_FOLLOW PF, USERS U WHERE PF.FOLLOWEE_ID = U.USER_ID AND PF.FOLLOWER_ID = %s ''' cursor.execute(query, [player_in_session_id]) followee_info = cursor.fetchall() if followees[0] == 0: followee_info = [("-", "-")] query = ''' SELECT U.USERNAME,U.IMAGE ,P.POST_ID, P.WRITER_ID,P.DESCRIPTION, P.IMAGE, P.TIME, COUNT(DISTINCT L.PLAYER_ID) AS LIKES, COUNT(DISTINCT C.COMMENT_ID) AS COMMENTS, GET_TAG(P.POST_ID) FROM POST P, USERS U, LIKES L, COMMENTS C WHERE P.WRITER_ID IN (SELECT FOLLOWEE_ID FROM PLAYER_FOLLOW WHERE FOLLOWER_ID = %s) AND P.WRITER_ID = U.USER_ID AND P.POST_ID = L.POST_ID(+) AND P.POST_ID = C.POST_ID(+) GROUP BY U.USERNAME,U.IMAGE ,P.POST_ID, P.WRITER_ID,P.DESCRIPTION, P.IMAGE, P.TIME UNION SELECT U.USERNAME,U.IMAGE ,P.POST_ID, P.WRITER_ID,P.DESCRIPTION, P.IMAGE, P.TIME, COUNT(DISTINCT L.PLAYER_ID) AS LIKES, COUNT(DISTINCT C.COMMENT_ID) AS COMMENTS, GET_TAG(P.POST_ID) FROM POST P, USERS U, LIKES L, COMMENTS C WHERE P.POST_ID IN ( SELECT DISTINCT (T.POST_ID) FROM TAG T WHERE T.TOPIC_ID IN (SELECT TF.TOPIC_ID FROM TOPIC_FOLLOW TF WHERE FOLLOWER_ID = %s) ) AND P.WRITER_ID = U.USER_ID AND P.POST_ID = L.POST_ID(+) AND P.POST_ID = C.POST_ID(+) GROUP BY U.USERNAME,U.IMAGE ,P.POST_ID, P.WRITER_ID,P.DESCRIPTION, P.IMAGE, P.TIME UNION SELECT U.USERNAME,U.IMAGE ,P.POST_ID, P.WRITER_ID,P.DESCRIPTION, P.IMAGE, P.TIME, COUNT(DISTINCT L.PLAYER_ID) AS LIKES, COUNT(DISTINCT C.COMMENT_ID) AS COMMENTS, GET_TAG(P.POST_ID) FROM POST P, USERS U, LIKES L, COMMENTS C WHERE P.WRITER_ID = %s AND P.WRITER_ID = U.USER_ID AND P.POST_ID = L.POST_ID(+) AND P.POST_ID = C.POST_ID(+) GROUP BY U.USERNAME,U.IMAGE ,P.POST_ID, P.WRITER_ID,P.DESCRIPTION, P.IMAGE, P.TIME ORDER BY TIME DESC ''' cursor.execute(query, [player_in_session_id, player_in_session_id, player_in_session_id]) all_posts = cursor.fetchall() query = ''' SELECT P.POST_ID FROM POST P WHERE P.WRITER_ID IN (SELECT FOLLOWEE_ID FROM PLAYER_FOLLOW WHERE FOLLOWER_ID = %s) /*PLAYER_ID*/ AND EXISTS ( SELECT L.PLAYER_ID FROM LIKES L WHERE L.POST_ID = P.POST_ID AND L.PLAYER_ID = %s /*USER IN SESSION*/ ) UNION SELECT P.POST_ID FROM POST P WHERE P.POST_ID IN ( SELECT DISTINCT (T.POST_ID) FROM TAG T WHERE T.TOPIC_ID IN (SELECT TF.TOPIC_ID FROM TOPIC_FOLLOW TF WHERE FOLLOWER_ID = %s) ) AND EXISTS ( SELECT L.PLAYER_ID FROM LIKES L WHERE L.POST_ID = P.POST_ID AND L.PLAYER_ID = %s /*USER IN SESSION*/ ) UNION SELECT P.POST_ID FROM POST P WHERE P.WRITER_ID = %s AND EXISTS ( SELECT L.PLAYER_ID FROM LIKES L WHERE L.POST_ID = P.POST_ID AND L.PLAYER_ID = %s /*USER IN SESSION*/ ) ''' cursor.execute(query, [player_in_session_id, player_in_session_id, player_in_session_id, player_in_session_id, player_in_session_id, player_in_session_id]) liked_posts = cursor.fetchall() # change list of tuples (all_post) to list of lists all_posts_list = [list(elem) for elem in all_posts] for post in all_posts_list: post[6] = time_edit(post[6]) if post[9] is not None: post[9] = list(post[9].split(" ")) post[9].pop(0) if post[2] in (like[0] for like in liked_posts): # print(post[2]) post.append("Liked") else: post.append("Like") # print(post) query = ''' SELECT * FROM ( SELECT U.USERNAME, P.RANK, U.IMAGE FROM PLAYER P, USERS U WHERE P.PLAYER_ID = U.USER_ID ORDER BY P.RANK DESC) LB WHERE ROWNUM <=5 ''' cursor.execute(query) leaderboard = cursor.fetchall() cursor.close() return render(request, 'feed/feed.html', {'player_info': player_info, 'followers': followers[0], 'followees': followees[0], 'follower_info': follower_info, 'followee_info': followee_info, 'followed_topics': followed_topics, 'topics': topics, 'all_posts_list': all_posts_list, 'leaderboard': leaderboard, }) else: return redirect('login')