File: versions.py

package info (click to toggle)
python-django-debug-toolbar 1%3A1.2.1-1~bpo70%2B1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy-backports
  • size: 1,628 kB
  • sloc: python: 2,789; makefile: 180; sh: 1
file content (75 lines) | stat: -rw-r--r-- 2,373 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
from __future__ import absolute_import, unicode_literals

import sys

import django
from django.conf import settings
from django.utils.importlib import import_module
from django.utils.translation import ugettext_lazy as _
try:
    from collections import OrderedDict
except ImportError:
    from django.utils.datastructures import SortedDict as OrderedDict

from debug_toolbar.panels import Panel


class VersionsPanel(Panel):
    """
    Shows versions of Python, Django, and installed apps if possible.
    """
    @property
    def nav_subtitle(self):
        return 'Django %s' % django.get_version()

    title = _("Versions")

    template = 'debug_toolbar/panels/versions.html'

    def process_response(self, request, response):
        versions = [
            ('Python', '%d.%d.%d' % sys.version_info[:3]),
            ('Django', self.get_app_version(django)),
        ]
        if django.VERSION[:2] >= (1, 7):
            versions += list(self.gen_app_versions_1_7())
        else:
            versions += list(self.gen_app_versions_1_6())
        self.record_stats({
            'versions': OrderedDict(sorted(versions, key=lambda v: v[0])),
            'paths': sys.path,
        })

    def gen_app_versions_1_7(self):
        from django.apps import apps
        for app_config in apps.get_app_configs():
            name = app_config.verbose_name
            app = app_config.module
            version = self.get_app_version(app)
            if version:
                yield name, version

    def gen_app_versions_1_6(self):
        for app in list(settings.INSTALLED_APPS):
            name = app.split('.')[-1].replace('_', ' ').capitalize()
            app = import_module(app)
            version = self.get_app_version(app)
            if version:
                yield name, version

    def get_app_version(self, app):
        if hasattr(app, 'get_version'):
            get_version = app.get_version
            if callable(get_version):
                version = get_version()
            else:
                version = get_version
        elif hasattr(app, 'VERSION'):
            version = app.VERSION
        elif hasattr(app, '__version__'):
            version = app.__version__
        else:
            return
        if isinstance(version, (list, tuple)):
            version = '.'.join(str(o) for o in version)
        return version