Passed
Push — master ( 62d7d9...8230b1 )
by Alexander
03:29
created

tcms.bugs.api   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 82
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 3
eloc 32
dl 0
loc 82
rs 10
c 0
b 0
f 0

3 Functions

Rating   Name   Duplication   Size   Complexity  
A remove_tag() 0 18 1
A add_tag() 0 21 1
A filter() 0 22 1
1
# -*- coding: utf-8 -*-
2
3
from modernrpc.core import rpc_method, REQUEST_KEY
4
5
from tcms.bugs.models import Bug
6
from tcms.management.models import Tag
7
from tcms.xmlrpc.decorators import permissions_required
8
9
__all__ = (
10
    'add_tag',
11
    'remove_tag',
12
    'filter',
13
)
14
15
16
@permissions_required('bugs.add_bugtag')
17
@rpc_method(name='Bug.add_tag')
18
def add_tag(bug_id, tag, **kwargs):
19
    """
20
    .. function:: XML-RPC Bug.add_tag(bug_id, tag)
21
22
        Add one tag to the specified Bug.
23
24
        :param bug_id: PK of Bug to modify
25
        :type bug_id: int
26
        :param tag: Tag name to add
27
        :type tag: str
28
        :return: None
29
        :raises: PermissionDenied if missing *bugs.add_bugtag* permission
30
        :raises: Bug.DoesNotExist if object specified by PK doesn't exist
31
        :raises: Tag.DoesNotExist if missing *management.add_tag* permission and *tag*
32
                 doesn't exist in the database!
33
    """
34
    request = kwargs.get(REQUEST_KEY)
35
    tag, _ = Tag.get_or_create(request.user, tag)
36
    Bug.objects.get(pk=bug_id).tags.add(tag)
37
38
39
@permissions_required('bugs.delete_bugtag')
40
@rpc_method(name='Bug.remove_tag')
41
def remove_tag(bug_id, tag):
42
    """
43
    .. function:: XML-RPC Bug.remove_tag(bug_id, tag)
44
45
        Remove tag from a Bug.
46
47
        :param bug_id: PK of Bug to modify
48
        :type bug_id: int
49
        :param tag: Tag name to remove
50
        :type tag: str
51
        :return: None
52
        :raises: PermissionDenied if missing *bugs.delete_bugtag* permission
53
        :raises: DoesNotExist if objects specified don't exist
54
    """
55
    Bug.objects.get(pk=bug_id).tags.remove(
56
        Tag.objects.get(name=tag)
57
    )
58
59
60
@rpc_method(name='Bug.filter')
61
def filter(query):  # pylint: disable=redefined-builtin
62
    """
63
    .. function:: XML-RPC Bug.filter(query)
64
65
        Get list of bugs.
66
67
        :param query: Field lookups for :class:`tcms.bugs.models.Bug`
68
        :type query: dict
69
        :return: List of serialized :class:`tcms.bugs.models.Bug` objects.
70
    """
71
    result = Bug.objects.filter(**query).values(
72
        'pk',
73
        'summary',
74
        'created_at',
75
        'product__name',
76
        'version__value',
77
        'build__name',
78
        'reporter__username',
79
        'assignee__username',
80
    )
81
    return list(result)
82