def slugify_field(self, field_name):
     pretranslate = load_slug_rule(self.language)
     slugify_url = Slugify(pretranslate=pretranslate)
     slugify_url.to_lower = True
     slugify_url.stop_words = ()
     slugify_url.max_length = 1000
     return slugify_url(self.field_dict[field_name], max_length=1000)
Beispiel #2
0
    def test_stop_words(self):
        slugify = Slugify(stop_words=['a', 'the'])

        self.assertEqual(slugify('A red apple'), 'red-apple')
        self.assertEqual(slugify('The4 red apple'), 'The4-red-apple')

        self.assertEqual(slugify('_The_red_the-apple'), 'red-apple')
        self.assertEqual(slugify('The__red_apple'), 'red-apple')

        slugify.safe_chars = '*'
        self.assertEqual(slugify('*The*red*apple'), '*-*red*apple')
        self.assertEqual(slugify('The**red*apple'), '**red*apple')

        slugify.stop_words = ['x', 'y']
        self.assertEqual(slugify('x y n'), 'n')
    def slugify(self):
        if (self.field_dict["product_name"] is not None
                and self.field_dict["brand"] is not None
                and self.field_dict["fingerprint"] is not None):
            key_word_list = self.keyword_list_lookup()
            pretranslate = load_slug_rule(self.language)
            slugify_url = Slugify(pretranslate=pretranslate)
            slugify_url.to_lower = True
            slugify_url.stop_words = ()
            slugify_url.max_length = 1000
            first_bit = slugify_url(
                "-".join(key_word for key_word in key_word_list
                         if key_word).lower(),
                max_length=1000,
            )
            if len(first_bit) == 0:
                return ""

            friendly_id = "-".join([first_bit, self.field_dict["fingerprint"]])
            return friendly_id
Beispiel #4
0
from django.db import models
from slugify import Slugify

slugify_text = Slugify()
slugify_text.to_lower = True
slugify_text.stop_words = ('a', 'an', 'the')
slugify_text.max_length = 200

def get_image_url(instance, filename):
    return f'blogs/{instance.slug}/{filename}'

class Blog(models.Model):
    title = models.CharField(max_length=200)
    slug = models.CharField(max_length=230)
    date_created = models.DateField()
    title_image = models.ImageField(upload_to=get_image_url)

    def save(self, *args, **kwargs):
        if not self.id:
            self.slug = slugify_text(self.title)
        super(Blog, self).save(*args, **kwargs)

    def __str__(self):
        return self.title