views.py
5.65 KB
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
import datetime
from django.db.models import F, Max
from django.shortcuts import render
from django.http import HttpResponseRedirect, HttpResponse
from .models import Board
from users.models import User
import json
import pdb
PAGESIZE=10
# Create your views here.
def list(request, page=1):
kwd = request.GET.get('kwd')
if kwd == None or kwd == '' or kwd == 'null':
start = (page - 1) * PAGESIZE
board_count = Board.objects.count()
boardlist = Board.objects.all().order_by('-groupno', 'orderno')[start:start+PAGESIZE]
else:
start = (page - 1) * PAGESIZE
board_count = Board.objects.filter(title__contains=kwd).count()
boardlist = Board.objects.filter(title__contains=kwd).order_by('-groupno', 'orderno')[start:start+PAGESIZE]
data = {
'boardlist': boardlist,
'board_count': board_count,
'current_page': page,
'page': page,
}
kwd = request.GET.get('kwd')
if kwd is None:
data['kwd'] = json.dumps(kwd)
else:
data['kwd'] = kwd
return render(request, 'board/list.html', data)
# 게시글 작성
def writeform(request, no=-1, page=1):
# 인증
authuser = request.session.get('authUser')
if authuser is None:
return HttpResponseRedirect('board/list')
if no == -1:
return render(request, 'board/write.html', {"page":page})
else:
data = {"no":no, "page":page}
kwd = request.GET.get('kwd')
if kwd is None:
data['kwd'] = json.dumps(kwd)
else:
data['kwd'] = kwd
return render(request, 'board/write.html', data)
def write(request, page=1):
# 인증
authuser = request.session.get('authUser')
if authuser is None:
return HttpResponseRedirect('board/list')
board = Board()
board.title = request.POST.get('title')
board.content = request.POST.get('content')
board.user = User.objects.get(email=request.session['authUser']['email'])
# 새글 작성하기
if request.POST.get('no') == '-1':
value = Board.objects.aggregate(max_groupno=Max('groupno'))
if value.get('max_groupno') is None:
value['max_groupno'] = 0
board.groupno = value.get('max_groupno') + 1
board.save()
else:
board2 = Board.objects.get(id=request.POST.get('no'))
Board.objects.filter(orderno__gte=board2.orderno+1).update(orderno=F('orderno')+1)
board.groupno = board2.groupno
board.orderno = board2.orderno + 1
board.depth = board2.depth + 1
board.save()
data = {
'page': 1
}
kwd = request.GET.get('kwd')
print(kwd, "@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@")
if kwd is None:
data['kwd'] = json.dumps(kwd)
else:
data['kwd'] = kwd
# if kwd is '':
# return HttpResponseRedirect(f'/board/list/{page}')
return HttpResponseRedirect(f'/board/list/{page}?kwd={kwd}')
# 상세보기
def view(request, no=0, page=1):
if no == 0:
return HttpResponseRedirect('list')
board = Board.objects.filter(id=no)
data = {
'board':board[0],
'page': page,
}
kwd = request.GET.get('kwd')
if kwd is None:
data['kwd'] = json.dumps(kwd)
else:
data['kwd'] = kwd
response = render(request, 'board/view.html', data)
if request.session.get('authUser') is None:
cookie_name = 'hit'
else:
cookie_name = f'hit:{request.session["authUser"]["email"]}'
cookie_name = cookie_name[:cookie_name.find('@')]
tomorrow = datetime.datetime.replace(datetime.datetime.now(), hour=23, minute=59, second=0)
expires = datetime.datetime.strftime(tomorrow, "%a, %d-%b-%Y %H:%M:%S GMT")
if request.COOKIES.get(cookie_name) is not None:
cookies = request.COOKIES.get(cookie_name)
cookies_list = cookies.split('|')
if str(no) not in cookies_list:
response.set_cookie(cookie_name, cookies + f'|{no}', expires=expires)
board.update(hit=F('hit')+1)
return response
else:
response.set_cookie(cookie_name, no, expires=expires)
board.update(hit=F('hit')+1)
return response
return render(request, 'board/view.html', data)
# 수정 및 삭제
def modifyform(request, no=0, page=1):
board = Board.objects.filter(id=no)[0]
authuser = request.session.get('authUser')
if authuser is None or board.user.email != authuser['email']:
return HttpResponseRedirect('board/list')
data = {
'board': board,
'page': page,
}
kwd = request.GET.get('kwd')
if kwd is None:
data['kwd'] = json.dumps(kwd)
else:
data['kwd'] = kwd
return render(request, 'board/modify.html', data)
def modify(request, page=1):
board_id = request.POST['id']
board = Board.objects.get(id=board_id)
authuser = request.session.get('authUser')
if authuser is None or board.user.email != authuser['email']:
return HttpResponseRedirect('board/list')
board.title = request.POST.get('title')
board.content = request.POST.get('content')
board.save()
data = {
'board': board,
'page': page,
}
kwd = request.GET.get('kwd')
if kwd is None:
data['kwd'] = json.dumps(kwd)
else:
data['kwd'] = kwd
return HttpResponseRedirect(f'/board/{board_id}/{page}', data)
def delete(request, no=0, page=1):
board = Board.objects.get(id=no)
authuser = request.session.get('authUser')
if authuser is None or board.user.email != authuser['email']:
return HttpResponseRedirect('board/list')
board.title = "삭제된 글입니다."
board.save()
return HttpResponseRedirect(f'/board/list/{page}')