summaryrefslogtreecommitdiff
path: root/main/models.py
blob: 758e40368455248c1ac06ba9b22dad1681d5dbd4 (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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
from django.db import models
from django.db.models import Q
from django.contrib.auth.models import User
from django.contrib.sites.models import Site

from main.utils import cache_function
from packages.models import PackageRelation

###########################
### User Profile Class ####
###########################
class UserProfile(models.Model):
    id = models.AutoField(primary_key=True) # not technically needed
    notify = models.BooleanField(
        "Send notifications",
        default=True,
        help_text="When enabled, send user 'flag out of date' notifications")
    alias = models.CharField(
        max_length=50,
        help_text="Required field")
    public_email = models.CharField(
        max_length=50,
        help_text="Required field")
    other_contact = models.CharField(max_length=100, null=True, blank=True)
    website = models.CharField(max_length=200, null=True, blank=True)
    yob = models.IntegerField(null=True, blank=True)
    location = models.CharField(max_length=50, null=True, blank=True)
    languages = models.CharField(max_length=50, null=True, blank=True)
    interests = models.CharField(max_length=255, null=True, blank=True)
    occupation = models.CharField(max_length=50, null=True, blank=True)
    roles = models.CharField(max_length=255, null=True, blank=True)
    favorite_distros = models.CharField(max_length=255, null=True, blank=True)
    picture = models.FileField(upload_to='devs', default='devs/silhouette.png')
    user = models.ForeignKey(
        User, related_name='userprofile_user', unique=True)
    allowed_repos = models.ManyToManyField('Repo', blank=True)
    class Meta:
        db_table = 'user_profiles'
        verbose_name = 'Additional Profile Data'
        verbose_name_plural = 'Additional Profile Data'


#######################
### Manager Classes ###
#######################
class TodolistManager(models.Manager):
    def incomplete(self):
        return self.filter(todolistpkg__complete=False).distinct()

class PackageManager(models.Manager):
    def flagged(self):
        return self.get_query_set().filter(needupdate=True)

#############################
### General Model Classes ###
#############################
TIER_CHOICES = (
    (0, 'Tier 0'),
    (1, 'Tier 1'),
    (2, 'Tier 2'),
    (-1, 'Untiered'),
)

class Mirror(models.Model):
    name = models.CharField(max_length=255)
    tier = models.SmallIntegerField(default=2, choices=TIER_CHOICES)
    upstream = models.ForeignKey('self', null=True)
    country = models.CharField(max_length=255, db_index=True)
    admin_email = models.EmailField(max_length=255, blank=True)
    public = models.BooleanField(default=True)
    active = models.BooleanField(default=True)
    isos = models.BooleanField(default=True)
    rsync_user = models.CharField(max_length=50, blank=True, default='')
    rsync_password = models.CharField(max_length=50, blank=True, default='')
    notes = models.TextField(blank=True)

    class Meta:
        ordering = ('country', 'name')

    def __unicode__(self):
        return self.name

    def supported_protocols(self):
        protocols = MirrorProtocol.objects.filter(urls__mirror=self).distinct()
        return ", ".join([p.protocol for p in protocols])

class MirrorProtocol(models.Model):
    protocol = models.CharField(max_length=10, unique=True)
    def __unicode__(self):
        return self.protocol
    class Meta:
        verbose_name = 'Mirror Protocol'

class MirrorUrl(models.Model):
    url = models.CharField(max_length=255)
    protocol = models.ForeignKey(MirrorProtocol, related_name="urls")
    mirror = models.ForeignKey(Mirror, related_name="urls")
    def __unicode__(self):
        return self.url
    class Meta:
        verbose_name = 'Mirror URL'

class MirrorRsync(models.Model):
    ip = models.CharField(max_length=24)
    mirror = models.ForeignKey(Mirror, related_name="rsync_ips")
    def __unicode__(self):
        return "%s" % (self.ip)
    class Meta:
        verbose_name = 'Mirror Rsync IP'

class Donor(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255, unique=True)

    def __unicode__(self):
        return self.name

    class Meta:
        db_table = 'donors'
        ordering = ['name']

class News(models.Model):
    id = models.AutoField(primary_key=True)
    author = models.ForeignKey(User, related_name='news_author')
    postdate = models.DateField(auto_now_add=True)
    title = models.CharField(max_length=255)
    content = models.TextField()
    def __unicode__(self):
        return self.title
    class Meta:
        db_table = 'news'
        verbose_name_plural = 'news'
        get_latest_by = 'postdate'
        ordering = ['-postdate', '-id']

    def get_absolute_url(self):
        return '/news/%i/' % self.id

class Arch(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255,unique=True)
    def __unicode__(self):
        return self.name
    class Meta:
        db_table = 'arches'
        ordering = ['name']
        verbose_name_plural = 'arches'

class Repo(models.Model):
    id = models.AutoField(primary_key=True)
    name = models.CharField(max_length=255,unique=True)
    testing = models.BooleanField(default=False,
        help_text="Is this repo meant for package testing?")
    def __unicode__(self):
        return self.name
    class Meta:
        db_table = 'repos'
        ordering = ['name']
        verbose_name_plural = 'repos'

class Package(models.Model):
    id = models.AutoField(primary_key=True)
    repo = models.ForeignKey(Repo, related_name="packages")
    arch = models.ForeignKey(Arch, related_name="packages")
    needupdate = models.BooleanField(default=False)
    pkgname = models.CharField(max_length=255, db_index=True)
    pkgbase = models.CharField(max_length=255, db_index=True)
    pkgver = models.CharField(max_length=255)
    pkgrel = models.CharField(max_length=255)
    pkgdesc = models.CharField(max_length=255, null=True)
    url = models.CharField(max_length=255, null=True)
    filename = models.CharField(max_length=255)
    compressed_size = models.PositiveIntegerField(null=True)
    installed_size = models.PositiveIntegerField(null=True)
    build_date = models.DateTimeField(null=True)
    last_update = models.DateTimeField(null=True, blank=True)
    files_last_update = models.DateTimeField(null=True, blank=True)
    license = models.CharField(max_length=255, null=True)
    flag_date = models.DateTimeField(null=True)

    objects = PackageManager()
    class Meta:
        db_table = 'packages'
        ordering = ('pkgname',)
        #get_latest_by = 'last_update'
        #ordering = ('-last_update',)

    def __unicode__(self):
        return self.pkgname

    def get_absolute_url(self):
        return '/packages/%s/%s/%s/' % (self.repo.name.lower(),
                self.arch.name, self.pkgname)

    def get_full_url(self, proto='http'):
        '''get a URL suitable for things like email including the domain'''
        domain = Site.objects.get_current().domain
        return '%s://%s%s' % (proto, domain, self.get_absolute_url())

    @property
    def maintainers(self):
        return User.objects.filter(
                package_relations__pkgbase=self.pkgbase,
                package_relations__type=PackageRelation.MAINTAINER)

    @property
    def signoffs(self):
        if 'signoffs_cache' in dir(self):
            if len(self.signoffs_cache) > 0:
                print self.signoffs_cache
            return self.signoffs_cache
        self.signoffs_cache = list(Signoff.objects.filter(
            pkg=self,
            pkgver=self.pkgver,
            pkgrel=self.pkgrel))
        return self.signoffs_cache

    def approved_for_signoff(self):
        return len(self.signoffs) >= 2

    @cache_function(300)
    def get_requiredby(self):
        """
        Returns a list of package objects.
        """
        requiredby = Package.objects.select_related('arch', 'repo').filter(
                packagedepend__depname=self.pkgname,
                arch__name__in=(self.arch.name, 'any')).distinct()
        return requiredby.order_by('pkgname')

    @cache_function(300)
    def get_depends(self):
        """
        Returns a list of dicts. Each dict contains ('pkg' and 'dep').
        If it represents a found package both vars will be available;
        else pkg will be None if it is a 'virtual' dependency.
        """
        deps = []
        # TODO: we can use list comprehension and an 'in' query to make this more effective
        for dep in self.packagedepend_set.order_by('depname'):
            pkgs = Package.objects.select_related('arch', 'repo').filter(pkgname=dep.depname)
            if self.arch.name != 'any':
                # make sure we match architectures if possible
                pkgs = pkgs.filter(arch__name__in=(self.arch.name, 'any'))
            if len(pkgs) == 0:
                # couldn't find a package in the DB
                # it should be a virtual depend (or a removed package)
                pkg = None
            elif len(pkgs) == 1:
                pkg = pkgs[0]
            else:
                # more than one package, see if we can't shrink it down
                # grab the first though in case we fail
                pkg = pkgs[0]
                if self.repo.testing:
                    pkgs = pkgs.filter(repo__testing=True)
                else:
                    pkgs = pkgs.filter(repo__testing=False)
                if len(pkgs) > 0:
                    pkg = pkgs[0]
            deps.append({'dep': dep, 'pkg': pkg})
        return deps

    def base_package(self):
        """
        Locate the base package for this package. It may be this very package,
        or if it was built in a way that the base package isn't real, will
        return None.
        """
        try:
            # start by looking for something in this repo
            return Package.objects.get(arch=self.arch,
                    repo=self.repo, pkgname=self.pkgbase)
        except Package.DoesNotExist:
            # this package might be split across repos? just find one
            # that matches the correct [testing] repo flag
            pkglist = Package.objects.filter(arch=self.arch,
                    repo__testing=self.repo.testing, pkgname=self.pkgbase)
            if len(pkglist) > 0:
                return pkglist[0]
            return None

    def split_packages(self):
        """
        Return all packages that were built with this one (e.g. share a pkgbase
        value). The package this method is called on will never be in the list,
        and we will never return a package that does not have the same
        repo.testing flag. For any non-split packages, the return value will be
        an empty list.
        """
        return Package.objects.filter(arch=self.arch,
                repo__testing=self.repo.testing, pkgbase=self.pkgbase).exclude(id=self.id)

    def get_svn_link(self, svnpath):
        linkbase = "http://repos.archlinux.org/wsvn/%s/%s/%s/"
        repo = self.repo.name.lower()
        if repo.startswith('community'):
            root = 'community'
        else:
            root = 'packages'
        return linkbase % (root, self.pkgbase, svnpath)

    def get_arch_svn_link(self):
        repo = self.repo.name.lower()
        return self.get_svn_link("repos/%s-%s" % (repo, self.arch.name))

    def get_trunk_svn_link(self):
        return self.get_svn_link("trunk")

    def get_bugs_link(self):
        repo = self.repo.name.lower()
        if repo.startswith('community'):
            project = 5
        else:
            project = 1
        return "http://bugs.archlinux.org/?project=%d&string=%s" % \
                (project, self.pkgname)

    def is_same_version(self, other):
        'is this package similar, name and version-wise, to another'
        return self.pkgname == other.pkgname \
                and self.pkgver == other.pkgver \
                and self.pkgrel == other.pkgrel


class Signoff(models.Model):
    pkg = models.ForeignKey(Package)
    pkgver = models.CharField(max_length=255)
    pkgrel = models.CharField(max_length=255)
    packager = models.ForeignKey(User)

class PackageFile(models.Model):
    id = models.AutoField(primary_key=True)
    pkg = models.ForeignKey('Package')
    path = models.CharField(max_length=255)
    class Meta:
        db_table = 'package_files'

class PackageDepend(models.Model):
    id = models.AutoField(primary_key=True)
    pkg = models.ForeignKey('Package')
    depname = models.CharField(db_index=True, max_length=255)
    depvcmp = models.CharField(max_length=255)
    class Meta:
        db_table = 'package_depends'

class Todolist(models.Model):
    id = models.AutoField(primary_key=True)
    creator = models.ForeignKey(User)
    name = models.CharField(max_length=255)
    description = models.TextField()
    date_added = models.DateField(auto_now_add=True)
    objects = TodolistManager()
    def __unicode__(self):
        return self.name

    @property
    def packages(self):
        # select_related() does not use LEFT OUTER JOIN for nullable ForeignKey
        # fields. That is why we need to explicitly list the ones we want.
        return TodolistPkg.objects.select_related(
            'pkg__repo', 'pkg__arch').filter(list=self).order_by('pkg')

    @property
    def package_names(self):
        return '\n'.join(set([p.pkg.pkgname for p in self.packages]))

    class Meta:
        db_table = 'todolists'

    def get_absolute_url(self):
        return '/todo/%i/' % self.id

class TodolistPkg(models.Model):
    id = models.AutoField(primary_key=True)
    list = models.ForeignKey('Todolist')
    pkg = models.ForeignKey('Package')
    complete = models.BooleanField(default=False)
    class Meta:
        db_table = 'todolist_pkgs'
        unique_together = (('list','pkg'),)

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