summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDan McGee <dan@archlinux.org>2012-04-07 14:44:06 -0500
committerDan McGee <dan@archlinux.org>2012-04-07 14:55:06 -0500
commit068bc8db6bbfd9e1ed078d72169f34da97914c82 (patch)
tree1c0b82aed0fbd61b25c444311a896dff5161a381
parentb5ab5b1e218219b09857b06f88e522bccb4b5600 (diff)
Move PackageJSONEncoder into package.utils module
This will allow it to be used elsewhere, and doesn't really belong in views anyway. Signed-off-by: Dan McGee <dan@archlinux.org>
-rw-r--r--packages/utils.py37
-rw-r--r--packages/views/__init__.py39
2 files changed, 39 insertions, 37 deletions
diff --git a/packages/utils.py b/packages/utils.py
index 2a43a08b..a3c13b17 100644
--- a/packages/utils.py
+++ b/packages/utils.py
@@ -2,13 +2,15 @@ from collections import defaultdict
from itertools import chain
from operator import itemgetter
+from django.core.serializers.json import DjangoJSONEncoder
from django.db import connection
from django.db.models import Count, Max, F
from django.contrib.auth.models import User
-from main.models import Package, Arch, Repo
+from main.models import Package, PackageDepend, PackageFile, Arch, Repo
from main.utils import cache_function, groupby_preserve_order, PackageStandin
from .models import (PackageGroup, PackageRelation,
+ License, Conflict, Provision, Replacement,
SignoffSpecification, Signoff, DEFAULT_SIGNOFF_SPEC)
@cache_function(127)
@@ -422,4 +424,37 @@ def get_signoff_groups(repos=None, user=None):
return signoff_groups
+
+class PackageJSONEncoder(DjangoJSONEncoder):
+ pkg_attributes = [ 'pkgname', 'pkgbase', 'repo', 'arch', 'pkgver',
+ 'pkgrel', 'epoch', 'pkgdesc', 'url', 'filename', 'compressed_size',
+ 'installed_size', 'build_date', 'last_update', 'flag_date',
+ 'maintainers', 'packager' ]
+ pkg_list_attributes = [ 'groups', 'licenses', 'conflicts',
+ 'provides', 'replaces', 'depends' ]
+
+ def default(self, obj):
+ if hasattr(obj, '__iter__'):
+ # mainly for queryset serialization
+ return list(obj)
+ if isinstance(obj, Package):
+ data = dict((attr, getattr(obj, attr))
+ for attr in self.pkg_attributes)
+ for attr in self.pkg_list_attributes:
+ data[attr] = getattr(obj, attr).all()
+ return data
+ if isinstance(obj, PackageFile):
+ filename = obj.filename or ''
+ return obj.directory + filename
+ if isinstance(obj, (Repo, Arch)):
+ return obj.name.lower()
+ if isinstance(obj, (PackageGroup, License)):
+ return obj.name
+ if isinstance(obj, (Conflict, Provision, Replacement, PackageDepend)):
+ return unicode(obj)
+ elif isinstance(obj, User):
+ return obj.username
+ return super(PackageJSONEncoder, self).default(obj)
+
+
# vim: set ts=4 sw=4 et:
diff --git a/packages/views/__init__.py b/packages/views/__init__.py
index abc86d78..2ed266e9 100644
--- a/packages/views/__init__.py
+++ b/packages/views/__init__.py
@@ -4,7 +4,6 @@ from urllib import urlencode
from django.contrib import messages
from django.contrib.auth.decorators import permission_required
from django.contrib.auth.models import User
-from django.core.serializers.json import DjangoJSONEncoder
from django.http import HttpResponse, Http404
from django.shortcuts import get_object_or_404, redirect
from django.utils import simplejson
@@ -12,13 +11,12 @@ from django.views.decorators.http import require_POST
from django.views.decorators.vary import vary_on_headers
from django.views.generic.simple import direct_to_template
-from main.models import Package, PackageFile, PackageDepend, Arch, Repo
+from main.models import Package, PackageFile, Arch, Repo
from mirrors.models import MirrorUrl
from mirrors.utils import get_mirror_url_for_download
-from ..models import (PackageRelation, PackageGroup, License,
- Conflict, Provision, Replacement)
+from ..models import PackageRelation
from ..utils import (get_group_info, get_differences_info,
- multilib_differences, get_wrong_permissions)
+ multilib_differences, get_wrong_permissions, PackageJSONEncoder)
# make other views available from this same package
from .flag import flaghelp, flag, flag_confirmed, unflag, unflag_all
@@ -26,37 +24,6 @@ from .search import search
from .signoff import signoffs, signoff_package, signoff_options, signoffs_json
-class PackageJSONEncoder(DjangoJSONEncoder):
- pkg_attributes = [ 'pkgname', 'pkgbase', 'repo', 'arch', 'pkgver',
- 'pkgrel', 'epoch', 'pkgdesc', 'url', 'filename', 'compressed_size',
- 'installed_size', 'build_date', 'last_update', 'flag_date',
- 'maintainers', 'packager' ]
- pkg_list_attributes = [ 'groups', 'licenses', 'conflicts',
- 'provides', 'replaces', 'depends' ]
-
- def default(self, obj):
- if hasattr(obj, '__iter__'):
- # mainly for queryset serialization
- return list(obj)
- if isinstance(obj, Package):
- data = dict((attr, getattr(obj, attr))
- for attr in self.pkg_attributes)
- for attr in self.pkg_list_attributes:
- data[attr] = getattr(obj, attr).all()
- return data
- if isinstance(obj, PackageFile):
- filename = obj.filename or ''
- return obj.directory + filename
- if isinstance(obj, (Repo, Arch)):
- return obj.name.lower()
- if isinstance(obj, (PackageGroup, License)):
- return obj.name
- if isinstance(obj, (Conflict, Provision, Replacement, PackageDepend)):
- return unicode(obj)
- elif isinstance(obj, User):
- return obj.username
- return super(PackageJSONEncoder, self).default(obj)
-
def opensearch(request):
if request.is_secure():
domain = "https://%s" % request.META['HTTP_HOST']