-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathviews.py
146 lines (133 loc) · 5.83 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
from django.shortcuts import render, get_object_or_404
from .models import Article, Category, Tag
from comments.forms import CommentForm
from django.views.generic import ListView, DetailView
import markdown
from markdown.extensions.toc import TocExtension
from django.utils.text import slugify
class ArticleListView(ListView):
model = Article
template_name = 'blog_list.html'
context_object_name = 'article_list'
paginate_by = 3
title = 'Jingtao Blog List'
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
# paginate attributes
paginator = context.get('paginator')
page = context.get('page_obj')
is_paginated = context.get('is_paginated')
# invoke customized my_pagination()
paginate_data = self.my_pagination(paginator,page,is_paginated)
context.update(paginate_data)
context.update({'title':self.title})
return context
def my_pagination(self,paginator,page,is_paginated):
if not is_paginated:
return {}
range_num = 2
left_of_current = []
right_of_current = []
left_has_more = False
right_has_more = False
display_first = False
display_last = False
current_page_no = page.number
total_pages = paginator.num_pages
page_range = paginator.page_range
if current_page_no == 1:
right_of_current = page_range[1:1+range_num]
if right_of_current[-1] < total_pages -1:
right_has_more = True
if right_of_current[-1] < total_pages:
display_last = True
elif current_page_no == total_pages:
left_of_current = page_range[-1-range_num:-1]
if left_of_current[0] > 2:
left_has_more = True
if left_of_current[0] > 1:
display_first = True
else:
right_of_current = page_range[current_page_no:current_page_no+range_num]
left_of_current = page_range[-total_pages+current_page_no-range_num-1:-total_pages+current_page_no-1]
if right_of_current[-1] < total_pages -1:
right_has_more = True
if right_of_current[-1] < total_pages:
display_last = True
if left_of_current[0] > 2:
left_has_more = True
if left_of_current[0] > 1:
display_first = True
ret = {
'left': left_of_current,
'right': right_of_current,
'left_has_more':left_has_more,
'right_has_more':right_has_more,
'first':display_first,
'last':display_last,
}
return ret
"""
def article_list(request):
article_list = Article.objects.all()
return render(request, 'blog_list.html', context={'title':'Jingtao Blog List','article_list':article_list})
"""
class CategoryView(ArticleListView):
def get_queryset(self):
category = get_object_or_404(Category, pk=self.kwargs.get('pk'))
self.title = category.name + ' BLOG LIST'
return super(CategoryView,self).get_queryset().filter(category=category)
"""
def category(request,id):
category = get_object_or_404(Category, pk=id)
article_list = Article.objects.filter(category=category)
return render(request, 'blog_list.html',context={'title':category.name+' BLOG LIST','article_list':article_list})
"""
class ArticleDetailView(DetailView):
model = Article
template_name = 'blog_content.html'
context_object_name = 'article'
#override get() to increase the number of browsing article
def get(self,request,*args,**kwargs):
response = super(ArticleDetailView,self).get(request,*args,**kwargs)
self.object.add_views()
return response
#override get_object() to render article body to support markdown language
def get_object(self, queryset=None):
article = super(ArticleDetailView,self).get_object(queryset=None)
md = markdown.Markdown(extensions=[
'markdown.extensions.extra',
'markdown.extensions.codehilite',
TocExtension(slugify=slugify),
])
article.body = md.convert(article.body)
article.toc = md.toc
return article
#override get_context_data() to pass other objects to template such as form and comments except article (defined above)
def get_context_data(self, **kwargs):
context = super(ArticleDetailView,self).get_context_data(**kwargs)
form = CommentForm()
comment_list = self.object.comment_set.all()
context.update({'form':form,'comment_list':comment_list,'title':self.object.title})
return context
"""
def article_detail(request,id):
article = get_object_or_404(Article, pk=id)
# increase the number of article views
article.add_views()
# make article body to support markdown language
article.body = markdown.markdown(article.body,extensions=['markdown.extensions.extra',
'markdown.extensions.codehilite',
'markdown.extensions.toc',] )
form = CommentForm()
#get all the comments of this article
comment_list = article.comment_set.all() # == Comment.objects.filter(article=article)
# pass article, form and comments to render the detail page
context = {'title':article.title,'article':article,'form':form,'comment_list':comment_list}
return render(request, 'blog_content.html', context=context)
"""
class TagView(ArticleListView):
def get_queryset(self):
tag = get_object_or_404(Tag, pk=self.kwargs.get('pk'))
self.title = tag.name + ' BLOG LIST'
return super(TagView,self).get_queryset().filter(tags=tag)