~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).
7440.4.1 by Muharem Hrnjadovic
initial checkin
3
4
__metaclass__ = type
5
__all__ = ['ArchiveArch', 'ArchiveArchSet']
6
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
7
from storm.expr import (
8
    And,
9
    Join,
10
    LeftJoin,
11
    )
12
from storm.locals import (
13
    Int,
14
    Reference,
15
    Storm,
16
    )
7440.4.1 by Muharem Hrnjadovic
initial checkin
17
from zope.component import getUtility
18
from zope.interface import implements
19
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
20
from canonical.launchpad.webapp.interfaces import (
21
    DEFAULT_FLAVOR,
22
    IStoreSelector,
23
    MAIN_STORE,
24
    )
8294.6.1 by Julian Edwards
First stab at code-reorg. Still got a discrepancy on stuff I assigned to registry but not migrated yet.
25
from lp.soyuz.interfaces.archivearch import (
11403.1.4 by Henning Eggers
Reformatted imports using format-imports script r32.
26
    IArchiveArch,
27
    IArchiveArchSet,
28
    )
7675.558.2 by Muharem Hrnjadovic
added getRestrictedfamilies() and tests
29
from lp.soyuz.model.processor import ProcessorFamily
7440.4.1 by Muharem Hrnjadovic
initial checkin
30
31
32
class ArchiveArch(Storm):
33
    """See `IArchiveArch`."""
34
    implements(IArchiveArch)
35
    __storm_table__ = 'ArchiveArch'
36
    id = Int(primary=True)
37
38
    archive_id = Int(name='archive', allow_none=False)
39
    archive = Reference(archive_id, 'Archive.id')
40
    processorfamily_id = Int(name='processorfamily', allow_none=True)
41
    processorfamily = Reference(processorfamily_id, 'ProcessorFamily.id')
42
43
44
class ArchiveArchSet:
45
    """See `IArchiveArchSet`."""
46
    implements(IArchiveArchSet)
47
48
    def new(self, archive, processorfamily):
49
        """See `IArchiveArchSet`."""
50
        store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
51
        archivearch = ArchiveArch()
52
        archivearch.archive = archive
53
        archivearch.processorfamily = processorfamily
54
        store.add(archivearch)
55
        return archivearch
56
57
    def getByArchive(self, archive, processorfamily=None):
58
        """See `IArchiveArchSet`."""
59
        store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
60
        base_clauses = (ArchiveArch.archive == archive,)
61
        if processorfamily is not None:
62
            optional_clauses = (
63
                ArchiveArch.processorfamily == processorfamily,)
64
        else:
65
            optional_clauses = ()
8537.2.1 by Celso Providelo
Fixing issues on ArchiveArch ZCML and doctest style.
66
67
        results = store.find(
7440.4.1 by Muharem Hrnjadovic
initial checkin
68
            ArchiveArch, *(base_clauses + optional_clauses))
8537.2.1 by Celso Providelo
Fixing issues on ArchiveArch ZCML and doctest style.
69
        results = results.order_by(ArchiveArch.id)
70
71
        return results
7675.558.2 by Muharem Hrnjadovic
added getRestrictedfamilies() and tests
72
11091.1.4 by Julian Edwards
Take jelmer's branch and apply henning's review suggestions
73
    def getRestrictedFamilies(self, archive):
7675.558.2 by Muharem Hrnjadovic
added getRestrictedfamilies() and tests
74
        """See `IArchiveArchSet`."""
75
        store = getUtility(IStoreSelector).get(MAIN_STORE, DEFAULT_FLAVOR)
76
        origin = (
77
            ProcessorFamily,
78
            LeftJoin(
79
                ArchiveArch,
11091.1.2 by Jelmer Vernooij
Actually use the archive argument to ArchiveArchSet.getByArchive().
80
                And(ArchiveArch.archive == archive.id,
81
                    ArchiveArch.processorfamily == ProcessorFamily.id)))
7675.558.2 by Muharem Hrnjadovic
added getRestrictedfamilies() and tests
82
        result_set = store.using(*origin).find(
83
            (ProcessorFamily, ArchiveArch),
84
            (ProcessorFamily.restricted == True))
85
86
        return result_set.order_by(ProcessorFamily.name)