2012-05-01 16:56:09 +02:00
|
|
|
"""
|
|
|
|
djeuscan.helpers
|
|
|
|
"""
|
|
|
|
|
2012-05-01 21:15:55 +02:00
|
|
|
from distutils.version import StrictVersion, LooseVersion
|
|
|
|
|
2012-05-01 16:56:09 +02:00
|
|
|
|
|
|
|
def xint(i):
|
2012-05-01 21:15:55 +02:00
|
|
|
"""
|
|
|
|
Tries to cast to int, fallbacks to 0
|
|
|
|
"""
|
2012-05-01 16:56:09 +02:00
|
|
|
try:
|
|
|
|
return int(i)
|
|
|
|
except Exception:
|
|
|
|
return 0
|
2012-05-01 21:15:55 +02:00
|
|
|
|
|
|
|
|
2012-05-05 10:54:04 +02:00
|
|
|
def select_related_last_versions(queryset):
|
|
|
|
queryset = queryset.select_related(
|
|
|
|
'last_version_gentoo',
|
|
|
|
'last_version_overlay',
|
|
|
|
'last_version_upstream'
|
|
|
|
)
|
|
|
|
|
|
|
|
|
2012-05-01 21:15:55 +02:00
|
|
|
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
|
|
|
|
"""
|
2012-05-05 10:02:17 +02:00
|
|
|
|
|
|
|
from djeuscan.models import Package
|
|
|
|
|
2012-05-01 21:15:55 +02:00
|
|
|
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
|
2012-05-05 10:22:59 +02:00
|
|
|
|
|
|
|
|
|
|
|
def rename_fields(vqs, fields):
|
|
|
|
ret = []
|
|
|
|
for n in vqs:
|
|
|
|
for tr in fields:
|
|
|
|
if tr[0] in n:
|
|
|
|
n[tr[1]] = n[tr[0]]
|
|
|
|
del n[tr[0]]
|
|
|
|
ret.append(n)
|
|
|
|
return ret
|
|
|
|
|
|
|
|
|
|
|
|
class catch_and_return(object):
|
|
|
|
def __init__(self, err, response):
|
|
|
|
self.err = err
|
|
|
|
self.response = response
|
|
|
|
|
|
|
|
def __call__(self, fn):
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
try:
|
|
|
|
return fn(*args, **kwargs)
|
|
|
|
except self.err:
|
|
|
|
return self.response
|
|
|
|
return wrapper
|
2012-07-14 13:56:21 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_account_categories(user):
|
|
|
|
from djeuscan.models import Package, CategoryAssociation
|
|
|
|
|
|
|
|
# TODO: This is quite ugly
|
|
|
|
category_names = [obj.category for obj in
|
|
|
|
CategoryAssociation.objects.filter(user=user)]
|
|
|
|
return [c for c in Package.objects.categories()
|
|
|
|
if c["category"] in category_names]
|
|
|
|
|
|
|
|
|
|
|
|
def get_account_herds(user):
|
|
|
|
from djeuscan.models import Package, HerdAssociation
|
|
|
|
|
|
|
|
ids = [obj.herd.pk for obj in
|
|
|
|
HerdAssociation.objects.filter(user=user)]
|
|
|
|
return Package.objects.herds(ids=ids)
|
|
|
|
|
|
|
|
|
|
|
|
def get_account_maintainers(user):
|
|
|
|
from djeuscan.models import Package, MaintainerAssociation
|
|
|
|
|
|
|
|
ids = [obj.maintainer.pk for obj in
|
|
|
|
MaintainerAssociation.objects.filter(user=user)]
|
|
|
|
return Package.objects.maintainers(ids=ids)
|
|
|
|
|
|
|
|
|
|
|
|
def get_account_packages(user):
|
|
|
|
from djeuscan.models import PackageAssociation
|
|
|
|
return [obj.package for obj in
|
|
|
|
PackageAssociation.objects.filter(user=user)]
|