From 7640082d14af2827ab22caa6dce3575f5112d7cf Mon Sep 17 00:00:00 2001 From: eliott Date: Sat, 22 Mar 2008 19:51:01 -0700 Subject: Refactored the model to remove arch and repo tables. Refactored the model to remove the arch and repo tables. Those data points are now encapsulated in the package table as an ENUM field. Changes to models and templates as needed. --- main/models.py | 79 ++++++++++++++++++++++++++++++++++------------------------ 1 file changed, 47 insertions(+), 32 deletions(-) (limited to 'main/models.py') diff --git a/main/models.py b/main/models.py index 44a8065e..74f2cdbf 100644 --- a/main/models.py +++ b/main/models.py @@ -2,7 +2,34 @@ from django.contrib.auth.models import User import re +########################### +### Model help classes ### +########################### +class Container(dict): + def __init__(self, dict_entries=None, **entries): + if dict_entries: + self.update(dict_entries) + if entries: + self.update(entries) + + def __iter__(self): + rev_items = [(v, k) for k, v in self.items()] + rev_items.sort() + items = [k for v, k in rev_items] + return items.__iter__() + + def itertransp(self): + rev_items = [(v, k) for k, v in self.items()] + rev_items.sort() + return rev_items.__iter__() + + def __getattr__(self,name): + return self[name] + def __setattr__(self,name,val): + self[name] = val + + ########################### ### User Profile Class #### ########################### @@ -133,33 +160,19 @@ class Meta: def get_absolute_url(self): return '/news/%i/' % self.id -class Arch(models.Model): - id = models.AutoField(primary_key=True) - name = models.CharField(unique=True,maxlength=255) - class Meta: - db_table = 'archs' - ordering = ['name'] - def __str__(self): - return self.name - -class Repo(models.Model): - id = models.AutoField(primary_key=True) - name = models.CharField(maxlength=255) - class Meta: - db_table = 'repos' - ordering = ['name'] - def last_update(self): - try: - latest = Package.objects.filter( - repo__name__exact=self.name).order_by('-last_update')[0] - return latest.last_update - except IndexError: - return "N/A" - class Package(models.Model): + ## note: purposefully inlining arch and repo. + ## they don't change very often (rarely), and it should help compact + ## the general model + # architectures + ARCHES = Container(i686=1, x86_64=2) + # repositories + REPOS = Container(core=1, extra=2, testing=3, unstable=4) + id = models.AutoField(primary_key=True) - repo = models.ForeignKey('Repo') - arch = models.ForeignKey('Arch') + ## note: the arch and repo elements might need to be indexed. not sure. + repo = models.IntegerField(choices=REPOS.itertransp()) + arch = models.IntegerField(choices=ARCHES.itertransp()) maintainer = models.ForeignKey(User, related_name='package_maintainer') needupdate = models.BooleanField(default=False) pkgname = models.CharField(maxlength=255) @@ -178,7 +191,8 @@ def get_absolute_url(self): def required_by_urlize(self): urls = [] - requiredby = PackageDepends.objects.filter(depname=self.pkgname) + requiredby = PackageDepend.objects.filter( + depname=self.pkgname).order_by('depname') for req in requiredby: urls.append( '
  • %s
  • ' % \ @@ -187,15 +201,16 @@ def required_by_urlize(self): def depends_urlize(self): urls = [] - for dep in self.packagedepends_set.all(): + for dep in self.packagedepend_set.order_by('depname'): try: + # we only need depend on same-arch-packages p = Package.objects.get( pkgname=dep.depname, arch=self.arch) - except IndexError: + except Package.DoesNotExist, IndexError: # couldn't find a package in the DB # it might be a virtual depend - urls.append('
  • %s (v)
  • ' % dep.depname) + urls.append('
  • %s
  • ' % dep.depname) continue urls.append( '
  • %s%s
  • ' % \ @@ -207,15 +222,15 @@ class PackageFile(models.Model): pkg = models.ForeignKey('Package') path = models.CharField(maxlength=255) class Meta: - db_table = 'packages_files' + db_table = 'package_files' -class PackageDepends(models.Model): +class PackageDepend(models.Model): id = models.AutoField(primary_key=True) pkg = models.ForeignKey('Package') depname = models.CharField(db_index=True, maxlength=255) depvcmp = models.CharField(maxlength=255) class Meta: - db_table = 'packages_depends' + db_table = 'package_depends' class Todolist(models.Model): id = models.AutoField(primary_key=True) -- cgit v1.2.3-54-g00ecf