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

__metaclass__ = type

__all__ = [
    'SourcePackageFormatSelection',
    'SourcePackageFormatSelectionSet',
    ]

from storm.locals import (
    Int,
    Reference,
    Storm,
    )
from zope.component import getUtility
from zope.interface import implements

from canonical.database.enumcol import DBEnum
from canonical.launchpad.webapp.interfaces import (
    DEFAULT_FLAVOR,
    IStoreSelector,
    MAIN_STORE,
    MASTER_FLAVOR,
    )
from lp.soyuz.enums import SourcePackageFormat
from lp.soyuz.interfaces.sourcepackageformat import (
    ISourcePackageFormatSelection,
    ISourcePackageFormatSelectionSet,
    )


class SourcePackageFormatSelection(Storm):
    """See ISourcePackageFormatSelection."""

    implements(ISourcePackageFormatSelection)

    __storm_table__ = 'sourcepackageformatselection'

    id = Int(primary=True)

    distroseries_id = Int(name="distroseries")
    distroseries = Reference(distroseries_id, 'DistroSeries.id')

    format = DBEnum(enum=SourcePackageFormat)


class SourcePackageFormatSelectionSet:
    """See ISourcePackageFormatSelectionSet."""

    implements(ISourcePackageFormatSelectionSet)

    def getBySeriesAndFormat(self, distroseries, format):
        """See `ISourcePackageFormatSelection`."""
        return getUtility(IStoreSelector).get(
            MAIN_STORE, DEFAULT_FLAVOR).find(
                SourcePackageFormatSelection, distroseries=distroseries,
                format=format).one()

    def add(self, distroseries, format):
        """See `ISourcePackageFormatSelection`."""
        spfs = SourcePackageFormatSelection()
        spfs.distroseries = distroseries
        spfs.format = format
        return getUtility(IStoreSelector).get(MAIN_STORE, MASTER_FLAVOR).add(
            spfs)