Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ venv/
ENV/
env.bak/
venv.bak/
/myenv
myenv/

# Spyder project settings
.spyderproject
Expand Down
75 changes: 67 additions & 8 deletions app/app/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
"""

from pathlib import Path

from datetime import timedelta
from decouple import config
import os
# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

Expand All @@ -20,13 +22,14 @@
# See https://docs.djangoproject.com/en/4.0/howto/deployment/checklist/

# SECURITY WARNING: keep the secret key used in production secret!
SECRET_KEY = 'django-insecure-h-h69cr05lmc*w4vtkf+5qltg8#&#xf8fe(v9j9oxs-*-^#vjd'
SECRET_KEY = os.getenv('SECRET_KEY','')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True
DEBUG = os.getenv('DEBUG',True)

ALLOWED_HOSTS = []
ALLOWED_HOSTS = [ ]

FRONTEND_URL ='http://localhost:8000'

# Application definition

Expand All @@ -37,7 +40,12 @@
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'core'
#...third party apps
'rest_framework',
'rest_framework_simplejwt',
# local apps
'core',
'inventory',
]

MIDDLEWARE = [
Expand Down Expand Up @@ -75,9 +83,18 @@
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = {
# 'default': {
# 'ENGINE': 'django.db.backends.sqlite3',
# 'NAME': BASE_DIR / 'db.sqlite3',
# }

'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
'ENGINE': 'django.db.backends.postgresql',
'NAME': 'inventory_app',
'USER': os.getenv('POSTGRESUSER',''),
'PASSWORD': os.getenv('POSTGRESPASS',''),
'HOST': os.getenv('POSTGRESHOST',''),
'PORT': os.getenv('POSTGRESPORT','5432'),
}
}

Expand Down Expand Up @@ -123,4 +140,46 @@

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

AUTH_USER_MODEL = 'core.User'
AUTH_USER_MODEL = 'core.User'


REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': [
'rest_framework_simplejwt.authentication.JWTAuthentication',
],

'DEFAULT_PERMISSION_CLASSES': [
'rest_framework.permissions.IsAuthenticated',
],
}

AUTHENTICATION_BACKENDS = ['django.contrib.auth.backends.ModelBackend','inventory.custom_auth.EmailNameAuthBackend']


SIMPLE_JWT = {
"ACCESS_TOKEN_LIFETIME": timedelta(minutes=60),
"REFRESH_TOKEN_LIFETIME": timedelta(days=1),
"ROTATE_REFRESH_TOKENS": True,
"BLACKLIST_AFTER_ROTATION": True,
"UPDATE_LAST_LOGIN": False,
"ALGORITHM": "HS256",
# 'SIGNING_KEY': SECRET_KEY,
"VERIFYING_KEY": None,
"AUDIENCE": None,
"ISSUER": None,
"JWK_URL": None,
"LEEWAY": 0,
"AUTH_HEADER_TYPES": ("Bearer",),
"AUTH_HEADER_NAME": "HTTP_AUTHORIZATION",
"USER_ID_FIELD": "id",
"USER_ID_CLAIM": "user_id",
"USER_AUTHENTICATION_RULE": "rest_framework_simplejwt.authentication.default_user_authentication_rule",
"AUTH_TOKEN_CLASSES": ("rest_framework_simplejwt.tokens.AccessToken",),
"TOKEN_TYPE_CLAIM": "token_type",
"TOKEN_USER_CLASS": "rest_framework_simplejwt.models.TokenUser",
"JTI_CLAIM": "jti",
"SLIDING_TOKEN_REFRESH_EXP_CLAIM": "refresh_exp",
"SLIDING_TOKEN_LIFETIME": timedelta(minutes=60),
"SLIDING_TOKEN_REFRESH_LIFETIME": timedelta(days=1),
}

5 changes: 4 additions & 1 deletion app/app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@
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
from inventory.views import endpoints

urlpatterns = [
path('admin/', admin.site.urls),
path('',endpoints),
path('api/v1/',include('inventory.urls')),
]
5 changes: 3 additions & 2 deletions app/core/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""Manage admin page for main app."""

# from django.contrib import admin

from django.contrib import admin
from .models import User
# Register your models here.
admin.site.register(User)
2 changes: 1 addition & 1 deletion app/core/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated by Django 4.0.1 on 2022-02-19 10:18
# Generated by Django 4.0.1 on 2024-07-21 08:49

from django.db import migrations, models

Expand Down
2 changes: 1 addition & 1 deletion app/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def create_superuser(self, email, password):
user = self.create_user(email, password)
user.is_staff = True
user.is_superuser = True

user.save()
return user


Expand Down
Empty file added app/inventory/__init__.py
Empty file.
9 changes: 9 additions & 0 deletions app/inventory/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from django.contrib import admin
from .models import Category, Product,Order,OrderItem

# Register your models here.

admin.site.register(Category)
admin.site.register(Product)
admin.site.register(Order)
admin.site.register(OrderItem)
8 changes: 8 additions & 0 deletions app/inventory/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
from django.apps import AppConfig


class InventoryConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'inventory'


25 changes: 25 additions & 0 deletions app/inventory/custom_auth.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django.contrib.auth.backends import ModelBackend

from django.contrib.auth import get_user_model
User = get_user_model()#get the user model used for the entire project


'''
This class is responsible for user login
permiting them to use their names or email to login
'''
class EmailNameAuthBackend(ModelBackend):
def authenticate(self,requst,email =None, password = None):
try:
user = User.objects.get(email=email)
success = user.check_password(password)
if success:
return user
except User.DoesNotExist:
try:
user = User.objects.get(name=email)
success = user.check_password(password)
if success:
return user
except User.DoesNotExist:
return None
83 changes: 83 additions & 0 deletions app/inventory/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Generated by Django 4.0.1 on 2024-07-19 11:17

from django.conf import settings
from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

initial = True

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

operations = [
migrations.CreateModel(
name='Category',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('slug', models.SlugField(allow_unicode=True, editable=False, max_length=100)),
('name', models.CharField(max_length=100)),
('date_created', models.DateTimeField(auto_now_add=True)),
],
options={
'verbose_name_plural': 'categories',
'db_table': 'inventory_category',
},
),
migrations.CreateModel(
name='Order',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('slug', models.SlugField(allow_unicode=True, editable=False, max_length=100)),
('date_created', models.DateTimeField(auto_now_add=True)),
],
options={
'verbose_name_plural': 'orders',
'db_table': 'inventory_order',
},
),
migrations.CreateModel(
name='Product',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('slug', models.SlugField(allow_unicode=True, editable=False, max_length=100)),
('name', models.CharField(max_length=100)),
('description', models.TextField()),
('price', models.DecimalField(decimal_places=2, max_digits=10)),
('date_created', models.DateTimeField(auto_now_add=True)),
('category', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='products', to='inventory.category')),
],
options={
'verbose_name_plural': 'products',
'db_table': 'inventory_product',
},
),
migrations.CreateModel(
name='OrderItem',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('slug', models.SlugField(allow_unicode=True, editable=False, max_length=100)),
('quantity', models.PositiveIntegerField()),
('date_created', models.DateTimeField(auto_now_add=True)),
('order', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='order_items', to='inventory.order')),
('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='order_items', to='inventory.product')),
],
options={
'verbose_name_plural': 'order_items',
'db_table': 'inventory_order_item',
},
),
migrations.AddField(
model_name='order',
name='products',
field=models.ManyToManyField(through='inventory.OrderItem', to='inventory.Product'),
),
migrations.AddField(
model_name='order',
name='user',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='orders', to=settings.AUTH_USER_MODEL),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Generated by Django 4.0.1 on 2024-07-21 08:33

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('inventory', '0001_initial'),
]

operations = [
migrations.AlterField(
model_name='orderitem',
name='quantity',
field=models.PositiveIntegerField(default=1),
),
migrations.AlterField(
model_name='product',
name='category',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='products', to='inventory.category'),
),
migrations.AlterField(
model_name='product',
name='description',
field=models.TextField(blank=True, null=True),
),
migrations.AlterField(
model_name='product',
name='name',
field=models.CharField(blank=True, max_length=100, null=True),
),
migrations.AlterField(
model_name='product',
name='price',
field=models.DecimalField(decimal_places=2, default=0.0, max_digits=10),
),
]
Empty file.
Loading