commit dd6a70c37f0cbd446aa1adf5fece7a38d8f0ba2c Author: Guamss Date: Tue Nov 4 21:01:09 2025 +0100 initial commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d4b69cf --- /dev/null +++ b/.gitignore @@ -0,0 +1,5 @@ +.settings/* +.project +.pydevproject +poetry.lock + diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 diff --git a/api/__init__.py b/api/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/api/asgi.py b/api/asgi.py new file mode 100644 index 0000000..46a1236 --- /dev/null +++ b/api/asgi.py @@ -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() diff --git a/api/settings.py b/api/settings.py new file mode 100644 index 0000000..4bf2927 --- /dev/null +++ b/api/settings.py @@ -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' diff --git a/api/urls.py b/api/urls.py new file mode 100644 index 0000000..8d7d739 --- /dev/null +++ b/api/urls.py @@ -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), +] \ No newline at end of file diff --git a/api/wsgi.py b/api/wsgi.py new file mode 100644 index 0000000..a4688f6 --- /dev/null +++ b/api/wsgi.py @@ -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() diff --git a/books/__init__.py b/books/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/books/admin.py b/books/admin.py new file mode 100644 index 0000000..a49d6ae --- /dev/null +++ b/books/admin.py @@ -0,0 +1,4 @@ +from django.contrib import admin +from books.models import Book +# Register your models here. +admin.site.register(Book) \ No newline at end of file diff --git a/books/apps.py b/books/apps.py new file mode 100644 index 0000000..a53388c --- /dev/null +++ b/books/apps.py @@ -0,0 +1,6 @@ +from django.apps import AppConfig + + +class BooksConfig(AppConfig): + default_auto_field = 'django.db.models.BigAutoField' + name = 'books' diff --git a/books/migrations/0001_initial.py b/books/migrations/0001_initial.py new file mode 100644 index 0000000..d6e1370 --- /dev/null +++ b/books/migrations/0001_initial.py @@ -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)), + ], + ), + ] diff --git a/books/migrations/0002_book_note.py b/books/migrations/0002_book_note.py new file mode 100644 index 0000000..962cc8c --- /dev/null +++ b/books/migrations/0002_book_note.py @@ -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'), + ), + ] diff --git a/books/migrations/0003_alter_book_note.py b/books/migrations/0003_alter_book_note.py new file mode 100644 index 0000000..52cb9d6 --- /dev/null +++ b/books/migrations/0003_alter_book_note.py @@ -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'), + ), + ] diff --git a/books/migrations/__init__.py b/books/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/books/models.py b/books/models.py new file mode 100644 index 0000000..13352ea --- /dev/null +++ b/books/models.py @@ -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) + + \ No newline at end of file diff --git a/books/serializers.py b/books/serializers.py new file mode 100644 index 0000000..4e6d71d --- /dev/null +++ b/books/serializers.py @@ -0,0 +1,7 @@ +from rest_framework import serializers +from .models import Book + +class BookSerializer(serializers.ModelSerializer): + class Meta: + model = Book + fields = '__all__' \ No newline at end of file diff --git a/books/tests.py b/books/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/books/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/books/urls.py b/books/urls.py new file mode 100644 index 0000000..a04114d --- /dev/null +++ b/books/urls.py @@ -0,0 +1,6 @@ +from django.urls import path +from . import views + +urlpatterns = [ + path('', views.BookListCreateView.as_view()) +] \ No newline at end of file diff --git a/books/views.py b/books/views.py new file mode 100644 index 0000000..12000f4 --- /dev/null +++ b/books/views.py @@ -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) \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..28fc483 --- /dev/null +++ b/docker-compose.yml @@ -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 diff --git a/manage.py b/manage.py new file mode 100755 index 0000000..8c45ccf --- /dev/null +++ b/manage.py @@ -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() diff --git a/pyproject.toml b/pyproject.toml new file mode 100644 index 0000000..0ce217a --- /dev/null +++ b/pyproject.toml @@ -0,0 +1,17 @@ +[tool.poetry] +name = "book-list-api" +version = "0.1.0" +description = "" +authors = ["Guamss "] +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" \ No newline at end of file