From fa67dc6c3478a2ffbbd16835498e392a564b088c Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Wed, 3 Oct 2018 22:00:08 +0200 Subject: [PATCH] Added a form object and the ability to write reviews. Only needs styling now. --- backend/RateMyCourse/urls.py | 3 ++ backend/db.sqlite3 | Bin 77824 -> 77824 bytes backend/postings/forms.py | 12 ++++++ backend/postings/models.py | 2 + .../postings/entity_pages/entity.html | 28 +++++++++++++- backend/postings/views.py | 35 +++++++++++++++++- 6 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 backend/postings/forms.py diff --git a/backend/RateMyCourse/urls.py b/backend/RateMyCourse/urls.py index cc3e5cb..00a147e 100644 --- a/backend/RateMyCourse/urls.py +++ b/backend/RateMyCourse/urls.py @@ -24,6 +24,9 @@ urlpatterns = [ # / routes to index.html path('', views.index, name='homepage'), + # /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'), diff --git a/backend/db.sqlite3 b/backend/db.sqlite3 index ae677ce941400f5703b6f0150e516df40ddb6bc3..823e9728193627bbd30a3deb9a2ae0eb65b50980 100644 GIT binary patch delta 735 zcmZvZzi-n(6vurz+fo_{rGljbsc$Pnt;sn*YG;c&Kr(^g55S49^(l_8@|_!HN-QC< zg8COA7FHBhu<;kb-_Uk|rAryg&<;G87D0-1`kwCnxc7PQ%?{1kq51kQ>AktPMS34z ztX7CUpH*JIuiEs;pcnKf{XviDH+nK(HowrSNz25xC?O@QP;3A6(d~EUClkYu4SGsX z=y!UIX=fPzmEJL_LF_`TLd-(w{Ot3ssA?Mr27OgJFOA3rd1@T&FPm?t`HH<{kk$NQ zr&Bhp$DGFy^K2-FuqTobLZKl`#2!zT&>SDpb;LT1f*=J99CNNA5h!D{$WdVn0}Ugd zh-3#d`i|qcHRf%w+D5Go%x^dA?Z&3#`n9R=HUIBoHP+1UTL`z6uXQ@au%1Uk0UjN3 z=yUZK*rb=?v(E=XrZ~h>E0Ei;9gJkAV3*6?n1j+;D0qlCDPPAw5D@>Gj-v^5Ar?ap z*j-R0FR(WJwu`JDbA9(;)IXlO>z1ThHptqug4BGHa;+zzB7xT>_8<Name: {{ entity.name }} Average rating: {{ entity.average_rating }} +

Name: {{ entity.name }}

Average rating: {{ entity.average_rating|floatformat:"-2" }} +{# Child templates can redefine this block for displaying data pertaining to that specific entity. #} {% block entity_info %} {% endblock %} +{# This section displays all reviews for a given entity. #}

Reviews

    @@ -21,4 +23,28 @@
+{# This section is where the user can write a review for a particular entity and submit it. #} +
+

Write a Review

+
+ + +
+ + + +
+ + + +
+ + {# The following csrf_token and input fields are hidden values needed for form submission. #} + {% csrf_token %} + + + +
+
+ {% endblock %} \ No newline at end of file diff --git a/backend/postings/views.py b/backend/postings/views.py index d8ae6df..4eb60cb 100644 --- a/backend/postings/views.py +++ b/backend/postings/views.py @@ -1,6 +1,7 @@ from django.shortcuts import render -from django.http import HttpResponse, Http404 +from django.http import HttpResponse, Http404, HttpResponseBadRequest, HttpResponseRedirect from postings.models import * +from postings.forms import * # Create your views here. @@ -41,4 +42,34 @@ def course_entity(request, course_id): 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}) \ No newline at end of file + 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") \ No newline at end of file