RateMyCourse/backend/postings/views.py

100 lines
3.3 KiB
Python
Raw Normal View History

2018-09-16 16:52:23 +00:00
from django.shortcuts import render
from django.http import HttpResponse, Http404, HttpResponseBadRequest, HttpResponseRedirect
2018-10-02 09:56:04 +00:00
from postings.models import *
from postings.forms import *
2018-09-16 16:52:23 +00:00
2018-09-16 16:52:23 +00:00
# Create your views here.
2018-10-02 09:56:04 +00:00
# The view for the homepage, or index.html
2018-10-02 12:48:26 +00:00
# There is an optional 'search_query GET parameter, which, if provided, gives the template a 'results' variable.
2018-10-02 09:56:04 +00:00
def index(request):
2018-10-02 12:48:26 +00:00
search_query = request.GET.get('search_query', None)
2018-10-11 09:18:48 +00:00
results = []
2018-10-02 12:48:26 +00:00
if search_query:
# Filter objects based on case-insensitive contains filter.
2018-10-02 12:48:26 +00:00
results = RateableEntity.objects.filter(name__icontains=search_query)
2018-10-11 09:18:48 +00:00
context = {
'results': results,
'search_query': search_query
}
return render(request, 'postings/frontend/results.html', context)
return render(request, 'postings/frontend/landing.html')
2018-10-02 09:56:04 +00:00
# The view for listing all rateable entities.
def rateables(request):
entity_type = request.GET.get('type', None)
entities = None
if entity_type == "university":
entities = University.objects.all()
elif entity_type == "course":
entities = Course.objects.all()
else:
entities = RateableEntity.objects.all()
return render(request, "postings/rateables/entities.html", {'entities': entities})
# The view for any rateable entity.
def rateable_entity(request, entity_id):
2018-10-02 12:06:53 +00:00
try:
entity = RateableEntity.objects.get(pk=entity_id)
# Try and get a more specific entity type from what is provided.
if entity.entity_type == RateableEntity.UNIVERSITY:
entity = University.objects.get(pk=entity.pk)
template = "university.html"
elif entity.entity_type == RateableEntity.COURSE:
entity = Course.objects.get(pk=entity.pk)
template = "course.html"
# Set any auxiliary variables needed, like average rating.
# This MUST be done after categorizing the object above.
entity.average_rating = entity.getAverageRating()
2018-10-18 08:04:24 +00:00
entity.rating_distribution = entity.getRatingDistribution()
print(entity.rating_distribution)
except RateableEntity.DoesNotExist:
raise Http404("RateableEntity with id " + str(entity_id) + " does not exist.")
2018-10-11 09:18:48 +00:00
reviews = entity.review_set.all().order_by('-created_date')
context = {
'entity': entity,
'reviews': reviews
}
return render(request, 'postings/frontend/entity.html', context)
# return render(request, "postings/rateables/" + template, {'entity': entity})
# The view for receiving POST requests for new reviews.
def post_review(request):
if request.method == 'POST':
form = EntityReviewForm(request.POST)
if form.is_valid():
# Only if the request is a POST and the form is valid do we do anything.
rating = form.cleaned_data['rating']
title = form.cleaned_data['title']
content = form.cleaned_data['content']
entity_id = form.cleaned_data['entity_id']
2018-10-18 08:47:21 +00:00
if 'name' in request.POST:
author_name = form.cleaned_data['name']
else:
author_name = None
2018-10-03 21:08:57 +00:00
try:
entity = RateableEntity.objects.get(pk=entity_id)
except RateableEntity.DoesNotExist:
raise HttpResponseBadRequest("Bad Request: Invalid entity id.")
# Creates the new Review object from the posted data.
review = Review.objects.create(
rating=rating,
2018-10-18 08:47:21 +00:00
author_name=author_name,
title=title,
content=content,
rateable_entity=entity
)
# Send the user back to the entity they were viewing.
2018-10-03 21:08:57 +00:00
return HttpResponseRedirect('/rateables/' + str(entity_id))
return HttpResponseBadRequest("Bad Request")