Browse Source

feat: automatically import extension blueprints

fee_issues
Eneko Illarramendi 5 years ago
parent
commit
30cf2e9f99
  1. 4
      .editorconfig
  2. 37
      lnbits/__init__.py
  3. 6
      lnbits/extensions/withdraw/config.json
  4. 192
      lnbits/templates/extensions.html

4
.editorconfig

@ -6,6 +6,10 @@ end_of_line = lf
insert_final_newline = true insert_final_newline = true
trim_trailing_whitespace = true trim_trailing_whitespace = true
[*.{html,js,json}]
indent_size = 2
indent_style = space
[*.md] [*.md]
trim_trailing_whitespace = false trim_trailing_whitespace = false

37
lnbits/__init__.py

@ -1,3 +1,4 @@
import importlib
import json import json
import requests import requests
import uuid import uuid
@ -9,12 +10,17 @@ from lnurl import Lnurl, LnurlWithdrawResponse
from . import bolt11 from . import bolt11
from .core import core_app from .core import core_app
from .db import init_databases, open_db from .db import init_databases, open_db
from .extensions.withdraw import withdraw_ext
from .helpers import ExtensionManager, megajson from .helpers import ExtensionManager, megajson
from .settings import WALLET, DEFAULT_USER_WALLET_NAME, FEE_RESERVE from .settings import WALLET, DEFAULT_USER_WALLET_NAME, FEE_RESERVE
app = Flask(__name__) app = Flask(__name__)
valid_extensions = [ext for ext in ExtensionManager().extensions if ext.is_valid]
# optimization & security
# -----------------------
Talisman( Talisman(
app, app,
content_security_policy={ content_security_policy={
@ -32,20 +38,41 @@ Talisman(
}, },
) )
# blueprints / extensions
# -----------------------
app.register_blueprint(core_app)
for ext in valid_extensions:
try:
ext_module = importlib.import_module(f"lnbits.extensions.{ext.code}")
app.register_blueprint(getattr(ext_module, f"{ext.code}_ext"), url_prefix=f"/{ext.code}")
except Exception:
raise ImportError(f"Please make sure that the extension `{ext.code}` follows conventions.")
# filters # filters
app.jinja_env.globals["EXTENSIONS"] = [ext for ext in ExtensionManager().extensions if ext.is_valid] # -------
app.jinja_env.globals["EXTENSIONS"] = valid_extensions
app.jinja_env.filters["megajson"] = megajson app.jinja_env.filters["megajson"] = megajson
# blueprints
app.register_blueprint(core_app)
app.register_blueprint(withdraw_ext, url_prefix="/withdraw")
# init
# ----
@app.before_first_request @app.before_first_request
def init(): def init():
init_databases() init_databases()
# vvvvvvvvvvvvvvvvvvvvvvvvvvv
# move the rest to `core_app`
# vvvvvvvvvvvvvvvvvvvvvvvvvvv
# vvvvvvvvvvvvvvvvvvvvvvvvvvv
@app.route("/deletewallet") @app.route("/deletewallet")
def deletewallet(): def deletewallet():
user_id = request.args.get("usr") user_id = request.args.get("usr")

6
lnbits/extensions/withdraw/config.json

@ -1,5 +1,5 @@
{ {
"name": "LNURLw", "name": "LNURLw",
"short_description": "Make LNURL withdraw links.", "short_description": "Make LNURL withdraw links.",
"ion_icon": "beer" "ion_icon": "beer"
} }

192
lnbits/templates/extensions.html

@ -4,115 +4,111 @@
{% block messages %} {% block messages %}
<a href="#" class="dropdown-toggle" data-toggle="dropdown"> <a href="#" class="dropdown-toggle" data-toggle="dropdown">
<i class="fa fa-bell-o"></i> <i class="fa fa-bell-o"></i>
<span class="label label-danger">!</span> <span class="label label-danger">!</span>
</a> </a>
<ul class="dropdown-menu"> <ul class="dropdown-menu">
<li class="header"><b>Instant wallet, bookmark to save</b></li> <li class="header"><b>Instant wallet, bookmark to save</b></li>
<li></li> <li></li>
</ul> </ul>
{% endblock %} {% endblock %}
{% block menuitems %} {% block menuitems %}
<li class="treeview"> <li class="treeview">
<a href="#"> <a href="#">
<i class="fa fa-bitcoin"></i> <span>Wallets</span> <i class="fa fa-bitcoin"></i> <span>Wallets</span>
<i class="fa fa-angle-left pull-right"></i> <i class="fa fa-angle-left pull-right"></i>
</a> </a>
<ul class="treeview-menu"> <ul class="treeview-menu">
{% for w in user_wallets %} {% for w in user_wallets %}
<li> <li>
<a href="{{ url_for('wallet') }}?wal={{ w.id }}&usr={{ w.user }}" <a href="{{ url_for('wallet') }}?wal={{ w.id }}&usr={{ w.user }}"><i class="fa fa-bolt"></i> {{ w.name }}</a>
><i class="fa fa-bolt"></i> {{ w.name }}</a </li>
> {% endfor %}
</li> <li><a onclick="sidebarmake()">Add a wallet +</a></li>
{% endfor %} <div id="sidebarmake"></div>
<li><a onclick="sidebarmake()">Add a wallet +</a></li> </ul>
<div id="sidebarmake"></div> </li>
</ul> <li class="active treeview">
</li> <a href="#">
<li class="active treeview"> <i class="fa fa-th"></i> <span>Extensions</span>
<a href="#"> <i class="fa fa-angle-left pull-right"></i>
<i class="fa fa-th"></i> <span>Extensions</span> </a>
<i class="fa fa-angle-left pull-right"></i> <ul class="treeview-menu">
</a> {% for extension in EXTENSIONS %}
<ul class="treeview-menu"> {% if extension.code in user_ext %}
{% for extension in EXTENSIONS %} <li>
{% if extension.code in user_ext %} <a href="{{ url_for(extension.code + '.index') }}?usr={{ user }}"><i class="fa fa-plus"></i> {{ extension.name }}</a>
<li> </li>
<a href="{{ url_for(extension.code + '.index') }}?usr={{ user }}"><i class="fa fa-plus"></i> {{ extension.name }}</a> {% endif %}
</li> {% endfor %}
{% endif %} <li>
{% endfor %} <a href="{{ url_for('extensions') }}?usr={{ user }}">Manager</a></li>
<li> </ul>
<a href="{{ url_for('extensions') }}?usr={{ user }}">Manager </a></li> </li>
</ul>
</li>
{% endblock %} {% endblock %}
{% block body %} {% block body %}
<!-- Right side column. Contains the navbar and content of the page --> <!-- Right side column. Contains the navbar and content of the page -->
<div class="content-wrapper"> <div class="content-wrapper">
<!-- Content Header (Page header) --> <!-- Content Header (Page header) -->
<section class="content-header"> <section class="content-header">
<div id="wonga"></div> <div id="wonga"></div>
<h1>Wallet <small>Control panel</small></h1> <h1>Wallet <small>Control panel</small></h1>
<ol class="breadcrumb"> <ol class="breadcrumb">
<li> <li><a href="#"><i class="fa fa-dashboard"></i> Home</a></li>
<a href="#"><i class="fa fa-dashboard"></i> Home</a> <li class="active">Extensions</li>
</li> </ol>
<li class="active">Extensions</li>
</ol>
<br /> <br />
<br /> <br />
<div class="alert alert-danger alert-dismissable"> <div class="alert alert-danger alert-dismissable">
<h4>Bookmark to save your wallet. Wallet is in BETA, use with caution.</h4> <h4>Bookmark to save your wallet. Wallet is in BETA, use with caution.</h4>
</div> </div>
</section> </section>
<!-- Main content --> <!-- Main content -->
<section class="content"> <section class="content">
<!-- Small boxes (Stat box) --> <!-- Small boxes (Stat box) -->
<div class="row"> <div class="row">
{% for extension in EXTENSIONS %} {% for extension in EXTENSIONS %}
<div class="col-lg-3 col-xs-6"> <div class="col-lg-3 col-xs-6">
<!-- small box --> <!-- small box -->
<div class="small-box bg-blue"> <div class="small-box bg-blue">
<div class="inner"> <div class="inner">
{% if extension.code in user_ext %} {% if extension.code in user_ext %}
<a href="{{ url_for(extension.code + '.index') }}?usr={{ user }}" style="color: inherit"> <a href="{{ url_for(extension.code + '.index') }}?usr={{ user }}" style="color: inherit">
{% endif %} {% endif %}
<h3>{{ extension.name }}</h3> <h3>{{ extension.name }}</h3>
<p>{{ extension.short_description }}</p> <p>{{ extension.short_description }}</p>
{% if extension.code in user_ext %} {% if extension.code in user_ext %}
</a> </a>
{% endif %} {% endif %}
</div> </div>
<div class="icon"> <div class="icon">
<i class="ion ion-{{ extension.ion_icon }}"></i> <i class="ion ion-{{ extension.ion_icon }}"></i>
</div> </div>
{% if extension.code in user_ext %} {% if extension.code in user_ext %}
<a href="{{ url_for('extensions') }}?usr={{user}}&disable={{ extension.code }}" class="small-box-footer">Disable <i class="fa fa-arrow-circle-right"></i></a> <a href="{{ url_for('extensions') }}?usr={{user}}&disable={{ extension.code }}" class="small-box-footer">Disable <i class="fa fa-arrow-circle-right"></i></a>
{% else %} {% else %}
<a href="{{ url_for('extensions') }}?usr={{user}}&enable={{ extension.code }}" class="small-box-footer">Enable <i class="fa fa-arrow-circle-right"></i></a> <a href="{{ url_for('extensions') }}?usr={{user}}&enable={{ extension.code }}" class="small-box-footer">Enable <i class="fa fa-arrow-circle-right"></i></a>
{% endif %} {% endif %}
</div> </div>
</div> </div>
{% endfor %} {% endfor %}
</div> </div>
<!-- /.content --> <!-- /.content -->
</section> </section>
</div> </div>
<script> <script>
window.user = {{ user | megajson | safe }} window.user = {{ user | megajson | safe }}
window.user_wallets = {{ user_wallets | megajson | safe }} window.user_wallets = {{ user_wallets | megajson | safe }}
window.user_ext = {{ user_ext | megajson | safe }} window.user_ext = {{ user_ext | megajson | safe }}
</script> </script>
{% endblock %} {% endblock %}

Loading…
Cancel
Save