Exemplo n.º 1
0
def application(request):
  response = None

  try:
    rollback = True

    init_request(request)

    frappe.recorder.record()

    if should_redirect_http() and not hasattr(application, 'config'):
      response = Response(status=302, headers={
          "Location": "https://{}{}".format(frappe.request.host, frappe.request.path)})

    elif frappe.local.form_dict.cmd:
      response = frappe.handler.handle()

    elif frappe.request.path.startswith("/api/"):
      response = frappe.api.handle()

    elif frappe.request.path.startswith('/backups'):
      response = frappe.utils.response.download_backup(request.path)

    elif frappe.request.path.startswith('/private/files/'):
      from frappe.api import validate_auth
      validate_auth()
      response = frappe.utils.response.download_private_file(request.path)

    elif frappe.local.request.method in ('GET', 'HEAD', 'POST'):
      response = frappe.website.render.render()

    else:
      raise NotFound

  except HTTPException as e:
    return e

  except frappe.SessionStopped as e:
    response = frappe.utils.response.handle_session_stopped()

  except Exception as e:
    response = handle_exception(e)

  else:
    rollback = after_request(rollback)

  finally:
    if frappe.local.request.method in ("POST", "PUT") and frappe.db and rollback:
      frappe.db.rollback()

    # set cookies
    if response and hasattr(frappe.local, 'cookie_manager'):
      frappe.local.cookie_manager.flush_cookies(response=response)

    frappe.recorder.dump()
    log_request(response)

    frappe.destroy()

  return response
Exemplo n.º 2
0
def application(request):
    response = None
    log_info = _dict()
    log_info.request_start = datetime.utcnow()
    try:
        rollback = True

        frappe.local.self_worker_compatible = True
        init_request(request)

        log_info.request_start = frappe.utils.convert_utc_to_user_timezone(
            log_info.request_start).replace(tzinfo=None)

        if frappe.local.form_dict.cmd:
            response = frappe.handler.handle()

        elif frappe.request.path.startswith("/api/"):
            if frappe.local.form_dict.data is None:
                frappe.local.form_dict.data = request.get_data(as_text=True)
            response = frappe.api.handle()

        elif frappe.request.path.startswith('/backups'):
            response = frappe.utils.response.download_backup(request.path)

        elif frappe.request.path.startswith('/private/files/'):
            response = frappe.utils.response.download_private_file(
                request.path)

        elif frappe.local.request.method in ('GET', 'HEAD', 'POST'):
            response = frappe.website.render.render()

        else:
            raise NotFound

    except HTTPException as e:
        return e

    except frappe.SessionStopped as e:
        response = frappe.utils.response.handle_session_stopped()

    except Exception as e:
        response = handle_exception(e)

    else:
        rollback = after_request(rollback)

    finally:
        if frappe.local.request.method in ("POST",
                                           "PUT") and frappe.db and rollback:
            frappe.db.rollback()

        # set cookies
        if response and hasattr(frappe.local, 'cookie_manager'):
            frappe.local.cookie_manager.flush_cookies(response=response)

        log_info.request_end = frappe.utils.now_datetime()
        log_info.turnaround_time = (log_info.request_end -
                                    log_info.request_start).total_seconds()
        log_info.cmd = frappe.local.form_dict.cmd
        log_info.type = 'request_log'
        frappe.logger().debug(log_info)
        frappe.destroy()

    return response