Exemplo n.º 1
0
    def auto_tag(self):
        """Apply tags to myself that are implied by my metadata.

        You don't need to call save on the question after this.

        """
        to_add = self.product_config.get('tags', []) + self.category_config.get('tags', [])
        version = self.metadata.get('ff_version', '')

        # Remove the beta (b*), aurora (a2) or nightly (a1) suffix.
        version = re.split('[a-b]', version)[0]

        dev_releases = product_details.firefox_history_development_releases

        if (version in dev_releases or
                version in product_details.firefox_history_stability_releases or
                version in product_details.firefox_history_major_releases):
            to_add.append('Firefox %s' % version)
            tenths = _tenths_version(version)
            if tenths:
                to_add.append('Firefox %s' % tenths)
        elif _has_beta(version, dev_releases):
            to_add.append('Firefox %s' % version)
            to_add.append('beta')

        self.tags.add(*to_add)

        # Add a tag for the OS if it already exists as a tag:
        os = self.metadata.get('os')
        if os:
            try:
                add_existing_tag(os, self.tags)
            except Tag.DoesNotExist:
                pass
Exemplo n.º 2
0
    def auto_tag(self):
        """Apply tags to myself that are implied by my metadata.

        You don't need to call save on the question after this.

        """
        to_add = self.product_config.get('tags', []) + self.category_config.get('tags', [])
        version = self.metadata.get('ff_version', '')

        # Remove the beta (b*), aurora (a2) or nightly (a1) suffix.
        version = re.split('[a-b]', version)[0]

        dev_releases = product_details.firefox_history_development_releases

        if (version in dev_releases or
                version in product_details.firefox_history_stability_releases or
                version in product_details.firefox_history_major_releases):
            to_add.append('Firefox %s' % version)
            tenths = _tenths_version(version)
            if tenths:
                to_add.append('Firefox %s' % tenths)
        elif _has_beta(version, dev_releases):
            to_add.append('Firefox %s' % version)
            to_add.append('beta')

        self.tags.add(*to_add)

        # Add a tag for the OS if it already exists as a tag:
        os = self.metadata.get('os')
        if os:
            try:
                add_existing_tag(os, self.tags)
            except Tag.DoesNotExist:
                pass
Exemplo n.º 3
0
def _add_tag(request, question_id):
    """Add a named tag to a question, creating it first if appropriate.

    Tag name (case-insensitive) must be in request.POST['tag-name'].

    If there is no such tag and the user is not allowed to make new tags, raise
    Tag.DoesNotExist. If no tag name is provided, return None. Otherwise,
    return the canonicalized tag name.

    """
    tag_name = request.POST.get("tag-name", "").strip()
    if tag_name:
        question = get_object_or_404(Question, pk=question_id)
        try:
            canonical_name = add_existing_tag(tag_name, question.tags)
        except Tag.DoesNotExist:
            if request.user.has_perm("taggit.add_tag"):
                question.tags.add(tag_name)  # implicitly creates if needed
                canonical_name = tag_name
            else:
                raise

        return question, canonical_name

    return None, None
Exemplo n.º 4
0
    def add_tags(self, request, pk=None):
        question = self.get_object()

        if 'tags' not in request.DATA:
            return Response({'tags': 'This field is required.'},
                            status=status.HTTP_400_BAD_REQUEST)

        tags = request.DATA['tags']

        for tag in tags:
            try:
                add_existing_tag(tag, question.tags)
            except Tag.DoesNotExist:
                if request.user.has_perm('taggit.add_tag'):
                    question.tags.add(tag)
                else:
                    raise GenericAPIException(403, 'You are not authorized to create new tags.')

        tags = question.tags.all()
        return Response(QuestionTagSerializer(tags).data)
Exemplo n.º 5
0
    def add_tags(self, request, pk=None):
        question = self.get_object()

        if 'tags' not in request.data:
            return Response({'tags': 'This field is required.'},
                            status=status.HTTP_400_BAD_REQUEST)

        tags = request.data['tags']

        for tag in tags:
            try:
                add_existing_tag(tag, question.tags)
            except Tag.DoesNotExist:
                if request.user.has_perm('taggit.add_tag'):
                    question.tags.add(tag)
                else:
                    raise GenericAPIException(403, 'You are not authorized to create new tags.')

        data = [{'name': tag.name, 'slug': tag.slug} for tag in question.tags.all()]
        return Response(data)
Exemplo n.º 6
0
Arquivo: api.py Projeto: zu83/kitsune
    def add_tags(self, request, pk=None):
        question = self.get_object()

        if "tags" not in request.data:
            return Response({"tags": "This field is required."},
                            status=status.HTTP_400_BAD_REQUEST)

        tags = request.data["tags"]

        for tag in tags:
            try:
                add_existing_tag(tag, question.tags)
            except Tag.DoesNotExist:
                if request.user.has_perm("taggit.add_tag"):
                    question.tags.add(tag)
                else:
                    raise GenericAPIException(
                        403, "You are not authorized to create new tags.")

        data = [{
            "name": tag.name,
            "slug": tag.slug
        } for tag in question.tags.all()]
        return Response(data)
Exemplo n.º 7
0
 def test_add_existing_no_such_tag(self):
     """Assert add_existing_tag doesn't work when the tag doesn't exist."""
     add_existing_tag('nonexistent tag', self.untagged_question.tags)
Exemplo n.º 8
0
 def test_add_existing_case_insensitive(self):
     """Assert add_existing_tag works case-insensitively."""
     tag(name='lemon', slug='lemon', save=True)
     add_existing_tag('LEMON', self.untagged_question.tags)
     tags_eq(self.untagged_question, [u'lemon'])
Exemplo n.º 9
0
 def test_add_existing_no_such_tag(self):
     """Assert add_existing_tag doesn't work when the tag doesn't exist."""
     add_existing_tag("nonexistent tag", self.untagged_question.tags)
Exemplo n.º 10
0
 def test_add_existing_case_insensitive(self):
     """Assert add_existing_tag works case-insensitively."""
     TagFactory(name='lemon', slug='lemon')
     add_existing_tag('LEMON', self.untagged_question.tags)
     tags_eq(self.untagged_question, ['lemon'])
Exemplo n.º 11
0
 def test_add_existing_case_insensitive(self):
     """Assert add_existing_tag works case-insensitively."""
     TagFactory(name="lemon", slug="lemon")
     add_existing_tag("LEMON", self.untagged_question.tags)
     tags_eq(self.untagged_question, [u"lemon"])