~launchpad-pqm/launchpad/devel

8687.15.17 by Karl Fogel
Add the copyright header block to the rest of the files under lib/lp/.
1
# Copyright 2009 Canonical Ltd.  This software is licensed under the
2
# GNU Affero General Public License version 3 (see the file LICENSE).
3
4983.1.1 by Curtis Hovey
Added lint exceptions to __init__.py and interface/*.py.
4
# pylint: disable-msg=E0211,E0213
4338.1.1 by Brad Crittenden
non-working development checkpoint
5
6
"""Entitlement interfaces."""
7
8
__metaclass__ = type
9
4338.1.7 by Brad Crittenden
post-review changes
10
__all__ = [
4338.1.11 by Brad Crittenden
Second round review comments incorporated
11
    'EntitlementInvalidError',
4338.1.7 by Brad Crittenden
post-review changes
12
    'EntitlementQuota',
4338.1.11 by Brad Crittenden
Second round review comments incorporated
13
    'EntitlementQuotaExceededError',
4676.1.3 by Brad Crittenden
Fixed indentation per Statik review comments
14
    'EntitlementState',
15
    'EntitlementType',
4338.1.7 by Brad Crittenden
post-review changes
16
    'IEntitlement',
4536.1.2 by Brad Crittenden
Reviewer changes from Tim.
17
    'IEntitlementSet',
4338.1.7 by Brad Crittenden
post-review changes
18
    ]
4338.1.1 by Brad Crittenden
non-working development checkpoint
19
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
20
from lazr.enum import (
21
    DBEnumeratedType,
22
    DBItem,
23
    )
24
from zope.interface import (
25
    Attribute,
26
    Interface,
27
    )
28
from zope.schema import (
29
    Bool,
30
    Choice,
31
    Datetime,
32
    Int,
33
    )
4338.1.1 by Brad Crittenden
non-working development checkpoint
34
35
from canonical.launchpad import _
11318.5.1 by j.c.sackett
Migrated canonical.launchpad.fields to lp.services.fields
36
from lp.services.fields import Whiteboard
4676.1.1 by Brad Crittenden
Enum migration
37
4338.1.1 by Brad Crittenden
non-working development checkpoint
38
4338.1.11 by Brad Crittenden
Second round review comments incorporated
39
class EntitlementQuotaExceededError(Exception):
40
    """The quota has been exceeded for the entitlement."""
41
4338.1.13 by Brad Crittenden
reviewer changes
42
4338.1.11 by Brad Crittenden
Second round review comments incorporated
43
class EntitlementInvalidError(Exception):
44
    """The entitlement is not valid."""
45
4338.1.13 by Brad Crittenden
reviewer changes
46
4676.1.1 by Brad Crittenden
Enum migration
47
class EntitlementType(DBEnumeratedType):
48
    """The set of features supported via entitlements.
49
50
    The listed features may be enabled by the granting of an entitlement.
51
    """
52
53
    PRIVATE_BRANCHES = DBItem(10, """
54
        Private Branches
55
56
        The ability to create branches which are only visible to the team.
57
        """)
58
59
    PRIVATE_BUGS = DBItem(20, """
60
        Private Bugs
61
62
        The ability to create private bugs which are only visible to the team.
63
        """)
64
65
    PRIVATE_TEAMS = DBItem(30, """
66
        Private Teams
67
68
        The ability to create private teams which are only visible to parent
69
        teams.
70
        """)
71
72
73
class EntitlementState(DBEnumeratedType):
74
    """States for an entitlement.
75
76
    The entitlement may start life as a REQUEST that is then granted and
77
    made ACTIVE.  At some point the entitlement may be revoked by marking
78
    as INACTIVE.
79
    """
80
81
    REQUESTED = DBItem(10, """
82
        Entitlement has been requested.
83
84
        The entitlement is inactive in this state.
85
        """)
86
87
    ACTIVE = DBItem(20, """
88
        The entitlement is active.
89
90
        The entitlement is approved in Launchpad or was imported in the
91
        active state.
92
        """)
4676.1.5 by Brad Crittenden
Change due to salgado reviewer comments.
93
4676.1.1 by Brad Crittenden
Enum migration
94
    INACTIVE = DBItem(30, """
95
        The entitlement is inactive.
96
97
        The entitlement has be deactivated.
98
        """)
99
100
4338.1.1 by Brad Crittenden
non-working development checkpoint
101
class IEntitlement(Interface):
4338.1.7 by Brad Crittenden
post-review changes
102
    """An entitlement the right to use a specific feature in Launchpad.
103
104
    Entitlements can be granted in an unlimited quantity or with a given
105
    quota.  They have a start date and optionally an expiration date.  An
106
    entitlement is invalid if it is not active, the quota is exceeded, or if
107
    it is expired.
108
    """
109
110
    id = Int(
111
        title=_("Entitlement id"),
112
        required=True,
113
        readonly=True)
5485.1.24 by Edwin Grubbs
Undid entitlement changes
114
    person = Choice(
4338.1.7 by Brad Crittenden
post-review changes
115
        title=_('Person'),
116
        required=True,
117
        readonly=True,
118
        vocabulary='ValidPersonOrTeam',
119
        description=_("Person or team to whom the entitlements is assigned."))
4338.1.1 by Brad Crittenden
non-working development checkpoint
120
    date_created = Datetime(
121
        title=_("Date Created"),
122
        description=_("The date on which this entitlement was created."),
4338.1.7 by Brad Crittenden
post-review changes
123
        required=True,
124
        readonly=True)
4338.1.1 by Brad Crittenden
non-working development checkpoint
125
    date_starts = Datetime(
126
        title=_("Date Starts"),
127
        description=_("The date on which this entitlement starts."),
4536.1.2 by Brad Crittenden
Reviewer changes from Tim.
128
        readonly=False)
4338.1.1 by Brad Crittenden
non-working development checkpoint
129
    date_expires = Datetime(
130
        title=_("Date Expires"),
131
        description=_("The date on which this entitlement expires."),
4536.1.2 by Brad Crittenden
Reviewer changes from Tim.
132
        readonly=False)
4338.1.7 by Brad Crittenden
post-review changes
133
    entitlement_type = Choice(
134
        title=_("Type of entitlement."),
135
        required=True,
136
        vocabulary='EntitlementType',
137
        description=_("Type of feature for this entitlement."),
138
        readonly=True)
139
    quota = Int(
140
        title=_("Allocated quota."),
4338.1.11 by Brad Crittenden
Second round review comments incorporated
141
        required=True,
142
        description=_(
143
            "A quota is the number of a feature allowed by this entitlement, "
144
            "for instance 50 private bugs."))
4338.1.7 by Brad Crittenden
post-review changes
145
    amount_used = Int(
4338.1.11 by Brad Crittenden
Second round review comments incorporated
146
        title=_("Amount used."),
147
        description=_(
148
            "The amount used is the number of instances of a feature "
149
            "the person has used so far."))
5485.1.24 by Edwin Grubbs
Undid entitlement changes
150
    registrant = Choice(
4338.1.7 by Brad Crittenden
post-review changes
151
        title=_('Registrant'),
152
        vocabulary='ValidPersonOrTeam',
153
        description=_(
154
            "Person who registered the entitlement.  "
4338.1.14 by Brad Crittenden
review changes
155
            "May be None if created automatically."),
4338.1.7 by Brad Crittenden
post-review changes
156
        readonly=True)
5485.1.24 by Edwin Grubbs
Undid entitlement changes
157
    approved_by = Choice(
4338.1.7 by Brad Crittenden
post-review changes
158
        title=_('Approved By'),
159
        vocabulary='ValidPersonOrTeam',
160
        description=_(
161
            "Person who approved the entitlement.  "
4338.1.14 by Brad Crittenden
review changes
162
            "May be None if created automatically."),
4338.1.7 by Brad Crittenden
post-review changes
163
        readonly=True)
4338.1.11 by Brad Crittenden
Second round review comments incorporated
164
    state = Choice(
165
        title=_("State"),
4338.1.7 by Brad Crittenden
post-review changes
166
        required=True,
167
        vocabulary='EntitlementState',
168
        description = _("Current state of the entitlement."))
169
4338.1.5 by Brad Crittenden
Added new fields per review.
170
    whiteboard = Whiteboard(title=_('Whiteboard'), required=False,
171
        description=_('Notes on the current status of the entitlement.'))
4338.1.1 by Brad Crittenden
non-working development checkpoint
172
4536.1.2 by Brad Crittenden
Reviewer changes from Tim.
173
    is_dirty = Bool(
174
        title=_("Dirty?"),
175
        description=_(
176
            "Is the entitlement 'dirty', i.e. has been written since the "
177
            "most recent update to an external system?"))
178
4338.1.7 by Brad Crittenden
post-review changes
179
    is_valid = Attribute(
180
        "Is this entitlement valid?")
181
182
    exceeded_quota = Attribute(
183
        "If the quota is not unlimited, is it exceeded?")
184
185
    in_date_range = Attribute(
186
        "Has the start date passed but not the expiration date?")
187
188
    def incrementAmountUsed():
189
        """Add one to the amount used."""
190
4338.1.13 by Brad Crittenden
reviewer changes
191
4536.1.2 by Brad Crittenden
Reviewer changes from Tim.
192
class IEntitlementSet(Interface):
193
    """Interface representing a set of Entitlements."""
194
195
    def __getitem__(entitlement_id):
196
        """Return the entitlement with the given id.
197
198
        Raise NotFoundError if there is no such entitlement.
199
        """
200
201
    def __iter__():
202
        """Return an iterator that will go through all entitlements."""
203
204
    def count():
205
        """Return the number of entitlements in the database."""
206
207
    def get(entitlement_id, default=None):
208
        """Return the entitlement with the given id.
209
210
        Return the default value if there is no such entitlement.
211
        """
212
213
    def getForPerson(person):
214
        """Return the entitlements for the person or team.
215
216
        Get all entitlements for a person.
217
        """
218
219
    def getValidForPerson(person):
220
        """Return a list of valid entitlements for the person or team.
221
222
        Get all valid entitlements for a person.  None is returned if no valid
223
        entitlements are found.
224
        """
225
226
    def getDirty():
227
        """Return the entitlements that have the dirty bit set.
228
229
        Get all entitlements that are marked as dirty.
230
        """
231
232
    def new(person, quota, entitlement_type, state,
233
            is_dirty=True, date_created=None, date_expires=None,
234
            date_starts=None, amount_used=None, registrant=None,
235
            approved_by=None):
236
        """Create a new entitlement."""
237
238
4338.1.7 by Brad Crittenden
post-review changes
239
class EntitlementQuota:
240
    """This class stores constants for entitlements quotas."""
4338.1.1 by Brad Crittenden
non-working development checkpoint
241
4338.1.11 by Brad Crittenden
Second round review comments incorporated
242
    UNLIMITED = 0