-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.txt
253 lines (229 loc) · 8.66 KB
/
views.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
from django.shortcuts import render, get_object_or_404, redirect
from django.utils import timezone
from django.core.paginator import Paginator
from django.contrib import messages
from .forms import QuestionForm, AnswerForm, CommentForm
from .models import Question, Answer, Comment
from django.contrib.auth.decorators import login_required
def index(request):
"""
pybo 목록 출력
"""
#입력 파라메터
page = request.GET.get('page', '1')
#조회
question_list = Question.objects.order_by('-create_date')
#페이징 처리
paginator = Paginator(question_list,10)
page_obj = paginator.get_page(page)
context = {'question_list': page_obj}
return render(request, 'pybo/question_list.html', context)
def detail(request, question_id):
"""
pybo 내용 출력
"""
question = get_object_or_404(Question, pk=question_id)
context = {'question': question}
return render(request, 'pybo/question_detail.html', context)
@login_required(login_url='common:login')
def question_create(request):
"""
pybo 질문 등록
"""
if request.method == 'POST':
form = QuestionForm(request.POST)
if form.is_valid():
question = form.save(commit=False)
question.author = request.user
question.create_date = timezone.now()
question.save()
return redirect('pybo:index')
else:
form = QuestionForm()
context = {'form':form}
return render(request, 'pybo/question_form.html', context)
@login_required(login_url='common:login')
def question_modify(request, question_id):
"""
pybo 질문수정
"""
question = get_object_or_404(Question, pk=question_id)
if request.user != question.author:
messages.error(request, '수정권한이 없습니다')
return redirect('pybo:detail', question_id=question.id)
if request.method == "POST":
form = QuestionForm(request.POST, instance=question)
if form.is_valid():
question = form.save(commit=False)
question.modify_date = timezone.now() # 수정일시 저장
question.save()
return redirect('pybo:detail', question_id=question.id)
else:
form = QuestionForm(instance=question)
context = {'form': form}
return render(request, 'pybo/question_form.html', context)
@login_required(login_url='common:login')
def question_delete(request, question_id):
"""
pybo 질문삭제
"""
question = get_object_or_404(Question, pk=question_id)
if request.user != question.author:
messages.error(request, '삭제권한이 없습니다')
return redirect('pybo:detail', question_id=question.id)
question.delete()
return redirect('pybo:index')
@login_required(login_url='common:login')
def answer_modify(request, answer_id):
"""
pybo 답변수정
"""
answer = get_object_or_404(Answer, pk=answer_id)
if request.user != answer.author:
messages.error(request, '수정권한이 없습니다')
return redirect('pybo:detail', question_id=answer.question.id)
if request.method == "POST":
form = AnswerForm(request.POST, instance=answer)
if form.is_valid():
answer = form.save(commit=False)
answer.modify_date = timezone.now()
answer.save()
return redirect('pybo:detail', question_id=answer.question.id)
else:
form = AnswerForm(instance=answer)
context = {'answer': answer, 'form': form}
return render(request, 'pybo/answer_form.html', context)
@login_required(login_url='common:login')
def answer_create(request, question_id):
"""
pybo 답변 출력
"""
question = get_object_or_404(Question, pk=question_id)
if request.method == "POST":
form = AnswerForm(request.POST)
if form.is_valid():
answer = form.save(commit=False)
answer.author = request.user
answer.create_date = timezone.now()
answer.question = question
answer.save()
return redirect('pybo:detail', question_id=question.id)
else:
form = AnswerForm()
context = {'question': question, 'form': form}
return render(request, 'pybo/question_detail.html', context)
@login_required(login_url='common:login')
def answer_delete(request, answer_id):
"""
pybo 답변삭제
"""
answer = get_object_or_404(Answer, pk=answer_id)
if request.user != answer.author:
messages.error(request, '삭제권한이 없습니다')
else:
answer.delete()
return redirect('pybo:detail', question_id=answer.question.id)
""" 질문 댓글 관련 구분 시작 """
@login_required(login_url='common:login')
def comment_create_question(request, question_id):
"""
pybo 질문댓글등록
"""
question = get_object_or_404(Question, pk=question_id)
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.author = request.user
comment.create_date = timezone.now()
comment.question = question
comment.save()
return redirect('pybo:detail', question_id=question.id)
else:
form = CommentForm()
context = {'form': form}
return render(request, 'pybo/comment_form.html', context)
@login_required(login_url='common:login')
def comment_modify_question(request, comment_id):
"""
pybo 질문댓글수정
"""
comment = get_object_or_404(Comment, pk=comment_id)
if request.user != comment.author:
messages.error(request, '댓글수정권한이 없습니다')
return redirect('pybo:detail', question_id=comment.question.id)
if request.method == "POST":
form = CommentForm(request.POST, instance=comment)
if form.is_valid():
comment = form.save(commit=False)
comment.modify_date = timezone.now()
comment.save()
return redirect('pybo:detail', question_id=comment.question.id)
else:
form = CommentForm(instance=comment)
context = {'form': form}
return render(request, 'pybo/comment_form.html', context)
@login_required(login_url='common:login')
def comment_delete_question(request, comment_id):
"""
pybo 질문댓글삭제
"""
comment = get_object_or_404(Comment, pk=comment_id)
if request.user != comment.author:
messages.error(request, '댓글삭제권한이 없습니다')
return redirect('pybo:detail',question_id=comment.question.id)
else:
comment.delete()
return redirect('pybo:detail', question_id=comment.question.id)
""" 답변 댓글 관련 구분 시작 """
@login_required(login_url='common:login')
def comment_create_answer(request, answer_id):
"""
pybo 답변댓글등록
"""
answer = get_object_or_404(Answer, pk=answer_id)
if request.method == "POST":
form = CommentForm(request.POST)
if form.is_valid():
comment = form.save(commit=False)
comment.author = request.user
comment.create_date = timezone.now()
comment.answer = answer
comment.save()
return redirect('pybo:detail', question_id=comment.answer.question.id)
else:
form = CommentForm()
context = {'form': form}
return render(request, 'pybo/comment_form.html', context)
@login_required(login_url='common:login')
def comment_modify_answer(request, comment_id):
"""
pybo 답변댓글수정
"""
comment = get_object_or_404(Comment, pk=comment_id)
if request.user != comment.author:
messages.error(request, '댓글수정권한이 없습니다')
return redirect('pybo:detail', question_id=comment.answer.question.id)
if request.method == "POST":
form = CommentForm(request.POST, instance=comment)
if form.is_valid():
comment = form.save(commit=False)
comment.modify_date = timezone.now()
comment.save()
return redirect('pybo:detail', question_id=comment.answer.question.id)
else:
form = CommentForm(instance=comment)
context = {'form': form}
return render(request, 'pybo/comment_form.html', context)
@login_required(login_url='common:login')
def comment_delete_answer(request, comment_id):
"""
pybo 답변댓글삭제
"""
comment = get_object_or_404(Comment, pk=comment_id)
if request.user != comment.author:
messages.error(request, '댓글삭제권한이 없습니다')
return redirect('pybo:detail',question_id=comment.answer.question.id)
else:
comment.delete()
return redirect('pybo:detail', question_id=comment.answer.question.id)