diff --git a/functions/reviews.py b/functions/reviews.py index 9653033e3c..8194a7121e 100644 --- a/functions/reviews.py +++ b/functions/reviews.py @@ -1,6 +1,6 @@ from cloudant.client import Cloudant from cloudant.query import Query -from flask import Flask, jsonify, request +from flask import Flask, jsonify, request, abort import atexit #Add your Cloudant service credentials here @@ -56,6 +56,7 @@ def post_review(): # Extract review data from the request JSON review_data = request.json + print(review_data) # Validate that the required fields are present in the review data required_fields = ['id', 'name', 'dealership', 'review', 'purchase', 'purchase_date', 'car_make', 'car_model', 'car_year'] diff --git a/server/db.sqlite3 b/server/db.sqlite3 index 1d34d38a3b..f9855cc1fb 100644 Binary files a/server/db.sqlite3 and b/server/db.sqlite3 differ diff --git a/server/djangoapp/restapis.py b/server/djangoapp/restapis.py index 87f212a385..acab121efc 100644 --- a/server/djangoapp/restapis.py +++ b/server/djangoapp/restapis.py @@ -39,6 +39,26 @@ def get_request(url, api_key=None, **kwargs): # Create a `post_request` to make HTTP POST requests # e.g., response = requests.post(url, params=kwargs, json=payload) +def post_request(url, json_payload, **kwargs): + print("POST to {} ".format(url)) + response = requests.post( + url, + params=kwargs, + data=json.dumps(json_payload), + headers={'Content-Type': 'application/json'}, + ) + + status_code = response.status_code + print("With status {} ".format(status_code)) + print("Response text: ", response.text) + + try: + json_data = json.loads(response.text) + return json_data + except json.JSONDecodeError: + print("Error decoding JSON response") + return None + # Create a get_dealers_from_cf method to get dealers from a cloud function @@ -70,23 +90,41 @@ def get_dealers_from_cf(url, **kwargs): return results +# def get_dealer_by_id_from_cf(url, dealerId): +def get_dealer_by_id_from_cf(url, dealer_id): + json_result = get_request(url, id=dealer_id) + if json_result: + print(json_result,'dealer_get') + dealer = json_result[0] + + dealer_obj = CarDealer( + address=dealer["address"], + city=dealer["city"], + full_name=dealer["full_name"], + id=dealer["id"], + lat=dealer["lat"], + long=dealer["long"], + short_name=dealer["short_name"], + st=dealer["st"], + zip=dealer["zip"] + ) + return dealer_obj + # Create a get_dealer_reviews_from_cf method to get reviews by dealer id from a cloud function -# def get_dealer_by_id_from_cf(url, dealerId): # - Call get_request() with specified arguments # - Parse JSON results into a DealerView object list def get_dealer_reviews_from_cf(url, dealer_id): results = [] # Call get_request with a URL parameter - json_result = get_request(url, api_key=None, id=dealer_id) - print("IT IS IN GET DEALER REVIEWS FROM CF") + json_result = get_request(url, api_key=None, dealer_id=dealer_id) if isinstance(json_result, list): reviews = json_result # Print or log the reviews for debugging print(reviews) # For each dealer object for review in reviews: - # Get its content in `doc` object + # Get its content review_obj = DealerReview( dealership=review.get("dealership", ""), name=review.get("name", ""), diff --git a/server/djangoapp/templates/djangoapp/add_review.html b/server/djangoapp/templates/djangoapp/add_review.html index 768ddf508c..f8e2da696e 100644 --- a/server/djangoapp/templates/djangoapp/add_review.html +++ b/server/djangoapp/templates/djangoapp/add_review.html @@ -10,7 +10,48 @@ - - - + + +
+

Add Review for {{dealer.full_name}}

+
+ +
+ {% csrf_token %} +
+
Your Name:

+ {% csrf_token %} +
+
+
Enter your review here:
+ +
+
+ +
+
+
Select your car (Model-Make-Year):
+ +
+
+ + +
+ + +
+ +
+ + + \ No newline at end of file diff --git a/server/djangoapp/templates/djangoapp/dealer_details.html b/server/djangoapp/templates/djangoapp/dealer_details.html index 25bd9a223d..2d3216d9ea 100644 --- a/server/djangoapp/templates/djangoapp/dealer_details.html +++ b/server/djangoapp/templates/djangoapp/dealer_details.html @@ -7,12 +7,85 @@ - + + + + +
+

Reviews for {{ dealer.full_name }}

+
- +
+{% if reviews_list %} +
+ {% for review in reviews_list %} +
+ {% if review.sentiment == "positive" %} + + {% elif review.sentiment == "neutral" %} + + {% elif review.sentiment == "negative" %} + + {% else %} + + {% endif %} - +
+ {% if review.purchase %} +
{{review.car_make}}
+
{{review.car_model}}
+
{{review.purchase_date}}
+ {% else %} +
Reviewer did not buy any car
+ {% endif %} + +
+
+ {% endfor %} +
+{% else %} +
+
+
+
No reviews yet
+
+
+
+{% endif %} +
+ \ No newline at end of file diff --git a/server/djangoapp/urls.py b/server/djangoapp/urls.py index 7ec646a14e..708f876bb3 100644 --- a/server/djangoapp/urls.py +++ b/server/djangoapp/urls.py @@ -32,8 +32,8 @@ path(route='', view=views.get_dealerships, name='index'), # path for dealer reviews view - path(route='dealer//', view=views.get_dealer_details, name='dealer_details') + path(route='dealer//', view=views.get_dealer_details, name='dealer_details'), # path for add a review view - + path(route='add_review//', view=views.add_review, name='add_review') ] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT) \ No newline at end of file diff --git a/server/djangoapp/views.py b/server/djangoapp/views.py index 3c79b81ace..f682953414 100644 --- a/server/djangoapp/views.py +++ b/server/djangoapp/views.py @@ -1,9 +1,9 @@ from django.shortcuts import render -from django.http import HttpResponseRedirect, HttpResponse +from django.http import HttpResponseRedirect, HttpResponse, JsonResponse from django.contrib.auth.models import User from django.shortcuts import get_object_or_404, render, redirect from .models import CarMake, CarModel, CarDealer, DealerReview -from .restapis import get_dealers_from_cf, get_request, get_dealer_reviews_from_cf +from .restapis import get_dealers_from_cf, get_request, get_dealer_reviews_from_cf, get_dealer_by_id_from_cf, post_request from django.contrib.auth import login, logout, authenticate from django.contrib import messages from datetime import datetime @@ -97,6 +97,7 @@ def get_dealerships(request): url = "https://gduzel-3001.theiadockernext-1-labs-prod-theiak8s-4-tor01.proxy.cognitiveclass.ai/dealerships/get" # Get dealers from the URL dealerships = get_dealers_from_cf(url) + # append the dealership list to context context["dealership_list"] = dealerships # Concat all dealer's short name dealer_names = ' '.join([dealer.short_name for dealer in dealerships]) @@ -106,20 +107,63 @@ def get_dealerships(request): # Create a `get_dealer_details` view to render the reviews of a dealer def get_dealer_details(request, dealer_id): + context={} if request.method == "GET": - url = "https://gduzel-5001.theiadockernext-1-labs-prod-theiak8s-4-tor01.proxy.cognitiveclass.ai/api/get_reviews" + url_reviews = "https://gduzel-5001.theiadockernext-1-labs-prod-theiak8s-4-tor01.proxy.cognitiveclass.ai/api/get_reviews" + url_dealership = "https://gduzel-3001.theiadockernext-1-labs-prod-theiak8s-4-tor01.proxy.cognitiveclass.ai/dealerships/get" # Get reviews from the URL - reviews = get_dealer_reviews_from_cf(url, dealer_id) - # Concat all review's name - review_names = ' '.join([review.name for review in reviews]) - # Return a list of dealer short name - print(review_names) - return HttpResponse(review_names) + reviews = get_dealer_reviews_from_cf(url_reviews, dealer_id) + # Get dealership by id from the URL + dealer = get_dealer_by_id_from_cf(url_dealership, dealer_id) + # append the reviews list to context + context["reviews_list"] = reviews + # append the dealer to context + context["dealer"] = dealer + + + return render(request, 'djangoapp/dealer_details.html', context) # Create a `add_review` view to submit a review -# def add_review(request, dealer_id): -# ... +def add_review(request, dealer_id): + context = {} + if request.method == "GET": + url = "https://gduzel-3001.theiadockernext-1-labs-prod-theiak8s-4-tor01.proxy.cognitiveclass.ai/dealerships/get" + dealer = get_dealer_by_id_from_cf(url, dealer_id) + print(dealer) + cars = CarModel.objects.filter(dealer_id=dealer_id) + context["dealer"] = dealer + context["cars"] = cars + return render(request, 'djangoapp/add_review.html', context) + if request.method == "POST": + if request.user.is_authenticated: + url = "https://gduzel-5001.theiadockernext-1-labs-prod-theiak8s-4-tor01.proxy.cognitiveclass.ai/api/post_review" + username = request.user.username + if 'purchasecheck' in request.POST: + was_purchased = True + else: + was_purchased = False + + cars = CarModel.objects.filter(dealer_id=dealer_id) + print(cars,"CARS") + for car in cars: + print(car,"car") + if car.id == int(request.POST['car']): + review_car = car + review = {} + review["id"] = dealer_id + review["name"] = request.POST['name'] + review["dealership"] = dealer_id + review["review"] = request.POST['content'] + review["purchase"] = was_purchased + review["purchase_date"] = request.POST['purchasedate'] + review["car_make"] = review_car.car_make.name + review["car_model"] = review_car.name + review["car_year"] = review_car.year.strftime("%Y") + json_payload = {} + json_payload["review"] = review + response = post_request(url, json_payload, dealer_id=dealer_id) + return redirect("djangoapp:dealer_details", dealer_id=dealer_id) \ No newline at end of file