From 8d1b2c357e60baf97b1bef80d340fdac6807b1d2 Mon Sep 17 00:00:00 2001 From: Arity-T Date: Thu, 16 Jan 2025 13:36:07 +0300 Subject: [PATCH] =?UTF-8?q?=D0=9F=D0=B5=D1=80=D0=B2=D0=B0=D1=8F=20=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=D1=81=D0=B8=D1=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 1 + bookify/.gitignore | 36 +++++ bookify/bookify/__init__.py | 0 bookify/bookify/asgi.py | 16 +++ bookify/bookify/settings.py | 124 ++++++++++++++++++ bookify/bookify/urls.py | 7 + bookify/bookify/wsgi.py | 16 +++ bookify/books/__init__.py | 0 bookify/books/admin.py | 21 +++ bookify/books/apps.py | 6 + bookify/books/forms.py | 27 ++++ bookify/books/models.py | 32 +++++ bookify/books/static/css/style.css | 73 +++++++++++ bookify/books/templates/books/add_book.html | 10 ++ bookify/books/templates/books/base.html | 26 ++++ .../books/templates/books/book_detail.html | 44 +++++++ bookify/books/templates/books/book_list.html | 21 +++ .../books/genre_recommendations.html | 13 ++ bookify/books/tests.py | 3 + bookify/books/urls.py | 18 +++ bookify/books/views.py | 63 +++++++++ bookify/manage.py | 22 ++++ 22 files changed, 579 insertions(+) create mode 100644 .gitignore create mode 100644 bookify/.gitignore create mode 100644 bookify/bookify/__init__.py create mode 100644 bookify/bookify/asgi.py create mode 100644 bookify/bookify/settings.py create mode 100644 bookify/bookify/urls.py create mode 100644 bookify/bookify/wsgi.py create mode 100644 bookify/books/__init__.py create mode 100644 bookify/books/admin.py create mode 100644 bookify/books/apps.py create mode 100644 bookify/books/forms.py create mode 100644 bookify/books/models.py create mode 100644 bookify/books/static/css/style.css create mode 100644 bookify/books/templates/books/add_book.html create mode 100644 bookify/books/templates/books/base.html create mode 100644 bookify/books/templates/books/book_detail.html create mode 100644 bookify/books/templates/books/book_list.html create mode 100644 bookify/books/templates/books/genre_recommendations.html create mode 100644 bookify/books/tests.py create mode 100644 bookify/books/urls.py create mode 100644 bookify/books/views.py create mode 100644 bookify/manage.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f5e96db --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +venv \ No newline at end of file diff --git a/bookify/.gitignore b/bookify/.gitignore new file mode 100644 index 0000000..b9ad002 --- /dev/null +++ b/bookify/.gitignore @@ -0,0 +1,36 @@ +.idea +idea +.DS_Store + +*.egg-info/ +.installed.cfg +*.egg + +dist +db.sqlite3 + +venv + +prod_settings.py +local_settings.py + +__pycache__/ +*/__pycache__/ +*.py[cod] +.pyc +*.pyc + +!*media +media/* + +log/debug.log + +build +doc +home.rst +make.bat +Makefile + +doc-dev + +books/migrations diff --git a/bookify/bookify/__init__.py b/bookify/bookify/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/bookify/bookify/asgi.py b/bookify/bookify/asgi.py new file mode 100644 index 0000000..acfd2fa --- /dev/null +++ b/bookify/bookify/asgi.py @@ -0,0 +1,16 @@ +""" +ASGI config for bookify 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.1/howto/deployment/asgi/ +""" + +import os + +from django.core.asgi import get_asgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'bookify.settings') + +application = get_asgi_application() diff --git a/bookify/bookify/settings.py b/bookify/bookify/settings.py new file mode 100644 index 0000000..56b0c94 --- /dev/null +++ b/bookify/bookify/settings.py @@ -0,0 +1,124 @@ +""" +Django settings for bookify project. + +Generated by 'django-admin startproject' using Django 5.1.5. + +For more information on this file, see +https://docs.djangoproject.com/en/5.1/topics/settings/ + +For the full list of settings and their values, see +https://docs.djangoproject.com/en/5.1/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.1/howto/deployment/checklist/ + +# SECURITY WARNING: keep the secret key used in production secret! +SECRET_KEY = "django-insecure-c1_r=$!h*n-@r1u-r#9x*xsgs7$a*2cnr7!c8=+irf!*4@g$$2" + +# SECURITY WARNING: don't run with debug turned on in production! +DEBUG = True + +ALLOWED_HOSTS = [] + + +# Application definition + +INSTALLED_APPS = [ + "django.contrib.admin", + "django.contrib.auth", + "django.contrib.contenttypes", + "django.contrib.sessions", + "django.contrib.messages", + "django.contrib.staticfiles", + "books", +] + +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", +] + +ROOT_URLCONF = "bookify.urls" + +TEMPLATES = [ + { + "BACKEND": "django.template.backends.django.DjangoTemplates", + "DIRS": [], + "APP_DIRS": True, + "OPTIONS": { + "context_processors": [ + "django.template.context_processors.debug", + "django.template.context_processors.request", + "django.contrib.auth.context_processors.auth", + "django.contrib.messages.context_processors.messages", + ], + }, + }, +] + +WSGI_APPLICATION = "bookify.wsgi.application" + + +# Database +# https://docs.djangoproject.com/en/5.1/ref/settings/#databases + +DATABASES = { + "default": { + "ENGINE": "django.db.backends.sqlite3", + "NAME": BASE_DIR / "db.sqlite3", + } +} + + +# Password validation +# https://docs.djangoproject.com/en/5.1/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.1/topics/i18n/ + +LANGUAGE_CODE = "ru" + +TIME_ZONE = "UTC" + +USE_I18N = True + +USE_TZ = True + + +# Static files (CSS, JavaScript, Images) +# https://docs.djangoproject.com/en/5.1/howto/static-files/ + +STATIC_URL = "static/" + +# Default primary key field type +# https://docs.djangoproject.com/en/5.1/ref/settings/#default-auto-field + +DEFAULT_AUTO_FIELD = "django.db.models.BigAutoField" diff --git a/bookify/bookify/urls.py b/bookify/bookify/urls.py new file mode 100644 index 0000000..1fc22eb --- /dev/null +++ b/bookify/bookify/urls.py @@ -0,0 +1,7 @@ +from django.contrib import admin +from django.urls import include, path + +urlpatterns = [ + path("admin/", admin.site.urls), + path("", include("books.urls", namespace="books")), +] diff --git a/bookify/bookify/wsgi.py b/bookify/bookify/wsgi.py new file mode 100644 index 0000000..c5ddf4b --- /dev/null +++ b/bookify/bookify/wsgi.py @@ -0,0 +1,16 @@ +""" +WSGI config for bookify 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.1/howto/deployment/wsgi/ +""" + +import os + +from django.core.wsgi import get_wsgi_application + +os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'bookify.settings') + +application = get_wsgi_application() diff --git a/bookify/books/__init__.py b/bookify/books/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/bookify/books/admin.py b/bookify/books/admin.py new file mode 100644 index 0000000..b5d9935 --- /dev/null +++ b/bookify/books/admin.py @@ -0,0 +1,21 @@ +from django.contrib import admin + +from .models import Book, Genre, Review + + +@admin.register(Genre) +class GenreAdmin(admin.ModelAdmin): + list_display = ("id", "name") + + +@admin.register(Book) +class BookAdmin(admin.ModelAdmin): + list_display = ("id", "title", "author") + search_fields = ("title", "author") + list_filter = ("genres",) + + +@admin.register(Review) +class ReviewAdmin(admin.ModelAdmin): + list_display = ("id", "book", "user_name", "rating", "created_at") + list_filter = ("book", "rating") diff --git a/bookify/books/apps.py b/bookify/books/apps.py new file mode 100644 index 0000000..a53388c --- /dev/null +++ b/bookify/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/bookify/books/forms.py b/bookify/books/forms.py new file mode 100644 index 0000000..5dd791c --- /dev/null +++ b/bookify/books/forms.py @@ -0,0 +1,27 @@ +from django import forms + +from .models import Book, Genre, Review + + +class BookForm(forms.ModelForm): + class Meta: + model = Book + fields = ["title", "author", "description", "cover_image", "genres"] + widgets = { + "genres": forms.CheckboxSelectMultiple(), + } + + +class ReviewForm(forms.ModelForm): + class Meta: + model = Review + fields = ["user_name", "rating", "text"] + widgets = { + "rating": forms.NumberInput(attrs={"min": 1, "max": 5}), + } + + +class GenreForm(forms.ModelForm): + class Meta: + model = Genre + fields = ["name"] diff --git a/bookify/books/models.py b/bookify/books/models.py new file mode 100644 index 0000000..95e935f --- /dev/null +++ b/bookify/books/models.py @@ -0,0 +1,32 @@ +from django.contrib.auth.models import User +from django.db import models + + +class Genre(models.Model): + name = models.CharField(max_length=100, unique=True) + + def __str__(self): + return self.name + + +class Book(models.Model): + title = models.CharField(max_length=200) + author = models.CharField(max_length=100) + description = models.TextField() + genres = models.ManyToManyField(Genre, related_name="books") + cover_image = models.URLField(blank=True, null=True) # Можно хранить URL обложки + + def __str__(self): + return self.title + + +class Review(models.Model): + book = models.ForeignKey(Book, on_delete=models.CASCADE, related_name="reviews") + # Если планируется использовать Django-аутентификацию, можно связать с User + user_name = models.CharField(max_length=100) + rating = models.PositiveIntegerField(default=1) # от 1 до 5, например + text = models.TextField() + created_at = models.DateTimeField(auto_now_add=True) + + def __str__(self): + return f"Отзыв от {self.user_name} на книгу «{self.book}»" diff --git a/bookify/books/static/css/style.css b/bookify/books/static/css/style.css new file mode 100644 index 0000000..09a9844 --- /dev/null +++ b/bookify/books/static/css/style.css @@ -0,0 +1,73 @@ +/* static/css/style.css */ +body { + font-family: Arial, sans-serif; + margin: 0; + padding: 0; +} + +header, footer { + background-color: #eee; + padding: 15px; +} + +header h1, header nav { + display: inline-block; + vertical-align: middle; + margin: 0; +} + +header h1 a { + text-decoration: none; + color: #333; +} + +header nav { + float: right; +} + +header nav a { + margin-left: 10px; + text-decoration: none; + color: #333; +} + +main { + padding: 20px; +} + +.book-list { + display: flex; + flex-direction: column; + gap: 1rem; +} + +.book-item { + padding: 10px; + border-bottom: 1px solid #ccc; +} + +.book-item h3 { + margin: 5px 0; +} + +.btn-delete { + color: red; + text-decoration: none; +} + +.reviews-section { + margin-top: 20px; +} + +.review { + margin-bottom: 10px; +} + +.add-review { + margin-top: 20px; +} + +footer p { + text-align: center; + margin: 0; +} diff --git a/bookify/books/templates/books/add_book.html b/bookify/books/templates/books/add_book.html new file mode 100644 index 0000000..568edf2 --- /dev/null +++ b/bookify/books/templates/books/add_book.html @@ -0,0 +1,10 @@ + +{% extends 'books/base.html' %} +{% block content %} +

Добавить книгу

+
+ {% csrf_token %} + {{ form.as_p }} + +
+{% endblock %} diff --git a/bookify/books/templates/books/base.html b/bookify/books/templates/books/base.html new file mode 100644 index 0000000..92e008d --- /dev/null +++ b/bookify/books/templates/books/base.html @@ -0,0 +1,26 @@ + +{% load static %} + + + + + Приложение для книг + + + +
+

Bookify

+ +
+ +
+ {% block content %}{% endblock %} +
+ + + + diff --git a/bookify/books/templates/books/book_detail.html b/bookify/books/templates/books/book_detail.html new file mode 100644 index 0000000..fcca495 --- /dev/null +++ b/bookify/books/templates/books/book_detail.html @@ -0,0 +1,44 @@ + +{% extends 'books/base.html' %} +{% block content %} +
+

{{ book.title }}

+

Автор: {{ book.author }}

+

Описание: {{ book.description }}

+ {% if book.cover_image %} + Обложка книги + {% endif %} + +

Жанры: + {% for g in book.genres.all %} + {{ g.name }}{% if not forloop.last %}, {% endif %} + {% endfor %} +

+ +

+ Удалить книгу +

+
+ +
+

Отзывы

+ {% for review in reviews %} +
+

{{ review.user_name }} ({{ review.rating }}/5)

+

{{ review.text }}

+
+
+ {% empty %} +

Пока нет отзывов.

+ {% endfor %} +
+ +
+

Добавить отзыв

+
+ {% csrf_token %} + {{ review_form.as_p }} + +
+
+{% endblock %} diff --git a/bookify/books/templates/books/book_list.html b/bookify/books/templates/books/book_list.html new file mode 100644 index 0000000..5cf3118 --- /dev/null +++ b/bookify/books/templates/books/book_list.html @@ -0,0 +1,21 @@ + +{% extends 'books/base.html' %} +{% load static %} +{% block content %} +

Список книг

+
+ {% for book in books %} +
+

{{ book.title }}

+

Автор: {{ book.author }}

+ {% if book.genres.all %} +

Жанры: + {% for g in book.genres.all %} + {{ g.name }}{% if not forloop.last %}, {% endif %} + {% endfor %} +

+ {% endif %} +
+ {% endfor %} +
+{% endblock %} diff --git a/bookify/books/templates/books/genre_recommendations.html b/bookify/books/templates/books/genre_recommendations.html new file mode 100644 index 0000000..ada6911 --- /dev/null +++ b/bookify/books/templates/books/genre_recommendations.html @@ -0,0 +1,13 @@ + +{% extends 'books/base.html' %} +{% block content %} +

Рекомендации по жанру "{{ genre.name }}"

+
+ {% for book in books %} +
+

{{ book.title }}

+

Автор: {{ book.author }}

+
+ {% endfor %} +
+{% endblock %} diff --git a/bookify/books/tests.py b/bookify/books/tests.py new file mode 100644 index 0000000..7ce503c --- /dev/null +++ b/bookify/books/tests.py @@ -0,0 +1,3 @@ +from django.test import TestCase + +# Create your tests here. diff --git a/bookify/books/urls.py b/bookify/books/urls.py new file mode 100644 index 0000000..40a0bc5 --- /dev/null +++ b/bookify/books/urls.py @@ -0,0 +1,18 @@ +from django.urls import path + +from . import views + +app_name = "books" + +urlpatterns = [ + path("", views.book_list, name="book_list"), + path("book//", views.book_detail, name="book_detail"), + path("book/add/", views.add_book, name="add_book"), + path("book/delete//", views.delete_book, name="delete_book"), + path("book//add_review/", views.add_review, name="add_review"), + path( + "genres//", + views.genre_recommendations, + name="genre_recommendations", + ), +] diff --git a/bookify/books/views.py b/bookify/books/views.py new file mode 100644 index 0000000..c309554 --- /dev/null +++ b/bookify/books/views.py @@ -0,0 +1,63 @@ +from django.shortcuts import get_object_or_404, redirect, render + +from .forms import BookForm, ReviewForm +from .models import Book, Genre, Review + + +def book_list(request): + """Главная страница со списком всех книг.""" + books = Book.objects.all() + return render(request, "books/book_list.html", {"books": books}) + + +def book_detail(request, pk): + """Детальная страница книги.""" + book = get_object_or_404(Book, pk=pk) + reviews = book.reviews.all() + review_form = ReviewForm() + return render( + request, + "books/book_detail.html", + {"book": book, "reviews": reviews, "review_form": review_form}, + ) + + +def add_book(request): + """Добавление новой книги.""" + if request.method == "POST": + form = BookForm(request.POST) + if form.is_valid(): + form.save() + return redirect("books:book_list") + else: + form = BookForm() + return render(request, "books/add_book.html", {"form": form}) + + +def delete_book(request, pk): + """Удаление книги.""" + book = get_object_or_404(Book, pk=pk) + book.delete() + return redirect("books:book_list") + + +def add_review(request, pk): + """Добавление отзыва к книге.""" + book = get_object_or_404(Book, pk=pk) + if request.method == "POST": + form = ReviewForm(request.POST) + if form.is_valid(): + review = form.save(commit=False) + review.book = book + review.save() + return redirect("books:book_detail", pk=pk) + + +def genre_recommendations(request, genre_name): + """Рекомендации книг по заданному жанру.""" + genre = get_object_or_404(Genre, name=genre_name) + # Выберем все книги, которые относятся к данному жанру + books = genre.books.all() + return render( + request, "books/genre_recommendations.html", {"genre": genre, "books": books} + ) diff --git a/bookify/manage.py b/bookify/manage.py new file mode 100644 index 0000000..715c9c9 --- /dev/null +++ b/bookify/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', 'bookify.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()