2012-07-23 07:45:44 -07:00
|
|
|
import datetime
|
2012-07-21 09:26:34 -07:00
|
|
|
import json
|
|
|
|
from string import Template
|
|
|
|
from urllib import urlencode
|
|
|
|
|
|
|
|
from django.http import HttpResponse, Http404
|
2012-07-23 07:45:44 -07:00
|
|
|
from django.shortcuts import get_object_or_404, redirect, render
|
|
|
|
from django.utils.timezone import now
|
2012-07-21 09:26:34 -07:00
|
|
|
|
|
|
|
from main.models import Package, PackageFile, Arch, Repo
|
|
|
|
from mirrors.utils import get_mirror_url_for_download
|
2012-07-23 07:45:44 -07:00
|
|
|
from ..models import Update
|
2012-07-21 09:26:34 -07:00
|
|
|
from ..utils import get_group_info, PackageJSONEncoder
|
|
|
|
|
|
|
|
|
2012-10-26 15:05:39 -07:00
|
|
|
def arch_plus_agnostic(arch):
|
|
|
|
arches = [ arch ]
|
|
|
|
arches.extend(Arch.objects.filter(agnostic=True).order_by())
|
|
|
|
return arches
|
|
|
|
|
|
|
|
|
2012-07-23 07:45:44 -07:00
|
|
|
def split_package_details(request, name, repo, arch):
|
2012-07-31 17:01:45 -07:00
|
|
|
'''Check if we have a split package (e.g. pkgbase) value matching this
|
|
|
|
name. If so, we can show a listing page for the entire set of packages.'''
|
2012-10-26 15:05:39 -07:00
|
|
|
arches = arch_plus_agnostic(arch)
|
2012-07-21 09:26:34 -07:00
|
|
|
pkgs = Package.objects.normal().filter(pkgbase=name,
|
|
|
|
repo__testing=repo.testing, repo__staging=repo.staging,
|
|
|
|
arch__in=arches).order_by('pkgname')
|
|
|
|
if len(pkgs) == 0:
|
2012-07-23 07:45:44 -07:00
|
|
|
return None
|
2012-07-21 09:26:34 -07:00
|
|
|
# we have packages, but ensure at least one is in the given repo
|
|
|
|
if not any(True for pkg in pkgs if pkg.repo == repo):
|
2012-07-23 07:45:44 -07:00
|
|
|
return None
|
2012-07-21 09:26:34 -07:00
|
|
|
context = {
|
|
|
|
'list_title': 'Split Package Details',
|
|
|
|
'name': name,
|
|
|
|
'arch': arch,
|
|
|
|
'packages': pkgs,
|
|
|
|
}
|
2012-07-24 07:35:55 -07:00
|
|
|
return render(request, 'packages/packages_list.html', context)
|
2012-07-21 09:26:34 -07:00
|
|
|
|
|
|
|
|
2012-07-23 07:45:44 -07:00
|
|
|
CUTOFF = datetime.timedelta(days=60)
|
|
|
|
|
|
|
|
|
|
|
|
def recently_removed_package(request, name, repo, arch, cutoff=CUTOFF):
|
2012-07-31 17:01:45 -07:00
|
|
|
'''Check our packages update table to see if this package has existed in
|
|
|
|
this repo before. If so, we can show a 410 Gone page and point the
|
|
|
|
requester in the right direction.'''
|
2012-10-26 15:05:39 -07:00
|
|
|
arches = arch_plus_agnostic(arch)
|
2012-07-23 07:45:44 -07:00
|
|
|
match = Update.objects.select_related('arch', 'repo').filter(
|
2012-07-31 17:01:45 -07:00
|
|
|
pkgname=name, repo=repo, arch__in=arches)
|
2012-07-23 07:45:44 -07:00
|
|
|
if cutoff is not None:
|
|
|
|
when = now() - cutoff
|
|
|
|
match = match.filter(created__gte=when)
|
|
|
|
try:
|
2012-07-31 17:31:44 -07:00
|
|
|
update = match.latest()
|
|
|
|
elsewhere = update.elsewhere()
|
2012-07-31 17:12:27 -07:00
|
|
|
if len(elsewhere) == 1:
|
|
|
|
return redirect(elsewhere[0])
|
2012-07-31 17:31:44 -07:00
|
|
|
context = {
|
|
|
|
'update': update,
|
|
|
|
'elsewhere': elsewhere,
|
|
|
|
'name': name,
|
|
|
|
'version': update.old_version,
|
|
|
|
'arch': arch,
|
|
|
|
'repo': repo,
|
|
|
|
}
|
|
|
|
return render(request, 'packages/removed.html', context, status=410)
|
2012-07-23 07:45:44 -07:00
|
|
|
except Update.DoesNotExist:
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
2012-07-31 17:12:27 -07:00
|
|
|
def replaced_package(request, name, repo, arch):
|
|
|
|
'''Check our package replacements to see if this is a package we used to
|
|
|
|
have but no longer do.'''
|
|
|
|
match = Package.objects.filter(replaces__name=name, repo=repo, arch=arch)
|
|
|
|
if len(match) == 1:
|
|
|
|
return redirect(match[0], permanent=True)
|
2012-07-31 17:31:44 -07:00
|
|
|
elif len(match) > 1:
|
|
|
|
context = {
|
|
|
|
'elsewhere': match,
|
|
|
|
'name': name,
|
|
|
|
'version': '',
|
|
|
|
'arch': arch,
|
|
|
|
'repo': repo,
|
|
|
|
}
|
|
|
|
return render(request, 'packages/removed.html', context, status=410)
|
2012-07-31 17:12:27 -07:00
|
|
|
return None
|
|
|
|
|
|
|
|
|
2012-07-31 17:01:45 -07:00
|
|
|
def redirect_agnostic(request, name, repo, arch):
|
|
|
|
'''For arch='any' packages, we can issue a redirect to them if we have a
|
|
|
|
single non-ambiguous option by changing the arch to match any arch-agnostic
|
|
|
|
package.'''
|
|
|
|
if not arch.agnostic:
|
|
|
|
# limit to 2 results, we only need to know whether there is anything
|
|
|
|
# except only one matching result
|
|
|
|
pkgs = Package.objects.select_related(
|
|
|
|
'arch', 'repo', 'packager').filter(pkgname=name,
|
|
|
|
repo=repo, arch__agnostic=True)[:2]
|
|
|
|
if len(pkgs) == 1:
|
|
|
|
return redirect(pkgs[0], permanent=True)
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def redirect_to_search(request, name, repo, arch):
|
|
|
|
pkg_data = [
|
|
|
|
('arch', arch.lower()),
|
|
|
|
('repo', repo.lower()),
|
|
|
|
('q', name),
|
|
|
|
]
|
|
|
|
# only include non-blank values in the query we generate
|
|
|
|
pkg_data = [(x, y.encode('utf-8')) for x, y in pkg_data if y]
|
|
|
|
return redirect("/packages/?%s" % urlencode(pkg_data))
|
|
|
|
|
|
|
|
|
2012-07-21 09:26:34 -07:00
|
|
|
def details(request, name='', repo='', arch=''):
|
|
|
|
if all([name, repo, arch]):
|
2012-07-31 17:01:45 -07:00
|
|
|
arch_obj = get_object_or_404(Arch, name=arch)
|
|
|
|
repo_obj = get_object_or_404(Repo, name__iexact=repo)
|
2012-07-21 09:26:34 -07:00
|
|
|
try:
|
|
|
|
pkg = Package.objects.select_related(
|
|
|
|
'arch', 'repo', 'packager').get(pkgname=name,
|
2012-07-31 17:01:45 -07:00
|
|
|
repo=repo_obj, arch=arch_obj)
|
2012-07-24 07:35:55 -07:00
|
|
|
return render(request, 'packages/details.html', {'pkg': pkg})
|
2012-07-21 09:26:34 -07:00
|
|
|
except Package.DoesNotExist:
|
2012-07-31 17:01:45 -07:00
|
|
|
# attempt a variety of fallback options before 404ing
|
|
|
|
options = (redirect_agnostic, split_package_details,
|
2012-07-31 17:12:27 -07:00
|
|
|
recently_removed_package, replaced_package)
|
2012-07-31 17:01:45 -07:00
|
|
|
for method in options:
|
|
|
|
ret = method(request, name, repo_obj, arch_obj)
|
|
|
|
if ret:
|
|
|
|
return ret
|
|
|
|
# we've tried everything at this point, nothing to see
|
|
|
|
raise Http404
|
2012-07-21 09:26:34 -07:00
|
|
|
else:
|
2012-07-31 17:01:45 -07:00
|
|
|
return redirect_to_search(request, name, repo, arch)
|
2012-07-21 09:26:34 -07:00
|
|
|
|
|
|
|
|
|
|
|
def groups(request, arch=None):
|
|
|
|
arches = []
|
|
|
|
if arch:
|
|
|
|
get_object_or_404(Arch, name=arch, agnostic=False)
|
|
|
|
arches.append(arch)
|
|
|
|
grps = get_group_info(arches)
|
|
|
|
context = {
|
|
|
|
'groups': grps,
|
|
|
|
'arch': arch,
|
|
|
|
}
|
2012-07-24 07:35:55 -07:00
|
|
|
return render(request, 'packages/groups.html', context)
|
2012-07-21 09:26:34 -07:00
|
|
|
|
|
|
|
|
|
|
|
def group_details(request, arch, name):
|
|
|
|
arch = get_object_or_404(Arch, name=arch)
|
2012-10-26 15:05:39 -07:00
|
|
|
arches = arch_plus_agnostic(arch)
|
2012-07-21 09:26:34 -07:00
|
|
|
pkgs = Package.objects.normal().filter(
|
|
|
|
groups__name=name, arch__in=arches).order_by('pkgname')
|
|
|
|
if len(pkgs) == 0:
|
|
|
|
raise Http404
|
|
|
|
context = {
|
|
|
|
'list_title': 'Group Details',
|
|
|
|
'name': name,
|
|
|
|
'arch': arch,
|
|
|
|
'packages': pkgs,
|
|
|
|
}
|
2012-07-24 07:35:55 -07:00
|
|
|
return render(request, 'packages/packages_list.html', context)
|
2012-07-21 09:26:34 -07:00
|
|
|
|
|
|
|
|
|
|
|
def files(request, name, repo, arch):
|
2012-07-31 17:49:37 -07:00
|
|
|
pkg = get_object_or_404(Package,
|
|
|
|
pkgname=name, repo__name__iexact=repo, arch__name=arch)
|
2012-07-21 09:26:34 -07:00
|
|
|
# files are inserted in sorted order, so preserve that
|
|
|
|
fileslist = PackageFile.objects.filter(pkg=pkg).order_by('id')
|
|
|
|
dir_count = sum(1 for f in fileslist if f.is_directory)
|
|
|
|
files_count = len(fileslist) - dir_count
|
|
|
|
context = {
|
|
|
|
'pkg': pkg,
|
|
|
|
'files': fileslist,
|
|
|
|
'files_count': files_count,
|
|
|
|
'dir_count': dir_count,
|
|
|
|
}
|
|
|
|
template = 'packages/files.html'
|
2012-07-24 07:35:55 -07:00
|
|
|
return render(request, template, context)
|
2012-07-21 09:26:34 -07:00
|
|
|
|
|
|
|
|
|
|
|
def details_json(request, name, repo, arch):
|
|
|
|
pkg = get_object_or_404(Package,
|
|
|
|
pkgname=name, repo__name__iexact=repo, arch__name=arch)
|
|
|
|
to_json = json.dumps(pkg, ensure_ascii=False, cls=PackageJSONEncoder)
|
2013-01-13 20:34:33 -08:00
|
|
|
return HttpResponse(to_json, content_type='application/json')
|
2012-07-21 09:26:34 -07:00
|
|
|
|
|
|
|
|
|
|
|
def files_json(request, name, repo, arch):
|
|
|
|
pkg = get_object_or_404(Package,
|
|
|
|
pkgname=name, repo__name__iexact=repo, arch__name=arch)
|
|
|
|
# files are inserted in sorted order, so preserve that
|
|
|
|
fileslist = PackageFile.objects.filter(pkg=pkg).order_by('id')
|
2012-07-23 19:31:17 -07:00
|
|
|
dir_count = sum(1 for f in fileslist if f.is_directory)
|
|
|
|
files_count = len(fileslist) - dir_count
|
2012-07-21 09:26:34 -07:00
|
|
|
data = {
|
|
|
|
'pkgname': pkg.pkgname,
|
|
|
|
'repo': pkg.repo.name.lower(),
|
|
|
|
'arch': pkg.arch.name.lower(),
|
2012-07-23 19:31:17 -07:00
|
|
|
'pkg_last_update': pkg.last_update,
|
|
|
|
'files_last_update': pkg.files_last_update,
|
|
|
|
'files_count': files_count,
|
|
|
|
'dir_count': dir_count,
|
2012-07-21 09:26:34 -07:00
|
|
|
'files': fileslist,
|
|
|
|
}
|
|
|
|
to_json = json.dumps(data, ensure_ascii=False, cls=PackageJSONEncoder)
|
2013-01-13 20:34:33 -08:00
|
|
|
return HttpResponse(to_json, content_type='application/json')
|
2012-07-21 09:26:34 -07:00
|
|
|
|
|
|
|
|
|
|
|
def download(request, name, repo, arch):
|
|
|
|
pkg = get_object_or_404(Package,
|
|
|
|
pkgname=name, repo__name__iexact=repo, arch__name=arch)
|
|
|
|
url = get_mirror_url_for_download()
|
|
|
|
if not url:
|
|
|
|
raise Http404
|
|
|
|
arch = pkg.arch.name
|
|
|
|
if pkg.arch.agnostic:
|
|
|
|
# grab the first non-any arch to fake the download path
|
|
|
|
arch = Arch.objects.exclude(agnostic=True)[0].name
|
|
|
|
values = {
|
|
|
|
'host': url.url,
|
|
|
|
'arch': arch,
|
|
|
|
'repo': pkg.repo.name.lower(),
|
|
|
|
'file': pkg.filename,
|
|
|
|
}
|
|
|
|
url = Template('${host}${repo}/os/${arch}/${file}').substitute(values)
|
|
|
|
return redirect(url)
|
|
|
|
|
|
|
|
# vim: set ts=4 sw=4 et:
|