3
3
from django .shortcuts import get_object_or_404 , render , redirect
4
4
from django .contrib .auth import login , authenticate , logout
5
5
from django .contrib .auth .models import User
6
- from django .http import HttpResponseBadRequest , HttpResponseNotFound
6
+ from django .http import HttpResponse
7
7
8
8
from .models import *
9
9
from .forms import *
@@ -14,11 +14,13 @@ def index(req):
14
14
15
15
16
16
def user_login (req ):
17
+ if req .user .is_authenticated :
18
+ return HttpResponse (status = 404 )
17
19
if req .method == 'GET' :
18
20
return login_form (req )
19
21
if req .method == 'POST' :
20
22
return login_post (req )
21
- return HttpResponseNotFound ( )
23
+ return HttpResponse ( status = 404 )
22
24
23
25
24
26
def login_form (req ):
@@ -30,22 +32,28 @@ def login_form(req):
30
32
def login_post (req ):
31
33
form = LoginForm (req .POST )
32
34
if not form .is_valid ():
33
- return HttpResponseBadRequest ( )
35
+ return HttpResponse ( status = 400 )
34
36
35
37
user = authenticate (
36
38
username = form .cleaned_data ['username' ],
37
39
password = form .cleaned_data ['password' ],
38
40
)
39
- login (req , user )
40
- return redirect ('index' )
41
+
42
+ if user :
43
+ login (req , user )
44
+ return redirect ('index' )
45
+ else :
46
+ return HttpResponse (status = 401 )
41
47
42
48
43
49
def register (req ):
50
+ if req .user .is_authenticated :
51
+ return HttpResponse (status = 404 )
44
52
if req .method == 'GET' :
45
53
return register_form (req )
46
54
if req .method == 'POST' :
47
55
return register_post (req )
48
- return HttpResponseNotFound ( )
56
+ return HttpResponse ( status = 404 )
49
57
50
58
51
59
def register_form (req ):
@@ -57,14 +65,15 @@ def register_form(req):
57
65
def register_post (req ):
58
66
form = RegisterForm (req .POST )
59
67
if not form .is_valid ():
60
- return HttpResponseBadRequest ( )
68
+ return HttpResponse ( status = 400 )
61
69
62
- User .objects .create_user (
70
+ user = User .objects .create_user (
63
71
username = form .cleaned_data ['username' ],
64
72
password = form .cleaned_data ['password' ],
65
73
first_name = form .cleaned_data ['first_name' ],
66
74
last_name = form .cleaned_data ['last_name' ],
67
75
)
76
+ user .save ()
68
77
return redirect ('login' )
69
78
70
79
@@ -91,9 +100,7 @@ def get_article_list(req, page_num):
91
100
92
101
93
102
def get_article (req , article_id ):
94
- article = get_object_or_404 (Article , id = article_id )
95
- if article .is_deleted :
96
- return HttpResponseNotFound ()
103
+ article = get_object_or_404 (Article , id = article_id , is_deleted = False )
97
104
98
105
return render (req , 'articles/details.html' , {
99
106
'article' : article ,
@@ -102,12 +109,12 @@ def get_article(req, article_id):
102
109
103
110
def compose_article (req ):
104
111
if not req .user .is_authenticated :
105
- return HttpResponseNotFound ( )
112
+ return HttpResponse ( status = 404 )
106
113
if req .method == 'GET' :
107
114
return compose_article_form (req )
108
115
if req .method == 'POST' :
109
116
return compose_article_post (req )
110
- return HttpResponseNotFound ( )
117
+ return HttpResponse ( status = 404 )
111
118
112
119
113
120
def compose_article_form (req ):
@@ -119,7 +126,7 @@ def compose_article_form(req):
119
126
def compose_article_post (req ):
120
127
form = ArticleForm (req .POST )
121
128
if not form .is_valid ():
122
- return HttpResponseBadRequest ( )
129
+ return HttpResponse ( status = 400 )
123
130
124
131
new_article = Article .objects .create (
125
132
title = form .cleaned_data ['title' ],
@@ -133,17 +140,16 @@ def compose_article_post(req):
133
140
134
141
def edit_article (req , article_id ):
135
142
if not req .user .is_authenticated :
136
- return HttpResponseNotFound ( )
143
+ return HttpResponse ( status = 404 )
137
144
138
- article = get_object_or_404 (Article , id = article_id )
139
- if req .user != article .author or article .is_deleted :
140
- return HttpResponseNotFound ()
145
+ article = get_object_or_404 (Article ,
146
+ id = article_id , is_deleted = False , author = req .user )
141
147
142
148
if req .method == 'GET' :
143
149
return edit_article_form (req , article )
144
150
if req .method == 'POST' :
145
151
return edit_article_post (req , article )
146
- return HttpResponseNotFound ( )
152
+ return HttpResponse ( status = 404 )
147
153
148
154
149
155
def edit_article_form (req , article ):
@@ -158,7 +164,7 @@ def edit_article_form(req, article):
158
164
def edit_article_post (req , article ):
159
165
form = ArticleForm (req .POST )
160
166
if not form .is_valid ():
161
- return HttpResponseBadRequest ( )
167
+ return HttpResponse ( status = 400 )
162
168
163
169
article .title = form .cleaned_data ['title' ]
164
170
article .content = form .cleaned_data ['content' ]
@@ -168,11 +174,10 @@ def edit_article_post(req, article):
168
174
169
175
def delete_article (req , article_id ):
170
176
if not req .user .is_authenticated :
171
- return HttpResponseNotFound ( )
177
+ return HttpResponse ( status = 404 )
172
178
173
- article = get_object_or_404 (Article , id = article_id )
174
- if req .user != article .author or article .is_deleted :
175
- return HttpResponseNotFound ()
179
+ article = get_object_or_404 (Article ,
180
+ id = article_id , is_deleted = False , author = req .user )
176
181
177
182
article .is_deleted = True
178
183
article .save ()
0 commit comments