-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
48 additions
and
5 deletions.
There are no files selected for viewing
53 changes: 48 additions & 5 deletions
53
blockchain_integration/pi_network/PiFusion/pi_fusion_dashboard/app.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,55 @@ | ||
from flask import Flask, render_template | ||
from pi_fusion_dashboard.models import Node | ||
from flask import Flask, render_template, request, jsonify | ||
from flask_sqlalchemy import SQLAlchemy | ||
from flask_marshmallow import Marshmallow | ||
from flask_restful import Api, Resource | ||
from flask_cors import CORS | ||
from models import db, Node, Transaction | ||
from views import NodeView, TransactionView | ||
|
||
app = Flask(__name__) | ||
app.config['SQLALCHEMY_DATABASE_URI'] = 'postgresql://user:password@host:port/dbname' | ||
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False | ||
|
||
db.init_app(app) | ||
ma = Marshmallow(app) | ||
api = Api(app) | ||
cors = CORS(app, resources={r"/api/*": {"origins": "*"}}) | ||
|
||
class NodeResource(Resource): | ||
def get(self, node_id): | ||
node = Node.query.get(node_id) | ||
return node_schema.dump(node) | ||
|
||
def put(self, node_id): | ||
node = Node.query.get(node_id) | ||
node.name = request.json['name'] | ||
node.description = request.json['description'] | ||
db.session.commit() | ||
return node_schema.dump(node) | ||
|
||
class TransactionResource(Resource): | ||
def get(self, transaction_id): | ||
transaction = Transaction.query.get(transaction_id) | ||
return transaction_schema.dump(transaction) | ||
|
||
def post(self): | ||
transaction = Transaction( | ||
type=request.json['type'], | ||
amount=request.json['amount'], | ||
timestamp=request.json['timestamp'], | ||
node_id=request.json['node_id'] | ||
) | ||
db.session.add(transaction) | ||
db.session.commit() | ||
return transaction_schema.dump(transaction) | ||
|
||
api.add_resource(NodeResource, '/api/nodes/<int:node_id>') | ||
api.add_resource(TransactionResource, '/api/transactions/<int:transaction_id>') | ||
api.add_resource(TransactionResource, '/api/transactions', endpoint='transactions') | ||
|
||
@app.route('/') | ||
def index(): | ||
nodes = Node.query.all() | ||
return render_template('dashboard.html', nodes=nodes) | ||
return render_template('index.html') | ||
|
||
if __name__ == '__main__': | ||
app.run(debug=True) | ||
app.run(debug=True) |