summaryrefslogtreecommitdiffstats
path: root/packages/models.py
blob: 5dbdea453238b183ea546dd7d4dec82d15cb5fc2 (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
from django.db import models
from django.db.models.signals import post_save
from django.contrib.auth.models import User

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)

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

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

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

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

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)

    def __unicode__(self):
        return "%s: %s" % (name, 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']

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")

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