changed the main script

This commit is contained in:
Oskar Hahn 2012-08-14 14:19:59 +02:00
parent a2be848932
commit b7a0e83c68
5 changed files with 112 additions and 120 deletions

View File

@ -21,13 +21,9 @@ import time
import threading import threading
import base64 import base64
import webbrowser import webbrowser
from contextlib import nested
import django.conf import django.conf
from django.core.management import execute_from_command_line from django.core.management import execute_from_command_line
from django.utils.crypto import get_random_string
import openslides
CONFIG_TEMPLATE = """ CONFIG_TEMPLATE = """
from openslides.openslides_settings import * from openslides.openslides_settings import *
@ -66,6 +62,7 @@ INSTALLED_APPS += INSTALLED_PLUGINS
KEY_LENGTH = 30 KEY_LENGTH = 30
def main(argv=None): def main(argv=None):
if argv is None: if argv is None:
argv = sys.argv[1:] argv = sys.argv[1:]
@ -74,58 +71,76 @@ def main(argv=None):
description="Run openslides using django's builtin webserver") description="Run openslides using django's builtin webserver")
parser.add_option("-a", "--address", help="IP Address to listen on") parser.add_option("-a", "--address", help="IP Address to listen on")
parser.add_option("-p", "--port", type="int", help="Port to listen on") parser.add_option("-p", "--port", type="int", help="Port to listen on")
parser.add_option("--nothread", action="store_true", parser.add_option(
help="Do not use threading") "--syncdb", action="store_true",
parser.add_option("--syncdb", action="store_true",
help="Update/create database before starting the server") help="Update/create database before starting the server")
parser.add_option("--reset-admin", action="store_true", parser.add_option(
"--reset-admin", action="store_true",
help="Make sure the user 'admin' exists and uses 'admin' as password") help="Make sure the user 'admin' exists and uses 'admin' as password")
parser.add_option("-s", "--settings", help="Path to the setting.")
parser.add_option(
"--no-reload", action="store_true", help="Do not reload the development server")
opts, args = parser.parse_args(argv) opts, args = parser.parse_args(argv)
if not args: if args:
main(argv + ['init', 'openslides']) sys.stderr.write("This command does not take arguments!\n\n")
main(argv + ['start', 'openslides']) parser.print_help()
return 0 sys.exit(1)
command = args[0] # Find the path to the settings
settings = opts.settings or \
os.path.expanduser('~/.openslides/openslidessettings.py')
# Create settings if necessary
if not os.path.exists(settings):
create_settings(settings)
# Set the django environment to the settings
setup_django_environment(settings)
# Find url to openslides
addr, port = detect_listen_opts(opts.address, opts.port) addr, port = detect_listen_opts(opts.address, opts.port)
if port == 80: if port == 80:
url = "http://%s" % (addr, ) url = "http://%s" % addr
else: else:
url = "http://%s:%d" % (addr, port) url = "http://%s:%d" % (addr, port)
try: # Create Database if necessary
environment_name = args[1] if not database_exists() or opts.syncdb:
except IndexError: run_syncdb()
environment_name = None set_system_url(url)
create_or_reset_admin_user()
if command == 'init': # Reset Admin
if check_environment(environment_name): elif opts.reset_admin:
print "'%s' is allready an environment" % environment_name create_or_reset_admin_user()
return 1
create_environment(environment_name or 'openslides', url)
elif command == 'start': # Start OpenSlides
set_settings_environment(environment_name or os.getcwd()) if opts.no_reload:
if not check_environment(): extra_args = ['--noreload']
print "'%s' is not a valid OpenSlides environment." % environment_name else:
sys.exit(1) extra_args = []
# NOTE: --insecure is needed so static files will be served if start_openslides(addr, port, start_browser_url=url, extra_args=extra_args)
# DEBUG is set to False
argv = ["", "runserver", "--noreload", "--insecure"]
if opts.nothread:
argv.append("--nothread")
argv.append("%s:%d" % (addr, port))
start_browser(url)
execute_from_command_line(argv)
def set_settings_environment(environment): def create_settings(settings):
sys.path.append(environment) path_to_dir = os.path.dirname(settings)
os.environ[django.conf.ENVIRONMENT_VARIABLE] = 'settings'
setting_content = CONFIG_TEMPLATE % dict(
default_key=base64.b64encode(os.urandom(KEY_LENGTH)),
dbpath=os.path.join(path_to_dir, 'database.db'))
if not os.path.exists(path_to_dir):
os.makedirs(path_to_dir)
with open(settings, 'w') as settings_file:
settings_file.write(setting_content)
def setup_django_environment(settings):
sys.path.append(os.path.dirname(settings))
setting_module = os.path.basename(settings)[:-3]
os.environ[django.conf.ENVIRONMENT_VARIABLE] = setting_module
def detect_listen_opts(address, port): def detect_listen_opts(address, port):
@ -150,91 +165,29 @@ def detect_listen_opts(address, port):
return address, port return address, port
def start_browser(url): def database_exists():
browser = webbrowser.get() """Detect if database exists"""
def f():
time.sleep(1)
browser.open(url)
t = threading.Thread(target = f)
t.start()
def create_environment(environment, url=None):
setting_content = CONFIG_TEMPLATE % dict(
default_key=base64.b64encode(os.urandom(KEY_LENGTH)),
dbpath=os.path.join(os.path.abspath(environment), 'database.db'))
dirname = environment
if dirname and not os.path.exists(dirname):
os.makedirs(dirname)
settings = os.path.join(environment, 'settings.py')
if not os.path.exists(settings):
with open(settings, 'w') as fp:
fp.write(setting_content)
set_settings_environment(environment)
run_syncdb(url)
create_or_reset_admin_user()
def run_syncdb(url=None):
# now initialize the database
argv = ["", "syncdb", "--noinput"]
execute_from_command_line(argv)
if url is not None:
set_system_url(url)
def check_environment(environment=None):
if environment is not None:
set_settings_environment(environment)
try:
import settings
except ImportError:
return False
if not check_database():
return False
return True
def check_database():
"""Detect if database was deleted and recreate if necessary"""
# can't be imported in global scope as they already require # can't be imported in global scope as they already require
# the settings module during import # the settings module during import
from django.db import DatabaseError from django.db import DatabaseError
from django.contrib.auth.models import User from django.core.exceptions import ImproperlyConfigured
from openslides.participant.models import User
try: try:
User.objects.count() User.objects.count()
except DatabaseError: except DatabaseError:
return False return False
except ImproperlyConfigured:
print "Your settings file seems broken"
sys.exit(0)
else: else:
return True return True
def create_or_reset_admin_user(): def run_syncdb():
# can't be imported in global scope as it already requires # now initialize the database
# the settings module during import argv = ["", "syncdb", "--noinput"]
from django.contrib.auth.models import User execute_from_command_line(argv)
from openslides.config.models import config
try:
obj = User.objects.get(username = "admin")
except User.DoesNotExist:
User.objects.create_superuser(
username="admin",
password="admin",
email="admin@example.com")
config['admin_password'] = "admin"
print("Created default admin user")
return
print("Password for user admin was reset to 'admin'")
obj.set_password("admin")
obj.save()
def set_system_url(url): def set_system_url(url):
@ -248,5 +201,45 @@ def set_system_url(url):
config[key] = url config[key] = url
def create_or_reset_admin_user():
# can't be imported in global scope as it already requires
# the settings module during import
from openslides.participant.models import User
try:
admin = User.objects.get(username="admin")
print("Password for user admin was reset to 'admin'")
except User.DoesNotExist:
admin = User()
admin.username = 'admin'
admin.last_name = 'Admin User'
print("Created default admin user")
admin.is_superuser = True
admin.default_password = 'admin'
admin.set_password(admin.default_password)
admin.save()
def start_openslides(addr, port, start_browser_url=None, extra_args=[]):
argv = ["", "runserver", '--noreload'] + extra_args
argv.append("%s:%d" % (addr, port))
if start_browser_url:
start_browser(start_browser_url)
execute_from_command_line(argv)
def start_browser(url):
browser = webbrowser.get()
def f():
time.sleep(1)
browser.open(url)
t = threading.Thread(target=f)
t.start()
if __name__ == "__main__": if __name__ == "__main__":
main() main()

View File

@ -47,7 +47,7 @@ USE_I18N = True
USE_L10N = True USE_L10N = True
LOCALE_PATHS = ( LOCALE_PATHS = (
_fs2unicode(os.path.join(SITE_ROOT, 'locale')) _fs2unicode(os.path.join(SITE_ROOT, 'locale')),
) )
# Absolute path to the directory that holds media. # Absolute path to the directory that holds media.

View File

@ -169,7 +169,6 @@ def default_config(sender, key, **kwargs):
return { return {
'participant_pdf_system_url': 'http://example.com:8000', 'participant_pdf_system_url': 'http://example.com:8000',
'participant_pdf_welcometext': _('Welcome to OpenSlides!'), 'participant_pdf_welcometext': _('Welcome to OpenSlides!'),
'admin_password': None,
}.get(key) }.get(key)

View File

@ -428,7 +428,7 @@ def login(request):
extra_content = {} extra_content = {}
try: try:
admin = User.objects.get(pk=1) admin = User.objects.get(pk=1)
if admin.check_password(config['admin_password']): if admin.check_password(admin.default_password):
extra_content['first_time_message'] = _( extra_content['first_time_message'] = _(
"Installation was successfully! Use %(user)s " "Installation was successfully! Use %(user)s "
"(password: %(password)s) for first login.<br>" "(password: %(password)s) for first login.<br>"
@ -436,7 +436,7 @@ def login(request):
"first login! Otherwise this message still appears for " "first login! Otherwise this message still appears for "
"everyone and could be a security risk.") % { "everyone and could be a security risk.") % {
'user': html_strong(admin.username), 'user': html_strong(admin.username),
'password': html_strong(config['admin_password'])} 'password': html_strong(admin.default_password)}
extra_content['next'] = reverse('password_change') extra_content['next'] = reverse('password_change')
except User.DoesNotExist: except User.DoesNotExist:
pass pass

View File

@ -7,7 +7,7 @@
:license: GNU GPL, see LICENSE for more details. :license: GNU GPL, see LICENSE for more details.
""" """
import openslides.main from openslides.main import main
if __name__ == "__main__": if __name__ == "__main__":
openslides.main.main() main()