Fix bug FS#8417
[archweb_dev-nj.git] / packages / models.py
blobc1b6c84d8f40e8972e15ce2184f832bb6c5e34cf
1 from django.db import models
2 from django.contrib.auth.models import User
3 import re
5 class PackageManager(models.Manager):
6 def get_flag_stats(self):
7 results = []
8 # first the orphans
9 unflagged = self.filter(maintainer=0).count()
10 flagged = self.filter(maintainer=0).filter(needupdate=True).count()
11 results.append((User(id=0,first_name='Orphans'), unflagged, flagged))
12 # now the rest
13 for maint in User.objects.all().order_by('first_name'):
14 unflagged = self.filter(maintainer=maint.id).count()
15 flagged = self.filter(maintainer=maint.id).filter(needupdate=True).count()
16 results.append((maint, unflagged, flagged))
17 return results
19 class Category(models.Model):
20 id = models.AutoField(primary_key=True)
21 category = models.CharField(maxlength=255)
22 class Meta:
23 db_table = 'categories'
24 verbose_name_plural = 'categories'
26 class Repo(models.Model):
27 id = models.AutoField(primary_key=True)
28 name = models.CharField(maxlength=255)
29 class Meta:
30 db_table = 'repos'
31 def last_update(self):
32 try:
33 latest = Package.objects.filter(repo__name__exact=self.name).order_by('-last_update')[0]
34 return latest.last_update
35 except IndexError:
36 return "N/A"
38 class Package(models.Model):
39 id = models.AutoField(primary_key=True)
40 repo = models.ForeignKey(Repo)
41 maintainer = models.ForeignKey(User)
42 category = models.ForeignKey(Category)
43 needupdate = models.BooleanField(default=False)
44 pkgname = models.CharField(maxlength=255)
45 pkgver = models.CharField(maxlength=255)
46 pkgrel = models.CharField(maxlength=255)
47 pkgdesc = models.CharField(maxlength=255)
48 url = models.URLField()
49 sources = models.TextField()
50 depends = models.TextField()
51 last_update = models.DateTimeField(null=True, blank=True)
52 objects = PackageManager()
53 class Meta:
54 db_table = 'packages'
55 get_latest_by = 'last_update'
57 def get_absolute_url(self):
58 return '/packages/%i/' % self.id
60 def depends_urlize(self):
61 urls = ''
62 for dep in self.depends.split(' '):
63 # shave off any version qualifiers
64 nameonly = re.match(r"([a-z0-9-]+)", dep).group(1)
65 try:
66 p = Package.objects.filter(pkgname=nameonly)[0]
67 except IndexError:
68 # couldn't find a package in the DB -- it might be a virtual depend
69 urls = urls + '<li>' + dep + '</li>'
70 continue
71 url = '<li><a href="/packages/' + str(p.id) + '">' + dep + '</a></li>'
72 urls = urls + url
73 return urls
75 def sources_urlize(self):
76 urls = ''
77 for source in self.sources.split(' '):
78 if re.search('://', source):
79 url = '<li><a href="' + source + '">' + source + '</a></li>'
80 else:
81 url = '<li>' + source + '</li>'
82 urls = urls + url
83 return urls
85 class PackageFile(models.Model):
86 id = models.AutoField(primary_key=True)
87 pkg = models.ForeignKey(Package)
88 path = models.CharField(maxlength=255)
89 class Meta:
90 db_table = 'packages_files'