Dodanie REST API

This commit is contained in:
Wojciech Jarmosz 2022-01-03 00:09:16 +01:00
parent 682c7a9ff9
commit 6a35148188
13 changed files with 84 additions and 3 deletions

View File

@ -88,6 +88,7 @@ WSGI_APPLICATION = "backend.wsgi.application"
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases
DATABASES = {
"default": {
"ENGINE": "django.db.backends.postgresql_psycopg2",

View File

@ -14,8 +14,9 @@ Including another URLconf
2. Add a URL to urlpatterns: path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path
from django.urls import path, include
urlpatterns = [
path("admin/", admin.site.urls),
path("", include('cars.urls'))
]

0
backend/cars/__init__.py Normal file
View File

3
backend/cars/admin.py Normal file
View File

@ -0,0 +1,3 @@
from django.contrib import admin
# Register your models here.

6
backend/cars/apps.py Normal file
View File

@ -0,0 +1,6 @@
from django.apps import AppConfig
class CarsConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'cars'

View File

View File

13
backend/cars/models.py Normal file
View File

@ -0,0 +1,13 @@
from django.db import models
class Car(models.Model):
mark = models.CharField(max_length=150)
model = models.CharField(max_length=150)
mileage = models.PositiveIntegerField()
production_year = models.PositiveSmallIntegerField()
engine_capacity = models.DecimalField()
combustion = models.DecimalField()
price = models.DecimalField()
def __str__(self) -> str:
return "Car"

View File

@ -0,0 +1,15 @@
from rest_framework import serializers
from .models import Car
class CarSerializer(serializers.ModelSerializer):
class Meta:
model = Car
fields = [
'mark',
'model',
'mileage',
'production_year',
'engine_capacity',
'combustion',
'price'
]

3
backend/cars/tests.py Normal file
View File

@ -0,0 +1,3 @@
from django.test import TestCase
# Create your tests here.

12
backend/cars/urls.py Normal file
View File

@ -0,0 +1,12 @@
from django.urls import path, include
from rest_framework.routers import DefaultRouter
from .views import CarViews
# Create a router and register our viewsets with it.
router = DefaultRouter()
router.register(r'cars', CarViews.as_view(), name="cars")
# The API URLs are now determined automatically by the router.
urlpatterns = [
path('', include(router.urls)),
]

19
backend/cars/views.py Normal file
View File

@ -0,0 +1,19 @@
from rest_framework.views import APIView
from .models import Car
from rest_framework.response import Response
class CarViews(APIView):
def get(self, request, format=None):
"""
Returns car list based on fuzzy_logic filters.
"""
combustion = request.query_params.get('combustion')
mileage = request.query_params.get('mileage')
production_year = request.query_params.get('production_year')
engine_capacity = request.query_params.get('engine_capacity')
print(combustion, mileage, production_year, engine_capacity)
cars = list(Car.objects.all())
return Response(cars)

View File

@ -2610,6 +2610,7 @@
"thread-loader": "^2.1.3",
"url-loader": "^2.2.0",
"vue-loader": "^15.9.2",
"vue-loader-v16": "npm:vue-loader@^16.1.0",
"vue-style-loader": "^4.1.2",
"webpack": "^4.0.0",
"webpack-bundle-analyzer": "^3.8.0",
@ -3710,6 +3711,7 @@
"@types/node": "*",
"anymatch": "^3.0.3",
"fb-watchman": "^2.0.0",
"fsevents": "^2.3.2",
"graceful-fs": "^4.2.4",
"jest-regex-util": "^27.0.6",
"jest-serializer": "^27.0.6",
@ -4860,6 +4862,7 @@
"dependencies": {
"anymatch": "~3.1.2",
"braces": "~3.0.2",
"fsevents": "~2.3.2",
"glob-parent": "~5.1.2",
"is-binary-path": "~2.1.0",
"is-glob": "~4.0.1",
@ -6308,6 +6311,7 @@
"resolved": "https://registry.npmjs.org/deasync/-/deasync-0.1.24.tgz",
"integrity": "sha512-i98vg42xNfRZCymummMAN0rIcQ1gZFinSe3btvPIvy6JFTaeHcumeKybRo2HTv86nasfmT0nEgAn2ggLZhOCVA==",
"dev": true,
"hasInstallScript": true,
"dependencies": {
"bindings": "^1.5.0",
"node-addon-api": "^1.7.1"
@ -7203,7 +7207,8 @@
"esprima": "^4.0.1",
"estraverse": "^4.2.0",
"esutils": "^2.0.2",
"optionator": "^0.8.1"
"optionator": "^0.8.1",
"source-map": "~0.6.1"
},
"bin": {
"escodegen": "bin/escodegen.js",
@ -10504,6 +10509,7 @@
"@jest/types": "^24.9.0",
"anymatch": "^2.0.0",
"fb-watchman": "^2.0.0",
"fsevents": "^1.2.7",
"graceful-fs": "^4.1.15",
"invariant": "^2.2.4",
"jest-serializer": "^24.9.0",
@ -18086,7 +18092,8 @@
"dependencies": {
"chokidar": "^3.4.1",
"graceful-fs": "^4.1.2",
"neo-async": "^2.5.0"
"neo-async": "^2.5.0",
"watchpack-chokidar2": "^2.0.1"
},
"optionalDependencies": {
"watchpack-chokidar2": "^2.0.1"
@ -18460,6 +18467,7 @@
"anymatch": "^2.0.0",
"async-each": "^1.0.1",
"braces": "^2.3.2",
"fsevents": "^1.2.7",
"glob-parent": "^3.1.0",
"inherits": "^2.0.3",
"is-binary-path": "^1.0.0",