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

# pylint: disable-msg=E0611,W0212

__metaclass__ = type
__all__ = [
    'Section',
    'SectionSelection',
    'SectionSet'
    ]

from sqlobject import (
    ForeignKey,
    StringCol,
    )
from zope.interface import implements

from canonical.database.sqlbase import SQLBase
from lp.app.errors import NotFoundError
from lp.soyuz.interfaces.section import (
    ISection,
    ISectionSelection,
    ISectionSet,
    )


class Section(SQLBase):
    """See ISection"""
    implements(ISection)

    _defaultOrder = ['id']

    name = StringCol(notNull=True, alternateID=True)


class SectionSelection(SQLBase):
    """See ISectionSelection."""

    implements(ISectionSelection)

    _defaultOrder = ['id']

    distroseries = ForeignKey(dbName='distroseries',
        foreignKey='DistroSeries', notNull=True)
    section = ForeignKey(dbName='section',
        foreignKey='Section', notNull=True)


class SectionSet:
    """See ISectionSet."""
    implements(ISectionSet)

    def __iter__(self):
        """See ISectionSet."""
        return iter(Section.select())

    def __getitem__(self, name):
        """See ISectionSet."""
        section = Section.selectOneBy(name=name)
        if section is not None:
            return section
        raise NotFoundError(name)

    def get(self, section_id):
        """See ISectionSet."""
        return Section.get(section_id)

    def ensure(self, name):
        """See ISectionSet."""
        section = Section.selectOneBy(name=name)
        if section is not None:
            return section
        return self.new(name)

    def new(self, name):
        """See ISectionSet."""
        return Section(name=name)