Skip to content

Commit

Permalink
Update app.py
Browse files Browse the repository at this point in the history
  • Loading branch information
KOSASIH authored Aug 10, 2024
1 parent 73d2b41 commit 2a00663
Showing 1 changed file with 48 additions and 5 deletions.
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)

0 comments on commit 2a00663

Please sign in to comment.