Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
f9aef85
ADD[Added REST_FRAMEWORK & SIMPLE_JWT configs, postgres config for pr…
Py-Timson Jul 24, 2024
1cc0708
ADD[Created views, serializers, and urls files for logic, config, and…
Py-Timson Jul 24, 2024
2360f24
ADD[Connected the base urls to core's app url for easy routing, and b…
Py-Timson Jul 24, 2024
915b51d
ADD[Created models for Product and Category, and implemented CRUD ope…
Py-Timson Jul 24, 2024
58a490d
ADD[Serialized Category and Product, as well as making necessary impo…
Py-Timson Jul 24, 2024
56e7e02
ADD[Implemented the logic for Category and Product using viewset]
Py-Timson Jul 24, 2024
55d68a3
ADD[Created a router config for Category and Product]
Py-Timson Jul 24, 2024
0364ee2
ADD[connected the home page of the router config to base urls]
Py-Timson Jul 24, 2024
22e5ead
ADD[Created Order and OrderProduct Models, by allowing users to place…
Py-Timson Jul 24, 2024
5044f71
ADD[Implemented the OrderedProductSerializers, and OrderSerializers]
Py-Timson Jul 24, 2024
cd9e358
ADD[Implemented the logic to handle the Order and the Order Hisotry]
Py-Timson Jul 24, 2024
a65e06f
ADD[Updated the urls.py to capture the Order and the Order History]
Py-Timson Jul 24, 2024
fd22eca
ADD[Added rest_framework_simplejwt to THIRD_PARTY_APPS of the INSTALL…
Py-Timson Jul 24, 2024
5db9b8b
ADD[Configured the admin.py file]
Py-Timson Jul 24, 2024
03b05aa
ADD[implemented unit tests for all the endpoints, as well as made cha…
Py-Timson Jul 24, 2024
e379a18
ADD[made some changes, as well as implementing simple search function…
Py-Timson Jul 24, 2024
615cc98
ADD[Updated the requirements.txt file]
Py-Timson Jul 24, 2024
bd8d17e
ADD[Changed the time zone to Nigeria in settings]
Py-Timson Jul 25, 2024
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
96 changes: 71 additions & 25 deletions app/app/settings.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,7 @@
"""
Django settings for app project.

Generated by 'django-admin startproject' using Django 4.0.1.

For more information on this file, see
https://docs.djangoproject.com/en/4.0/topics/settings/

For the full list of settings and their values, see
https://docs.djangoproject.com/en/4.0/ref/settings/
"""

import os
from decouple import config, Csv
from pathlib import Path
from datetime import timedelta

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent
Expand All @@ -20,26 +11,40 @@
# 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 = config('SECRET_KEY', default='secret-key')

# SECURITY WARNING: don't run with debug turned on in production!
DEBUG = True

ALLOWED_HOSTS = []
DEBUG = config('DEBUG', default=True, cast=bool)






# Allowed hosts
ALLOWED_HOSTS = config('ALLOWED_HOSTS', default='127.0.0.1', cast=Csv())



# Application definition

INSTALLED_APPS = [
DJANGO_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'core'

]

CUSTOM_APPS = ['core']

THIRD_PARTY_APPS = ['rest_framework', 'rest_framework_simplejwt',]

INSTALLED_APPS = DJANGO_APPS + CUSTOM_APPS + THIRD_PARTY_APPS

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
Expand Down Expand Up @@ -71,17 +76,58 @@
WSGI_APPLICATION = 'app.wsgi.application'


# Database
# https://docs.djangoproject.com/en/4.0/ref/settings/#databases

DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
# Environment-specific settings
ENVIRONMENT = config('ENVIRONMENT', default='development')

if ENVIRONMENT == 'production':
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.postgresql',
'NAME': config('DB_NAME'),
'USER': config('DB_USER'),
'PASSWORD': config('DB_PASSWORD'),
'HOST': config('DB_HOST', default='localhost'),
'PORT': config('DB_PORT', default='5432'),
}
}
else:
DATABASES = {
'default': {
'ENGINE': 'django.db.backends.sqlite3',
'NAME': BASE_DIR / 'db.sqlite3',
}
}



REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
),
}


SIMPLE_JWT = {
'ACCESS_TOKEN_LIFETIME': timedelta(minutes=5),
'REFRESH_TOKEN_LIFETIME': timedelta(days=1),
'ROTATE_REFRESH_TOKENS': False,
'BLACKLIST_AFTER_ROTATION': True,
'ALGORITHM': 'HS256',
'SIGNING_KEY': SECRET_KEY,
'VERIFYING_KEY': None,
'AUDIENCE': None,
'ISSUER': None,
'AUTH_HEADER_TYPES': ('Bearer',),
'USER_ID_FIELD': 'id',
'USER_ID_CLAIM': 'user_id',
'AUTH_TOKEN_CLASSES': ('rest_framework_simplejwt.tokens.AccessToken',),
'TOKEN_TYPE_CLAIM': 'token_type',
'TOKEN_OBTAIN_PAIR_SERIALIZER': 'core.serializers.MyTokenObtainPairSerializer',
}



# Password validation
# https://docs.djangoproject.com/en/4.0/ref/settings/#auth-password-validators

Expand All @@ -106,7 +152,7 @@

LANGUAGE_CODE = 'en-us'

TIME_ZONE = 'UTC'
TIME_ZONE = 'Africa/Lagos'

USE_I18N = True

Expand All @@ -123,4 +169,4 @@

DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'

AUTH_USER_MODEL = 'core.User'
#AUTH_USER_MODEL = 'core.User'
21 changes: 6 additions & 15 deletions app/app/urls.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,12 @@
"""app URL Configuration.

The `urlpatterns` list routes URLs to views. For more information please see:
https://docs.djangoproject.com/en/4.0/topics/http/urls/
Examples:
Function views
1. Add an import: from my_app import views
2. Add a URL to urlpatterns: path('', views.home, name='home')
Class-based views
1. Add an import: from other_app.views import Home
2. Add a URL to urlpatterns: path('', Home.as_view(), name='home')
Including another URLconf
1. Import the include() function: from django.urls import include, path
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('core.urls')),
path('user/api/', include('core.urls')),

]
5 changes: 5 additions & 0 deletions app/app/wsgi.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

import os

from decouple import config




from django.core.wsgi import get_wsgi_application

os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'app.settings')
Expand Down
36 changes: 33 additions & 3 deletions app/core/admin.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,35 @@
"""Manage admin page for main app."""

# from django.contrib import admin
from django.contrib import admin
from .models import Category, Product, Order, OrderProduct

@admin.register(Category)
class CategoryAdmin(admin.ModelAdmin):
list_display = ('id', 'name')
search_fields = ('name',)
ordering = ('name',)
list_per_page = 20

@admin.register(Product)
class ProductAdmin(admin.ModelAdmin):
list_display = ('id', 'name', 'price', 'category')
search_fields = ('name', 'description')
list_filter = ('category',)
ordering = ('name',)
list_per_page = 20

@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
list_display = ('id', 'user', 'date')
search_fields = ('user__username',) #Here, I Used related field lookup for search
list_filter = ('date',)
ordering = ('-date',)
list_per_page = 20

@admin.register(OrderProduct)
class OrderProductAdmin(admin.ModelAdmin):
list_display = ('order', 'product', 'quantity')
search_fields = ('order__id', 'product__name')
list_filter = ('order', 'product')
ordering = ('order', 'product')
list_per_page = 20

# Register your models here.
29 changes: 14 additions & 15 deletions app/core/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
# Generated by Django 4.0.1 on 2022-02-19 10:18
# Generated by Django 4.0.1 on 2024-07-24 11:33

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


class Migration(migrations.Migration):

initial = True

dependencies = [
('auth', '0012_alter_user_first_name_max_length'),
]

operations = [
migrations.CreateModel(
name='User',
name='Category',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('password', models.CharField(max_length=128, verbose_name='password')),
('last_login', models.DateTimeField(blank=True, null=True, verbose_name='last login')),
('is_superuser', models.BooleanField(default=False, help_text='Designates that this user has all permissions without explicitly assigning them.', verbose_name='superuser status')),
('email', models.EmailField(max_length=255, unique=True)),
('name', models.CharField(max_length=255)),
('is_active', models.BooleanField(default=True)),
('is_staff', models.BooleanField(default=False)),
('groups', models.ManyToManyField(blank=True, help_text='The groups this user belongs to. A user will get all permissions granted to each of their groups.', related_name='user_set', related_query_name='user', to='auth.Group', verbose_name='groups')),
('user_permissions', models.ManyToManyField(blank=True, help_text='Specific permissions for this user.', related_name='user_set', related_query_name='user', to='auth.Permission', verbose_name='user permissions')),
('name', models.CharField(max_length=100)),
],
),
migrations.CreateModel(
name='Product',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=100)),
('description', models.TextField()),
('price', models.DecimalField(decimal_places=2, max_digits=10)),
('category', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='products', to='core.category')),
],
options={
'abstract': False,
},
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# Generated by Django 4.0.1 on 2024-07-24 12:25

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


class Migration(migrations.Migration):

dependencies = [
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
('core', '0001_initial'),
]

operations = [
migrations.CreateModel(
name='Order',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('date', models.DateTimeField(auto_now_add=True)),
],
),
migrations.CreateModel(
name='OrderProduct',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('quantity', models.PositiveIntegerField()),
('order', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='core.order')),
('product', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='core.product')),
],
),
migrations.AddField(
model_name='order',
name='products',
field=models.ManyToManyField(through='core.OrderProduct', to='core.Product'),
),
migrations.AddField(
model_name='order',
name='user',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Generated by Django 4.0.1 on 2024-07-24 21:15

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


class Migration(migrations.Migration):

dependencies = [
('core', '0002_order_orderproduct_order_products_order_user'),
]

operations = [
migrations.AddField(
model_name='product',
name='order',
field=models.IntegerField(default=0),
),
migrations.AlterField(
model_name='order',
name='products',
field=models.ManyToManyField(related_name='product', through='core.OrderProduct', to='core.Product'),
),
migrations.AlterField(
model_name='product',
name='category',
field=models.ForeignKey(default=0, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='products', to='core.category'),
),
]
17 changes: 17 additions & 0 deletions app/core/migrations/0004_remove_product_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Generated by Django 4.0.1 on 2024-07-24 21:22

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('core', '0003_product_order_alter_order_products_and_more'),
]

operations = [
migrations.RemoveField(
model_name='product',
name='order',
),
]
18 changes: 18 additions & 0 deletions app/core/migrations/0005_product_order.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 4.0.1 on 2024-07-24 21:23

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('core', '0004_remove_product_order'),
]

operations = [
migrations.AddField(
model_name='product',
name='order',
field=models.IntegerField(default=0),
),
]
Loading