From fbb0c890a1ae01cad635f269f6080db7939c46d8 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Wed, 3 Oct 2018 22:59:09 +0200 Subject: [PATCH 1/2] Removed old paths, now use just a rateables path for all things. --- backend/RateMyCourse/urls.py | 14 ++--- backend/db.sqlite3 | Bin 77824 -> 77824 bytes .../postings/static/postings}/favicon.ico | Bin .../postings/collections/courses.html | 11 ---- .../postings/collections/universities.html | 11 ---- .../postings/entity_pages/course.html | 11 ---- .../postings/templates/postings/index.html | 4 +- .../templates/postings/rateables/course.html | 11 ++++ .../entities.html} | 4 +- .../{entity_pages => rateables}/entity.html | 8 +-- .../templates/postings/rateables/review.html | 4 ++ .../university.html | 4 +- backend/postings/views.py | 53 ++++++++++-------- 13 files changed, 57 insertions(+), 78 deletions(-) rename {frontpage => backend/postings/static/postings}/favicon.ico (100%) delete mode 100644 backend/postings/templates/postings/collections/courses.html delete mode 100644 backend/postings/templates/postings/collections/universities.html delete mode 100644 backend/postings/templates/postings/entity_pages/course.html create mode 100644 backend/postings/templates/postings/rateables/course.html rename backend/postings/templates/postings/{collections/collection.html => rateables/entities.html} (75%) rename backend/postings/templates/postings/{entity_pages => rateables}/entity.html (79%) create mode 100644 backend/postings/templates/postings/rateables/review.html rename backend/postings/templates/postings/{entity_pages => rateables}/university.html (65%) 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 823e9728193627bbd30a3deb9a2ae0eb65b50980..ef66ed954b5bca41d0b26e7fed3a786748c63b36 100644 GIT binary patch delta 330 zcmZuty-EW?7~JLC%Ys2Mwt{|OkqAlH#Gi}lqE(D3#23h2Tyl$XFL)QiRVLsA=u3#L z9B3n0S@{SdkfhK?5N#}C;cD%`3=G3eG5s;?kJ;t&e2oxtVEBitj7@8|jnyROaX)!+ zv&3OY;2XZ+6DIHiZ!nBg>7{!YZ&tNeiW@7rd(zwe| z#u60j1nQ={2j>I9=LlUN99AlnSVy7kcv7PZ&~;=LkA0mE@k~1Hc7!L|@1dik&{MjF zj_Y?r?08Y2nn4y_V^o+9P^+fI&_~y((`|Vnx)BEcsr0o(rP0BjCuO_oIqEE5+!cjV vt{`$^1B;?8O6AR+yeMsL+oJe?VhfvF<{k>O-;?%X7N_tXfK(ILS;~F@8%kWq delta 134 zcmZp8z|!!5Wr8%X7Xt%>4-kWBrimKnj9wcPVGd}_<`396a$GrK`ez}Crf-3KsCo8Z>@G=4o d^#o!AAl3sac*8tNgJn630wc?I79GYv%mAr%Cz1dF 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..0dec975 --- /dev/null +++ b/backend/postings/templates/postings/rateables/review.html @@ -0,0 +1,4 @@ +{# Template for displaying one review. #} + +

{{ review.title }}

{{ review.rating }} +

{{ 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..481b24a 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): From a8597b1ce425964e8e0ac9a3a36439e228220b56 Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Wed, 3 Oct 2018 23:08:57 +0200 Subject: [PATCH 2/2] Added date display to review html. --- backend/db.sqlite3 | Bin 77824 -> 77824 bytes .../templates/postings/rateables/review.html | 5 ++++- backend/postings/views.py | 12 +++++------- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/backend/db.sqlite3 b/backend/db.sqlite3 index ef66ed954b5bca41d0b26e7fed3a786748c63b36..e6ee6dc39ee4fd2bed65d254cf5f6a73cc64540a 100644 GIT binary patch delta 594 zcmchSu}%U(5QcA^GvUC-#zu|P5fdQ?9EeyE3q!#(;K422hLtmS3rH*gV?(EJVQVgi zSosJP#KeSNLMP`- z8VnS8hc|eEJ9vN_7{C*Z{6+N=7UqEv&^~LxANAK&=^iTZEFF(<4`aB6p}c*S?kfnY zpb%|Z0{-|qYz2Yz6v)q2Nl)rWq}?rOL4|A|uV%AEfh>1f#TFY_tP6A+)ID%niCQ`2 zOiWWnoa25AIV}sUGOI>g({+UkWHs3cn|bJ!Y{%hWX-tGYY&x#6kqIo) zl3DjCwrfrkbJ%3lG_y+4V~1E~mAXqk86-!}N=^+M@;7Zo<9aL|iRlqNf$>;IPh<=| vn%dG0!RY^4yH{{ review.title }} {{ review.rating }} +

{{ 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/views.py b/backend/postings/views.py index 481b24a..1f65c22 100644 --- a/backend/postings/views.py +++ b/backend/postings/views.py @@ -59,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( @@ -70,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