~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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# 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__ = ['Packaging', 'PackagingUtil']

from lazr.lifecycle.event import (
    ObjectCreatedEvent,
    ObjectDeletedEvent,
    )
from sqlobject import ForeignKey
from zope.component import getUtility
from zope.event import notify
from zope.interface import implements
from zope.security.interfaces import Unauthorized

from lp.app.interfaces.launchpad import ILaunchpadCelebrities
from lp.registry.interfaces.packaging import (
    IPackaging,
    IPackagingUtil,
    PackagingType,
    )
from lp.registry.interfaces.person import validate_public_person
from lp.services.database.constants import (
    DEFAULT,
    UTC_NOW,
    )
from lp.services.database.datetimecol import UtcDateTimeCol
from lp.services.database.enumcol import EnumCol
from lp.services.database.sqlbase import SQLBase
from lp.services.webapp.interfaces import ILaunchBag


class Packaging(SQLBase):
    """A Packaging relating a SourcePackageName in DistroSeries and a Product.
    """

    implements(IPackaging)

    _table = 'Packaging'

    productseries = ForeignKey(foreignKey="ProductSeries",
                               dbName="productseries",
                               notNull=True)
    sourcepackagename = ForeignKey(foreignKey="SourcePackageName",
                                   dbName="sourcepackagename",
                                   notNull=True)
    distroseries = ForeignKey(foreignKey='DistroSeries',
                               dbName='distroseries',
                               notNull=True)
    packaging = EnumCol(dbName='packaging', notNull=True,
                        enum=PackagingType)
    datecreated = UtcDateTimeCol(notNull=True, default=UTC_NOW)
    owner = ForeignKey(
        dbName='owner', foreignKey='Person',
        storm_validator=validate_public_person,
        notNull=False, default=DEFAULT)

    @property
    def sourcepackage(self):
        from lp.registry.model.sourcepackage import SourcePackage
        return SourcePackage(distroseries=self.distroseries,
            sourcepackagename=self.sourcepackagename)

    def __init__(self, **kwargs):
        super(Packaging, self).__init__(**kwargs)
        notify(ObjectCreatedEvent(self))

    def userCanDelete(self):
        """See `IPackaging`."""
        user = getUtility(ILaunchBag).user
        if user is None:
            return False
        admin = getUtility(ILaunchpadCelebrities).admin
        registry_experts = (
            getUtility(ILaunchpadCelebrities).registry_experts)
        return (
            user.inTeam(self.owner) or
            user.canAccess(self.sourcepackage, 'setBranch') or
            user.inTeam(registry_experts) or user.inTeam(admin))

    def destroySelf(self):
        if not self.userCanDelete():
            raise Unauthorized(
                'Only the person who created the packaging and package '
                'maintainers can delete it.')
        notify(ObjectDeletedEvent(self))
        super(Packaging, self).destroySelf()


class PackagingUtil:
    """Utilities for Packaging."""
    implements(IPackagingUtil)

    def createPackaging(self, productseries, sourcepackagename,
                        distroseries, packaging, owner):
        """See `IPackaging`.

        Raises an assertion error if there is already packaging for
        the sourcepackagename in the distroseries.
        """
        if self.packagingEntryExists(sourcepackagename, distroseries):
            raise AssertionError(
                "A packaging entry for %s in %s already exists." %
                (sourcepackagename.name, distroseries.name))
        return Packaging(productseries=productseries,
                         sourcepackagename=sourcepackagename,
                         distroseries=distroseries,
                         packaging=packaging,
                         owner=owner)

    def deletePackaging(self, productseries, sourcepackagename, distroseries):
        """See `IPackaging`."""
        packaging = Packaging.selectOneBy(
            productseries=productseries,
            sourcepackagename=sourcepackagename,
            distroseries=distroseries)
        assert packaging is not None, (
            "Tried to delete non-existent Packaging: "
            "productseries=%s/%s, sourcepackagename=%s, distroseries=%s/%s"
            % (productseries.name, productseries.product.name,
               sourcepackagename.name,
               distroseries.parent.name, distroseries.name))
        packaging.destroySelf()

    def packagingEntryExists(self, sourcepackagename, distroseries,
                             productseries=None):
        """See `IPackaging`."""
        criteria = dict(
            sourcepackagename=sourcepackagename,
            distroseries=distroseries)
        if productseries is not None:
            criteria['productseries'] = productseries
        result = Packaging.selectOneBy(**criteria)
        if result is None:
            return False
        return True