Skip to content
Snippets Groups Projects
Verified Commit 2c1009a7 authored by Nik | Klampfradler's avatar Nik | Klampfradler
Browse files

Rework `get_version` to not rely on import

What did I drink when I wrote the original code? The relative import
`from .. import __version__` in the utility code in core, of course,
always imported from `aleksis.core`, no matter the real `AppConfig`
that inherited the utility code. Thus, the versions always showed the
version of AlekSIS-Core everywhere (whose version information was
broken ever since we renamed AlekSIS to AlekSIS-Core).

This change decouples the `get_version` code from the package level
`__version__` information, which effectively becomes optional now.
parent 0ead6f79
No related branches found
No related tags found
1 merge request!487Resolve "[About] Shows versions as "unknown""
Pipeline #5835 passed
...@@ -21,6 +21,7 @@ from .util.sass_helpers import clean_scss ...@@ -21,6 +21,7 @@ from .util.sass_helpers import clean_scss
class CoreConfig(AppConfig): class CoreConfig(AppConfig):
name = "aleksis.core" name = "aleksis.core"
verbose_name = "AlekSIS — The Free School Information System" verbose_name = "AlekSIS — The Free School Information System"
dist_name = "AlekSIS-Core"
urls = { urls = {
"Repository": "https://edugit.org/AlekSIS/official/AlekSIS/", "Repository": "https://edugit.org/AlekSIS/official/AlekSIS/",
......
...@@ -5,6 +5,7 @@ from django.contrib.auth.signals import user_logged_in, user_logged_out ...@@ -5,6 +5,7 @@ from django.contrib.auth.signals import user_logged_in, user_logged_out
from django.db.models.signals import post_migrate, pre_migrate from django.db.models.signals import post_migrate, pre_migrate
from django.http import HttpRequest from django.http import HttpRequest
import pkg_resources
from dynamic_preferences.signals import preference_updated from dynamic_preferences.signals import preference_updated
from license_expression import Licensing from license_expression import Licensing
from spdx_license_list import LICENSES from spdx_license_list import LICENSES
...@@ -28,21 +29,46 @@ class AppConfig(django.apps.AppConfig): ...@@ -28,21 +29,46 @@ class AppConfig(django.apps.AppConfig):
# Getting an app ready means it should look at its config once # Getting an app ready means it should look at its config once
self.preference_updated(self) self.preference_updated(self)
@classmethod def get_distribution_name(self):
def get_name(cls): """Get pkg_resources distribution name of application package."""
if hasattr(self, "dist_name"):
return self.dist_name
elif self.name.lower().startswith("aleksis.apps."):
return self.name.lower().replace("aleksis.apps.", "AlekSIS-App-")
return None
def get_distribution(self):
"""Get pkg_resources distribution of application package."""
dist_name = self.get_distribution_name()
if dist_name:
try:
dist = pkg_resources.get_distribution(dist_name)
except pkg_resources.DistributionNotFound:
return None
return dist
def get_name(self):
"""Get name of application package.""" """Get name of application package."""
return getattr(cls, "verbose_name", cls.name) if hasattr(self, "verbose_name"):
# TODO Try getting from distribution if not set return self.verbose_name
else:
dist_name = self.get_distribution_name()
if dist_name:
return dist_name
return self.name
@classmethod def get_version(self):
def get_version(cls):
"""Get version of application package.""" """Get version of application package."""
try: if hasattr(self, "version"):
from .. import __version__ # noqa return self.version
except ImportError: else:
__version__ = None dist = self.get_distribution()
if dist:
return getattr(cls, "version", __version__) return dist.version
else:
return "unknown"
@classmethod @classmethod
def get_licence(cls) -> Tuple: def get_licence(cls) -> Tuple:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment