Slugs Updated again, Fixed some things, Version …


Blog Updates

Post #306

EN

Slugs Updated again, Fixed some things, Version 0.5.0-0

Wednesday, 11 August 2021 14:18:35 by Khodok

3 min read

Okay well, this one is interesting

What’s new (for editors (so me))

  • Added Cloning option
    • Lets an editor (again… me) clone a post and have most of the fields already filled.
    • Extremely useful for Wednesday.
      • Just used it to make more Wednesday for the future weeks and bro… took literally a second.

What’s new (for all of us)

  • Added again a thing I added a really long time ago, but that proved itself broken really fast… because of me being dumb, lemme explain.

Lil story and some code

A Long time ago, see post here, I added something allowing me to give the same title to two posts and both would have a different slug at creation.

The code for it was this:

GDScript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
def save(self, *args, **kwargs):
        max_length = Post._meta.get_field('slug').max_length
        self.slug = orig = slugify(self.title)[:max_length]
        for x in itertools.count(2):
            if self.pk:
                if Post.objects.filter(Q(slug=self.slug),
                                       Q(author=self.author),
                                       Q(id=self.pk),
                                       ).exists():
                    break
            if not Post.objects.filter(slug=self.slug).exists():
                break

            # Truncate & Minus 1 for the hyphen.
            self.slug = "%s-%d" % (orig[:max_length - len(str(x)) - 1], x)
        return super().save(*args, **kwargs)

So there’s one considerable problem with this code, it doesn’t test if the post already has a slug… so if I were to edit the post, it’d just recreate the slug… ultimately adding a 2, then a 3, etc.
That was obviously not the point… I wanted it to check at the CREATION of a post (not at the update) if the post had a slug, if not, it’d create one.

So how did I fix it ? Well again… I removed it all this time because I thought it was a MUCH more difficult thing to fix…
Here’s the fix:

GDScript
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
def save(self, *args, **kwargs):
    if not self.slug:
        max_length = Post._meta.get_field('slug').max_length
        self.slug = orig = slugify(self.title)[:max_length]
        for x in itertools.count(2):
            if self.pk:
                if Post.objects.filter(Q(slug=self.slug),
                                       Q(author=self.author),
                                       Q(id=self.pk),
                                       ).exists():
                    break
            if not Post.objects.filter(slug=self.slug).exists():
                break

            # Truncate & Minus 1 for the hyphen.
            self.slug = "%s-%d" % (orig[:max_length - len(str(x)) - 1], x)
    return super().save(*args, **kwargs)

The difference ? this line: if not self.slug:
Basically it will do all the thing below ONLY if the post doesn’t have a slug, else it’ll just not care about it.
I was dumb…

A few weeks ago I understood the problem, but let it slide for a while because I was kinda lazy to fix it.

What changed

  • Fixed some bugs, mostly files named wrongly and I was always too lazy to name them correctly, so I kept them like that for literally months.
  • And same for some code at different places.

Just before the conclusion a cool thing

  • Yesterday I took way too much time adding a song in More song lyrics post, so go check it out.
  • The reason why it took time is because there’s no lyrics for it online as it’s a mashup, takes too much effort, so nobody does it, but it’s probably my most listened song of the year, and I wanted to do it.

Conclusion

Well hope you had fun reading all this (it’s better than a big book isn’t it ?)
And hf in your life.
Till next time.

Comments

Please Log in to leave a comment.


This post has 0 comments.