示例#1
0
    def test_name_is_required(self):
        # does not raise
        Tag(name=self.VALID_NAME).full_clean()

        with pytest.raises(ValidationError) as excinfo:
            Tag().full_clean()

        assert str(
            excinfo.value) == ("{'name': [u'This field cannot be blank.']}")
示例#2
0
def add_tags(import_task, galaxy_info, role):
    # Add tags / categories. Remove ':' and only allow alpha-numeric characters
    add_message(import_task, u"INFO", u"Parsing galaxy_tags")
    meta_tags = []
    if galaxy_info.get("categories"):
        add_message(import_task, u"WARNING", (u"Found categories in meta data. Update the meta data to use "
                                              u"galaxy_tags rather than categories."))
        cats = galaxy_info.get("categories")
        if not isinstance(cats, list):
            add_message(import_task, u"ERROR", u"Expected categories in meta data to be a list")
        else:
            for category in cats:
                for cat in category.split(':'):
                    if re.match('^[a-zA-Z0-9]+$', cat):
                        meta_tags.append(cat)
                    else:
                        add_message(import_task, u"WARNING", u"%s is not a valid tag" % cat)

    if galaxy_info.get("galaxy_tags"):
        tags = galaxy_info.get("galaxy_tags")
        if not isinstance(tags, list):
            add_message(import_task, u"ERROR", u"Expected galaxy_tags in meta data to be a list.")
        else:
            for tag in tags:
                for t in tag.split(':'):
                    if re.match('^[a-zA-Z0-9:]+$', t):
                        meta_tags.append(t)
                    else:
                        add_message(import_task, u"WARNING", u"'%s' is not a valid tag. Skipping." % t)

    if len(meta_tags) == 0:
        add_message(import_task, u"WARNING", u"No galaxy_tags found in meta data.")

    if len(meta_tags) > 20:
        add_message(import_task, u"WARNING", u"Found more than 20 galaxy_tag values in the meta data. "
                                             u"Only the first 20 will be used.")
        meta_tags = meta_tags[0:20]

    meta_tags = list(set(meta_tags))

    for tag in meta_tags:
        pg_tags = Tag.objects.filter(name=tag).all()
        if len(pg_tags) == 0:
            pg_tag = Tag(name=tag, description=tag, active=True)
            pg_tag.save()
        else:
            pg_tag = pg_tags[0]
        if not role.tags.filter(name=tag).exists():
            role.tags.add(pg_tag)

    # Remove tags no longer listed in the metadata
    for tag in role.tags.all():
        if tag.name not in meta_tags:
            role.tags.remove(tag)
示例#3
0
    def test_name_length_is_limited(self):
        # does not raise
        Tag(name=self.VALID_NAME).full_clean()

        with pytest.raises(ValidationError) as excinfo:
            Tag(name='*' * (self.NAME_MAX_LENGTH + 1)).full_clean()

        assert str(excinfo.value) == (
            "{{'name': [u'Ensure this value has at most {valid} "
            "characters (it has {given}).']}}").format(
                valid=self.NAME_MAX_LENGTH, given=self.NAME_MAX_LENGTH + 1)
示例#4
0
    def test_repr(self):
        # __str__ returns name, but it did not affected __repr__
        tag = Tag(name=self.VALID_NAME)

        assert repr(tag) == ('<Tag: {name}>'.format(name=self.VALID_NAME))
示例#5
0
    def test_convert_to_string(self):
        # __str__ will return name
        tag = Tag(name=self.VALID_NAME)

        assert str(tag) == self.VALID_NAME