diff options
author | Luke Shumaker <LukeShu@sbcglobal.net> | 2012-12-12 20:18:51 -0500 |
---|---|---|
committer | Luke Shumaker <LukeShu@sbcglobal.net> | 2012-12-12 20:18:51 -0500 |
commit | 6c5c3355a97d35afb7f1eee284966ad0bf8cee3b (patch) | |
tree | 8c68a1c118209a7d18c60d9c3c6960a6ae051bcf /packages/utils.py | |
parent | 03558d8b474d91be6cf21310916672ed62e920b7 (diff) | |
parent | 5d9f87c02bd8ad73fdb27600e0afe71284e3082f (diff) |
Merge tag 'release_2012-04-07'
I hate writing tag messages.
Conflicts:
packages/templatetags/package_extras.py
templates/packages/flag.html
templates/packages/flaghelp.html
templates/public/download.html
templates/public/index.html
templates/public/svn.html
Diffstat (limited to 'packages/utils.py')
-rw-r--r-- | packages/utils.py | 37 |
1 files changed, 36 insertions, 1 deletions
diff --git a/packages/utils.py b/packages/utils.py index ab3c074f..55a2901a 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) @@ -426,4 +428,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: |