42 lines
1.2 KiB
Python
42 lines
1.2 KiB
Python
from datetime import datetime
|
|
from os.path import basename, join
|
|
from uuid import uuid4
|
|
|
|
from django.db import models
|
|
|
|
|
|
def change_upload_path(instance, filename):
|
|
ext = filename.split('.')[-1]
|
|
now = datetime.now()
|
|
return join(str(now.year), str(now.month),
|
|
f'{uuid4()}.{ext}')
|
|
|
|
|
|
class Post(models.Model):
|
|
message = models.CharField('Nachricht', max_length=4096)
|
|
posted = models.BooleanField('Gepostet', default=False)
|
|
publication_date = models.DateTimeField('Publizieren am')
|
|
|
|
class Meta():
|
|
verbose_name = 'Post'
|
|
|
|
def __str__(self):
|
|
date = self.publication_date.strftime('%d.%m.%Y %H:%M')
|
|
return f'{date}: {self.message[0:50]}'
|
|
|
|
|
|
class Image(models.Model):
|
|
path = models.ImageField('Bild', upload_to=change_upload_path,
|
|
default=None)
|
|
caption = models.CharField('Beschreibung', max_length=2048)
|
|
|
|
post = models.ForeignKey(Post, on_delete=models.CASCADE, default=None)
|
|
|
|
class Meta():
|
|
verbose_name = 'Bild'
|
|
verbose_name_plural = 'Bilder'
|
|
|
|
def __str__(self):
|
|
path = basename(str(self.path))
|
|
return f'{path}: {self.caption[0:50]}'
|