Skip to content

Commit

Permalink
Added review page
Browse files Browse the repository at this point in the history
  • Loading branch information
cnscwong committed Jan 13, 2024
1 parent b1b3443 commit a20e6a5
Show file tree
Hide file tree
Showing 6 changed files with 131 additions and 25 deletions.
2 changes: 1 addition & 1 deletion functions/get-dealership.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ app.get('/dealerships/get', (req, res) => {

const queryOptions = {
selector,
limit: 10, // Limit the number of documents returned to 10
limit: 20, // Limit the number of documents returned to 10
};

db.find(queryOptions, (err, body) => {
Expand Down
Binary file modified server/db.sqlite3
Binary file not shown.
2 changes: 1 addition & 1 deletion server/djangoapp/restapis.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ def analyze_review_sentiments(text):
authenticator = IAMAuthenticator(api_key)
natural_language_understanding = NaturalLanguageUnderstandingV1(version='2021-08-01',authenticator=authenticator)
natural_language_understanding.set_service_url(url)
response = natural_language_understanding.analyze( text=text,features=Features(sentiment=SentimentOptions(targets=[text]))).get_result()
response = natural_language_understanding.analyze( text=text + "hello hello hello",features=Features(sentiment=SentimentOptions(targets=[text + "hello hello hello"]))).get_result()
label=json.dumps(response, indent=2)
label = response['sentiment']['document']['label']

Expand Down
54 changes: 53 additions & 1 deletion server/djangoapp/templates/djangoapp/add_review.html
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,58 @@

</head>
<body>
<!--Add review form here -->
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="{% url 'djangoapp:index' %}">Best Cars</a>
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="{% url 'djangoapp:about' %}">About Us</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="{% url 'djangoapp:contact' %}">Contact Us</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
{% if user.is_authenticated %}
<li>
<a class="btn btn-link" href="#">{{ user.username }}</a>
<a class="btn btn-link" href="{% url 'djangoapp:logout' %}">Logout</a>
</li>
{% else %}
<li>
<form class="form-inline" action="{% url 'djangoapp:login' %}" method="post">
{% csrf_token %}
<div class="input-group">
<input type="text" class="form-control" placeholder="Username" name="username" >
<input type="password" class="form-control" placeholder="Password" name="psw" >
<button class="btn btn-primary" type="submit">Login</button>
<a class="btn btn-link" href="{% url 'djangoapp:registration' %}">Sign Up</a>
</div>
</form>
</li>
{% endif %}
</ul>
</nav>

<h2 class="ml-5">Add Review</h2>
<form action="{% url 'djangoapp:add_review' dealer_id %}" method="post">
{% csrf_token %}
<div class="form-group mr-5 ml-5">
<label for="username">Username</label>
<input type="text" class="form-control" name="username" placeholder="Enter User Name:" required>
</div>
<div class="form-group mr-5 ml-5">
<label for="first_name">First Name</label>
<input type="text" class="form-control" name="first_name" placeholder="Enter First Name:" required>
</div>
<div class="form-group mr-5 ml-5">
<label for="last_name">Last Name</label>
<input type="text" class="form-control" name="last_name" placeholder="Enter Last Name:" required>
</div>
<div class="form-group mr-5 ml-5">
<label for="password">Password</label>
<input type="password" class="form-control" name="psw" placeholder="Enter Password:" required>
</div>
<button type="submit" class="btn btn-primary ml-5">Sign Up</button>
</form>
</body>
</html>
54 changes: 51 additions & 3 deletions server/djangoapp/templates/djangoapp/dealer_details.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,57 @@

<body>

<nav></nav>

<!-- Add reviews as cards -->
<nav class="navbar navbar-expand-lg navbar-light bg-light">
<a class="navbar-brand" href="{% url 'djangoapp:index' %}">Best Cars</a>
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="{% url 'djangoapp:about' %}">About Us</a>
</li>
<li class="nav-item active">
<a class="nav-link" href="{% url 'djangoapp:contact' %}">Contact Us</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
{% if user.is_authenticated %}
<li>
<a class="btn btn-link" href="djangoapp/add_review/{{dealer_id}}">Add Review</a>
<a class="btn btn-link" href="#">{{ user.username }}</a>
<a class="btn btn-link" href="{% url 'djangoapp:logout' %}">Logout</a>
</li>
{% else %}
<li>
<form class="form-inline" action="{% url 'djangoapp:login' %}" method="post">
{% csrf_token %}
<div class="input-group">
<input type="text" class="form-control" placeholder="Username" name="username" >
<input type="password" class="form-control" placeholder="Password" name="psw" >
<button class="btn btn-primary" type="submit">Login</button>
<a class="btn btn-link" href="{% url 'djangoapp:registration' %}">Sign Up</a>
</div>
</form>
</li>
{% endif %}
</ul>
</nav>
<h1>Reviews</h1>
<div class="card-columns">
{% for review in reviews %}
<div class="card">
{% if review.sentiment == 'positive' %}
<img class="card-img-left" src="{{MEDIA_URL}}/emoji/positive.png" height="30" width="30">
{% elif review.sentiment == 'negative' %}
<img class="card-img-left" src="{{MEDIA_URL}}/emoji/negative.png" height="30" width="30">
{% else %}
<img class="card-img-left" src="{{MEDIA_URL}}/emoji/neutral.png" height="30" width="30">
{% endif %}
<div class="card-body">
<h5 class="card-title">{{review.car_model}}, {{review.car_make}}</h5>
<h5 class="card-title">{{review.car_year}}</h5>
<p class="card-text">{{review.review}}</p>
</div>
</div>
{% endfor %}
</div>

</body>

Expand Down
44 changes: 25 additions & 19 deletions server/djangoapp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,28 +90,34 @@ def get_dealer_details(request, dealer_id):
if request.method == "GET":
url = f"https://cncw18-5000.theiadockernext-0-labs-prod-theiak8s-4-tor01.proxy.cognitiveclass.ai/api/get_reviews?id={dealer_id}"
reviews = get_dealer_reviews_from_cf(url)
text = ' '.join([f"{review.review}: {review.sentiment}" for review in reviews])
return HttpResponse(text)
context["dealer_id"] = dealer_id
context["reviews"] = reviews
return render(request, 'djangoapp/dealer_details.html', context)

def add_review(request, dealer_id):
if request.user.is_authenticated:
url = "https://cncw18-5000.theiadockernext-0-labs-prod-theiak8s-4-tor01.proxy.cognitiveclass.ai/api/post_review"
review = {
"id": 1114,
"name": "Upkar Lidder",
"dealership": dealer_id,
"review": "Great service!",
"purchase": False,
"another": "field",
"purchase_date": "02/16/2021",
"car_make": "Audi",
"car_model": "Car",
"car_year": 2021
}
json_payload = {}
json_payload["review"] = review
res = post_request(url, json_payload, dealerId=dealer_id)
return HttpResponse(res["message"])
if request.method == "GET":
context = {}
context["dealer_id"] = dealer_id
return render(request, 'djangoapp/add_review.html', context)
elif request.method == "POST":
url = "https://cncw18-5000.theiadockernext-0-labs-prod-theiak8s-4-tor01.proxy.cognitiveclass.ai/api/post_review"
review = {
"id": 1114,
"name": "Upkar Lidder",
"dealership": dealer_id,
"review": "Great service!",
"purchase": False,
"another": "field",
"purchase_date": "02/16/2021",
"car_make": "Audi",
"car_model": "Car",
"car_year": 2021
}
json_payload = {}
json_payload["review"] = review
res = post_request(url, json_payload, dealerId=dealer_id)
return HttpResponse(res["message"])
else:
context = {}
context['message'] = "You must be logged in to post a review"
Expand Down

0 comments on commit a20e6a5

Please sign in to comment.