Browse Source

Add Gallery model

master
Nyymix 2 months ago
parent
commit
7e760bd183
  1. 18
      gallery/admin.py
  2. 34
      gallery/migrations/0001_initial.py
  3. 36
      gallery/models/gallery.py
  4. 11
      gallery/models/storage.py
  5. BIN
      media/default.png

18
gallery/admin.py

@ -1,3 +1,21 @@
from django.contrib import admin
# Register your models here.
from django.utils.safestring import mark_safe
from .models import Gallery
admin.site.site_header = 'Gallyx admin'
admin.site.site_title = 'Gallyx admin'
admin.site.index_title = 'Gallyx administration'
class GalleryAdmin(admin.ModelAdmin):
list_display = ('name', 'desc',)
list_display_links = ('name',)
list_editable = ('desc',)
search_fields = ('name', 'slug',)
prepopulated_fields = {'slug': ('name',)}
list_per_page = 30
admin.site.register(Gallery, GalleryAdmin)

34
gallery/migrations/0001_initial.py

@ -0,0 +1,34 @@
# Generated by Django 4.0.4 on 2022-05-11 13:16
from django.db import migrations, models
import gallery.models.gallery
import gallery.models.storage
class Migration(migrations.Migration):
initial = True
dependencies = [
]
operations = [
migrations.CreateModel(
name='Gallery',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('name', models.CharField(max_length=150, verbose_name='Gallery Name')),
('slug', models.SlugField(blank=True, max_length=150, unique=True, verbose_name='Gallery Slug')),
('desc', models.CharField(blank=True, max_length=250, null=True, verbose_name='Gallery Description')),
('cover', models.ImageField(blank=True, default='default.png', storage=gallery.models.storage.OverwriteStorage(), upload_to=gallery.models.gallery.Gallery.get_upload_path, verbose_name='Gallery cover')),
],
options={
'verbose_name_plural': 'Galleries',
'ordering': ['name'],
},
),
migrations.AddIndex(
model_name='gallery',
index=models.Index(fields=['slug'], name='gallery_gal_slug_3e5c10_idx'),
),
]

36
gallery/models/gallery.py

@ -1,3 +1,37 @@
from django.db import models
from django.urls import reverse
from django.utils.text import slugify
# Create your models here.
from .storage import OverwriteStorage
# Galleries
class Gallery(models.Model):
def get_upload_path(instance, filename):
return 'covers/gallery_{0}.{1}'.format(instance.slug, filename.split(".")[-1])
name = models.CharField(max_length=150, verbose_name="Gallery Name")
slug = models.SlugField(max_length=150, unique=True, blank=True, db_index=True, verbose_name="Gallery Slug")
desc = models.CharField(max_length=250, blank=True, null=True, verbose_name="Gallery Description")
cover = models.ImageField(upload_to=get_upload_path, storage=OverwriteStorage(), blank=True, default="default.png", verbose_name="Gallery cover")
class Meta:
ordering = ['name']
verbose_name_plural = "Galleries"
indexes = [
models.Index(fields=['slug']),
]
def __str__(self):
return '{}'.format(self.name)
def save(self, *args, **kwargs):
if not self.slug:
self.slug = slugify(self.name)
if not self.cover:
self.cover = 'default.png'
super().save(*args, **kwargs)
def get_absolute_url(self):
return reverse('gallery:gallery_url', args=[self.slug])

11
gallery/models/storage.py

@ -0,0 +1,11 @@
from django.core.files.storage import FileSystemStorage
from django.conf import settings
import os
class OverwriteStorage(FileSystemStorage):
def get_available_name(self, name, max_length=None):
if self.exists(name):
os.remove(os.path.join(settings.MEDIA_ROOT, name))
return name

BIN
media/default.png

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.5 KiB

Loading…
Cancel
Save