~launchpad-pqm/launchpad/devel

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
# Copyright 2010 Canonical Ltd.  This software is licensed under the
# GNU Affero General Public License version 3 (see the file LICENSE).

"""Classes for managing the NameBlacklist table."""

__metaclass__ = type
__all__ = [
    'NameBlacklist',
    'NameBlacklistSet',
    ]


from storm.locals import (
    Int,
    Reference,
    Unicode,
    )
from zope.interface import implements

from lp.registry.interfaces.nameblacklist import (
    INameBlacklist,
    INameBlacklistSet,
    )
from lp.registry.model.person import Person
from lp.services.database.lpstorm import IStore
from lp.services.database.stormbase import StormBase


class NameBlacklist(StormBase):
    """Class for the NameBlacklist table."""

    implements(INameBlacklist)

    __storm_table__ = 'NameBlacklist'

    id = Int(primary=True)
    regexp = Unicode(name='regexp', allow_none=False)
    comment = Unicode(name='comment', allow_none=True)
    admin_id = Int(name='admin', allow_none=True)
    admin = Reference(admin_id, Person.id)


class NameBlacklistSet:
    """Class for creating and retrieving NameBlacklist objects."""

    implements(INameBlacklistSet)

    def getAll(self):
        """See `INameBlacklistSet`."""
        store = IStore(NameBlacklist)
        return store.find(NameBlacklist).order_by(NameBlacklist.regexp)

    def create(self, regexp, comment=None, admin=None):
        """See `INameBlacklistSet`."""
        nameblacklist = NameBlacklist()
        nameblacklist.regexp = regexp
        nameblacklist.comment = comment
        nameblacklist.admin = admin
        store = IStore(NameBlacklist)
        store.add(nameblacklist)
        return nameblacklist

    def get(self, id):
        """See `INameBlacklistSet`."""
        try:
            id = int(id)
        except ValueError:
            return None
        store = IStore(NameBlacklist)
        return store.find(NameBlacklist, NameBlacklist.id == id).one()

    def getByRegExp(self, regexp):
        """See `INameBlacklistSet`."""
        store = IStore(NameBlacklist)
        return store.find(NameBlacklist, NameBlacklist.regexp == regexp).one()