summaryrefslogtreecommitdiff
path: root/packages/models.py
blob: ad082501015861a92ed9b5cf596be5ff0f238452 (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
from django.db import models
from django.db.models.signals import pre_save, post_save
from django.contrib.auth.models import User

from main.utils import set_created_field

class PackageRelation(models.Model):
    '''
    Represents maintainership (or interest) in a package by a given developer.
    It is not a true foreign key to packages as we want to key off
    pkgbase/pkgname instead, as well as preserve this information across
    package deletes, adds, and in all repositories.
    '''
    MAINTAINER = 1
    WATCHER = 2
    TYPE_CHOICES = (
            (MAINTAINER, 'Maintainer'),
            (WATCHER, 'Watcher'),
    )
    pkgbase = models.CharField(max_length=255)
    user = models.ForeignKey(User, related_name="package_relations")
    type = models.PositiveIntegerField(choices=TYPE_CHOICES, default=MAINTAINER)
    created = models.DateTimeField(editable=False)

    def get_associated_packages(self):
        # TODO: delayed import to avoid circular reference
        from main.models import Package
        return Package.objects.normal().filter(pkgbase=self.pkgbase)

    def repositories(self):
        packages = self.get_associated_packages()
        return sorted(set([p.repo for p in packages]))

    def __unicode__(self):
        return u'%s: %s (%s)' % (
                self.pkgbase, self.user, self.get_type_display())

    class Meta:
        unique_together = (('pkgbase', 'user', 'type'),)

class SignoffSpecification(models.Model):
    '''
    A specification for the signoff policy for this particular revision of a
    pakcage. The default is requiring two signoffs for a given package. These
    are created only if necessary; e.g., if one wanted to override the
    required=2 attribute, otherwise a sane default object is used.
    '''
    pkgbase = models.CharField(max_length=255, db_index=True)
    pkgver = models.CharField(max_length=255)
    pkgrel = models.CharField(max_length=255)
    epoch = models.PositiveIntegerField(default=0)
    arch = models.ForeignKey('main.Arch')
    repo = models.ForeignKey('main.Repo')
    user = models.ForeignKey(User)
    created = models.DateTimeField(editable=False)
    required = models.PositiveIntegerField(default=2)
    enabled = models.BooleanField(default=True)
    known_bad = models.BooleanField(default=False)
    comments = models.TextField(null=True, blank=True)

class SignoffManager(models.Manager):
    def get_from_package(self, pkg, user, revoked=False):
        '''Utility method to pull all relevant name-version fields from a
        package and create a matching signoff.'''
        not_revoked = not revoked
        return Signoff.objects.get(
                pkgbase=pkg.pkgbase, pkgver=pkg.pkgver, pkgrel=pkg.pkgrel,
                epoch=pkg.epoch, arch=pkg.arch, repo=pkg.repo,
                revoked__isnull=not_revoked, user=user)

    def get_or_create_from_package(self, pkg, user):
        '''Utility method to pull all relevant name-version fields from a
        package and create a matching signoff.'''
        return Signoff.objects.get_or_create(
                pkgbase=pkg.pkgbase, pkgver=pkg.pkgver, pkgrel=pkg.pkgrel,
                epoch=pkg.epoch, arch=pkg.arch, repo=pkg.repo,
                revoked=None, user=user)

    def for_package(self, pkg):
        return self.select_related('user').filter(
                pkgbase=pkg.pkgbase, pkgver=pkg.pkgver, pkgrel=pkg.pkgrel,
                epoch=pkg.epoch, arch=pkg.arch, repo=pkg.repo)

class Signoff(models.Model):
    '''
    A signoff for a package (by pkgbase) at a given point in time. These are
    not keyed directly to a Package object so they don't ever get deleted when
    Packages come and go from testing repositories.
    '''
    pkgbase = models.CharField(max_length=255, db_index=True)
    pkgver = models.CharField(max_length=255)
    pkgrel = models.CharField(max_length=255)
    epoch = models.PositiveIntegerField(default=0)
    arch = models.ForeignKey('main.Arch')
    repo = models.ForeignKey('main.Repo')
    user = models.ForeignKey(User, related_name="package_signoffs")
    created = models.DateTimeField(editable=False)
    revoked = models.DateTimeField(null=True)
    comments = models.TextField(null=True, blank=True)

    objects = SignoffManager()

    @property
    def packages(self):
        # TODO: delayed import to avoid circular reference
        from main.models import Package
        return Package.objects.normal().filter(pkgbase=self.pkgbase,
                pkgver=self.pkgver, pkgrel=self.pkgrel, epoch=self.epoch,
                arch=self.arch, repo=self.repo)

    @property
    def full_version(self):
        if self.epoch > 0:
            return u'%d:%s-%s' % (self.epoch, self.pkgver, self.pkgrel)
        return u'%s-%s' % (self.pkgver, self.pkgrel)

    def __unicode__(self):
        return u'%s-%s: %s' % (
                self.pkgbase, self.full_version, self.user)

class PackageGroup(models.Model):
    '''
    Represents a group a package is in. There is no actual group entity,
    only names that link to given packages.
    '''
    pkg = models.ForeignKey('main.Package', related_name='groups')
    name = models.CharField(max_length=255, db_index=True)

    def __unicode__(self):
        return "%s: %s" % (self.name, self.pkg)

class License(models.Model):
    pkg = models.ForeignKey('main.Package', related_name='licenses')
    name = models.CharField(max_length=255)

    def __unicode__(self):
        return self.name

    class Meta:
        ordering = ['name']

class Conflict(models.Model):
    pkg = models.ForeignKey('main.Package', related_name='conflicts')
    name = models.CharField(max_length=255, db_index=True)
    comparison = models.CharField(max_length=255, default='')
    version = models.CharField(max_length=255, default='')

    def __unicode__(self):
        if self.version:
            return u'%s%s%s' % (self.name, self.comparison, self.version)
        return self.name

    class Meta:
        ordering = ['name']

class Provision(models.Model):
    pkg = models.ForeignKey('main.Package', related_name='provides')
    name = models.CharField(max_length=255, db_index=True)
    # comparison must be '=' for provides
    comparison = '='
    version = models.CharField(max_length=255, default='')

    def __unicode__(self):
        if self.version:
            return u'%s=%s' % (self.name, self.version)
        return self.name

    class Meta:
        ordering = ['name']

class Replacement(models.Model):
    pkg = models.ForeignKey('main.Package', related_name='replaces')
    name = models.CharField(max_length=255, db_index=True)
    comparison = models.CharField(max_length=255, default='')
    version = models.CharField(max_length=255, default='')

    def __unicode__(self):
        if self.version:
            return u'%s%s%s' % (self.name, self.comparison, self.version)
        return self.name

    class Meta:
        ordering = ['name']


def remove_inactive_maintainers(sender, instance, created, **kwargs):
    # instance is an auth.models.User; we want to remove any existing
    # maintainer relations if the user is no longer active
    if not instance.is_active:
        maint_relations = PackageRelation.objects.filter(user=instance,
                type=PackageRelation.MAINTAINER)
        maint_relations.delete()

post_save.connect(remove_inactive_maintainers, sender=User,
        dispatch_uid="packages.models")
pre_save.connect(set_created_field, sender=PackageRelation,
        dispatch_uid="packages.models")
pre_save.connect(set_created_field, sender=Signoff,
        dispatch_uid="packages.models")

# vim: set ts=4 sw=4 et: