from __future__ import absolute_import, unicode_literals

import sys

import django
from django.apps import apps
from django.utils.translation import ugettext_lazy as _

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 generate_stats(self, request, response):
        versions = [
            ('Python', '', '%d.%d.%d' % sys.version_info[:3]),
            ('Django', '', self.get_app_version(django)),
        ]
        versions += list(self.gen_app_versions())
        self.record_stats({
            'versions': sorted(versions, key=lambda v: v[0]),
            'paths': sys.path,
        })

    def gen_app_versions(self):
        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 app.__name__, 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

debug_toolbar/panels/versions.py
from __future__ import absolute_import, unicode_literals

import sys

import django
from django.apps import apps
from django.utils.translation import ugettext_lazy as _

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 generate_stats(self, request, response):
        versions = [
            ('Python', '', '%d.%d.%d' % sys.version_info[:3]),
            ('Django', '', self.get_app_version(django)),
        ]
        versions += list(self.gen_app_versions())
        self.record_stats({
            'versions': sorted(versions, key=lambda v: v[0]),
            'paths': sys.path,
        })

    def gen_app_versions(self):
        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 app.__name__, 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)):
            # We strip dots from the right because we do not want to show
            # trailing dots if there are empty elements in the list/tuple
            version = '.'.join(str(o) for o in version).rstrip('.')
        return version

debug_toolbar/panels/versions.py
diff --git a/debug_toolbar/panels/versions.py b/debug_toolbar/panels/versions.py
index 26650d716..079a9d194 100644
--- a/debug_toolbar/panels/versions.py
+++ b/debug_toolbar/panels/versions.py
@@ -54,5 +54,7 @@ def get_app_version(self, app):
         else:
             return
         if isinstance(version, (list, tuple)):
-            version = '.'.join(str(o) for o in version)
+            # We strip dots from the right because we do not want to show
+            # trailing dots if there are empty elements in the list/tuple
+            version = '.'.join(str(o) for o in version).rstrip('.')
         return version
diff --git a/tests/panels/test_versions.py b/tests/panels/test_versions.py
new file mode 100644
index 000000000..0ebd0ab08
--- /dev/null
+++ b/tests/panels/test_versions.py
@@ -0,0 +1,40 @@
+# coding: utf-8
+
+from __future__ import absolute_import, unicode_literals
+from collections import namedtuple
+
+from ..base import BaseTestCase
+
+
+version_info_t = namedtuple('version_info_t', (
+    'major', 'minor', 'micro', 'releaselevel', 'serial',
+))
+
+
+class VersionsPanelTestCase(BaseTestCase):
+
+    def setUp(self):
+        super(VersionsPanelTestCase, self).setUp()
+        self.panel = self.toolbar.get_panel_by_id('VersionsPanel')
+
+    def test_app_version_from_get_version_fn(self):
+
+        class FakeApp:
+            def get_version(self):
+                return version_info_t(1, 2, 3, '', '')
+
+        self.assertEqual(self.panel.get_app_version(FakeApp()), '1.2.3')
+
+    def test_app_version_from_VERSION(self):
+
+        class FakeApp:
+            VERSION = version_info_t(1, 2, 3, '', '')
+
+        self.assertEqual(self.panel.get_app_version(FakeApp()), '1.2.3')
+
+    def test_app_version_from_underscode_version(self):
+
+        class FakeApp:
+            __version__ = version_info_t(1, 2, 3, '', '')
+
+        self.assertEqual(self.panel.get_app_version(FakeApp()), '1.2.3')

Incorrectly parse versions of some apps
I saw that for `django-celery-beat` and `django-celery-results` it shows extra dots at the end of the version:
```
django_celery_beat	Django_Celery_Beat	1.0.1..
django_celery_results	Django_Celery_Results	1.0.1..
```

This is because both apps use custom `namedtuple`:
```
version_info_t = namedtuple('version_info_t', (
    'major', 'minor', 'micro', 'releaselevel', 'serial',
))
```

When `releaselevel` and `serial` are not set they are empty strings. This will lead to joining dots in the string representation.

I'm preparing a fix and will create a PR.
