From bf96383030016e83c96cc22967070f805239c52f Mon Sep 17 00:00:00 2001 From: andrewlalis Date: Thu, 27 Sep 2018 14:41:34 +0200 Subject: [PATCH] Added functionality for mvp. --- backend/RateMyCourse/urls.py | 4 +- backend/db.sqlite3 | Bin 70656 -> 71680 bytes backend/postings/api/serializers.py | 5 +- .../migrations/0002_auto_20180927_1115.py | 18 ++ backend/postings/models.py | 2 +- frontpage/index.html | 285 ++++++++++++++---- frontpage/index2.html | 255 ---------------- frontpage/js/load_reviews.js | 19 +- frontpage/js/main.js | 33 -- frontpage/js/post_review.js | 47 +++ 10 files changed, 301 insertions(+), 367 deletions(-) create mode 100644 backend/postings/migrations/0002_auto_20180927_1115.py delete mode 100644 frontpage/index2.html create mode 100644 frontpage/js/post_review.js diff --git a/backend/RateMyCourse/urls.py b/backend/RateMyCourse/urls.py index a3f6829..9cbc84d 100644 --- a/backend/RateMyCourse/urls.py +++ b/backend/RateMyCourse/urls.py @@ -20,8 +20,8 @@ from django.contrib.staticfiles.views import serve from django.views.generic import RedirectView urlpatterns = [ - # / routes to index2.html - url(r'^$', serve, kwargs={'path': 'index2.html'}), + # / routes to index.html + url(r'^$', serve, kwargs={'path': 'index.html'}), # 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 2361f255e3df37658e8c6ea71bfc8cccad0c2947..359edf330bb4fdc20cdd32a017fd03ced1c164be 100644 GIT binary patch delta 1180 zcmbW$O-vI(6ae6vc?<3GBdkUXbs^0@3uyvk`x1^rUnQHO({^D651+VAViab z5hb29n}9-M8jXgV*m}`}dhq5I4_=Jm1uKR`4<7X3G(vz7dT=N6W|Pf)FK=hw)Dknb z%-pT#IuJsBI$d;PIxpR8lyS%N{z1V0+&ehS9QGmbK_2?x3CzP)cn{7T$GE|H1j9)@ zG#pMulc9JlIvO@($wZ2?`pP3U9F=yShAL>m7WbgWdb?2Sv^xNrwl{`{;;}?%BpGG1 z70gI23o8izz%Td#-(elTz$f?!D>UCLsAGV|Y$4bS!Bz;Rgf0FbvVqROtYM zcS%$zEiMR(q$~1%A-z?HOo@`Bgf3|wMOXF{TY^Fbrl_DP9$7ckJwkf-&r~S^MG4iQ z=b>tQh~0ul1yWQ{G*y-*eOqwsJ5k*Yx#ip4)n$YP5u6}F5~Me!jpR1(Kf^hW-voBB(Rs*> z#Up&+d^aEH>FRP@YwPotls|1vetl_8h)ak@S!#it6rG5WS-274&9ZF;rd3&HWtaYby&yF$lp!^m}K} e3`3ZZSz5t0LdxlqCio6rOoAPBiQ}gIw delta 471 zcmZqJz|ydSWrDPzIRgWOD-gp#)I=R)M)Qpc$;^zdn~yL%u?Sc(Fu!EJ!W_Z8lzBRH zA+yzHK^7HeRvA8Kd(O!hxFsh?aHld#O>`6!6=r7EkYd z!_4rWfoUG&2_W@;vLMU-&4IJlGx9gZo3o4C+cUQKmLw+Sq=Fq%_Ck_TWAcO-4<}E0 zDKa_rr53j)#Kbua%yWPyPGU|;V&q_OR%CQk)D6na$}TR=%PdPRD$XoP%`8!HFUrr$ z%u7$r^GM7~$xL<3OQ~cH$w&d<+LG%_%>&^55sH8OWA zP0m&*$w*Zw$j`}4PSxAAfW-vp>Q)9OM@Bb>R%SDxt4lYxURl7zcn`?DwJC;4Kp>xi zmEjQsvl&x4VbCOIGC8HF)*KIo(A+=7xU(6pR*JIR5g%h diff --git a/backend/postings/api/serializers.py b/backend/postings/api/serializers.py index b8052b0..af78342 100644 --- a/backend/postings/api/serializers.py +++ b/backend/postings/api/serializers.py @@ -15,8 +15,7 @@ class UniversityReviewSerializer(serializers.ModelSerializer): 'content', ] read_only_fields =[ - 'pk', - 'username' + 'pk' ] # Serializes the generic Review object. @@ -75,4 +74,4 @@ class ProfessorSerializer(serializers.ModelSerializer): ] read_only_fields = [ 'pk' - ] \ No newline at end of file + ] diff --git a/backend/postings/migrations/0002_auto_20180927_1115.py b/backend/postings/migrations/0002_auto_20180927_1115.py new file mode 100644 index 0000000..68acb33 --- /dev/null +++ b/backend/postings/migrations/0002_auto_20180927_1115.py @@ -0,0 +1,18 @@ +# Generated by Django 2.1.1 on 2018-09-27 11:15 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('postings', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='universityreview', + name='date_published', + field=models.DateTimeField(auto_now_add=True), + ), + ] diff --git a/backend/postings/models.py b/backend/postings/models.py index b1d8bb8..f6cf652 100644 --- a/backend/postings/models.py +++ b/backend/postings/models.py @@ -50,6 +50,6 @@ class UniversityReview(models.Model): username = models.CharField(max_length=200) rating = models.IntegerField(default=1) title = models.CharField(max_length=200) - date_published = models.DateField('date published') + date_published = models.DateTimeField(auto_now_add=True) content = models.CharField(max_length=200) \ No newline at end of file diff --git a/frontpage/index.html b/frontpage/index.html index 3b3b033..fdc2fa0 100644 --- a/frontpage/index.html +++ b/frontpage/index.html @@ -3,81 +3,240 @@ - - - - - - - + + + + + + + - - - - + + + + - - - - - -
-
-

Find rated courses

-

The best course reviews by your fellow students!

-
-
-
- -
- + +
+ +
+
+

Software engineering and startups

+

Rijksuniversiteit Groningen

+
+
+
+ + +
+
+
+
+
+ + + - +
+
+
+ +
+
+
- - - - - - - - + + + + + + + + + + + + + diff --git a/frontpage/index2.html b/frontpage/index2.html deleted file mode 100644 index c3e23b5..0000000 --- a/frontpage/index2.html +++ /dev/null @@ -1,255 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - -
- -
-
-

Software engineering and startups

-

Rijksuniversiteit Groningen

-
-
-
-
-
-
-

Average user rating

-

3 / 5

- - - - - -
-
-
-

Rating breakdown

-
-
-
5
-
-
-
-
- 80% Complete (danger) -
-
-
-
0
-
-
-
-
4
-
-
-
-
- 80% Complete (danger) -
-
-
-
1
-
-
-
-
3
-
-
-
-
- 80% Complete (danger) -
-
-
-
1
-
-
-
-
2
-
-
-
-
- 80% Complete (danger) -
-
-
-
1
-
-
-
-
1
-
-
-
-
- 80% Complete (danger) -
-
-
-
0
-
-
-
- -
-
-
-
- - - - -
-
-
-
- - - -
- -
-
- - - -
-
-
- -
- - - - - - - - - - - - - - - - diff --git a/frontpage/js/load_reviews.js b/frontpage/js/load_reviews.js index 3902ad8..ed4c725 100644 --- a/frontpage/js/load_reviews.js +++ b/frontpage/js/load_reviews.js @@ -1,28 +1,27 @@ var $ = jQuery; +Handlebars.registerHelper('times', function(n, block) { + var accum = ''; + for(var i = 0; i < n; ++i) + accum += block.fn(i); + return accum; +}); + // Load the handlebars template and use it to append all reviews. function displayReviews(reviews) { var template = Handlebars.compile($("#review_item_handlebars").html()); var container = $("#review_container"); + container.remove(".review_item"); reviews.forEach(function (review) { var review_html = template(review); container.append(review_html); - // If a rating is defined, then hide only those rating stars which are higher than the current rating. - // TODO: remove this check once this is added to the database. - if (typeof(review.rating) !== "undefined") { - $(".js_review_rating").each(function (index) { - if ((index + 1) > review.rating) { - this.hide(); - } - }); - } container.append("

"); }); } function loadReviews () { $.ajax({ - url: "http://localhost:8000/api/postings/", + url: "/api/postings/", method: "GET" }).done(function (response) { displayReviews(response); diff --git a/frontpage/js/main.js b/frontpage/js/main.js index dc305f4..b61dd7d 100644 --- a/frontpage/js/main.js +++ b/frontpage/js/main.js @@ -34,37 +34,4 @@ $(function(){ closeReviewBtn.hide(); }); - - $('.starrr').on('starrr:change', function(e, value){ - ratingsField.val(value); - }); - var i; - for(i = 1; i<6; i++) { - $(".stars").append(""); - } - $(".stars .btn").click(function(e, value) { - $(".starrr").attr("data-rating", this.value); - for (let j = 1; j <= this.value; j++) { - $("#star" + j).removeClass("btn-default btn-grey").addClass("btn-warning"); - } - for (let g = this.value; g <= 5; g++) { - $("#star" + (g+1)).addClass("btn-default btn-grey").removeClass("btn-warning"); - } - }); - - - var d = new Date(); - - var month = d.getMonth()+1; - var day = d.getDate(); - - var output = - - ((''+day).length<2 ? '0' : '') + day + '/' + - ((''+month).length<2 ? '0' : '') + month + '/' + - d.getFullYear(); - $(".date").append(output); - }); \ No newline at end of file diff --git a/frontpage/js/post_review.js b/frontpage/js/post_review.js new file mode 100644 index 0000000..33d0bfc --- /dev/null +++ b/frontpage/js/post_review.js @@ -0,0 +1,47 @@ +var $ = jQuery; + +// Sends POST request to api. +function postReview (data) { + $.ajax({ + url: "api/postings/", + method: "POST", + data: JSON.stringify(data), + contentType: "application/json", + headers: { + 'X-CSRFToken': Cookies.get("csrftoken") + } + }).done(function (response) { + var template = Handlebars.compile($("#review_item_handlebars").html()); + var container = $("#review_container"); + var review_html = template(response); + container.append(review_html); + container.append("

"); + }).fail(function (response) { + console.log(response); + alert("The review could not be sent."); + }).always(function () { + $("#submit_review_form").trigger("reset"); + $('#close-review-box').trigger("click"); + }); +} + +// What to do when the user clicks submit. +function onSubmitReviewClicked () { + var serialized_data = $("#submit_review_form").serializeArray(); + var formatted_data = {}; + for (var i = 0; i < serialized_data.length; i++) { + formatted_data[serialized_data[i]['name']] = serialized_data[i]['value']; + } + console.log(formatted_data); + postReview(formatted_data); +} + +// Add a listener for when the user clicks to submit a review. +$(document.body) + .off("click.submit_review_button") + .on("click.submit_review_button", + "#submit_review_button", + function (event) { + event.preventDefault(); + onSubmitReviewClicked(); + }); \ No newline at end of file -- 2.34.1