From d98c88482d395fd02c4c5855a6cb12889c6dc689 Mon Sep 17 00:00:00 2001 From: keannu125 Date: Sun, 5 Mar 2023 21:56:48 +0800 Subject: [PATCH] Added products model --- Pipfile | 1 + Pipfile.lock | 10 +++++++++- ivy/api/urls.py | 3 ++- ivy/config/settings.py | 4 +++- ivy/products/__init__.py | 0 ivy/products/admin.py | 3 +++ ivy/products/apps.py | 6 ++++++ ivy/products/migrations/0001_initial.py | 24 ++++++++++++++++++++++++ ivy/products/migrations/__init__.py | 0 ivy/products/models.py | 12 ++++++++++++ ivy/products/serializers.py | 12 ++++++++++++ ivy/products/tests.py | 3 +++ ivy/products/urls.py | 13 +++++++++++++ ivy/products/views.py | 10 ++++++++++ 14 files changed, 98 insertions(+), 3 deletions(-) create mode 100644 ivy/products/__init__.py create mode 100644 ivy/products/admin.py create mode 100644 ivy/products/apps.py create mode 100644 ivy/products/migrations/0001_initial.py create mode 100644 ivy/products/migrations/__init__.py create mode 100644 ivy/products/models.py create mode 100644 ivy/products/serializers.py create mode 100644 ivy/products/tests.py create mode 100644 ivy/products/urls.py create mode 100644 ivy/products/views.py diff --git a/Pipfile b/Pipfile index 8faada7..31a082c 100644 --- a/Pipfile +++ b/Pipfile @@ -8,6 +8,7 @@ django = "*" djangorestframework = "*" djoser = "*" djangorestframework-simplejwt = "*" +django-cors-headers = "*" [dev-packages] autopep8 = "*" diff --git a/Pipfile.lock b/Pipfile.lock index eb12124..2ea45b4 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "309b0b7b7b2f3dce9f8501a57dac5257ba3cca7d8e01cfc7284aa6c48279b23b" + "sha256": "24c6a4fe52c048f750d36c10e0e1e0fd578de1b2455b1dd082ef830c272e461a" }, "pipfile-spec": 6, "requires": { @@ -253,6 +253,14 @@ "index": "pypi", "version": "==4.1.7" }, + "django-cors-headers": { + "hashes": [ + "sha256:5fbd58a6fb4119d975754b2bc090f35ec160a8373f276612c675b00e8a138739", + "sha256:684180013cc7277bdd8702b80a3c5a4b3fcae4abb2bf134dceb9f5dfe300228e" + ], + "index": "pypi", + "version": "==3.14.0" + }, "django-templated-mail": { "hashes": [ "sha256:8db807effebb42a532622e2d142dfd453dafcd0d7794c4c3332acb90656315f9", diff --git a/ivy/api/urls.py b/ivy/api/urls.py index b296cca..04f17b6 100644 --- a/ivy/api/urls.py +++ b/ivy/api/urls.py @@ -2,5 +2,6 @@ from django.contrib import admin from django.urls import path, include urlpatterns = [ - path('accounts/', include('accounts.urls')) + path('', include('products.urls')), + path('accounts/', include('accounts.urls')), ] diff --git a/ivy/config/settings.py b/ivy/config/settings.py index ed7d9db..ea0ba80 100644 --- a/ivy/config/settings.py +++ b/ivy/config/settings.py @@ -41,6 +41,7 @@ INSTALLED_APPS = [ 'rest_framework.authtoken', 'djoser', 'corsheaders', + 'products', ] MIDDLEWARE = [ @@ -110,7 +111,7 @@ AUTH_PASSWORD_VALIDATORS = [ LANGUAGE_CODE = 'en-us' -TIME_ZONE = 'UTC' +TIME_ZONE = 'Asia/Manila' USE_I18N = True @@ -146,4 +147,5 @@ EMAIL_PORT = '2525' CORS_ALLOWED_ORIGINS = [ "http://localhost:3000", + "http://localhost:8000", ] diff --git a/ivy/products/__init__.py b/ivy/products/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ivy/products/admin.py b/ivy/products/admin.py new file mode 100644 index 0000000..ea5d68b --- /dev/null +++ b/ivy/products/admin.py @@ -0,0 +1,3 @@ +from django.contrib import admin + +# Register your models here. diff --git a/ivy/products/apps.py b/ivy/products/apps.py new file mode 100644 index 0000000..2282266 --- /dev/null +++ b/ivy/products/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class ProductsConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'products' diff --git a/ivy/products/migrations/0001_initial.py b/ivy/products/migrations/0001_initial.py new file mode 100644 index 0000000..e1ebd81 --- /dev/null +++ b/ivy/products/migrations/0001_initial.py @@ -0,0 +1,24 @@ +# Generated by Django 4.1.7 on 2023-03-05 13:54 + +from django.db import migrations, models +import django.utils.timezone + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + ] + + operations = [ + migrations.CreateModel( + name='Product', + fields=[ + ('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('name', models.CharField(max_length=20)), + ('quantity', models.IntegerField(default=0)), + ('date_added', models.DateTimeField(default=django.utils.timezone.now, editable=False)), + ], + ), + ] diff --git a/ivy/products/migrations/__init__.py b/ivy/products/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/ivy/products/models.py b/ivy/products/models.py new file mode 100644 index 0000000..5476a26 --- /dev/null +++ b/ivy/products/models.py @@ -0,0 +1,12 @@ +from django.db import models +from django.utils.timezone import now +# Create your models here. + + +class Product(models.Model): + name = models.CharField(max_length=20) + quantity = models.IntegerField(default=0) + date_added = models.DateTimeField(default=now, editable=False) + + def __str__(self): + return self.title diff --git a/ivy/products/serializers.py b/ivy/products/serializers.py new file mode 100644 index 0000000..321221e --- /dev/null +++ b/ivy/products/serializers.py @@ -0,0 +1,12 @@ +from rest_framework import serializers +from .models import Product + + +class ProductSerializer(serializers.HyperlinkedModelSerializer): + date_added = serializers.DateTimeField( + format="%d-%m-%Y %I:%M%p", read_only=True) + + class Meta: + model = Product + fields = ('name', 'quantity', 'date_added') + read_only_fields = ('id', 'date_added') diff --git a/ivy/products/tests.py b/ivy/products/tests.py new file mode 100644 index 0000000..de8bdc0 --- /dev/null +++ b/ivy/products/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/ivy/products/urls.py b/ivy/products/urls.py new file mode 100644 index 0000000..033f097 --- /dev/null +++ b/ivy/products/urls.py @@ -0,0 +1,13 @@ +from django.urls import include, path +from rest_framework import routers +from . import views + +router = routers.DefaultRouter() +router.register(r'products', views.ProductViewSet) + +# Wire up our API using automatic URL routing. +# Additionally, we include login URLs for the browsable API. +urlpatterns = [ + path('', include(router.urls)), + +] diff --git a/ivy/products/views.py b/ivy/products/views.py new file mode 100644 index 0000000..1fbc04b --- /dev/null +++ b/ivy/products/views.py @@ -0,0 +1,10 @@ +from rest_framework.permissions import IsAuthenticated +from rest_framework import viewsets +from .serializers import ProductSerializer +from .models import Product + + +class ProductViewSet(viewsets.ModelViewSet): + permission_classes = [IsAuthenticated] + serializer_class = ProductSerializer + queryset = Product.objects.all()