moved more stuff from views to helpers or managers

This commit is contained in:
volpino 2012-05-01 21:15:55 +02:00
parent c3d28402c7
commit ab6ea22ed3
4 changed files with 91 additions and 42 deletions

View File

@ -2,9 +2,41 @@
djeuscan.helpers djeuscan.helpers
""" """
from distutils.version import StrictVersion, LooseVersion
from djeuscan.models import Package
def xint(i): def xint(i):
"""
Tries to cast to int, fallbacks to 0
"""
try: try:
return int(i) return int(i)
except Exception: except Exception:
return 0 return 0
def version_key(version):
version = version.version
try:
return StrictVersion(version)
# in case of abnormal version number, fall back to LooseVersion
except ValueError:
return LooseVersion(version)
def packages_from_names(data):
"""
Returns a list of Package objects from a string of names
"""
packages = []
data = data.replace("\r", "")
for pkg in data.split('\n'):
if '/' in pkg:
cat, pkg = pkg.split('/')
packages.extend(Package.objects.filter(category=cat, name=pkg))
else:
packages.extend(Package.objects.filter(name=pkg))
return packages

View File

@ -75,3 +75,48 @@ class PackageManager(models.Manager):
'id', 'name', 'category', 'n_versions', 'n_packaged', 'n_overlay' 'id', 'name', 'category', 'n_versions', 'n_packaged', 'n_overlay'
) )
return packages.filter(version__overlay=overlay).distinct() return packages.filter(version__overlay=overlay).distinct()
def for_maintainer(self, maintainer):
"""
Returns packages that belong to the given maintainer
"""
res = self.filter(maintainers__id=maintainer.id)
# performance boost
res = res.select_related(
'last_version_gentoo',
'last_version_overlay',
'last_version_upstream'
)
return res
def for_herd(self, herd):
"""
Returns packages that belong to the given herd
"""
res = self.filter(herds__id=herd.id)
# performance boost
res = res.select_related(
'last_version_gentoo',
'last_version_overlay',
'last_version_upstream'
)
return res
def for_category(self, category):
"""
Returns packages that belong to the given category
"""
res = self.filter(category=category)
# performance boost
res = res.select_related(
'last_version_gentoo',
'last_version_overlay',
'last_version_upstream'
)
return res

View File

@ -62,6 +62,10 @@ class Package(models.Model):
objects = PackageManager() objects = PackageManager()
@property
def homepages(self):
return self.homepage.split(' ')
def __unicode__(self): def __unicode__(self):
return '%s/%s' % (self.category, self.name) return '%s/%s' % (self.category, self.name)
@ -129,6 +133,9 @@ class EuscanResult(models.Model):
datetime = models.DateTimeField() datetime = models.DateTimeField()
result = models.TextField(blank=True) result = models.TextField(blank=True)
class Meta:
get_latest_by = "datetime"
class Log(models.Model): class Log(models.Model):
""" """

View File

@ -3,8 +3,8 @@
from annoying.decorators import render_to from annoying.decorators import render_to
from django.http import Http404 from django.http import Http404
from django.shortcuts import get_object_or_404 from django.shortcuts import get_object_or_404
from django.db.models import Max
from djeuscan.helpers import version_key, packages_from_names
from djeuscan.models import Version, Package, Herd, Maintainer, EuscanResult, \ from djeuscan.models import Version, Package, Herd, Maintainer, EuscanResult, \
VersionLog VersionLog
from djeuscan.forms import WorldForm, PackagesForm from djeuscan.forms import WorldForm, PackagesForm
@ -24,10 +24,7 @@ def index(request):
'n_maintainers': Maintainer.objects.count(), 'n_maintainers': Maintainer.objects.count(),
} }
try: try:
context['last_scan'] = \ context['last_scan'] = EuscanResult.objects.latest().datetime
EuscanResult.objects.get(
id=EuscanResult.objects.aggregate(Max('id'))['id__max']
).datetime
except EuscanResult.DoesNotExist: except EuscanResult.DoesNotExist:
context['last_scan'] = None context['last_scan'] = None
@ -46,10 +43,7 @@ def categories(request):
@render_to('euscan/category.html') @render_to('euscan/category.html')
def category(request, category): def category(request, category):
packages = Package.objects.filter(category=category) packages = Package.objects.for_category(category)
packages = packages.select_related(
'last_version_gentoo', 'last_version_overlay', 'last_version_upstream'
)
if not packages: if not packages:
raise Http404 raise Http404
@ -65,10 +59,7 @@ def herds(request):
@render_to('euscan/herd.html') @render_to('euscan/herd.html')
def herd(request, herd): def herd(request, herd):
herd = get_object_or_404(Herd, herd=herd) herd = get_object_or_404(Herd, herd=herd)
packages = Package.objects.filter(herds__id=herd.id) packages = Package.for_herd(herd)
packages = packages.select_related(
'last_version_gentoo', 'last_version_overlay', 'last_version_upstream'
)
return {'herd': herd, 'packages': packages} return {'herd': herd, 'packages': packages}
@ -80,11 +71,8 @@ def maintainers(request):
@render_to('euscan/maintainer.html') @render_to('euscan/maintainer.html')
def maintainer(request, maintainer_id): def maintainer(request, maintainer_id):
maintainer = get_object_or_404(Maintainer, id=maintainer_id) maintainer = get_object_or_404(Maintainer, pk=maintainer_id)
packages = Package.objects.filter(maintainers__id=maintainer.id) packages = Package.objects.for_maintainer(maintainer)
packages = packages.select_related(
'last_version_gentoo', 'last_version_overlay', 'last_version_upstream'
)
return {'maintainer': maintainer, 'packages': packages} return {'maintainer': maintainer, 'packages': packages}
@ -104,19 +92,7 @@ def overlay(request, overlay):
@render_to('euscan/package.html') @render_to('euscan/package.html')
def package(request, category, package): def package(request, category, package):
def version_key(version):
from distutils.version import StrictVersion, LooseVersion
version = version.version
try:
return StrictVersion(version)
# in case of abnormal version number, fall back to LooseVersion
except ValueError:
return LooseVersion(version)
package = get_object_or_404(Package, category=category, name=package) package = get_object_or_404(Package, category=category, name=package)
package.homepages = package.homepage.split(' ')
packaged = Version.objects.filter(package=package, packaged=True) packaged = Version.objects.filter(package=package, packaged=True)
upstream = Version.objects.filter(package=package, packaged=False) upstream = Version.objects.filter(package=package, packaged=False)
@ -143,7 +119,6 @@ def world(request):
@render_to('euscan/world_scan.html') @render_to('euscan/world_scan.html')
def world_scan(request): def world_scan(request):
packages = []
if 'world' in request.FILES: if 'world' in request.FILES:
data = request.FILES['world'].read() data = request.FILES['world'].read()
@ -152,17 +127,7 @@ def world_scan(request):
else: else:
data = "" data = ""
data = data.replace("\r", "") packages = packages_from_names(data)
for pkg in data.split('\n'):
try:
if '/' in pkg:
cat, pkg = pkg.split('/')
packages.extend(Package.objects.filter(category=cat, name=pkg))
else:
packages.extend(Package.objects.filter(name=pkg))
except:
pass
return {'packages': packages} return {'packages': packages}