Try ERPNext Buy Support Partners Foundation

Create new site from python controller

I want to create a new site based on some conditions received in custom form.
I have checked in frappe we have site.py file that contains two method

new_site() with click decorator
and
__new_site() without other things

I tried to pass all desired parameters to both function and ended up with Internal server error.

On checking web.error.log I found error is there while creating database and it throws exception request not found.

Error handling request /
Traceback (most recent call last):
  File "/home/frappe/frappe-bench/apps/frappe/frappe/app.py", line 57, in application
    response = frappe.handler.handle()
  File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 22, in handle
    data = execute_cmd(cmd)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/handler.py", line 61, in execute_cmd
    return frappe.call(method, **frappe.form_dict)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 1042, in call
    return fn(*args, **newargs)
  File "/home/frappe/frappe-bench/apps/helpdesk/helpdesk/helpdesk/doctype/organisation/organisation.py", line 50, in sign_up
    ,db_type=None)
  File "/home/frappe/frappe-bench/apps/helpdesk/helpdesk/helpdesk/doctype/organisation/organisation.py", line 178, in create_new_site
    source_sql=source_sql, force=force, reinstall=reinstall, db_type=db_type)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/installer.py", line 34, in install_db
    setup_database(force, source_sql, verbose)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/database/__init__.py", line 16, in setup_database
    return frappe.database.mariadb.setup_db.setup_database(force, source_sql, verbose)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/database/mariadb/setup_db.py", line 43, in setup_database
 return frappe.database.mariadb.setup_db.setup_database(force, source_sql, verbose)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/database/mariadb/setup_db.py", line 43, in setup_database
    raise Exception("Database %s already exists" % (db_name,))
Exception: Database _875a8f2f42c570f5 already exists

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/werkzeug/local.py", line 72, in __getattr__
    return self.__storage__[self.__ident_func__()][name]
KeyError: 139655432140608

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/werkzeug/local.py", line 309, in _get_current_object
    return getattr(self.__local, self.__name__)
  File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/werkzeug/local.py", line 74, in __getattr__
    raise AttributeError(name)
AttributeError: request

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/frappe/frappe-bench/apps/frappe/frappe/app.py", line 81, in application
    response = handle_exception(e)
  File "/home/frappe/frappe-bench/apps/frappe/frappe/app.py", line 143, in handle_exception
    if frappe.get_request_header('Accept') and (frappe.local.is_ajax or 'application/json' in frappe.get_request_header('Accept')):
  File "/home/frappe/frappe-bench/apps/frappe/frappe/__init__.py", line 420, in get_request_header
    return request.headers.get(key, default)
  File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/werkzeug/local.py", line 348, in __getattr__
    return getattr(self._get_current_object(), name)
  File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/werkzeug/local.py", line 311, in _get_current_object
    raise RuntimeError("no object bound to %s" % self.__name__)
RuntimeError: no object bound to request

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/werkzeug/local.py", line 72, in __getattr__
    return self.__storage__[self.__ident_func__()][name]
KeyError: 139655432140608

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/gunicorn/workers/sync.py", line 134, in handle
    self.handle_request(listener, req, client, addr)
  File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/gunicorn/workers/sync.py", line 175, in handle_request
    respiter = self.wsgi(environ, resp.start_response)
  File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/werkzeug/local.py", line 231, in application
    return ClosingIterator(app(environ, start_response), self.cleanup)
  File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/werkzeug/wrappers/base_request.py", line 237, in application
    resp = f(*args[:-2] + (request,))
  File "/home/frappe/frappe-bench/apps/frappe/frappe/app.py", line 87, in application
    if frappe.local.request.method in ("POST", "PUT") and frappe.db and rollback:
  File "/home/frappe/frappe-bench/env/lib/python3.6/site-packages/werkzeug/local.py", line 74, in __getattr__
    raise AttributeError(name)
AttributeError: request

Please suggest, How I can Create new site without bench command.