Example #1
0
def test_slugify_non_ascii():
  slug = slugify(u"C'est l'été !")
  assert slug == u'c-est-l-ete'

  # with a special space character
  slug = slugify(u"a_b\u205fc")  # U+205F: MEDIUM MATHEMATICAL SPACE
  assert slug == u'a-b-c'

  # with non-ascii translatable chars, like EN DASH U+2013 (–) and EM DASH
  # U+2014 (—).
  # this test fails if regexp subst is done after unicode normalization
  assert slugify(u'a\u2013b\u2014c') == u'a-b-c'
Example #2
0
def test_slugify_non_ascii() -> None:
    slug = slugify("C'est l'été !")
    assert slug == "c-est-l-ete"

    # with a special space character
    slug = slugify("a_b\u205fc")  # U+205F: MEDIUM MATHEMATICAL SPACE
    assert slug == "a-b-c"

    # with non-ascii translatable chars, like EN DASH U+2013 (–) and EM DASH
    # U+2014 (—).
    # this test fails if regexp subst is done after Unicode normalization
    assert slugify("a\u2013b\u2014c") == "a-b-c"
Example #3
0
    def prepare_data(self):
        """Massage data before models creations."""
        for d in self.data["fields"]:
            vocabulary = d.get("vocabulary")
            if vocabulary:
                name = u"Vocabulary_"
                group = vocabulary.get("group", u"").strip()
                if group:
                    group = slugify(group, u"_")
                    name += group + u"__"
                name += vocabulary["name"]

                if name in self.vocabularies:
                    # already defined in another field, maybe on another model: share
                    # existing definition so that generated class is also accessible
                    d["vocabulary"] = self.vocabularies[name]
                else:
                    vocabulary["generated_name"] = name.encode("ascii")
                    self.vocabularies[name] = vocabulary
                continue

            # slugify list items
            from_list = d.get("from_list")
            if from_list is None:
                continue

            key_val_list = []
            seen = dict()
            for item in from_list:
                k = v = item
                if isinstance(item, (list, tuple)):
                    k, v = item
                    if isinstance(k, bytes):
                        k = k.decode("utf-8")
                    v = text_type(v)
                else:
                    if isinstance(item, bytes):
                        item = item.decode("utf-8")
                    k = text_type(slugify(item, u"_"))

                k = re.sub(u"[^\\w\\s-]", "", k).strip().upper()
                k = re.sub(u"[-\\s]+", "_", k)
                #  avoid duplicates: suffix by a number if needed
                current = k
                count = 0
                while k in seen and seen[k] != v:
                    count += 1
                    k = "{}_{}".format(current, count)

                seen[k] = v
                key_val_list.append((k, v))

            d["from_list"] = key_val_list
Example #4
0
  def __new__(cls, name, bases, d):
    meta = d.get('Meta')
    tblprefix = 'vocabulary_'

    group = slugify(meta.group or u'', u'_').encode('ascii')
    if group:
      tblprefix += group + '_'

    if not hasattr(meta, 'name'):
      vocname = name.lower().replace('vocabulary', '')
      meta.name = vocname
    d['__tablename__'] = tblprefix + meta.name
    return _BaseMeta.__new__(cls, name, bases, d)
Example #5
0
    def __new__(cls, name, bases, d):
        meta = d.get("Meta")
        tblprefix = "vocabulary_"

        group = slugify(meta.group or "", "_")
        if group:
            tblprefix += group + "_"

        if not hasattr(meta, "name"):
            vocname = name.lower().replace("vocabulary", "")
            meta.name = vocname
        d["__tablename__"] = tblprefix + meta.name
        return _BaseMeta.__new__(cls, name, bases, d)
Example #6
0
    def __new__(cls, name, bases, d):
        meta = d.get('Meta')
        tblprefix = 'vocabulary_'

        group = slugify(meta.group or u'', u'_')
        if group:
            tblprefix += group + '_'

        if not hasattr(meta, 'name'):
            vocname = name.lower().replace('vocabulary', '')
            meta.name = vocname
        d['__tablename__'] = tblprefix + meta.name
        return _BaseMeta.__new__(cls, name, bases, d)
Example #7
0
    def __new__(cls, name, bases, d):
        meta = d.get("Meta")
        tblprefix = "vocabulary_"

        group = slugify(meta.group or "", "_")
        if group:
            tblprefix += group + "_"

        if not hasattr(meta, "name"):
            vocname = name.lower().replace("vocabulary", "")
            meta.name = vocname
        d["__tablename__"] = tblprefix + meta.name
        return _BaseMeta.__new__(cls, name, bases, d)
Example #8
0
    def __new__(
        cls: Type["_VocabularyMeta"],
        name: str,
        bases: Tuple[Type[Model], ...],
        d: Dict[str, Any],
    ) -> Type["BaseVocabulary"]:
        meta = d.get("Meta")
        tblprefix = "vocabulary_"

        group = slugify(meta.group or "", "_")
        if group:
            tblprefix += group + "_"

        if not hasattr(meta, "name"):
            vocname = name.lower().replace("vocabulary", "")
            meta.name = vocname
        d["__tablename__"] = tblprefix + meta.name
        return _BaseMeta.__new__(cls, name, bases, d)
Example #9
0
def test_slugify_non_unicode_input():
  slug = slugify(b"a b c")
  assert slug == u'a-b-c'
  assert isinstance(slug, unicode)
Example #10
0
def test_slugify_separator() -> None:
    slug = slugify("a-b++ c-+", "+")
    assert slug == "a+b+c"
Example #11
0
def test_slugify_basic() -> None:
    slug = slugify("a b c")
    assert slug == "a-b-c"
    assert isinstance(slug, str)
    assert slugify(slug) == "a-b-c"  # idempotent
Example #12
0
def test_slugify_separator():
    slug = slugify("a-b++ c-+", '+')
    assert slug == 'a+b+c'
Example #13
0
 def test2(self):
   slug = slugify(u"C'est l'été")
   assert slug == 'c-est-l-ete'
   assert isinstance(slug, str)
Example #14
0
def test_slugify_basic():
    slug = slugify("a b c")
    assert slug == "a-b-c"
    assert isinstance(slug, text_type)
    assert slugify(slug) == "a-b-c"  # idempotent
Example #15
0
def test_slugify_separator():
  slug = slugify(u"a-b++ c-+", u'+')
  assert slug == u'a+b+c'
Example #16
0
def test_slugify_non_unicode_input():
    slug = slugify(b"a b c")
    assert slug == u'a-b-c'
    assert isinstance(slug, unicode)
Example #17
0
def test_slugify_separator():
    slug = slugify(u"a-b++ c-+", u'+')
    assert slug == u'a+b+c'
Example #18
0
def test_slugify_basic():
    slug = slugify(u'a b c')
    assert slug == u'a-b-c'
    assert isinstance(slug, unicode)
    assert slugify(slug) == u'a-b-c'  # idempotent
Example #19
0
def test_slugify_basic():
    slug = slugify('a b c')
    assert slug == 'a-b-c'
    assert isinstance(slug, text_type)
    assert slugify(slug) == 'a-b-c'  # idempotent
Example #20
0
def test_slugify_basic():
  slug = slugify(u'a b c')
  assert slug == u'a-b-c'
  assert isinstance(slug, unicode)
  assert slugify(slug) == u'a-b-c'  # idempotent
Example #21
0
 def test1(self):
   slug = slugify(u"a b c")
   assert slug == 'a-b-c'
   assert isinstance(slug, str)