~launchpad-pqm/launchpad/devel

« back to all changes in this revision

Viewing changes to lib/lp/soyuz/vocabularies.py

  • Committer: Canonical.com Patch Queue Manager
  • Date: 2004-08-03 09:17:25 UTC
  • mfrom: (unknown (missing))
  • Revision ID: Arch-1:rocketfuel@canonical.com%launchpad--devel--0--patch-19
Removed defaultSkin directive to make launchpad work with the latest zope.
Patches applied:

 * steve.alexander@canonical.com/launchpad--devel--0--patch-16
   merge from rocketfuel

 * steve.alexander@canonical.com/launchpad--devel--0--patch-17
   removed use of defaultSkin directive, which has been removed from zope3

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
# Copyright 2009-2011 Canonical Ltd.  This software is licensed under the GNU
2
 
# Affero General Public License version 3 (see the file LICENSE).
3
 
 
4
 
"""Soyuz vocabularies."""
5
 
 
6
 
__metaclass__ = type
7
 
 
8
 
__all__ = [
9
 
    'ComponentVocabulary',
10
 
    'FilteredDistroArchSeriesVocabulary',
11
 
    'PackageReleaseVocabulary',
12
 
    'PPAVocabulary',
13
 
    'ProcessorFamilyVocabulary',
14
 
    'ProcessorVocabulary',
15
 
    ]
16
 
 
17
 
from sqlobject import AND
18
 
from storm.expr import SQL
19
 
from zope.component import getUtility
20
 
from zope.interface import implements
21
 
from zope.schema.vocabulary import SimpleTerm
22
 
 
23
 
from canonical.database.sqlbase import (
24
 
    quote,
25
 
    sqlvalues,
26
 
    )
27
 
from canonical.launchpad.webapp.interfaces import ILaunchBag
28
 
from canonical.launchpad.webapp.vocabulary import (
29
 
    IHugeVocabulary,
30
 
    NamedSQLObjectVocabulary,
31
 
    SQLObjectVocabularyBase,
32
 
    )
33
 
from lp.registry.model.person import Person
34
 
from lp.soyuz.enums import ArchivePurpose
35
 
from lp.soyuz.model.archive import Archive
36
 
from lp.soyuz.model.component import Component
37
 
from lp.soyuz.model.distroarchseries import DistroArchSeries
38
 
from lp.soyuz.model.processor import (
39
 
    Processor,
40
 
    ProcessorFamily,
41
 
    )
42
 
from lp.soyuz.model.sourcepackagerelease import SourcePackageRelease
43
 
 
44
 
 
45
 
class ComponentVocabulary(SQLObjectVocabularyBase):
46
 
 
47
 
    _table = Component
48
 
    _orderBy = 'name'
49
 
 
50
 
    def toTerm(self, obj):
51
 
        return SimpleTerm(obj, obj.id, obj.name)
52
 
 
53
 
 
54
 
class FilteredDistroArchSeriesVocabulary(SQLObjectVocabularyBase):
55
 
    """All arch series of a particular distribution."""
56
 
 
57
 
    _table = DistroArchSeries
58
 
    _orderBy = ['DistroSeries.version', 'architecturetag', 'id']
59
 
    _clauseTables = ['DistroSeries']
60
 
 
61
 
    def toTerm(self, obj):
62
 
        name = "%s %s (%s)" % (obj.distroseries.distribution.name,
63
 
                               obj.distroseries.name, obj.architecturetag)
64
 
        return SimpleTerm(obj, obj.id, name)
65
 
 
66
 
    def __iter__(self):
67
 
        distribution = getUtility(ILaunchBag).distribution
68
 
        if distribution:
69
 
            query = """
70
 
                DistroSeries.id = DistroArchSeries.distroseries AND
71
 
                DistroSeries.distribution = %s
72
 
                """ % sqlvalues(distribution.id)
73
 
            results = self._table.select(
74
 
                query, orderBy=self._orderBy, clauseTables=self._clauseTables)
75
 
            for distroarchseries in results:
76
 
                yield self.toTerm(distroarchseries)
77
 
 
78
 
 
79
 
class PackageReleaseVocabulary(SQLObjectVocabularyBase):
80
 
    _table = SourcePackageRelease
81
 
    _orderBy = 'id'
82
 
 
83
 
    def toTerm(self, obj):
84
 
        return SimpleTerm(
85
 
            obj, obj.id, obj.name + " " + obj.version)
86
 
 
87
 
 
88
 
class PPAVocabulary(SQLObjectVocabularyBase):
89
 
 
90
 
    implements(IHugeVocabulary)
91
 
 
92
 
    _table = Archive
93
 
    _orderBy = ['Person.name, Archive.name']
94
 
    _clauseTables = ['Person']
95
 
    _filter = AND(
96
 
        Person.q.id == Archive.q.ownerID,
97
 
        Archive.q.purpose == ArchivePurpose.PPA)
98
 
    displayname = 'Select a PPA'
99
 
    step_title = 'Search'
100
 
 
101
 
    def toTerm(self, archive):
102
 
        """See `IVocabulary`."""
103
 
        description = archive.description
104
 
        if description is not None:
105
 
            summary = description.splitlines()[0]
106
 
        else:
107
 
            summary = "No description available"
108
 
 
109
 
        token = '%s/%s' % (archive.owner.name, archive.name)
110
 
 
111
 
        return SimpleTerm(archive, token, summary)
112
 
 
113
 
    def getTermByToken(self, token):
114
 
        """See `IVocabularyTokenized`."""
115
 
        try:
116
 
            owner_name, archive_name = token.split('/')
117
 
        except ValueError:
118
 
            raise LookupError(token)
119
 
 
120
 
        clause = AND(
121
 
            self._filter,
122
 
            Person.name == owner_name,
123
 
            Archive.name == archive_name)
124
 
 
125
 
        obj = self._table.selectOne(
126
 
            clause, clauseTables=self._clauseTables)
127
 
 
128
 
        if obj is None:
129
 
            raise LookupError(token)
130
 
        else:
131
 
            return self.toTerm(obj)
132
 
 
133
 
    def search(self, query, vocab_filter=None):
134
 
        """Return a resultset of archives.
135
 
 
136
 
        This is a helper required by `SQLObjectVocabularyBase.searchForTerms`.
137
 
        """
138
 
        if not query:
139
 
            return self.emptySelectResults()
140
 
 
141
 
        query = query.lower()
142
 
 
143
 
        try:
144
 
            owner_name, archive_name = query.split('/')
145
 
        except ValueError:
146
 
            clause = AND(
147
 
                self._filter,
148
 
                SQL("(Archive.fti @@ ftq(%s) OR Person.fti @@ ftq(%s))"
149
 
                    % (quote(query), quote(query))))
150
 
        else:
151
 
            clause = AND(
152
 
                self._filter,
153
 
                Person.name == owner_name,
154
 
                Archive.name == archive_name)
155
 
 
156
 
        return self._table.select(
157
 
            clause, orderBy=self._orderBy, clauseTables=self._clauseTables)
158
 
 
159
 
 
160
 
class ProcessorVocabulary(NamedSQLObjectVocabulary):
161
 
 
162
 
    displayname = 'Select a processor'
163
 
    _table = Processor
164
 
    _orderBy = 'name'
165
 
 
166
 
 
167
 
class ProcessorFamilyVocabulary(NamedSQLObjectVocabulary):
168
 
    displayname = 'Select a processor family'
169
 
    _table = ProcessorFamily
170
 
    _orderBy = 'name'