diff --git a/backend/RateMyCourse/urls.py b/backend/RateMyCourse/urls.py index 00a147e..2f70aec 100644 --- a/backend/RateMyCourse/urls.py +++ b/backend/RateMyCourse/urls.py @@ -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/ routes to a specific university. - path('universities/', views.university_entity, name='university entity'), - - # /courses routes to a list of courses. - path('courses', views.courses, name='courses_list'), - - # /courses/ routes to a specific course. - path('courses/', views.course_entity, name='course entity'), + # /rateables/ routes to a specific rateable entity. + path('rateables/', views.rateable_entity, name='rateable_entity'), # static files (*.css, *.js, *.jpg etc.) served on / # (assuming Django uses /static/ and /media/ for static/media urls) diff --git a/backend/db.sqlite3 b/backend/db.sqlite3 index 823e972..e6ee6dc 100644 Binary files a/backend/db.sqlite3 and b/backend/db.sqlite3 differ diff --git a/frontpage/favicon.ico b/backend/postings/static/postings/favicon.ico similarity index 100% rename from frontpage/favicon.ico rename to backend/postings/static/postings/favicon.ico diff --git a/backend/postings/templates/postings/collections/courses.html b/backend/postings/templates/postings/collections/courses.html deleted file mode 100644 index 0fc63cb..0000000 --- a/backend/postings/templates/postings/collections/courses.html +++ /dev/null @@ -1,11 +0,0 @@ -{% extends "postings/collections/collection.html" %} - -{# Represents a list of university entities. #} - -{% block collection_name %} -

Courses

-{% endblock %} - -{% block entity %} -

{{ entity.name }}

-{% endblock %} \ No newline at end of file diff --git a/backend/postings/templates/postings/collections/universities.html b/backend/postings/templates/postings/collections/universities.html deleted file mode 100644 index bb5374d..0000000 --- a/backend/postings/templates/postings/collections/universities.html +++ /dev/null @@ -1,11 +0,0 @@ -{% extends "postings/collections/collection.html" %} - -{# Represents a list of university entities. #} - -{% block collection_name %} -

Universities

-{% endblock %} - -{% block entity %} -

{{ entity.name }}

-{% endblock %} \ No newline at end of file diff --git a/backend/postings/templates/postings/entity_pages/course.html b/backend/postings/templates/postings/entity_pages/course.html deleted file mode 100644 index 8508196..0000000 --- a/backend/postings/templates/postings/entity_pages/course.html +++ /dev/null @@ -1,11 +0,0 @@ -{% extends "postings/entity_pages/entity.html" %} - -{% block entity_info %} - Taught at: {{ entity.taught_at_university.name }} -

Professors

-
    - {% for professor in entity.professors.all %} -
  • {{ professor.name }}
  • - {% endfor %} -
-{% endblock %} \ No newline at end of file diff --git a/backend/postings/templates/postings/index.html b/backend/postings/templates/postings/index.html index 6b4fc7b..2c2743d 100644 --- a/backend/postings/templates/postings/index.html +++ b/backend/postings/templates/postings/index.html @@ -10,8 +10,8 @@ diff --git a/backend/postings/templates/postings/rateables/course.html b/backend/postings/templates/postings/rateables/course.html new file mode 100644 index 0000000..6601e3d --- /dev/null +++ b/backend/postings/templates/postings/rateables/course.html @@ -0,0 +1,11 @@ +{% extends "postings/rateables/entity.html" %} + +{% block entity_info %} + Taught at: {{ entity.taught_at_university.name }} +

Professors

+
    + {% for professor in entity.professors.all %} +
  • {{ professor.name }}
  • + {% endfor %} +
+{% endblock %} \ No newline at end of file diff --git a/backend/postings/templates/postings/collections/collection.html b/backend/postings/templates/postings/rateables/entities.html similarity index 75% rename from backend/postings/templates/postings/collections/collection.html rename to backend/postings/templates/postings/rateables/entities.html index 90d7f3b..46d09b6 100644 --- a/backend/postings/templates/postings/collections/collection.html +++ b/backend/postings/templates/postings/rateables/entities.html @@ -10,9 +10,7 @@
    {% for entity in entities %}
  • - {% block entity %} - {{ entity.name }} - {% endblock %} + {{ entity.name }}
  • {% endfor %}
diff --git a/backend/postings/templates/postings/entity_pages/entity.html b/backend/postings/templates/postings/rateables/entity.html similarity index 79% rename from backend/postings/templates/postings/entity_pages/entity.html rename to backend/postings/templates/postings/rateables/entity.html index 49aa6ff..4b322a5 100644 --- a/backend/postings/templates/postings/entity_pages/entity.html +++ b/backend/postings/templates/postings/rateables/entity.html @@ -4,9 +4,10 @@ {% block content %} -

Name: {{ entity.name }}

Average rating: {{ entity.average_rating|floatformat:"-2" }} +

Name: {{ entity.name }} {{ entity.average }}

+

Average rating: {{ entity.average_rating|floatformat:"-2" }}

-{# 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 @@
    {% for review in entity.review_set.all %}
  • -

    {{ review.title }}

    {{ review.rating }} -

    {{ review.content }} + {% include "postings/rateables/review.html" with review=review only %}

  • {% endfor %}
diff --git a/backend/postings/templates/postings/rateables/review.html b/backend/postings/templates/postings/rateables/review.html new file mode 100644 index 0000000..7d60352 --- /dev/null +++ b/backend/postings/templates/postings/rateables/review.html @@ -0,0 +1,7 @@ +{# Template for displaying one review. #} + +

{{ review.title }}

+Rating: {{ review.rating }}
+Posted on {{ review.created_date|date:"j M, Y" }}
+ +

{{ review.content }}

\ No newline at end of file diff --git a/backend/postings/templates/postings/entity_pages/university.html b/backend/postings/templates/postings/rateables/university.html similarity index 65% rename from backend/postings/templates/postings/entity_pages/university.html rename to backend/postings/templates/postings/rateables/university.html index 0ea0b8c..c477e6b 100644 --- a/backend/postings/templates/postings/entity_pages/university.html +++ b/backend/postings/templates/postings/rateables/university.html @@ -1,10 +1,10 @@ -{% extends "postings/entity_pages/entity.html" %} +{% extends "postings/rateables/entity.html" %} {% block entity_info %}

Courses

Professors

diff --git a/backend/postings/views.py b/backend/postings/views.py index 4eb60cb..1f65c22 100644 --- a/backend/postings/views.py +++ b/backend/postings/views.py @@ -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/ 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'] - entity = RateableEntity.objects.get(pk=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") \ No newline at end of file