Skip to content

Commit

Permalink
Merge pull request #14 from rkshaon/rkshaon
Browse files Browse the repository at this point in the history
backend
  • Loading branch information
rkshaon authored Sep 6, 2024
2 parents b9c2a2f + 011342c commit d4ad482
Show file tree
Hide file tree
Showing 42 changed files with 675 additions and 11 deletions.
3 changes: 2 additions & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/env
__pycache__
__pycache__
/media
26 changes: 23 additions & 3 deletions backend/BookShelf/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

from pathlib import Path

import os

# Build paths inside the project like this: BASE_DIR / 'subdir'.
BASE_DIR = Path(__file__).resolve().parent.parent

Expand All @@ -28,17 +30,25 @@
ALLOWED_HOSTS = []


# Application definition

INSTALLED_APPS = [
DJANGO_APPS = [
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
]
LIBRARY_APPS = [
'rest_framework',
'drf_yasg',
]
PROJECT_APPS = [
'user_api',
'author_api',
'publisher_api',
'book_api',
]
INSTALLED_APPS = DJANGO_APPS + LIBRARY_APPS + PROJECT_APPS

MIDDLEWARE = [
'django.middleware.security.SecurityMiddleware',
Expand Down Expand Up @@ -81,6 +91,14 @@
}
}

REST_FRAMEWORK = {
'DEFAULT_AUTHENTICATION_CLASSES': (
'rest_framework_simplejwt.authentication.JWTAuthentication',
),
'DEFAULT_PERMISSION_CLASSES': (
'rest_framework.permissions.IsAuthenticated',
),
}

# Password validation
# https://docs.djangoproject.com/en/5.1/ref/settings/#auth-password-validators
Expand Down Expand Up @@ -117,6 +135,8 @@
# https://docs.djangoproject.com/en/5.1/howto/static-files/

STATIC_URL = 'static/'
MEDIA_URL = '/media/'
MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

# Default primary key field type
# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field
Expand Down
31 changes: 29 additions & 2 deletions backend/BookShelf/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,35 @@
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 django.conf.urls.static import static
from django.conf import settings
from rest_framework import permissions
from drf_yasg.views import get_schema_view
from drf_yasg import openapi


schema_view = get_schema_view(
openapi.Info(
title="Book Shelf",
default_version='v1',
description="API documentation for Book Shelf",
terms_of_service="https://www.google.com/policies/terms/",
contact=openapi.Contact(email="contact@example.com"),
license=openapi.License(name="BSD License"),
),
public=True,
permission_classes=(permissions.AllowAny,),
)

urlpatterns = [
# admin URL
path('admin/', admin.site.urls),
]
# app URLs
path('user/', include('user_api.urls')),
# Swagger documentation URLs
path('swagger/', schema_view.with_ui('swagger',
cache_timeout=0), name='schema-swagger-ui'),
path('redoc/', schema_view.with_ui('redoc',
cache_timeout=0), name='schema-redoc'),
] + static(settings.MEDIA_URL, document_root=settings.MEDIA_ROOT)
Empty file added backend/author_api/__init__.py
Empty file.
15 changes: 15 additions & 0 deletions backend/author_api/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from django.contrib import admin

from author_api.models import Author


@admin.register(Author)
class AuthorAdmin(admin.ModelAdmin):
list_display = [
'id', 'full_name', 'birth_date',
'died_date', 'is_alive',
]
list_display_links = ('id', 'full_name',)
list_filter = []
search_fields = ('first_name', 'last_name', 'birth_date')
readonly_fields = ('id',)
6 changes: 6 additions & 0 deletions backend/author_api/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class AuthorApiConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'author_api'
24 changes: 24 additions & 0 deletions backend/author_api/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 5.1.1 on 2024-09-06 19:40

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='Author',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('first_name', models.CharField(max_length=100)),
('last_name', models.CharField(max_length=100)),
('biography', models.TextField(blank=True, null=True)),
('birth_date', models.DateField(blank=True, null=True)),
],
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
# Generated by Django 5.1.1 on 2024-09-06 19:46

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


class Migration(migrations.Migration):

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

operations = [
migrations.AddField(
model_name='author',
name='added_by',
field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL),
),
migrations.AddField(
model_name='author',
name='added_date_time',
field=models.DateTimeField(auto_now_add=True, default=django.utils.timezone.now),
preserve_default=False,
),
migrations.AddField(
model_name='author',
name='is_deleted',
field=models.BooleanField(default=False),
),
migrations.AddField(
model_name='author',
name='updated_date_time',
field=models.DateTimeField(auto_now=True),
),
]
18 changes: 18 additions & 0 deletions backend/author_api/migrations/0003_author_died_date.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.1.1 on 2024-09-06 19:52

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('author_api', '0002_author_added_by_author_added_date_time_and_more'),
]

operations = [
migrations.AddField(
model_name='author',
name='died_date',
field=models.DateField(blank=True, null=True),
),
]
Empty file.
29 changes: 29 additions & 0 deletions backend/author_api/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from django.db import models


class Author(models.Model):
first_name = models.CharField(max_length=100)
last_name = models.CharField(max_length=100)
biography = models.TextField(blank=True, null=True) # Optional bio field
birth_date = models.DateField(null=True, blank=True)
died_date = models.DateField(null=True, blank=True)
added_by = models.ForeignKey(
'user_api.User',
blank=True,
null=True,
on_delete=models.CASCADE
)
is_deleted = models.BooleanField(default=False)
added_date_time = models.DateTimeField(auto_now_add=True)
updated_date_time = models.DateTimeField(auto_now=True)

@property
def full_name(self):
return f"{self.first_name} {self.last_name}"

@property
def is_alive(self):
return True if not self.died_date else False

def __str__(self):
return f'{self.first_name} {self.last_name}'
3 changes: 3 additions & 0 deletions backend/author_api/tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.test import TestCase

# Create your tests here.
File renamed without changes.
Empty file added backend/book_api/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions backend/book_api/admin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.contrib import admin

from book_api.models import Book


@admin.register(Book)
class BookAdmin(admin.ModelAdmin):
list_display = [
'id', 'title', 'edition', 'isbn',
'published_date', 'language',
'is_deleted',
]
list_display_links = [
'title',
]
list_filter = [
'authors', 'publisher',
]
search_fields = [
'title', 'edition', 'isbn',
'published_date', 'language',
'description',
]
readonly_fields = ['id',]
6 changes: 6 additions & 0 deletions backend/book_api/apps.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
from django.apps import AppConfig


class BookApiConfig(AppConfig):
default_auto_field = 'django.db.models.BigAutoField'
name = 'book_api'
39 changes: 39 additions & 0 deletions backend/book_api/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# Generated by Django 5.1.1 on 2024-09-06 20:28

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


class Migration(migrations.Migration):

initial = True

dependencies = [
('author_api', '0003_author_died_date'),
('publisher_api', '0002_publisher_added_by_publisher_added_date_time_and_more'),
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
]

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)),
('description', models.TextField(blank=True, null=True)),
('edition', models.CharField(blank=True, max_length=50, null=True)),
('isbn', models.CharField(blank=True, max_length=13, null=True, unique=True)),
('pages', models.PositiveIntegerField(blank=True, null=True)),
('published_date', models.DateField(blank=True, null=True)),
('pdf_file', models.FileField(blank=True, null=True, upload_to='books')),
('cover_image', models.ImageField(blank=True, null=True, upload_to='book_covers')),
('is_deleted', models.BooleanField(default=False)),
('added_date_time', models.DateTimeField(auto_now_add=True)),
('updated_date_time', models.DateTimeField(auto_now=True)),
('added_by', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
('authors', models.ManyToManyField(related_name='books', to='author_api.author')),
('publisher', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='books', to='publisher_api.publisher')),
],
),
]
18 changes: 18 additions & 0 deletions backend/book_api/migrations/0002_rename_pdf_file_book_book.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.1.1 on 2024-09-06 20:32

from django.db import migrations


class Migration(migrations.Migration):

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

operations = [
migrations.RenameField(
model_name='book',
old_name='pdf_file',
new_name='book',
),
]
18 changes: 18 additions & 0 deletions backend/book_api/migrations/0003_book_language.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# Generated by Django 5.1.1 on 2024-09-06 20:33

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('book_api', '0002_rename_pdf_file_book_book'),
]

operations = [
migrations.AddField(
model_name='book',
name='language',
field=models.CharField(blank=True, max_length=200, null=True),
),
]
19 changes: 19 additions & 0 deletions backend/book_api/migrations/0004_alter_book_book.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Generated by Django 5.1.1 on 2024-09-06 20:43

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


class Migration(migrations.Migration):

dependencies = [
('book_api', '0003_book_language'),
]

operations = [
migrations.AlterField(
model_name='book',
name='book',
field=models.FileField(blank=True, null=True, upload_to=book_api.models.book_upload_path),
),
]
Empty file.
Loading

0 comments on commit d4ad482

Please sign in to comment.