Beispiel #1
0
 def save(self, *args, **kwargs):
     """Handle slugs."""
     if self.id is None or self.label != get_object_or_404(
             Term, id=self.id).label:
         self.slug = default_slugify(
             self.label, Term._meta.model_name,
             lambda slug: Term.objects.filter(slug=slug).exists())
     return super(Term, self).save(*args, **kwargs)
Beispiel #2
0
 def test_default_slugify(self):
     """
     Test for the default slugify function.
     Since this function is generic, here we test the generic call
     """
     # a normal string
     self.assertEqual(default_slugify('foo', 'bar', lambda x: False), 'foo')
     # the normal string already exists
     self.assertEqual(default_slugify('foo', 'bar', lambda x: x == 'foo'),
                      'foo1')
     # a string that gives an empty slug
     self.assertEqual(default_slugify('%^%$', 'bar', lambda x: False),
                      'bar-slug')
     # the default slug already exists
     self.assertEqual(
         default_slugify('%^%$', 'bar', lambda x: x == 'bar-slug'),
         'bar-slug1')
Beispiel #3
0
    def save(self, *args, **kwargs):
        """Handle slugs."""
        if self.id is None or self.name != get_object_or_404(Vocabulary,
                                                             id=self.id).name:
            self.slug = default_slugify(
                self.name, Vocabulary._meta.model_name,
                lambda slug: Vocabulary.objects.filter(slug=slug).exists())

        return super(Vocabulary, self).save(*args, **kwargs)
Beispiel #4
0
 def save(self, *args, **kwargs):
     """Handle slugs."""
     if self.id is None or self.label != get_object_or_404(
             Term, id=self.id).label:
         self.slug = default_slugify(
             self.label,
             Term._meta.model_name,
             lambda slug: Term.objects.filter(slug=slug).exists()
         )
     return super(Term, self).save(*args, **kwargs)
Beispiel #5
0
def backfill_empty_slugs(apps, schema_editor):
    """
    Finds all the objects in the Repository
    that do not have a slug and change the name;
    this will automatically create a new slug.
    """
    Vocabulary = apps.get_model("taxonomy", "Vocabulary")
    Term = apps.get_model("taxonomy", "Term")

    for vocab in Vocabulary.objects.filter(slug=''):
        vocab.slug = default_slugify(
            vocab.name, Vocabulary._meta.model_name,
            lambda slug: Vocabulary.objects.filter(slug=slug).exists())
        vocab.save()
    for term in Term.objects.filter(slug=''):
        term.slug = default_slugify(
            term.label, Term._meta.model_name,
            lambda slug: Term.objects.filter(slug=slug).exists())
        term.save()
Beispiel #6
0
    def save(self, *args, **kwargs):
        """Handle slugs."""
        if self.id is None or self.name != get_object_or_404(
                Vocabulary, id=self.id).name:
            self.slug = default_slugify(
                self.name,
                Vocabulary._meta.model_name,
                lambda slug: Vocabulary.objects.filter(slug=slug).exists()
            )

        return super(Vocabulary, self).save(*args, **kwargs)
Beispiel #7
0
 def test_default_slugify(self):
     """
     Test for the default slugify function.
     Since this function is generic, here we test the generic call
     """
     # a normal string
     self.assertEqual(
         default_slugify(
             'foo',
             'bar',
             lambda x: False
         ),
         'foo'
     )
     # the normal string already exists
     self.assertEqual(
         default_slugify(
             'foo',
             'bar',
             lambda x: x == 'foo'
         ),
         'foo1'
     )
     # a string that gives an empty slug
     self.assertEqual(
         default_slugify(
             '%^%$',
             'bar',
             lambda x: False
         ),
         'bar-slug'
     )
     # the default slug already exists
     self.assertEqual(
         default_slugify(
             '%^%$',
             'bar',
             lambda x: x == 'bar-slug'
         ),
         'bar-slug1'
     )
Beispiel #8
0
def backfill_empty_slugs(apps, schema_editor):
    """
    Finds all the objects in the Repository
    that do not have a slug and change the name;
    this will automatically create a new slug.
    """
    Repository = apps.get_model("learningresources", "Repository")

    for repo in Repository.objects.filter(slug=''):
        repo.slug = default_slugify(
            repo.name, Repository._meta.model_name,
            lambda slug: Repository.objects.filter(slug=slug).exists())
        repo.save()
def backfill_empty_slugs(apps, schema_editor):
    """
    Finds all the objects in the Repository
    that do not have a slug and change the name;
    this will automatically create a new slug.
    """
    Vocabulary = apps.get_model("taxonomy", "Vocabulary")
    Term = apps.get_model("taxonomy", "Term")

    for vocab in Vocabulary.objects.filter(slug=''):
        vocab.slug = default_slugify(
            vocab.name,
            Vocabulary._meta.model_name,
            lambda slug: Vocabulary.objects.filter(slug=slug).exists()
        )
        vocab.save()
    for term in Term.objects.filter(slug=''):
        term.slug = default_slugify(
            term.label,
            Term._meta.model_name,
            lambda slug: Term.objects.filter(slug=slug).exists()
        )
        term.save()
Beispiel #10
0
def backfill_empty_slugs(apps, schema_editor):
    """
    Finds all the objects in the Repository
    that do not have a slug and change the name;
    this will automatically create a new slug.
    """
    Repository = apps.get_model("learningresources", "Repository")

    for repo in Repository.objects.filter(slug=''):
        repo.slug = default_slugify(
            repo.name,
            Repository._meta.model_name,
            lambda slug: Repository.objects.filter(slug=slug).exists()
        )
        repo.save()
Beispiel #11
0
 def save(self, *args, **kwargs):
     """
     Handle slugs and groups.
     """
     is_update = False
     if self.id is None or self.name != get_object_or_404(Repository,
                                                          id=self.id).name:
         # if it is an update of the repository, need the old slug
         if self.id is not None:
             is_update = True
             old_slug = get_object_or_404(Repository, id=self.id).slug
         self.slug = default_slugify(
             self.name, Repository._meta.model_name,
             lambda slug: Repository.objects.filter(slug=slug).exists())
         # check if it's necessary to initialize the permissions
     new_repository = super(Repository, self).save(*args, **kwargs)
     if is_update:
         roles_update_repo(self, old_slug)
     return new_repository
Beispiel #12
0
 def save(self, *args, **kwargs):
     """
     Handle slugs and groups.
     """
     is_update = False
     if self.id is None or self.name != get_object_or_404(
             Repository, id=self.id).name:
         # if it is an update of the repository, need the old slug
         if self.id is not None:
             is_update = True
             old_slug = get_object_or_404(Repository, id=self.id).slug
         self.slug = default_slugify(
             self.name,
             Repository._meta.model_name,
             lambda slug: Repository.objects.filter(slug=slug).exists()
         )
         # check if it's necessary to initialize the permissions
     new_repository = super(Repository, self).save(*args, **kwargs)
     if is_update:
         roles_update_repo(self, old_slug)
     return new_repository