Skip to content

Commit

Permalink
Automatically set a forum slug based on name if it's not provided
Browse files Browse the repository at this point in the history
Forum solr tests require this field
  • Loading branch information
alastair committed Nov 27, 2022
1 parent 4655c2a commit a81a761
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions forum/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
from utils.cache import invalidate_template_cache
from utils.search import SearchEngineException, get_search_engine
from utils.search.search_forum import delete_posts_from_search_engine
from utils.text import slugify

web_logger = logging.getLogger('web')

Expand Down Expand Up @@ -76,6 +77,14 @@ def get_absolute_url(self):
return reverse("forums-forum", args=[smart_unicode(self.name_slug)])


@receiver(pre_save, sender=Forum)
def forum_pre_save_set_slug(sender, instance, **kwargs):
"""If a forum has a name set but not a slug, automatically generate the slug
"""
if not instance.id and not instance.name_slug:
instance.name_slug = slugify(instance.name)


class Thread(models.Model):
forum = models.ForeignKey(Forum)
author = models.ForeignKey(User)
Expand Down

0 comments on commit a81a761

Please sign in to comment.