2018-09-16 16:52:23 +00:00
|
|
|
from django.shortcuts import render
|
2018-10-03 20:00:08 +00:00
|
|
|
from django.http import HttpResponse, Http404, HttpResponseBadRequest, HttpResponseRedirect
|
2018-10-02 09:56:04 +00:00
|
|
|
from postings.models import *
|
2018-10-03 20:00:08 +00:00
|
|
|
from postings.forms import *
|
2018-09-16 16:52:23 +00:00
|
|
|
|
2018-10-06 19:18:18 +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:
|
2018-10-03 20:59:09 +00:00
|
|
|
# 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
|
|
|
|
2018-10-03 20:59:09 +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:
|
2018-10-03 20:59:09 +00:00
|
|
|
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)
|
2018-10-03 20:59:09 +00:00
|
|
|
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)
|
2018-10-06 19:18:18 +00:00
|
|
|
# return render(request, "postings/rateables/" + template, {'entity': entity})
|
2018-10-03 20:00:08 +00:00
|
|
|
|
|
|
|
# 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']
|
2018-10-18 08:32:37 +00:00
|
|
|
name = form.cleaned_data['name']
|
2018-10-03 20:00:08 +00:00
|
|
|
title = form.cleaned_data['title']
|
|
|
|
content = form.cleaned_data['content']
|
|
|
|
entity_id = form.cleaned_data['entity_id']
|
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.")
|
2018-10-03 20:00:08 +00:00
|
|
|
|
|
|
|
# Creates the new Review object from the posted data.
|
|
|
|
review = Review.objects.create(
|
|
|
|
rating=rating,
|
|
|
|
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))
|
2018-10-03 20:00:08 +00:00
|
|
|
|
|
|
|
return HttpResponseBadRequest("Bad Request")
|