From c22807921deebcecaba868d9f8e4e9874aef8e80 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Mon, 2 Oct 2023 20:54:43 +0200 Subject: [PATCH 1/2] loading optimized, empty post bug --- gawa/blog/models.py | 58 ++++++-------------------------------------- gawa/start/models.py | 2 +- 2 files changed, 8 insertions(+), 52 deletions(-) diff --git a/gawa/blog/models.py b/gawa/blog/models.py index 395ec6d..0bc6f2c 100644 --- a/gawa/blog/models.py +++ b/gawa/blog/models.py @@ -228,72 +228,28 @@ class BlogPost(Searchable): files = [f for f in os.listdir(data_dir) if ( data_dir.joinpath(f)).is_file()] - logger.debug(f"discovered files: {files}") - # finding lang and title - regex = r"^(en|de)-(.*)\.md" - # TODO: only find toml files + # find the meta file + regex = r"^(.*)\.toml" - # filepath, language codes, slug - files = [[f, cls.DEFAULT_LANGS, ""] for f in files] + # filepath, slug + files = [[f, ""] for f in files] for file in files: # parse file name try: matches = re.match(regex, file[0]) if matches is None: - logger.warning( - f"Data file '{file[0]}' does not fit to the filename\ - regex") files.remove(file) else: current_lang = matches.group(1) - file[1][current_lang] = True - file[2] = matches.group(2) - except Exception as e: - logger.error(e) - files.remove(file) + file[1] = matches.group(1) - # PERF: - # Optimize for single loop, should be doable and better design - - # collapse diffrent versions - for file in files: - try: - if [_f[2] for _f in files].count(file[2]) >= 2: - logger.debug(f"multiple versions of '{file[2]}'") - versions = [_f for _f in files if _f[2] == file[2]] - lang: dict[str, bool] = file[1] - for version in versions: - for key in version[1]: - lang[key] |= version[1][key] - else: - # only a single version of this file - continue - except Exception as e: - logger.error( - f"Could not combine BlogPosts for '{file[0]}': {e}") - - # TODO: not needed when relying on toml files - try: - # deduplicate - _files = [] - for f in [[_f[1], _f[2]] - for _f in files]: # dont care about fname - if f not in _files: - _files.append(f) - files = _files - logger.debug(f"to save: {files}") - except Exception as e: - logger.error(f"Could not dedup BlogPosts: {e}") - - for file in files: - try: - obj = BlogPost(langs=file[0], slug=file[1]) + obj = BlogPost(slug=file[1]) obj.sync_file() obj.regenerate() - obj.save() except Exception as e: logger.error(f"Could not create BlogPost for '{file[1]}': {e}") + files.remove(file) class Meta: verbose_name = _("blog post") diff --git a/gawa/start/models.py b/gawa/start/models.py index 636e86d..71a8d0e 100644 --- a/gawa/start/models.py +++ b/gawa/start/models.py @@ -59,7 +59,7 @@ class Searchable(models.Model): obj.regenerate() def __str__(self): - return f"{{<{self.__class__.__name__}>\"{self.slug}\"}}" + return f"{{<{self.__class__.__name__}>\"{self.title_en}\"}}" def regenerate(self): """ -- 2.40.1 From 83d75823a411660a9c5398b934840d1e9403f5a0 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Mon, 2 Oct 2023 21:08:11 +0200 Subject: [PATCH 2/2] don't save empty slugs for blogposts --- .../migrations/0002_alter_blogpost_slug.py | 18 ++++++++++++++++++ gawa/blog/models.py | 14 ++++++++++++-- gawa/start/models.py | 2 +- 3 files changed, 31 insertions(+), 3 deletions(-) create mode 100644 gawa/blog/migrations/0002_alter_blogpost_slug.py diff --git a/gawa/blog/migrations/0002_alter_blogpost_slug.py b/gawa/blog/migrations/0002_alter_blogpost_slug.py new file mode 100644 index 0000000..d9fadbb --- /dev/null +++ b/gawa/blog/migrations/0002_alter_blogpost_slug.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.5 on 2023-10-02 19:03 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('blog', '0001_initial'), + ] + + operations = [ + migrations.AlterField( + model_name='blogpost', + name='slug', + field=models.SlugField(unique=True), + ), + ] diff --git a/gawa/blog/models.py b/gawa/blog/models.py index 0bc6f2c..89bc886 100644 --- a/gawa/blog/models.py +++ b/gawa/blog/models.py @@ -76,7 +76,7 @@ class BlogPost(Searchable): body_de = models.TextField(blank=True, default="""This aritcle is not available in english.""") category = models.ForeignKey( - Category, on_delete=models.SET_DEFAULT, null=False, + Category, on_delete=models.SET_DEFAULT, blank=False, default=Category.get_or_create_uncategorized) thumbnail = models.ImageField( blank=True, @@ -85,7 +85,15 @@ class BlogPost(Searchable): featured = models.BooleanField(default=False) langs = models.CharField( default=DEFAULT_LANGS.__repr__(), max_length=64) - slug = models.SlugField() + slug = models.SlugField(unique=True, blank=False) + + def save(self): + # check if the slug is empty if we remove whitespaces + if len(self.slug.strip()) == 0: + logger.error( + f"trying to save '{self.__class__}' with empty slug: {self}") + raise ValueError(f"trying to save '{self.__class__}' with empty slug: {self}") + super().save() def regenerate(self): """ @@ -239,7 +247,9 @@ class BlogPost(Searchable): try: matches = re.match(regex, file[0]) if matches is None: + # file is not a toml / meta file files.remove(file) + continue else: current_lang = matches.group(1) file[1] = matches.group(1) diff --git a/gawa/start/models.py b/gawa/start/models.py index 71a8d0e..b81660f 100644 --- a/gawa/start/models.py +++ b/gawa/start/models.py @@ -75,7 +75,7 @@ class Searchable(models.Model): class StaticSite(Searchable): """ - This model represents any static site, such as start:index, + This model represents any static site, such as start:index, that should show up in search. Every searchable view should inherit from start.views.SearchableView. -- 2.40.1