diff --git a/akarpov/common/views.py b/akarpov/common/views.py index 0f6fb33..3e7d65c 100644 --- a/akarpov/common/views.py +++ b/akarpov/common/views.py @@ -1,6 +1,17 @@ from django.contrib.auth.mixins import LoginRequiredMixin, UserPassesTestMixin +from django.views.generic.detail import SingleObjectMixin class SuperUserRequiredMixin(LoginRequiredMixin, UserPassesTestMixin): def test_func(self): return self.request.user.is_superuser + + +class HasPermissions(SingleObjectMixin): + def get_context_data(self, **kwargs): + context = super().get_context_data(**kwargs) + has_perm = False + if self.request.user.is_authentificated: + has_perm = self.object.user == self.request.user + context["has_permissions"] = has_perm + return context diff --git a/akarpov/gallery/__init__.py b/akarpov/gallery/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/akarpov/gallery/admin.py b/akarpov/gallery/admin.py new file mode 100644 index 0000000..6f4b919 --- /dev/null +++ b/akarpov/gallery/admin.py @@ -0,0 +1,6 @@ +from django.contrib import admin + +from akarpov.gallery.models import Collection, Image + +admin.site.register(Collection) +admin.site.register(Image) diff --git a/akarpov/gallery/apps.py b/akarpov/gallery/apps.py new file mode 100644 index 0000000..1b69b81 --- /dev/null +++ b/akarpov/gallery/apps.py @@ -0,0 +1,12 @@ +from django.apps import AppConfig + + +class GalleryConfig(AppConfig): + name = "akarpov.gallery" + verbose_name = "Gallery" + + def ready(self): + try: + import akarpov.gallery.signals # noqa F401 + except ImportError: + pass diff --git a/akarpov/gallery/migrations/0001_initial.py b/akarpov/gallery/migrations/0001_initial.py new file mode 100644 index 0000000..8e676c0 --- /dev/null +++ b/akarpov/gallery/migrations/0001_initial.py @@ -0,0 +1,134 @@ +# Generated by Django 4.2 on 2023-04-29 08:45 + +import akarpov.utils.files +from django.conf import settings +from django.db import migrations, models +import django.db.models.deletion +import django_extensions.db.fields + + +class Migration(migrations.Migration): + + initial = True + + dependencies = [ + migrations.swappable_dependency(settings.AUTH_USER_MODEL), + ("shortener", "0001_initial"), + ] + + operations = [ + migrations.CreateModel( + name="Collection", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("slug", models.SlugField(blank=True, max_length=20, unique=True)), + ( + "created", + django_extensions.db.fields.CreationDateTimeField( + auto_now_add=True, verbose_name="created" + ), + ), + ( + "modified", + django_extensions.db.fields.ModificationDateTimeField( + auto_now=True, verbose_name="modified" + ), + ), + ("name", models.CharField(blank=True, max_length=250)), + ("description", models.TextField()), + ("public", models.BooleanField(default=False)), + ( + "short_link", + models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.SET_NULL, + to="shortener.link", + ), + ), + ( + "user", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="collections", + to=settings.AUTH_USER_MODEL, + ), + ), + ], + options={ + "get_latest_by": "modified", + "abstract": False, + }, + ), + migrations.CreateModel( + name="Image", + fields=[ + ( + "id", + models.BigAutoField( + auto_created=True, + primary_key=True, + serialize=False, + verbose_name="ID", + ), + ), + ("image_cropped", models.ImageField(blank=True, upload_to="cropped/")), + ("slug", models.SlugField(blank=True, max_length=20, unique=True)), + ( + "created", + django_extensions.db.fields.CreationDateTimeField( + auto_now_add=True, verbose_name="created" + ), + ), + ( + "modified", + django_extensions.db.fields.ModificationDateTimeField( + auto_now=True, verbose_name="modified" + ), + ), + ( + "image", + models.ImageField( + upload_to=akarpov.utils.files.user_file_upload_mixin + ), + ), + ( + "collection", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="images", + to="gallery.collection", + ), + ), + ( + "short_link", + models.ForeignKey( + blank=True, + null=True, + on_delete=django.db.models.deletion.SET_NULL, + to="shortener.link", + ), + ), + ( + "user", + models.ForeignKey( + on_delete=django.db.models.deletion.CASCADE, + related_name="images", + to=settings.AUTH_USER_MODEL, + ), + ), + ], + options={ + "get_latest_by": "modified", + "abstract": False, + }, + ), + ] diff --git a/akarpov/gallery/migrations/__init__.py b/akarpov/gallery/migrations/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/akarpov/gallery/models.py b/akarpov/gallery/models.py new file mode 100644 index 0000000..8a902fe --- /dev/null +++ b/akarpov/gallery/models.py @@ -0,0 +1,38 @@ +from django.db import models +from django.urls import reverse +from django_extensions.db.models import TimeStampedModel + +from akarpov.common.models import BaseImageModel +from akarpov.tools.shortener.models import ShortLink +from akarpov.utils.files import user_file_upload_mixin + + +class Collection(TimeStampedModel, ShortLink): + name = models.CharField(max_length=250, blank=True) + description = models.TextField() + public = models.BooleanField(default=False) + user = models.ForeignKey( + "users.User", related_name="collections", on_delete=models.CASCADE + ) + + def get_absolute_url(self): + return reverse("gallery:collection", kwargs={"slug": self.slug}) + + def __str__(self): + return self.name + + +class Image(TimeStampedModel, ShortLink, BaseImageModel): + collection = models.ForeignKey( + "Collection", related_name="images", on_delete=models.CASCADE + ) + user = models.ForeignKey( + "users.User", related_name="images", on_delete=models.CASCADE + ) + image = models.ImageField(upload_to=user_file_upload_mixin, blank=False, null=False) + + def get_absolute_url(self): + return reverse("gallery:view", kwargs={"slug": self.slug}) + + def __str__(self): + return self.image.name diff --git a/akarpov/gallery/urls.py b/akarpov/gallery/urls.py new file mode 100644 index 0000000..149e900 --- /dev/null +++ b/akarpov/gallery/urls.py @@ -0,0 +1,10 @@ +from django.urls import path + +from akarpov.gallery.views import collection_view, image_view, list_collections_view + +app_name = "gallery" +urlpatterns = [ + path("", list_collections_view, name="list"), + path("", collection_view, name="collection"), + path("image/", image_view, name="view"), +] diff --git a/akarpov/gallery/views.py b/akarpov/gallery/views.py new file mode 100644 index 0000000..5b70c86 --- /dev/null +++ b/akarpov/gallery/views.py @@ -0,0 +1,33 @@ +from django.views import generic + +from akarpov.common.views import HasPermissions +from akarpov.gallery.models import Collection + + +class ListCollectionsView(generic.ListView): + model = Collection + template_name = "gallery/list.html" + + def get_queryset(self): + if self.request.user.is_authenticated: + return self.request.user.collections.all() + return Collection.objects.filter(public=True) + + +list_collections_view = ListCollectionsView.as_view() + + +class CollectionView(generic.DetailView, HasPermissions): + model = Collection + template_name = "gallery/collection.html" + + +collection_view = CollectionView.as_view() + + +class ImageView(generic.DetailView, HasPermissions): + model = Collection + template_name = "gallery/image.html" + + +image_view = ImageView.as_view() diff --git a/akarpov/templates/gallery/collection.html b/akarpov/templates/gallery/collection.html new file mode 100644 index 0000000..21f5da2 --- /dev/null +++ b/akarpov/templates/gallery/collection.html @@ -0,0 +1 @@ +{% extends 'base.html' %} diff --git a/akarpov/templates/gallery/image.html b/akarpov/templates/gallery/image.html new file mode 100644 index 0000000..21f5da2 --- /dev/null +++ b/akarpov/templates/gallery/image.html @@ -0,0 +1 @@ +{% extends 'base.html' %} diff --git a/akarpov/templates/gallery/list.html b/akarpov/templates/gallery/list.html new file mode 100644 index 0000000..21f5da2 --- /dev/null +++ b/akarpov/templates/gallery/list.html @@ -0,0 +1 @@ +{% extends 'base.html' %} diff --git a/akarpov/templates/shortener/create.html b/akarpov/templates/tools/shortener/create.html similarity index 100% rename from akarpov/templates/shortener/create.html rename to akarpov/templates/tools/shortener/create.html diff --git a/akarpov/test_platform/templates/shortener/revoked.html b/akarpov/templates/tools/shortener/revoked.html similarity index 100% rename from akarpov/test_platform/templates/shortener/revoked.html rename to akarpov/templates/tools/shortener/revoked.html diff --git a/akarpov/templates/shortener/view.html b/akarpov/templates/tools/shortener/view.html similarity index 100% rename from akarpov/templates/shortener/view.html rename to akarpov/templates/tools/shortener/view.html diff --git a/akarpov/test_platform/templates/about/project.html b/akarpov/test_platform/templates/about/project.html deleted file mode 100644 index 55b237d..0000000 --- a/akarpov/test_platform/templates/about/project.html +++ /dev/null @@ -1,29 +0,0 @@ -{% load markdown_extras %} - -{% block content %} -
- back to all projects -
-
- -
-
-
-
-

{{ project.name }}

-

{{ project.md | markdown | safe }}

-

{{ project.description }}

-

Created: {{ project.created|date:"d M Y" }}

-

- {% if project.source_link %} - Source code - {% endif %} - {% if project.view_link %} - View project - {% endif %} -

-
-
-
-
-{% endblock %} diff --git a/akarpov/tools/shortener/views.py b/akarpov/tools/shortener/views.py index a0742ee..d5f94c3 100644 --- a/akarpov/tools/shortener/views.py +++ b/akarpov/tools/shortener/views.py @@ -11,7 +11,7 @@ class ShortLinkCreateView(CreateView): model = Link form_class = LinkForm - template_name = "shortener/create.html" + template_name = "tools/shortener/create.html" def form_valid(self, form): if self.request.user.is_authenticated: @@ -24,7 +24,7 @@ def form_valid(self, form): class LinkDetailView(DetailView): - template_name = "shortener/view.html" + template_name = "tools/shortener/view.html" def get_object(self, *args, **kwargs): link = get_link_from_slug(self.kwargs["slug"]) @@ -39,7 +39,7 @@ def get_object(self, *args, **kwargs): class LinkRevokedView(TemplateView): - template_name = "shortener/revoked.html" + template_name = "tools/shortener/revoked.html" link_revoked_view = LinkRevokedView.as_view() diff --git a/config/settings/base.py b/config/settings/base.py index b4adb23..82ee625 100644 --- a/config/settings/base.py +++ b/config/settings/base.py @@ -136,7 +136,7 @@ "health_check.contrib.redis", ] -ALL_AUTH_PROVIDERS = [ +ALLAUTH_PROVIDERS = [ "allauth.socialaccount.providers.github", # "allauth.socialaccount.providers.google", # "allauth.socialaccount.providers.telegram", @@ -149,6 +149,7 @@ "akarpov.blog", "akarpov.files", "akarpov.music", + "akarpov.gallery", "akarpov.pipeliner", "akarpov.test_platform", "akarpov.tools.shortener", @@ -156,7 +157,7 @@ ] # https://docs.djangoproject.com/en/dev/ref/settings/#installed-apps INSTALLED_APPS = ( - DJANGO_APPS + LOCAL_APPS + THIRD_PARTY_APPS + HEALTH_CHECKS + ALL_AUTH_PROVIDERS + DJANGO_APPS + LOCAL_APPS + THIRD_PARTY_APPS + HEALTH_CHECKS + ALLAUTH_PROVIDERS ) # MIGRATIONS diff --git a/config/urls.py b/config/urls.py index ee337b7..4402299 100644 --- a/config/urls.py +++ b/config/urls.py @@ -36,6 +36,7 @@ path("music/", include("akarpov.music.urls", namespace="music")), path("forms/", include("akarpov.test_platform.urls", namespace="forms")), path("tools/", include("akarpov.tools.urls", namespace="tools")), + path("gallery/", include("akarpov.gallery.urls", namespace="gallery")), path("ckeditor/", include("ckeditor_uploader.urls")), path("accounts/", include("allauth.urls")), path("", include("akarpov.blog.urls", namespace="blog")),