Skip to content

Commit 04d6931

Browse files
authored
Merge pull request #18 from Animesh-Ghosh/context-updoot
Context updoot
2 parents f5ab559 + 73e8f63 commit 04d6931

3 files changed

Lines changed: 107 additions & 79 deletions

File tree

api/views.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,15 @@
1212

1313
# Just wraps a simple HTTP Response to a JSON Response
1414
class JSONResponse(HttpResponse):
15-
def __init__(self, data, **kwargs):
16-
content = JSONRenderer().render(data)
17-
kwargs['content_type'] = 'application/json'
18-
super(JSONResponse, self).__init__(content, **kwargs)
15+
def __init__(self, data, **kwargs):
16+
content = JSONRenderer().render(data)
17+
kwargs['content_type'] = 'application/json'
18+
super(JSONResponse, self).__init__(content, **kwargs)
1919

2020

2121
@api_view(['GET'])
2222
def index(request):
23-
return render(request, 'api.html')
23+
return render(request, 'api.html', {'title': 'API'})
2424

2525

2626
@api_view(['GET'])
@@ -66,7 +66,6 @@ def tutorial_Tags_Category(request, tags, category):
6666
def tags(request):
6767
"""
6868
Return all tags
69-
7069
"""
7170
paginator = PageNumberPagination()
7271
tags = tag.objects.all().order_by('id')
@@ -79,7 +78,6 @@ def tags(request):
7978
def tutorials(request):
8079
"""
8180
get: Return all tutorials
82-
8381
post: submit a tutorial
8482
"""
8583
if request.method == 'GET':

app/templates/base.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
<!DOCTYPE html>
33
<html>
44
<head>
5+
{% if title %}
6+
<title>tutorialdb - {{ title }}</title>
7+
{% else %}
58
<title>tutorialdb</title>
9+
{% endif %}
610
<meta charset="utf-8" />
711
<meta content='text/html; charset=utf-8' http-equiv='Content-Type'>
812
<meta http-equiv='X-UA-Compatible' content='IE=edge'>

app/views.py

Lines changed: 98 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -7,87 +7,113 @@
77
import time
88

99
class HomePageView(TemplateView):
10-
template_name = 'home.html'
10+
"""
11+
Home page view.
12+
"""
13+
template_name = 'home.html'
1114

1215
def latest(request):
13-
results = tutorial.objects.all().order_by('-id')[:10]
14-
results = { 'results': results }
15-
return render(request, 'latest.html', results)
16-
16+
"""
17+
View for the latest tutorial entries.
18+
"""
19+
results = tutorial.objects.all().order_by('-id')[:10]
20+
context = {'results': results, 'title': 'Latest'}
21+
return render(request, 'latest.html', context)
1722

1823
def about(request):
19-
return render(request, 'about.html')
20-
24+
"""
25+
About view.
26+
"""
27+
return render(request, 'about.html', {'title': 'About'})
2128

2229
def search_query(request):
23-
query = request.GET.get('q').lower()
24-
category = request.GET.get('category')
25-
list_query = query.split()
26-
27-
start_time = time.time()
28-
29-
if category is not None:
30-
object_list = tutorial.objects.filter(
31-
(Q(title__icontains=query) | Q(tags__name__in=list_query)) & Q(category__icontains=category)
32-
).order_by('id').distinct()
33-
else:
34-
object_list = tutorial.objects.filter(
35-
(Q(title__icontains=query) & Q(tags__name__in=list_query))
36-
).order_by('id').distinct()
37-
end_time = time.time()
38-
total = len(object_list)
39-
result_time = round(end_time - start_time, 3)
40-
41-
paginator = Paginator(object_list, 3)
42-
page = request.GET.get('page')
43-
try:
44-
object_list = paginator.page(page)
45-
except PageNotAnInteger:
46-
object_list = paginator.page(1)
47-
except EmptyPage:
48-
object_list = paginator.page(paginator.num_pages)
49-
50-
context = {'tquery':query, 'object_list':object_list, 'total': total, 'time': result_time}
51-
52-
return render(request, 'search_results.html', context)
53-
30+
"""
31+
View for the search results.
32+
"""
33+
query = request.GET.get('q').lower()
34+
category = request.GET.get('category')
35+
list_query = query.split()
36+
37+
start_time = time.time()
38+
39+
if category is not None:
40+
object_list = tutorial.objects.filter(
41+
(Q(title__icontains=query) | Q(tags__name__in=list_query)) & Q(category__icontains=category)
42+
).order_by('id').distinct()
43+
else:
44+
object_list = tutorial.objects.filter(
45+
(Q(title__icontains=query) & Q(tags__name__in=list_query))
46+
).order_by('id').distinct()
47+
end_time = time.time()
48+
total = len(object_list)
49+
result_time = round(end_time - start_time, 3)
50+
51+
paginator = Paginator(object_list, 3)
52+
page = request.GET.get('page')
53+
try:
54+
object_list = paginator.page(page)
55+
except PageNotAnInteger:
56+
object_list = paginator.page(1)
57+
except EmptyPage:
58+
object_list = paginator.page(paginator.num_pages)
59+
60+
context = {
61+
'tquery':query,
62+
'object_list':object_list,
63+
'total': total,
64+
'time': result_time,
65+
'title': query
66+
}
67+
68+
return render(request, 'search_results.html', context)
5469

5570
class ContributeView(TemplateView):
56-
def get(self, request):
57-
return render(request, 'contribute.html')
58-
59-
def post(self, request):
60-
linkCount = tutorial.objects.filter(link = request.POST['tlink']).count()
61-
if linkCount == 0:
62-
tags, title = generateTags(request.POST['tlink'])
63-
if 'other' in tags:
64-
return render(request, 'contribute.html', {'error': "Not a Tutorial Link, Try Again"})
65-
else:
66-
tutorialObject = tutorial.objects.create(
67-
title = title,
68-
link = request.POST['tlink'],
69-
category = request.POST['tcategory']
70-
)
71-
for t in tags:
72-
obj, created = tag.objects.get_or_create(name=t)
73-
74-
tagObjList = tag.objects.filter(name__in = tags)
75-
tutorialObject.tags.set(tagObjList)
76-
return render(request, 'thankyou.html')
77-
return render(request, 'thankyou.html')
78-
71+
"""
72+
View for the tutorial contribution page.
73+
"""
74+
def get(self, request):
75+
"""
76+
GET the contribution form.
77+
"""
78+
return render(request, 'contribute.html')
79+
80+
def post(self, request):
81+
"""
82+
POST a tutorial.
83+
"""
84+
linkCount = tutorial.objects.filter(link = request.POST['tlink']).count()
85+
if linkCount == 0:
86+
tags, title = generateTags(request.POST['tlink'])
87+
if 'other' in tags:
88+
return render(request, 'contribute.html', {'error': "Not a Tutorial Link, Try Again"})
89+
else:
90+
tutorialObject = tutorial.objects.create(
91+
title = title,
92+
link = request.POST['tlink'],
93+
category = request.POST['tcategory']
94+
)
95+
for t in tags:
96+
obj, created = tag.objects.get_or_create(name=t)
97+
98+
tagObjList = tag.objects.filter(name__in = tags)
99+
tutorialObject.tags.set(tagObjList)
100+
return render(request, 'thankyou.html')
101+
return render(request, 'thankyou.html')
79102

80103
def tags(request):
81-
object_list = tag.objects.all()
82-
context = {'object_list':object_list}
83-
84-
return render(request, 'tags.html', context)
85-
104+
"""
105+
View for the tags.
106+
"""
107+
object_list = tag.objects.all()
108+
context = {'object_list':object_list, 'title': 'Tags'}
109+
return render(request, 'tags.html', context)
86110

87111
def taglinks(request, tagname):
88-
taglist = []
89-
taglist.append(tagname)
90-
object_list = tutorial.objects.filter(tags__name__in = taglist)
91-
context = {'tag': tagname, 'object_list':object_list}
92-
93-
return render(request, 'taglinks.html', context)
112+
"""
113+
View for the tutorials with the {tagname}.
114+
"""
115+
taglist = []
116+
taglist.append(tagname)
117+
object_list = tutorial.objects.filter(tags__name__in = taglist)
118+
context = {'tag': tagname, 'object_list':object_list, 'title': tagname}
119+
return render(request, 'taglinks.html', context)

0 commit comments

Comments
 (0)