combo/combo/apps/momo/management/commands/update_momo_manifest.py

50 lines
2.0 KiB
Python

# combo - content management system
# Copyright (C) 2016 Entr'ouvert
#
# This program is free software: you can redistribute it and/or modify it
# under the terms of the GNU Affero General Public License as published
# by the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError
from django.db import connection
from django.test.client import RequestFactory
from django.utils import translation
from django.utils.six.moves.urllib.parse import urlparse
from combo.apps.momo.utils import generate_manifest, GenerationError, GenerationInfo
class Command(BaseCommand):
def handle(self, *args, **kwargs):
if not getattr(settings, 'ENABLE_MOMO', False):
return
tenant = connection.get_tenant()
parsed_base_url = urlparse(tenant.get_base_url())
if ':' in parsed_base_url.netloc:
server_name, server_port = parsed_base_url.netloc.split(':')
else:
server_name = parsed_base_url.netloc
server_port = '80' if parsed_base_url.scheme == 'http' else '443'
request = RequestFactory().get('/', SERVER_NAME=server_name,
SERVER_PORT=server_port)
request._get_scheme = lambda: parsed_base_url.scheme
translation.activate(settings.LANGUAGE_CODE)
try:
generate_manifest(request)
except GenerationError as e:
raise CommandError(e.message)
except GenerationInfo as e:
if int(kwargs.get('verbosity')) > 0:
print(e.message)
translation.deactivate()