Merge pull request #1 from lemeow125/feature/connect_frontend

Feature/connect frontend
This commit is contained in:
lemeow125 2023-03-06 21:08:28 +08:00 committed by GitHub
commit 7db50e72e5
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
14 changed files with 108 additions and 3 deletions

View file

@ -8,6 +8,7 @@ django = "*"
djangorestframework = "*" djangorestframework = "*"
djoser = "*" djoser = "*"
djangorestframework-simplejwt = "*" djangorestframework-simplejwt = "*"
django-cors-headers = "*"
[dev-packages] [dev-packages]
autopep8 = "*" autopep8 = "*"

10
Pipfile.lock generated
View file

@ -1,7 +1,7 @@
{ {
"_meta": { "_meta": {
"hash": { "hash": {
"sha256": "309b0b7b7b2f3dce9f8501a57dac5257ba3cca7d8e01cfc7284aa6c48279b23b" "sha256": "24c6a4fe52c048f750d36c10e0e1e0fd578de1b2455b1dd082ef830c272e461a"
}, },
"pipfile-spec": 6, "pipfile-spec": 6,
"requires": { "requires": {
@ -253,6 +253,14 @@
"index": "pypi", "index": "pypi",
"version": "==4.1.7" "version": "==4.1.7"
}, },
"django-cors-headers": {
"hashes": [
"sha256:5fbd58a6fb4119d975754b2bc090f35ec160a8373f276612c675b00e8a138739",
"sha256:684180013cc7277bdd8702b80a3c5a4b3fcae4abb2bf134dceb9f5dfe300228e"
],
"index": "pypi",
"version": "==3.14.0"
},
"django-templated-mail": { "django-templated-mail": {
"hashes": [ "hashes": [
"sha256:8db807effebb42a532622e2d142dfd453dafcd0d7794c4c3332acb90656315f9", "sha256:8db807effebb42a532622e2d142dfd453dafcd0d7794c4c3332acb90656315f9",

View file

@ -2,5 +2,6 @@ from django.contrib import admin
from django.urls import path, include from django.urls import path, include
urlpatterns = [ urlpatterns = [
path('accounts/', include('accounts.urls')) path('', include('products.urls')),
path('accounts/', include('accounts.urls')),
] ]

View file

@ -40,11 +40,14 @@ INSTALLED_APPS = [
'rest_framework', 'rest_framework',
'rest_framework.authtoken', 'rest_framework.authtoken',
'djoser', 'djoser',
'corsheaders',
'products',
] ]
MIDDLEWARE = [ MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware', 'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware', 'django.contrib.sessions.middleware.SessionMiddleware',
"corsheaders.middleware.CorsMiddleware",
'django.middleware.common.CommonMiddleware', 'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware', 'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware', 'django.contrib.auth.middleware.AuthenticationMiddleware',
@ -108,7 +111,7 @@ AUTH_PASSWORD_VALIDATORS = [
LANGUAGE_CODE = 'en-us' LANGUAGE_CODE = 'en-us'
TIME_ZONE = 'UTC' TIME_ZONE = 'Asia/Manila'
USE_I18N = True USE_I18N = True
@ -131,6 +134,9 @@ REST_FRAMEWORK = {
) )
} }
DOMAIN = 'localhost:3000'
SITE_NAME = 'localhost:3000'
DJOSER = { DJOSER = {
'SEND_ACTIVATION_EMAIL': True, 'SEND_ACTIVATION_EMAIL': True,
'SEND_CONFIRMATION_EMAIL': True, 'SEND_CONFIRMATION_EMAIL': True,
@ -141,3 +147,8 @@ EMAIL_HOST = 'sandbox.smtp.mailtrap.io'
EMAIL_HOST_USER = '54ff6949e39105' EMAIL_HOST_USER = '54ff6949e39105'
EMAIL_HOST_PASSWORD = 'c59d3eaa05f98d' EMAIL_HOST_PASSWORD = 'c59d3eaa05f98d'
EMAIL_PORT = '2525' EMAIL_PORT = '2525'
CORS_ALLOWED_ORIGINS = [
"http://localhost:3000",
"http://localhost:8000",
]

0
ivy/products/__init__.py Normal file
View file

3
ivy/products/admin.py Normal file
View file

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

6
ivy/products/apps.py Normal file
View file

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

View file

@ -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)),
],
),
]

View file

12
ivy/products/models.py Normal file
View file

@ -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

View file

@ -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')

3
ivy/products/tests.py Normal file
View file

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

13
ivy/products/urls.py Normal file
View file

@ -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)),
]

10
ivy/products/views.py Normal file
View file

@ -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()