Skip to content

Commit

Permalink
Merge pull request #18 from Animesh-Ghosh/context-updoot
Browse files Browse the repository at this point in the history
Context updoot
  • Loading branch information
Bhupesh-V committed Aug 9, 2019
2 parents f5ab559 + 73e8f63 commit 04d6931
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 79 deletions.
12 changes: 5 additions & 7 deletions api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@

# Just wraps a simple HTTP Response to a JSON Response
class JSONResponse(HttpResponse):
def __init__(self, data, **kwargs):
content = JSONRenderer().render(data)
kwargs['content_type'] = 'application/json'
super(JSONResponse, self).__init__(content, **kwargs)
def __init__(self, data, **kwargs):
content = JSONRenderer().render(data)
kwargs['content_type'] = 'application/json'
super(JSONResponse, self).__init__(content, **kwargs)


@api_view(['GET'])
def index(request):
return render(request, 'api.html')
return render(request, 'api.html', {'title': 'API'})


@api_view(['GET'])
Expand Down Expand Up @@ -66,7 +66,6 @@ def tutorial_Tags_Category(request, tags, category):
def tags(request):
"""
Return all tags
"""
paginator = PageNumberPagination()
tags = tag.objects.all().order_by('id')
Expand All @@ -79,7 +78,6 @@ def tags(request):
def tutorials(request):
"""
get: Return all tutorials
post: submit a tutorial
"""
if request.method == 'GET':
Expand Down
4 changes: 4 additions & 0 deletions app/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
<!DOCTYPE html>
<html>
<head>
{% if title %}
<title>tutorialdb - {{ title }}</title>
{% else %}
<title>tutorialdb</title>
{% endif %}
<meta charset="utf-8" />
<meta content='text/html; charset=utf-8' http-equiv='Content-Type'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
Expand Down
170 changes: 98 additions & 72 deletions app/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,87 +7,113 @@
import time

class HomePageView(TemplateView):
template_name = 'home.html'
"""
Home page view.
"""
template_name = 'home.html'

def latest(request):
results = tutorial.objects.all().order_by('-id')[:10]
results = { 'results': results }
return render(request, 'latest.html', results)

"""
View for the latest tutorial entries.
"""
results = tutorial.objects.all().order_by('-id')[:10]
context = {'results': results, 'title': 'Latest'}
return render(request, 'latest.html', context)

def about(request):
return render(request, 'about.html')

"""
About view.
"""
return render(request, 'about.html', {'title': 'About'})

def search_query(request):
query = request.GET.get('q').lower()
category = request.GET.get('category')
list_query = query.split()

start_time = time.time()

if category is not None:
object_list = tutorial.objects.filter(
(Q(title__icontains=query) | Q(tags__name__in=list_query)) & Q(category__icontains=category)
).order_by('id').distinct()
else:
object_list = tutorial.objects.filter(
(Q(title__icontains=query) & Q(tags__name__in=list_query))
).order_by('id').distinct()
end_time = time.time()
total = len(object_list)
result_time = round(end_time - start_time, 3)

paginator = Paginator(object_list, 3)
page = request.GET.get('page')
try:
object_list = paginator.page(page)
except PageNotAnInteger:
object_list = paginator.page(1)
except EmptyPage:
object_list = paginator.page(paginator.num_pages)

context = {'tquery':query, 'object_list':object_list, 'total': total, 'time': result_time}

return render(request, 'search_results.html', context)

"""
View for the search results.
"""
query = request.GET.get('q').lower()
category = request.GET.get('category')
list_query = query.split()

start_time = time.time()

if category is not None:
object_list = tutorial.objects.filter(
(Q(title__icontains=query) | Q(tags__name__in=list_query)) & Q(category__icontains=category)
).order_by('id').distinct()
else:
object_list = tutorial.objects.filter(
(Q(title__icontains=query) & Q(tags__name__in=list_query))
).order_by('id').distinct()
end_time = time.time()
total = len(object_list)
result_time = round(end_time - start_time, 3)

paginator = Paginator(object_list, 3)
page = request.GET.get('page')
try:
object_list = paginator.page(page)
except PageNotAnInteger:
object_list = paginator.page(1)
except EmptyPage:
object_list = paginator.page(paginator.num_pages)

context = {
'tquery':query,
'object_list':object_list,
'total': total,
'time': result_time,
'title': query
}

return render(request, 'search_results.html', context)

class ContributeView(TemplateView):
def get(self, request):
return render(request, 'contribute.html')

def post(self, request):
linkCount = tutorial.objects.filter(link = request.POST['tlink']).count()
if linkCount == 0:
tags, title = generateTags(request.POST['tlink'])
if 'other' in tags:
return render(request, 'contribute.html', {'error': "Not a Tutorial Link, Try Again"})
else:
tutorialObject = tutorial.objects.create(
title = title,
link = request.POST['tlink'],
category = request.POST['tcategory']
)
for t in tags:
obj, created = tag.objects.get_or_create(name=t)

tagObjList = tag.objects.filter(name__in = tags)
tutorialObject.tags.set(tagObjList)
return render(request, 'thankyou.html')
return render(request, 'thankyou.html')

"""
View for the tutorial contribution page.
"""
def get(self, request):
"""
GET the contribution form.
"""
return render(request, 'contribute.html')

def post(self, request):
"""
POST a tutorial.
"""
linkCount = tutorial.objects.filter(link = request.POST['tlink']).count()
if linkCount == 0:
tags, title = generateTags(request.POST['tlink'])
if 'other' in tags:
return render(request, 'contribute.html', {'error': "Not a Tutorial Link, Try Again"})
else:
tutorialObject = tutorial.objects.create(
title = title,
link = request.POST['tlink'],
category = request.POST['tcategory']
)
for t in tags:
obj, created = tag.objects.get_or_create(name=t)

tagObjList = tag.objects.filter(name__in = tags)
tutorialObject.tags.set(tagObjList)
return render(request, 'thankyou.html')
return render(request, 'thankyou.html')

def tags(request):
object_list = tag.objects.all()
context = {'object_list':object_list}

return render(request, 'tags.html', context)

"""
View for the tags.
"""
object_list = tag.objects.all()
context = {'object_list':object_list, 'title': 'Tags'}
return render(request, 'tags.html', context)

def taglinks(request, tagname):
taglist = []
taglist.append(tagname)
object_list = tutorial.objects.filter(tags__name__in = taglist)
context = {'tag': tagname, 'object_list':object_list}

return render(request, 'taglinks.html', context)
"""
View for the tutorials with the {tagname}.
"""
taglist = []
taglist.append(tagname)
object_list = tutorial.objects.filter(tags__name__in = taglist)
context = {'tag': tagname, 'object_list':object_list, 'title': tagname}
return render(request, 'taglinks.html', context)

0 comments on commit 04d6931

Please sign in to comment.