initial commit
This commit is contained in:
commit
dd6a70c37f
5
.gitignore
vendored
Normal file
5
.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
.settings/*
|
||||
.project
|
||||
.pydevproject
|
||||
poetry.lock
|
||||
|
||||
0
api/__init__.py
Normal file
0
api/__init__.py
Normal file
16
api/asgi.py
Normal file
16
api/asgi.py
Normal file
@ -0,0 +1,16 @@
|
||||
"""
|
||||
ASGI config for api project.
|
||||
|
||||
It exposes the ASGI callable as a module-level variable named ``application``.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/5.2/howto/deployment/asgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.asgi import get_asgi_application
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.settings')
|
||||
|
||||
application = get_asgi_application()
|
||||
149
api/settings.py
Normal file
149
api/settings.py
Normal file
@ -0,0 +1,149 @@
|
||||
"""
|
||||
Django settings for api project.
|
||||
|
||||
Generated by 'django-admin startproject' using Django 5.2.7.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/5.2/topics/settings/
|
||||
|
||||
For the full list of settings and their values, see
|
||||
https://docs.djangoproject.com/en/5.2/ref/settings/
|
||||
"""
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||
|
||||
|
||||
# Quick-start development settings - unsuitable for production
|
||||
# See https://docs.djangoproject.com/en/5.2/howto/deployment/checklist/
|
||||
|
||||
# SECURITY WARNING: keep the secret key used in production secret!
|
||||
SECRET_KEY = 'django-insecure-g)a6wc^%c58nxv6x2&&o1tnh=dcpy)jd01nuc(x-6+23&0ay3y'
|
||||
|
||||
# SECURITY WARNING: don't run with debug turned on in production!
|
||||
DEBUG = True
|
||||
|
||||
ALLOWED_HOSTS = [
|
||||
'localhost',
|
||||
'127.0.0.1'
|
||||
]
|
||||
|
||||
CORS_ALLOWED_ORIGINS = [
|
||||
"http://localhost:5173",
|
||||
'http://127.0.0.1:5173'
|
||||
]
|
||||
|
||||
|
||||
# Application definition
|
||||
|
||||
INSTALLED_APPS = [
|
||||
'django.contrib.admin',
|
||||
'django.contrib.auth',
|
||||
'django.contrib.contenttypes',
|
||||
'django.contrib.sessions',
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'books',
|
||||
'rest_framework',
|
||||
'corsheaders',
|
||||
'rest_framework_simplejwt',
|
||||
'djoser',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
'django.middleware.security.SecurityMiddleware',
|
||||
'django.contrib.sessions.middleware.SessionMiddleware',
|
||||
'django.middleware.common.CommonMiddleware',
|
||||
'django.middleware.csrf.CsrfViewMiddleware',
|
||||
'django.contrib.auth.middleware.AuthenticationMiddleware',
|
||||
'django.contrib.messages.middleware.MessageMiddleware',
|
||||
'django.middleware.clickjacking.XFrameOptionsMiddleware',
|
||||
]
|
||||
|
||||
REST_FRAMEWORK = {
|
||||
'DEFAULT_AUTHENTICATION_CLASSES': [
|
||||
'rest_framework_simplejwt.authentication.JWTAuthentication',
|
||||
],
|
||||
|
||||
'DEFAULT_PERMISSION_CLASSES': [
|
||||
'rest_framework.permissions.IsAuthenticatedOrReadOnly',
|
||||
]
|
||||
}
|
||||
|
||||
ROOT_URLCONF = 'api.urls'
|
||||
|
||||
TEMPLATES = [
|
||||
{
|
||||
'BACKEND': 'django.template.backends.django.DjangoTemplates',
|
||||
'DIRS': [],
|
||||
'APP_DIRS': True,
|
||||
'OPTIONS': {
|
||||
'context_processors': [
|
||||
'django.template.context_processors.request',
|
||||
'django.contrib.auth.context_processors.auth',
|
||||
'django.contrib.messages.context_processors.messages',
|
||||
],
|
||||
},
|
||||
},
|
||||
]
|
||||
|
||||
WSGI_APPLICATION = 'api.wsgi.application'
|
||||
|
||||
|
||||
# Database
|
||||
# https://docs.djangoproject.com/en/5.2/ref/settings/#databases
|
||||
|
||||
DATABASES = {
|
||||
'default': {
|
||||
'ENGINE': 'django.db.backends.postgresql',
|
||||
'NAME': 'pipi',
|
||||
'USER': 'pipi',
|
||||
'PASSWORD': 'pipi',
|
||||
'HOST': 'localhost',
|
||||
'PORT': '5434'
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
# Password validation
|
||||
# https://docs.djangoproject.com/en/5.2/ref/settings/#auth-password-validators
|
||||
|
||||
AUTH_PASSWORD_VALIDATORS = [
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
|
||||
},
|
||||
{
|
||||
'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
# Internationalization
|
||||
# https://docs.djangoproject.com/en/5.2/topics/i18n/
|
||||
|
||||
LANGUAGE_CODE = 'fr-fr'
|
||||
|
||||
TIME_ZONE = 'UTC'
|
||||
|
||||
USE_I18N = True
|
||||
|
||||
USE_TZ = True
|
||||
|
||||
|
||||
# Static files (CSS, JavaScript, Images)
|
||||
# https://docs.djangoproject.com/en/5.2/howto/static-files/
|
||||
|
||||
STATIC_URL = 'static/'
|
||||
|
||||
# Default primary key field type
|
||||
# https://docs.djangoproject.com/en/5.2/ref/settings/#default-auto-field
|
||||
|
||||
DEFAULT_AUTO_FIELD = 'django.db.models.BigAutoField'
|
||||
25
api/urls.py
Normal file
25
api/urls.py
Normal file
@ -0,0 +1,25 @@
|
||||
"""
|
||||
URL configuration for api project.
|
||||
|
||||
The `urlpatterns` list routes URLs to views. For more information please see:
|
||||
https://docs.djangoproject.com/en/5.2/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, include, re_path
|
||||
|
||||
urlpatterns = [
|
||||
re_path(r'^auth/', include('djoser.urls')),
|
||||
re_path(r'^auth/', include('djoser.urls.jwt')),
|
||||
path('', include('books.urls')),
|
||||
path('admin/', admin.site.urls),
|
||||
]
|
||||
16
api/wsgi.py
Normal file
16
api/wsgi.py
Normal file
@ -0,0 +1,16 @@
|
||||
"""
|
||||
WSGI config for api project.
|
||||
|
||||
It exposes the WSGI callable as a module-level variable named ``application``.
|
||||
|
||||
For more information on this file, see
|
||||
https://docs.djangoproject.com/en/5.2/howto/deployment/wsgi/
|
||||
"""
|
||||
|
||||
import os
|
||||
|
||||
from django.core.wsgi import get_wsgi_application
|
||||
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.settings')
|
||||
|
||||
application = get_wsgi_application()
|
||||
0
books/__init__.py
Normal file
0
books/__init__.py
Normal file
4
books/admin.py
Normal file
4
books/admin.py
Normal file
@ -0,0 +1,4 @@
|
||||
from django.contrib import admin
|
||||
from books.models import Book
|
||||
# Register your models here.
|
||||
admin.site.register(Book)
|
||||
6
books/apps.py
Normal file
6
books/apps.py
Normal file
@ -0,0 +1,6 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class BooksConfig(AppConfig):
|
||||
default_auto_field = 'django.db.models.BigAutoField'
|
||||
name = 'books'
|
||||
25
books/migrations/0001_initial.py
Normal file
25
books/migrations/0001_initial.py
Normal file
@ -0,0 +1,25 @@
|
||||
# Generated by Django 5.2.7 on 2025-11-02 22:07
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Book',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('title', models.CharField(max_length=255)),
|
||||
('author', models.CharField(max_length=255)),
|
||||
('state', models.CharField(choices=[('PLAN', 'Plan to Read'), ('READING', 'Reading'), ('COMPLETED', 'Completed'), ('DROPPED', 'Dropped')], db_index=True, default='PLAN', max_length=10, verbose_name='state')),
|
||||
('added_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
],
|
||||
),
|
||||
]
|
||||
19
books/migrations/0002_book_note.py
Normal file
19
books/migrations/0002_book_note.py
Normal file
@ -0,0 +1,19 @@
|
||||
# Generated by Django 5.2.7 on 2025-11-04 17:16
|
||||
|
||||
import django.core.validators
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('books', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='book',
|
||||
name='note',
|
||||
field=models.PositiveSmallIntegerField(blank=True, null=True, validators=[django.core.validators.MaxValueValidator(10)], verbose_name='note'),
|
||||
),
|
||||
]
|
||||
19
books/migrations/0003_alter_book_note.py
Normal file
19
books/migrations/0003_alter_book_note.py
Normal file
@ -0,0 +1,19 @@
|
||||
# Generated by Django 5.2.7 on 2025-11-04 17:18
|
||||
|
||||
import django.core.validators
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('books', '0002_book_note'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterField(
|
||||
model_name='book',
|
||||
name='note',
|
||||
field=models.PositiveSmallIntegerField(blank=True, default=0, null=True, validators=[django.core.validators.MaxValueValidator(10)], verbose_name='note'),
|
||||
),
|
||||
]
|
||||
0
books/migrations/__init__.py
Normal file
0
books/migrations/__init__.py
Normal file
38
books/models.py
Normal file
38
books/models.py
Normal file
@ -0,0 +1,38 @@
|
||||
from django.db import models
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
from django.core.validators import MaxValueValidator
|
||||
|
||||
# Create your models here.
|
||||
class Book(models.Model):
|
||||
PLAN_TO_READ = 'PLAN'
|
||||
READING = 'READING'
|
||||
COMPLETED = 'COMPLETED'
|
||||
DROPPED = 'DROPPED'
|
||||
STATE_CHOICES = [
|
||||
(PLAN_TO_READ, _('Plan to Read')),
|
||||
(READING, _('Reading')),
|
||||
(COMPLETED, _('Completed')),
|
||||
(DROPPED, _('Dropped')),
|
||||
]
|
||||
note = models.PositiveSmallIntegerField(
|
||||
_('note'),
|
||||
validators=[
|
||||
MaxValueValidator(10)
|
||||
],
|
||||
default=0,
|
||||
null=True,
|
||||
blank=True
|
||||
)
|
||||
title = models.CharField(max_length=255)
|
||||
author = models.CharField(max_length=255)
|
||||
state = models.CharField(
|
||||
_('state'),
|
||||
max_length=10,
|
||||
choices=STATE_CHOICES,
|
||||
default=PLAN_TO_READ,
|
||||
db_index=True
|
||||
)
|
||||
added_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
|
||||
7
books/serializers.py
Normal file
7
books/serializers.py
Normal file
@ -0,0 +1,7 @@
|
||||
from rest_framework import serializers
|
||||
from .models import Book
|
||||
|
||||
class BookSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Book
|
||||
fields = '__all__'
|
||||
3
books/tests.py
Normal file
3
books/tests.py
Normal file
@ -0,0 +1,3 @@
|
||||
from django.test import TestCase
|
||||
|
||||
# Create your tests here.
|
||||
6
books/urls.py
Normal file
6
books/urls.py
Normal file
@ -0,0 +1,6 @@
|
||||
from django.urls import path
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('', views.BookListCreateView.as_view())
|
||||
]
|
||||
23
books/views.py
Normal file
23
books/views.py
Normal file
@ -0,0 +1,23 @@
|
||||
from rest_framework.views import APIView
|
||||
from rest_framework.response import Response
|
||||
from rest_framework.permissions import IsAuthenticatedOrReadOnly
|
||||
from rest_framework import status
|
||||
|
||||
from .models import Book
|
||||
from .serializers import BookSerializer
|
||||
|
||||
class BookListCreateView(APIView):
|
||||
permission_classes = [IsAuthenticatedOrReadOnly]
|
||||
|
||||
def get(self, request):
|
||||
books = Book.objects.all()
|
||||
serializer = BookSerializer(books, many=True)
|
||||
return Response(serializer.data)
|
||||
|
||||
def post(self, request):
|
||||
serializer = BookSerializer(data=request.data)
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
return Response(serializer.data, status=status.HTTP_201_CREATED)
|
||||
|
||||
return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||
17
docker-compose.yml
Normal file
17
docker-compose.yml
Normal file
@ -0,0 +1,17 @@
|
||||
services:
|
||||
postgres:
|
||||
image: postgres:17
|
||||
container_name: mblbdd
|
||||
restart: always
|
||||
environment:
|
||||
POSTGRES_USER: pipi
|
||||
POSTGRES_PASSWORD: pipi
|
||||
POSTGRES_DB: pipi
|
||||
ports:
|
||||
- "5434:5432"
|
||||
volumes:
|
||||
- postgres_data:/var/lib/postgresql/data
|
||||
|
||||
volumes:
|
||||
postgres_data:
|
||||
driver: local
|
||||
22
manage.py
Executable file
22
manage.py
Executable file
@ -0,0 +1,22 @@
|
||||
#!/usr/bin/env python
|
||||
"""Django's command-line utility for administrative tasks."""
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
"""Run administrative tasks."""
|
||||
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'api.settings')
|
||||
try:
|
||||
from django.core.management import execute_from_command_line
|
||||
except ImportError as exc:
|
||||
raise ImportError(
|
||||
"Couldn't import Django. Are you sure it's installed and "
|
||||
"available on your PYTHONPATH environment variable? Did you "
|
||||
"forget to activate a virtual environment?"
|
||||
) from exc
|
||||
execute_from_command_line(sys.argv)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
||||
17
pyproject.toml
Normal file
17
pyproject.toml
Normal file
@ -0,0 +1,17 @@
|
||||
[tool.poetry]
|
||||
name = "book-list-api"
|
||||
version = "0.1.0"
|
||||
description = ""
|
||||
authors = ["Guamss <guams@duck.com>"]
|
||||
readme = "README.md"
|
||||
license = "MIT"
|
||||
|
||||
[tool.poetry.dependencies]
|
||||
python = ">=3.13,<4.0"
|
||||
Django = "^5.2.7"
|
||||
djoser = "^2.3.3"
|
||||
djangorestframework = ">=3.16.1"
|
||||
djangorestframework_simplejwt = "^5.5.1"
|
||||
django-cors-headers = "^4.9.0"
|
||||
psycopg = "^3.1"
|
||||
gunicorn = "^22.0"
|
||||
Loading…
Reference in New Issue
Block a user