~cr3/launchpad-results/trunk

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# Copyright 2010 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

__metaclass__ = type

__all__ = [
    "TestPerson",
    "test_person_set",
    ]

from storm.expr import (
    Alias,
    LeftJoin,
    Sum,
    )
from storm.locals import Max

from zope.interface import implements

from lazr.delegates import delegates

from lp.results.database.search import search_to_expression

from lp.results.services.formatters import item_to_tuple

from lp.results.interfaces import (
    IPerson,
    ITestPerson,
    ITestPersonSet,
    TestPersonSearchParams,
    )

from lp.results.models.person import (
    Person,
    PersonSet,
    )
from lp.results.models.testrank import HasTestRankProjectsMixin
from lp.results.models.teststat import (
    HasTestStatPeopleMixin,
    TestStat,
    TestStatPersonMixin,
    )


class TestPerson(TestStatPersonMixin, HasTestRankProjectsMixin):
    """Users within the Django authentication system are represented by
    this model.
    """
    __slots__ = ("person", "test_run_count", "last_test_run_date",)

    implements(ITestPerson)

    delegates(IPerson, context="person")

    def __init__(self, person, test_run_count, last_test_run_date):
        self.person = person
        self.test_run_count = test_run_count
        self.last_test_run_date = last_test_run_date

    def __cmp__(self, other):
        if ITestPerson.providedBy(other):
            other = other.person

        return cmp(self.person, other)


class TestPersonSet(PersonSet, HasTestStatPeopleMixin):
    """See `ITestPersonSet`."""
    implements(ITestPersonSet)

    search_params_factory = TestPersonSearchParams

    def buildSearchExpressions(self, search_params):
        """See SearchSet."""
        expressions = super(TestPersonSet, self).buildSearchExpressions(
            search_params)

        if search_params.date_modified:
            expressions.append(
                search_to_expression(
                    TestStat.date_modified,
                    search_params.date_modified))

        if search_params.project:
            expressions.append(
                search_to_expression(
                    TestStat.project_id,
                    search_params.project,
                    modifier=lambda x: x.id))

        return expressions

    def buildSearchResult(self, search_params):
        """See SearchSet."""
        result = super(TestPersonSet, self).buildSearchResult(search_params)

        return result.group_by(Person)

    def buildSearchTables(self, search_params):
        """See SearchSet."""
        tables = super(TestPersonSet, self).buildSearchTables(search_params)

        tables.append(
            LeftJoin(TestStat,
                TestStat.person_id == Person.id))

        return tables

    def getSearchResultDecorator(self, result):
        """See SearchSet."""
        return TestPerson(*result)

    def getSearchResultTables(self):
        """See SearchSet."""
        result_tables = super(TestPersonSet, self).getSearchResultTables()

        return item_to_tuple(result_tables) + (
            Alias(Sum(TestStat.counter), name="test_run_count"),
            Alias(Max(TestStat.date_modified), name="last_test_run_date"),
            )


test_person_set = TestPersonSet()