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
|
|
|
|
|
|
|
# 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)
|
|
|
|
results = None
|
|
|
|
if search_query:
|
|
|
|
results = RateableEntity.objects.filter(name__icontains=search_query)
|
|
|
|
|
|
|
|
return render(request, 'postings/index.html', {'results': results})
|
2018-10-02 09:56:04 +00:00
|
|
|
|
2018-10-02 12:06:53 +00:00
|
|
|
# The view for a listing of universities.
|
2018-10-02 09:56:04 +00:00
|
|
|
def universities(request):
|
|
|
|
universities_list = University.objects.all()
|
|
|
|
context = {'entities': universities_list}
|
|
|
|
return render(request, 'postings/collections/universities.html', context)
|
|
|
|
|
2018-10-02 12:06:53 +00:00
|
|
|
# The view for /universities/<pk> Displays one university entity.
|
|
|
|
def university_entity(request, university_id):
|
|
|
|
try:
|
|
|
|
university = University.objects.get(pk=university_id)
|
2018-10-02 14:27:29 +00:00
|
|
|
university.average_rating = university.getAverageRating()
|
2018-10-02 12:06:53 +00:00
|
|
|
except University.DoesNotExist:
|
|
|
|
raise Http404("University does not exist")
|
|
|
|
return render(request, 'postings/entity_pages/university.html', {'entity': university})
|
|
|
|
|
|
|
|
# The view for a listing of courses.
|
2018-10-02 09:56:04 +00:00
|
|
|
def courses(request):
|
|
|
|
courses_list = Course.objects.all()
|
|
|
|
context = {'entities': courses_list}
|
2018-10-02 12:06:53 +00:00
|
|
|
return render(request, 'postings/collections/courses.html', context)
|
|
|
|
|
|
|
|
# The view for a specific course entity.
|
|
|
|
def course_entity(request, course_id):
|
|
|
|
try:
|
|
|
|
course = Course.objects.get(pk=course_id)
|
|
|
|
except Course.DoesNotExist:
|
|
|
|
raise Http404("Course does not exist")
|
2018-10-03 20:00:08 +00:00
|
|
|
return render(request, 'postings/entity_pages/course.html', {'entity': course})
|
|
|
|
|
|
|
|
# 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']
|
|
|
|
entity = RateableEntity.objects.get(pk=entity_id)
|
|
|
|
|
|
|
|
# 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.
|
|
|
|
redirect_path = '/'
|
|
|
|
if entity.entity_type == RateableEntity.UNIVERSITY:
|
|
|
|
redirect_path = '/universities/' + str(entity_id)
|
|
|
|
elif entity.entity_type == RateableEntity.COURSE:
|
|
|
|
redirect_path = '/courses/' + str(entity_id)
|
|
|
|
return HttpResponseRedirect(redirect_path)
|
|
|
|
|
|
|
|
return HttpResponseBadRequest("Bad Request")
|