Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

first test #151

Open
wants to merge 14 commits into
base: master
Choose a base branch
from
1 change: 1 addition & 0 deletions .github/workflows/linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ on:
branches: [master, main]
pull_request:
branches: [master, main]
workflow_dispatch:

jobs: # list of things to do
lint_function_js:
Expand Down
28 changes: 28 additions & 0 deletions server/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
FROM python:3.8.2

ENV PYTHONBUFFERED 1
ENV PYTHONWRITEBYTECODE 1

RUN apt-get update \
&& apt-get install -y netcat

# Set the working directory to /app
ENV APP=/app
WORKDIR $APP

# Copy the requirements file into the container
COPY requirements.txt $APP

# Install the requirements
RUN pip3 install -r requirements.txt

# Copy the rest of the project files into the container
COPY . $APP

EXPOSE 8000

RUN chmod +x /app/entrypoint.sh

ENTRYPOINT ["/bin/bash", "/app/entrypoint.sh"]

CMD ["gunicorn", "--bind", ":8000", "--workers", "3", "djangobackend.wsgi"]
Binary file added server/db.sqlite3
Binary file not shown.
29 changes: 29 additions & 0 deletions server/deployment.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
apiVersion: apps/v1
kind: Deployment
metadata:
labels:
run: dealership
name: dealership
spec:
replicas: 1
selector:
matchLabels:
run: dealership
strategy:
rollingUpdate:
maxSurge: 25%
maxUnavailable: 25%
type: RollingUpdate
template:
metadata:
labels:
run: dealership
spec:
containers:
- image: us.icr.io/sn-labs-marekmarkiew/dealership:latest
imagePullPolicy: Always
name: dealership
ports:
- containerPort: 8000
protocol: TCP
restartPolicy: Always
15 changes: 12 additions & 3 deletions server/djangoapp/admin.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,22 @@
from django.contrib import admin
# from .models import related models
from .models import CarMake, CarModel


# Register your models here.

# CarModelInline class
class CarModelInline(admin.StackedInline):
model = CarModel
extra = 1

# CarModelAdmin class
# Register your models here.
@admin.register(CarMake)
class CarMakeAdmin(admin.ModelAdmin):
inlines = [CarModelInline]

# CarModelAdmin class
@admin.register(CarModel)
class CarModelAdmin(admin.ModelAdmin):
pass
# CarMakeAdmin class with CarModelInline

# Register models here
62 changes: 61 additions & 1 deletion server/djangoapp/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,66 @@
from django.db import models
from django.utils.timezone import now
from django.db import models

class CarMake(models.Model):
name = models.CharField(max_length=100)
description = models.TextField()

def __str__(self):
return self.name

class CarModel(models.Model):
make = models.ForeignKey(CarMake, on_delete=models.CASCADE)
dealer_id = models.IntegerField()
name = models.CharField(max_length=100)

TYPE_CHOICES = (
('Sedan', 'Sedan'),
('SUV', 'SUV'),
('WAGON', 'WAGON'),
# Add more choices here
)

type = models.CharField(max_length=10, choices=TYPE_CHOICES)
year = models.DateField()

def __str__(self):
return self.name

class CarDealer(models.Model):
address = models.CharField(max_length=200)
city = models.CharField(max_length=100)
full_name = models.CharField(max_length=100)
id = models.IntegerField(primary_key=True)
lat = models.DecimalField(max_digits=9, decimal_places=6)
long = models.DecimalField(max_digits=9, decimal_places=6)
short_name = models.CharField(max_length=50)
st = models.CharField(max_length=2)
zip = models.CharField(max_length=10)

def __str__(self):
return "Dealer name: " + self.full_name

class DealerReview(models.Model):
dealership = models.CharField(max_length=100)
name = models.CharField(max_length=100)
purchase = models.BooleanField()
review = models.TextField()
purchase_date = models.DateField()
car_make = models.CharField(max_length=100)
car_model = models.CharField(max_length=100)
car_year = models.IntegerField()

SENTIMENT_CHOICES = (
('Positive', 'Positive'),
('Negative', 'Negative'),
('Neutral', 'Neutral'),
)

sentiment = models.CharField(max_length=10, choices=SENTIMENT_CHOICES)
id = models.AutoField(primary_key=True)

def __str__(self):
return self.name

# Create your models here.

Expand Down
88 changes: 87 additions & 1 deletion server/djangoapp/restapis.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
import requests
import json
# import related models here
from .models import CarDealer, DealerReview
from requests.auth import HTTPBasicAuth

WATSON_NLU_API_KEY = 'NgRnMOGD9aZZbFEGJI9B_6EoOc4lYsHbjHXRPEOcKAyx'

# Create a `get_request` to make HTTP GET requests
# e.g., response = requests.get(url, params=params, headers={'Content-Type': 'application/json'},
# auth=HTTPBasicAuth('apikey', api_key))

def get_request(url, api_key=None, **kwargs):
print(kwargs)
print("GET from {} ".format(url))
try:
if api_key:
# Call GET method with basic authentication
response = requests.get(url, headers={'Content-Type': 'application/json'}, params=kwargs, auth=HTTPBasicAuth('apikey', api_key))
else:
# Call GET method without authentication
response = requests.get(url, headers={'Content-Type': 'application/json'}, params=kwargs)
except:
# If any error occurs
print("Network exception occurred")
status_code = response.status_code
print("With status {} ".format(status_code))
json_data = json.loads(response.text)
return json_data

# Create a `post_request` to make HTTP POST requests
# e.g., response = requests.post(url, params=kwargs, json=payload)
Expand All @@ -18,17 +36,85 @@
# - Call get_request() with specified arguments
# - Parse JSON results into a CarDealer object list

def get_dealers_from_cf(url, **kwargs):
results = []
# Call get_request with a URL parameter
json_result = get_request(url)
if json_result:
# Get the row list in JSON as dealers
dealers = json_result["rows"]
# For each dealer object
for dealer in dealers:
# Get its content in `doc` object
dealer_doc = dealer["doc"]
# Create a CarDealer object with values in `doc` object
dealer_obj = CarDealer(address=dealer_doc["address"], city=dealer_doc["city"], full_name=dealer_doc["full_name"],
id=dealer_doc["id"], lat=dealer_doc["lat"], long=dealer_doc["long"],
short_name=dealer_doc["short_name"],
st=dealer_doc["st"], zip=dealer_doc["zip"])
results.append(dealer_obj)
return results


# 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, api_key):
results = []

# Call get_request with a URL parameter
json_result = get_request(url, dealerId=dealer_id, api_key=api_key)

if json_result:
# Get the list of reviews
reviews = json_result

for review in reviews:
review_obj = DealerReview(
dealership=review["dealership"],
name=review["name"],
purchase=review["purchase"],
review=review["review"],
purchase_date=review["purchase_date"],
car_make=review["car_make"],
car_model=review["car_model"],
car_year=review["car_year"],
sentiment=analyze_review_sentiments(review["review"]), # Analyze sentiment
id=review["id"]
)
results.append(review_obj)

return results

# Create an `analyze_review_sentiments` method to call Watson NLU and analyze text
# def analyze_review_sentiments(text):
# - Call get_request() with specified arguments
# - Get the returned sentiment label such as Positive or Negative


def analyze_review_sentiments(text):
url = "https://api.us-south.natural-language-understanding.watson.cloud.ibm.com/instances/b47940d0-c41e-43b6-8430-b92ee938ce46/v1/analyze?version=2020-08-01"

params = {
"text": text,
"version": "2020-08-01",
"features": "sentiment",
"return_analyzed_text": True
}

response = requests.get(url, params=params, headers={'Content-Type': 'application/json'}, auth=HTTPBasicAuth('apikey', WATSON_NLU_API_KEY))

if response.status_code == 200:
json_response = response.json()
if "sentiment" in json_response:
return json_response["sentiment"]["document"]["label"]
else:
return "Unknown"
else:
return "API request failed with status code: " + str(response.status_code)




12 changes: 12 additions & 0 deletions server/djangoapp/templates/djangoapp/about.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>About Us - Dealership Review</title>
</head>
<body>
<h1>Welcome to Best Cars dealership</h1>
<p>Home to the best cars in North America. We sell domestic and imported cars at reasonable prices.</p>
<!-- Place for more infos -->
</body>
</html>
32 changes: 32 additions & 0 deletions server/djangoapp/templates/djangoapp/add_review.html
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,37 @@
</head>
<body>
<!--Add review form here -->
<div class="container mt-4">
<h2>Submit a Review</h2>
<form method="post" action="{% url 'djangoapp:add_review' dealer_id %}">
{% csrf_token %}
<div class="form-group">
<label for="content">Review Text</label>
<textarea class="form-control" id="content" name="content" rows="2" required></textarea>
</div>
<div class="form-check">
<input class="form-check-input" type="checkbox" name="purchasecheck" id="purchasecheck">
<label class="form-check-label" for="purchasecheck">Did you purchase a car from this dealer?</label>
</div>
<div class="form-group">
<label for="car">Select a Car</label>
<select name="car" id="car" class="form-select" required>
{% for car in cars %}
<option value="{{ car.id }}">{{ car.name }}-{{ car.make.name }}-{{ car.year|date:"Y" }}</option>
{% endfor %}
</select>
</div>
<div class="form-group">
<label for="purchasedate">Purchase Date</label>
<input class="form-control" type="text" name="purchasedate" id="purchasedate">
</div>
<script type="text/javascript">
$('.date-own').datepicker({
format: 'mm/dd/yyyy'
});
</script>
<button type="submit" class="btn btn-primary">Submit Review</button>
</form>
</div>
</body>
</html>
12 changes: 12 additions & 0 deletions server/djangoapp/templates/djangoapp/contact.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Contact Us - Dealership Review</title>
</head>
<body>
<h1>Contact Us</h1>
<p>Address: 123 Street, City, Country</p>
<p>Telephone: +1-123-456-7890</p>
</body>
</html>
Loading