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 5da41fe..a61ead5 100644 --- a/ivy/config/settings.py +++ b/ivy/config/settings.py @@ -40,11 +40,14 @@ INSTALLED_APPS = [ 'rest_framework', 'rest_framework.authtoken', 'djoser', + 'corsheaders', + 'products', ] MIDDLEWARE = [ 'django.middleware.security.SecurityMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware', + "corsheaders.middleware.CorsMiddleware", 'django.middleware.common.CommonMiddleware', 'django.middleware.csrf.CsrfViewMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware', @@ -108,7 +111,7 @@ AUTH_PASSWORD_VALIDATORS = [ LANGUAGE_CODE = 'en-us' -TIME_ZONE = 'UTC' +TIME_ZONE = 'Asia/Manila' USE_I18N = True @@ -131,6 +134,9 @@ REST_FRAMEWORK = { ) } +DOMAIN = 'localhost:3000' +SITE_NAME = 'localhost:3000' + DJOSER = { 'SEND_ACTIVATION_EMAIL': True, 'SEND_CONFIRMATION_EMAIL': True, @@ -141,3 +147,8 @@ EMAIL_HOST = 'sandbox.smtp.mailtrap.io' EMAIL_HOST_USER = '54ff6949e39105' EMAIL_HOST_PASSWORD = 'c59d3eaa05f98d' 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..85d5dc3 --- /dev/null +++ b/ivy/products/serializers.py @@ -0,0 +1,13 @@ +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) + quantity = serializers.IntegerField(required=False, default=0) + + class Meta: + model = Product + fields = ('id', '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()