-
Notifications
You must be signed in to change notification settings - Fork 0
/
views.py
183 lines (163 loc) · 5.84 KB
/
views.py
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
from django.shortcuts import render
from django.shortcuts import render, render_to_response, get_object_or_404
from newblog.models import Blog, Tag
from django.http import HttpResponseRedirect, HttpResponse, Http404
from django.template import RequestContext
from newblog.forms import BlogForm, LoginForm, TagForm
from django.contrib.auth.models import User
from django.contrib import auth
from django.contrib.auth.decorators import login_required
from django.db import IntegrityError
def blog_index(request):
blogs = Blog.objects.all()
tags = Tag.objects.all()
return render(request, 'newblog/blog_index.html', {'blogs':blogs, 'tags': tags})
def blog_filter(request, id=''):
tags = Tag.objects.all()
tag = Tag.objects.get(id=id)
blogs = tag.blog_set.all()
return render(request, "newblog/blog_filter.html",
{"blogs": blogs, "tag": tag, "tags": tags})
@login_required
def myblog(request):
user = request.user
blogs = Blog.objects.all().filter(user = user)
tags = Tag.objects.all()
return render(request, 'newblog/blog_index.html', {'blogs':blogs, 'tags': tags})
def blog_detail(request, blog_id):
blog = get_object_or_404(Blog, pk = blog_id)
return render(request, 'newblog/blog_detail.html', {'blog':blog})
@login_required
def blog_add(request):
if request.method == 'POST':
form = BlogForm(request.POST)
tag = TagForm(request.POST)
if form.is_valid() and tag.is_valid():
cd = form.cleaned_data
cdtag = tag.cleaned_data
tagname = cdtag['tag_name']
for taglist in tagname.split():
Tag.objects.get_or_create(tag_name=taglist.strip())
title = cd['title']
content = cd['content']
user = request.user
blog = Blog(title=title, user=user, content=content)
blog.save()
for taglist in tagname.split():
blog.tags.add(Tag.objects.get(tag_name=taglist.strip()))
blog.save()
id = Blog.objects.order_by('-published_time')[0].id
return HttpResponseRedirect('/newblog/%s' % id)
else:
return render(request, 'newblog/blog_add.html', {'form': form, 'tag':tag})
else:
form = BlogForm()
tag = TagForm(initial={'tag_name': 'Notags'})
return render(request, 'newblog/blog_add.html', {'form': form, 'tag':tag})
@login_required
def blog_update(request, blog_id=""):
if request.method == 'POST':
form = BlogForm(request.POST)
tag = TagForm(request.POST)
if form.is_valid() and tag.is_valid():
cd = form.cleaned_data
cdtag = tag.cleaned_data
tagname = cdtag['tag_name']
tagnamelist = tagname.split()
for taglist in tagnamelist:
Tag.objects.get_or_create(tag_name=taglist.strip())
title = cd['title']
content = cd['content']
blog = Blog.objects.get(pk=blog_id)
user = blog.user
if blog:
blog.title = title
blog.content = content
blog.save()
for taglist in tagnamelist:
blog.tags.add(Tag.objects.get(tag_name=taglist.strip()))
blog.save()
tags = blog.tags.all()
for tagname in tags:
tagname = unicode(str(tagname), "utf-8")
if tagname not in tagnamelist:
notag = blog.tags.get(tag_name=tagname)
blog.tags.remove(notag)
else:
blog = Blog(title=blog.title, content=blog.content, user=user)
blog.save()
return HttpResponseRedirect('/newblog/%s' % blog_id)
else:
return render(request, 'newblog/blog_add.html', {'form': form,'tag': tag})
else:
try:
blog = Blog.objects.get(id=blog_id)
except Exception:
raise Http404
form = BlogForm(initial={'title': blog.title, 'content': blog.content, 'user': blog.user}, auto_id=False)
tags = blog.tags.all()
if tags:
taginit = ''
for x in tags:
taginit += str(x) + ' '
tag = TagForm(initial={'tag_name': taginit})
else:
tag = TagForm(initial={'tag_name': 'Notags'})
return render(request, 'newblog/blog_add.html', {'blog': blog, 'form': form, 'blog_id': id, 'tag': tag})
@login_required
def blog_del(request, blog_id=""):
try:
blog = Blog.objects.get(id=blog_id)
except Exception:
raise Http404
if blog:
blog.delete()
return HttpResponseRedirect("/newblog/myblog")
blogs = Blog.objects.all()
return render(request, "newblog/blog_index.html", {"blogs": blogs})
def login(request):
if request.method == 'GET':
form = LoginForm()
return render(request, 'newblog/login.html', {'form': form})
else:
form = LoginForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
username = cd['username']
password = cd['password']
user = auth.authenticate(username=username, password=password)
if user is not None and user.is_active:
auth.login(request, user)
# return HttpResponseRedirect("newblog/")
return HttpResponseRedirect("/newblog/myblog")
else:
return render(request, 'newblog/login.html', {'form': form, 'error': 'Password is wrong or the username is not existed.'})
else:
return render(request, 'newblog/login.html', {'form': form, 'error': 'Username and Password is required.'})
def profile(request):
if not request.user.is_authenticated():
return render(request, 'newblog/login.html', {'form': form})
return render(request, 'newblog/blog_index.html')
def logout(request):
auth.logout(request)
return HttpResponseRedirect("/newblog/")
def signup(request):
if request.method == 'POST':
form = LoginForm(request.POST)
if form.is_valid():
cd = form.cleaned_data
username = cd['username']
password = cd['password']
try:
new_user = User.objects.create_user(username=username, password=password)
except IntegrityError:
error = "Username aleary existed, please choose another one"
return render(request, 'newblog/signup.html', {'form': form, 'error':error})
else:
user= auth.authenticate(username=username, password=password)
auth.login(request, user)
return HttpResponseRedirect("/newblog")
else:
form = LoginForm()
return render(request, 'newblog/signup.html', {'form': form})
# Create your views here.