Skip to content

Commit a7cea5f

Browse files
committed
m
1 parent 153b231 commit a7cea5f

File tree

8 files changed

+136
-18
lines changed

8 files changed

+136
-18
lines changed

server/djangoapp/admin.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1-
# from django.contrib import admin
2-
# from .models import related models
1+
from django.contrib import admin
2+
from .models import CarMake, CarModel
33

44

55
# Register your models here.
6+
admin.site.register(CarMake)
7+
admin.site.register(CarModel)
68

79
# CarModelInline class
810

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Generated by Django 5.2.4 on 2025-07-20 16:03
2+
3+
import django.core.validators
4+
import django.db.models.deletion
5+
from django.db import migrations, models
6+
7+
8+
class Migration(migrations.Migration):
9+
10+
initial = True
11+
12+
dependencies = [
13+
]
14+
15+
operations = [
16+
migrations.CreateModel(
17+
name='CarMake',
18+
fields=[
19+
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
20+
('name', models.CharField(max_length=100)),
21+
('description', models.TextField()),
22+
],
23+
),
24+
migrations.CreateModel(
25+
name='CarModel',
26+
fields=[
27+
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
28+
('dealer_id', models.IntegerField(null=True)),
29+
('name', models.CharField(max_length=100)),
30+
('type', models.CharField(choices=[('SEDAN', 'Sedan'), ('SUV', 'SUV'), ('WAGON', 'Wagon'), ('TRUCK', 'Truck'), ('HATCHBACK', 'Hatchback'), ('COUPE', 'Coupe'), ('CONVERTIBLE', 'Convertible'), ('MINIVAN', 'Minivan'), ('PICKUP', 'Pickup')], default='SUV', max_length=15)),
31+
('year', models.IntegerField(default=2023, validators=[django.core.validators.MaxValueValidator(2023), django.core.validators.MinValueValidator(2015)])),
32+
('car_make', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='djangoapp.carmake')),
33+
],
34+
),
35+
]

server/djangoapp/migrations/__init__.py

Whitespace-only changes.

server/djangoapp/models.py

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Uncomment the following imports before adding the Model code
22

3-
# from django.db import models
4-
# from django.utils.timezone import now
5-
# from django.core.validators import MaxValueValidator, MinValueValidator
3+
from django.db import models
4+
from django.utils.timezone import now
5+
from django.core.validators import MaxValueValidator, MinValueValidator
66

77

88
# Create your models here.
@@ -13,6 +13,13 @@
1313
# - Any other fields you would like to include in car make model
1414
# - __str__ method to print a car make object
1515

16+
class CarMake(models.Model):
17+
name = models.CharField(max_length=100)
18+
description = models.TextField()
19+
20+
def __str__(self):
21+
return self.name
22+
1623

1724
# <HINT> Create a Car Model model `class CarModel(models.Model):`:
1825
# - Many-To-One relationship to Car Make model (One Car Make has many
@@ -23,3 +30,28 @@
2330
# - Year (IntegerField) with min value 2015 and max value 2023
2431
# - Any other fields you would like to include in car model
2532
# - __str__ method to print a car make object
33+
class CarModel(models.Model):
34+
car_make = models.ForeignKey(CarMake, on_delete=models.CASCADE) # Many-to-One relationship
35+
dealer_id = models.IntegerField(null=True) # Refers to a dealer created in Cloudant database
36+
name = models.CharField(max_length=100)
37+
CAR_TYPES = [
38+
('SEDAN', 'Sedan'),
39+
('SUV', 'SUV'),
40+
('WAGON', 'Wagon'),
41+
('TRUCK', 'Truck'),
42+
('HATCHBACK', 'Hatchback'),
43+
('COUPE', 'Coupe'),
44+
('CONVERTIBLE', 'Convertible'),
45+
('MINIVAN', 'Minivan'),
46+
('PICKUP', 'Pickup'),
47+
# Add more choices as required
48+
]
49+
type = models.CharField(max_length=15, choices=CAR_TYPES, default='SUV')
50+
year = models.IntegerField(default=2023,
51+
validators=[
52+
MaxValueValidator(2023),
53+
MinValueValidator(2015)
54+
])
55+
56+
def __str__(self):
57+
return f"{self.car_make.name} - {self.name}"

server/djangoapp/populate.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,38 @@
1+
from .models import CarMake, CarModel
2+
13
def initiate():
2-
print("Populate not implemented. Add data manually")
4+
car_make_data = [
5+
{"name":"NISSAN", "description":"Great cars. Japanese technology"},
6+
{"name":"Mercedes", "description":"Great cars. German technology"},
7+
{"name":"Audi", "description":"Great cars. German technology"},
8+
{"name":"Kia", "description":"Great cars. Korean technology"},
9+
{"name":"Toyota", "description":"Great cars. Japanese technology"},
10+
]
11+
12+
car_make_instances = []
13+
for data in car_make_data:
14+
car_make_instances.append(CarMake.objects.create(name=data['name'], description=data['description']))
15+
16+
17+
# Create CarModel instances with the corresponding CarMake instances
18+
car_model_data = [
19+
{"name":"Pathfinder", "type":"SUV", "year": 2023, "car_make":car_make_instances[0]},
20+
{"name":"Qashqai", "type":"SUV", "year": 2023, "car_make":car_make_instances[0]},
21+
{"name":"XTRAIL", "type":"SUV", "year": 2023, "car_make":car_make_instances[0]},
22+
{"name":"A-Class", "type":"SUV", "year": 2023, "car_make":car_make_instances[1]},
23+
{"name":"C-Class", "type":"SUV", "year": 2023, "car_make":car_make_instances[1]},
24+
{"name":"E-Class", "type":"SUV", "year": 2023, "car_make":car_make_instances[1]},
25+
{"name":"A4", "type":"SUV", "year": 2023, "car_make":car_make_instances[2]},
26+
{"name":"A5", "type":"SUV", "year": 2023, "car_make":car_make_instances[2]},
27+
{"name":"A6", "type":"SUV", "year": 2023, "car_make":car_make_instances[2]},
28+
{"name":"Sorrento", "type":"SUV", "year": 2023, "car_make":car_make_instances[3]},
29+
{"name":"Carnival", "type":"SUV", "year": 2023, "car_make":car_make_instances[3]},
30+
{"name":"Cerato", "type":"Sedan", "year": 2023, "car_make":car_make_instances[3]},
31+
{"name":"Corolla", "type":"Sedan", "year": 2023, "car_make":car_make_instances[4]},
32+
{"name":"Camry", "type":"Sedan", "year": 2023, "car_make":car_make_instances[4]},
33+
{"name":"Kluger", "type":"SUV", "year": 2023, "car_make":car_make_instances[4]},
34+
# Add more CarModel instances as needed
35+
]
36+
37+
for data in car_model_data:
38+
CarModel.objects.create(name=data['name'], car_make=data['car_make'], type=data['type'], year=data['year'])

server/djangoapp/urls.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
# Uncomment the imports before you add the code
2-
# from django.urls import path
2+
from django.urls import path
33
from django.conf.urls.static import static
44
from django.conf import settings
5-
# from . import views
5+
from . import views
66

77
app_name = 'djangoapp'
88
urlpatterns = [
@@ -14,5 +14,6 @@
1414
# path for dealer reviews view
1515

1616
# path for add a review view
17+
path(route='get_cars', view=views.get_cars, name ='getcars'),
1718

1819
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)

server/djangoapp/views.py

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
# Uncomment the required imports before adding the code
22

3-
# from django.shortcuts import render
4-
# from django.http import HttpResponseRedirect, HttpResponse
5-
# from django.contrib.auth.models import User
6-
# from django.shortcuts import get_object_or_404, render, redirect
7-
# from django.contrib.auth import logout
8-
# from django.contrib import messages
9-
# from datetime import datetime
3+
from django.shortcuts import render
4+
from django.http import HttpResponseRedirect, HttpResponse
5+
from django.contrib.auth.models import User
6+
from django.shortcuts import get_object_or_404, render, redirect
7+
from django.contrib.auth import logout
8+
from django.contrib import messages
9+
from datetime import datetime
1010

1111
from django.http import JsonResponse
1212
from django.contrib.auth import login, authenticate
1313
import logging
1414
import json
1515
from django.views.decorators.csrf import csrf_exempt
16-
# from .populate import initiate
16+
from .populate import initiate
17+
from .models import CarMake, CarModel
1718

1819

1920
# Get an instance of a logger
@@ -63,3 +64,14 @@ def login_user(request):
6364
# Create a `add_review` view to submit a review
6465
# def add_review(request):
6566
# ...
67+
68+
def get_cars(request):
69+
count = CarMake.objects.filter().count()
70+
print(count)
71+
if(count == 0):
72+
initiate()
73+
car_models = CarModel.objects.select_related('car_make')
74+
cars = []
75+
for car_model in car_models:
76+
cars.append({"CarModel": car_model.name, "CarMake": car_model.car_make.name})
77+
return JsonResponse({"CarModels":cars})

server/djangoproj/settings.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828
# SECURITY WARNING: don't run with debug turned on in production!
2929
DEBUG = True
3030

31-
ALLOWED_HOSTS=['localhost','https://brody8991-8000.theianext-0-labs-prod-misc-tools-us-east-0.proxy.cognitiveclass.ai/']
32-
CSRF_TRUSTED_ORIGINS=['https://brody8991-8000.theianext-0-labs-prod-misc-tools-us-east-0.proxy.cognitiveclass.ai/']
31+
ALLOWED_HOSTS = ['localhost', 'brody8991-8000.theiadockernext-1-labs-prod-theiak8s-4-tor01.proxy.cognitiveclass.ai']
32+
CSRF_TRUSTED_ORIGINS = ['https://brody8991-8000.theiadockernext-1-labs-prod-theiak8s-4-tor01.proxy.cognitiveclass.ai']
3333

3434
REST_FRAMEWORK = {
3535
'DEFAULT_AUTHENTICATION_CLASSES': [],

0 commit comments

Comments
 (0)