Refactored directories and cleaned things up #10

Merged
andrewlalis merged 2 commits from al/directory_refactor into develop 2018-10-03 21:10:03 +00:00
13 changed files with 65 additions and 85 deletions

View File

@ -27,17 +27,11 @@ urlpatterns = [
# /reviews routes to the endpoint for POSTing new reviews.
path('reviews', views.post_review, name='post_review'),
# /universities routes to a list of universities.
path('universities', views.universities, name='universities_list'),
# /rateables routes to a list of all rateable items: courses and universities.
path('rateables', views.rateables, name='rateables'),
# /universities/<pk> routes to a specific university.
path('universities/<int:university_id>', views.university_entity, name='university entity'),
# /courses routes to a list of courses.
path('courses', views.courses, name='courses_list'),
# /courses/<pk> routes to a specific course.
path('courses/<int:course_id>', views.course_entity, name='course entity'),
# /rateables/<pk> routes to a specific rateable entity.
path('rateables/<int:entity_id>', views.rateable_entity, name='rateable_entity'),
# static files (*.css, *.js, *.jpg etc.) served on /
# (assuming Django uses /static/ and /media/ for static/media urls)

Binary file not shown.

View File

Before

Width:  |  Height:  |  Size: 766 B

After

Width:  |  Height:  |  Size: 766 B

View File

@ -1,11 +0,0 @@
{% extends "postings/collections/collection.html" %}
{# Represents a list of university entities. #}
{% block collection_name %}
<h2>Courses</h2>
{% endblock %}
{% block entity %}
<h3><a href="/courses/{{ entity.pk }}">{{ entity.name }}</a></h3>
{% endblock %}

View File

@ -1,11 +0,0 @@
{% extends "postings/collections/collection.html" %}
{# Represents a list of university entities. #}
{% block collection_name %}
<h2>Universities</h2>
{% endblock %}
{% block entity %}
<h3><a href="/universities/{{ entity.pk }}">{{ entity.name }}</a></h3>
{% endblock %}

View File

@ -1,11 +0,0 @@
{% extends "postings/entity_pages/entity.html" %}
{% block entity_info %}
Taught at: <a href="/universities/{{ entity.taught_at_university.pk }}">{{ entity.taught_at_university.name }}</a>
<h4>Professors</h4>
<ul>
{% for professor in entity.professors.all %}
<li>{{ professor.name }}</li>
{% endfor %}
</ul>
{% endblock %}

View File

@ -10,8 +10,8 @@
<button type="submit">Search</button>
</form>
<nav>
<a href="/universities">Universities</a>
<a href="/courses">Courses</a>
<a href="/rateables?type=university">Universities</a>
<a href="/rateables?type=course">Courses</a>
</nav>
</section>

View File

@ -0,0 +1,11 @@
{% extends "postings/rateables/entity.html" %}
{% block entity_info %}
Taught at: <a href="/rateables/{{ entity.taught_at_university.pk }}">{{ entity.taught_at_university.name }}</a>
<h4>Professors</h4>
<ul>
{% for professor in entity.professors.all %}
<li>{{ professor.name }}</li>
{% endfor %}
</ul>
{% endblock %}

View File

@ -10,9 +10,7 @@
<ul>
{% for entity in entities %}
<li>
{% block entity %}
{{ entity.name }}
{% endblock %}
<a href="/rateables/{{ entity.pk }}">{{ entity.name }}</a>
</li>
{% endfor %}
</ul>

View File

@ -4,9 +4,10 @@
{% block content %}
<h2>Name: {{ entity.name }}</h2> Average rating: {{ entity.average_rating|floatformat:"-2" }}
<h2>Name: {{ entity.name }} {{ entity.average }}</h2>
<h3>Average rating: {{ entity.average_rating|floatformat:"-2" }}</h3>
{# Child templates can redefine this block for displaying data pertaining to that specific entity. #}
{# Depending on the type of entity, some detailed info is provided here. #}
{% block entity_info %}
{% endblock %}
@ -16,8 +17,7 @@
<ul>
{% for review in entity.review_set.all %}
<li>
<h4>{{ review.title }}</h4> {{ review.rating }}
<p>{{ review.content }}
{% include "postings/rateables/review.html" with review=review only %}
</li>
{% endfor %}
</ul>

View File

@ -0,0 +1,7 @@
{# Template for displaying one review. #}
<h4>{{ review.title }}</h4>
<i>Rating: {{ review.rating }}</i><br>
<i>Posted on {{ review.created_date|date:"j M, Y" }}</i><br>
<p>{{ review.content }}</p>

View File

@ -1,10 +1,10 @@
{% extends "postings/entity_pages/entity.html" %}
{% extends "postings/rateables/entity.html" %}
{% block entity_info %}
<h4>Courses</h4>
<ul>
{% for course in entity.course_set.all %}
<li><a href="/courses/{{ course.pk }}">{{ course.name }}</a></li>
<li><a href="/rateables/{{ course.pk }}">{{ course.name }}</a></li>
{% endfor %}
</ul>
<h4>Professors</h4>

View File

@ -11,38 +11,43 @@ def index(request):
search_query = request.GET.get('search_query', None)
results = None
if search_query:
# Filter objects based on case-insensitive contains filter.
results = RateableEntity.objects.filter(name__icontains=search_query)
return render(request, 'postings/index.html', {'results': results})
# The view for a listing of universities.
def universities(request):
universities_list = University.objects.all()
context = {'entities': universities_list}
return render(request, 'postings/collections/universities.html', context)
# 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 /universities/<pk> Displays one university entity.
def university_entity(request, university_id):
# The view for any rateable entity.
def rateable_entity(request, entity_id):
try:
university = University.objects.get(pk=university_id)
university.average_rating = university.getAverageRating()
except University.DoesNotExist:
raise Http404("University does not exist")
return render(request, 'postings/entity_pages/university.html', {'entity': university})
entity = RateableEntity.objects.get(pk=entity_id)
# The view for a listing of courses.
def courses(request):
courses_list = Course.objects.all()
context = {'entities': courses_list}
return render(request, 'postings/collections/courses.html', context)
# 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"
# 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")
return render(request, 'postings/entity_pages/course.html', {'entity': course})
# Set any auxiliary variables needed, like average rating.
# This MUST be done after categorizing the object above.
entity.average_rating = entity.getAverageRating()
except RateableEntity.DoesNotExist:
raise Http404("RateableEntity with id " + str(entity_id) + " does not exist.")
return render(request, "postings/rateables/" + template, {'entity': entity})
# The view for receiving POST requests for new reviews.
def post_review(request):
@ -54,7 +59,10 @@ def post_review(request):
title = form.cleaned_data['title']
content = form.cleaned_data['content']
entity_id = form.cleaned_data['entity_id']
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(
@ -65,11 +73,6 @@ def post_review(request):
)
# 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 HttpResponseRedirect('/rateables/' + str(entity_id))
return HttpResponseBadRequest("Bad Request")