diff options
Diffstat (limited to 'cms/models.py')
-rw-r--r-- | cms/models.py | 38 |
1 files changed, 27 insertions, 11 deletions
diff --git a/cms/models.py b/cms/models.py index 1811a70..05b761f 100644 --- a/cms/models.py +++ b/cms/models.py @@ -1,11 +1,15 @@ +import os + +from django.core.exceptions import ValidationError +from django.core.files.storage import FileSystemStorage from django.db import models # Create your models here. class Nav(models.Model): - name = models.CharField(max=20) - link = models.CharField(max=20) + name = models.CharField(max_length=20) + link = models.CharField(max_length=20) position = models.IntegerField() def __str__(self): @@ -14,8 +18,8 @@ class Nav(models.Model): class SubNav(models.Model): nav = models.ForeignKey(Nav, on_delete=models.CASCADE) - name = models.CharField(max=20) - link = models.CharField(max=100) + name = models.CharField(max_length=20) + link = models.CharField(max_length=100) position = models.IntegerField() def __str__(self): @@ -23,10 +27,10 @@ class SubNav(models.Model): class Page(models.Model): - permalink = models.CharField(max=100, unique=True) - title = models.CharField(max=50) + permalink = models.CharField(max_length=100, unique=True) + title = models.CharField(max_length=50) imports = models.TextField(help_text='External imports like css,js files, will be placed in <head> tag (already ' - 'includes bootstrap4 and jQuery)') + 'includes bootstrap4 and jQuery)', null=True, blank=True) content = models.TextField(help_text='Body of the page') pub_date = models.DateTimeField('date published', auto_now_add=True) @@ -34,10 +38,22 @@ class Page(models.Model): return self.title -class StaticFiles(models.Model): - filename = models.CharField(max=70, unique=True) - file = models.FileField(upload_to='static/{}'.format(filename), blank=False, - help_text='Please upload static files (images, css, js, etc) one by one') +def get_filename(instance, _): + return 'static/cms/' + str(instance.filename) + + +def validate_filename(value): + if os.path.exists('workshop_app/static/' + value): + raise ValidationError('Static file with that name already exists! Please choose a unique name. You may use ' + 'foldername/filename to upload to a folder') + + +class StaticFile(models.Model): + filename = models.CharField(max_length=70, unique=True, validators=[validate_filename]) + file = models.FileField(upload_to=get_filename, storage=FileSystemStorage(location='workshop_app', base_url='/'), + blank=False, + help_text='Please upload static file (image, css, js, etc). This file will be accessible ' + 'at static/cms/filename') def __str__(self): return self.filename |