mirror of
https://github.com/jwinterm/modoboa.git
synced 2026-10-18 05:51:26 +02:00
Load extension list from the API.
This commit is contained in:
@@ -21,6 +21,7 @@ from django.template import Context, Template
|
||||
import dj_database_url
|
||||
|
||||
from modoboa.core.commands import Command
|
||||
from modoboa.lib.api_client import ModoAPIClient
|
||||
|
||||
DBCONN_TPL = """
|
||||
'{{ conn_name }}': {
|
||||
@@ -150,12 +151,14 @@ class DeployCommand(Command):
|
||||
|
||||
"""
|
||||
if "all" in extensions:
|
||||
# Retrieve official extension list from the API
|
||||
return
|
||||
official_exts = ModoAPIClient().list_extensions()
|
||||
extensions = [extension["name"] for extension in official_exts]
|
||||
|
||||
pip_args = ["install"] + extensions
|
||||
pip.main(pip_args)
|
||||
extra_settings = []
|
||||
for extension in extensions:
|
||||
extension = extension.replace("-", "_")
|
||||
module = __import__(extension, locals(), globals(), [])
|
||||
basedir = os.path.dirname(module.__file__)
|
||||
if not os.path.exists("{0}/settings.py".format(basedir)):
|
||||
|
||||
+3
-30
@@ -1,40 +1,13 @@
|
||||
"""
|
||||
Utility functions.
|
||||
"""
|
||||
import os
|
||||
import pkg_resources
|
||||
|
||||
import requests
|
||||
from requests.exceptions import RequestException
|
||||
|
||||
from django.conf import settings
|
||||
"""Utility functions."""
|
||||
|
||||
from django.contrib.sites.models import get_current_site
|
||||
|
||||
from versionfield.constants import DEFAULT_NUMBER_BITS
|
||||
from versionfield.version import Version
|
||||
|
||||
from modoboa.lib import parameters
|
||||
from modoboa.lib.api_client import ModoAPIClient
|
||||
|
||||
|
||||
def new_version_available(request):
|
||||
"""Check if a new version of Modoboa is available."""
|
||||
if parameters.get_admin("CHECK_NEW_VERSIONS") == "no":
|
||||
return None
|
||||
local_version = pkg_resources.get_distribution("modoboa").version
|
||||
url = os.path.join(settings.MODOBOA_API_URL, "current_version/")
|
||||
site = get_current_site(request)
|
||||
try:
|
||||
resp = requests.get(url, params={
|
||||
"client_version": pkg_resources.get_distribution("modoboa").version,
|
||||
"client_site": site
|
||||
})
|
||||
except RequestException:
|
||||
return None
|
||||
if resp.status_code != 200:
|
||||
return None
|
||||
resp = resp.json()
|
||||
version = Version(resp["version"], DEFAULT_NUMBER_BITS)
|
||||
if version <= Version(local_version, DEFAULT_NUMBER_BITS):
|
||||
return None
|
||||
return resp
|
||||
return ModoAPIClient().current_version(get_current_site(request))
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
"""A client for Modoboa's public API."""
|
||||
|
||||
import os
|
||||
import pkg_resources
|
||||
|
||||
import requests
|
||||
from requests.exceptions import RequestException
|
||||
|
||||
from django.conf import settings
|
||||
|
||||
from versionfield.constants import DEFAULT_NUMBER_BITS
|
||||
from versionfield.version import Version
|
||||
|
||||
|
||||
class ModoAPIClient(object):
|
||||
|
||||
"""A simple client for the public API."""
|
||||
|
||||
def __init__(self, api_url=None):
|
||||
"""Constructor."""
|
||||
self._api_url = (
|
||||
settings.MODOBOA_API_URL if api_url is None
|
||||
else api_url
|
||||
)
|
||||
|
||||
def __send_request(self, url, params=None):
|
||||
"""Send a request to the API."""
|
||||
if params is None:
|
||||
params = {}
|
||||
try:
|
||||
resp = requests.get(url, params=params)
|
||||
except RequestException:
|
||||
return None
|
||||
if resp.status_code != 200:
|
||||
return None
|
||||
return resp.json()
|
||||
|
||||
def current_version(self, site):
|
||||
"""Check the latest version."""
|
||||
client_version = pkg_resources.get_distribution("modoboa").version
|
||||
url = os.path.join(self._api_url, "current_version/")
|
||||
params = {"client_version": client_version, "site": site}
|
||||
resp = self.__send_request(url, params)
|
||||
if resp is None:
|
||||
return None
|
||||
version = Version(resp["version"], DEFAULT_NUMBER_BITS)
|
||||
if version <= Version(client_version, DEFAULT_NUMBER_BITS):
|
||||
return None
|
||||
return resp
|
||||
|
||||
def list_extensions(self):
|
||||
"""List all official extensions."""
|
||||
url = os.path.join(self._api_url, "extensions/")
|
||||
return self.__send_request(url)
|
||||
Reference in New Issue
Block a user