ktongue's picture
download
raw
3.07 kB
from flask import Flask, request, jsonify, send_from_directory
from flask_cors import CORS
from models import db, Notebook, Section, Page
from datetime import datetime
import os
app = Flask(__name__, static_folder='../static')
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///onenote.db'
CORS(app)
db.init_app(app)
@app.route('/')
def index():
return send_from_directory(app.static_folder, 'index.html')
@app.route('/<path:path>')
def serve_static(path):
return send_from_directory(app.static_folder, path)
@app.route('/api/notebooks', methods=['GET', 'POST'])
def notebooks():
if request.method == 'POST':
data = request.json
nb = Notebook(name=data['name'], color=data.get('color', '#7719AA'))
db.session.add(nb)
db.session.commit()
return jsonify({'id': nb.id, 'name': nb.name})
notebooks = Notebook.query.all()
return jsonify([{'id': n.id, 'name': n.name, 'color': n.color} for n in notebooks])
@app.route('/api/notebooks/<int:nb_id>/sections', methods=['GET', 'POST'])
def sections(nb_id):
if request.method == 'POST':
data = request.json
section = Section(name=data['name'], notebook_id=nb_id)
db.session.add(section)
db.session.commit()
return jsonify({'id': section.id, 'name': section.name})
sections = Section.query.filter_by(notebook_id=nb_id).all()
return jsonify([{'id': s.id, 'name': s.name} for s in sections])
@app.route('/api/sections/<int:sec_id>/pages', methods=['GET', 'POST'])
def pages(sec_id):
if request.method == 'POST':
data = request.json
page = Page(title=data['title'], content='', drawing_data='', section_id=sec_id)
db.session.add(page)
db.session.commit()
return jsonify({'id': page.id, 'title': page.title})
pages = Page.query.filter_by(section_id=sec_id).all()
return jsonify([{
'id': p.id,
'title': p.title,
'updated_at': p.updated_at.isoformat()
} for p in pages])
@app.route('/api/pages/<int:page_id>', methods=['GET', 'PUT', 'DELETE'])
def page_detail(page_id):
page = Page.query.get_or_404(page_id)
if request.method == 'GET':
return jsonify({
'id': page.id,
'title': page.title,
'content': page.content,
'drawing_data': page.drawing_data,
'updated_at': page.updated_at.isoformat()
})
elif request.method == 'PUT':
data = request.json
page.title = data.get('title', page.title)
page.content = data.get('content', page.content)
page.drawing_data = data.get('drawing_data', page.drawing_data)
page.updated_at = datetime.utcnow()
db.session.commit()
return jsonify({'success': True})
elif request.method == 'DELETE':
db.session.delete(page)
db.session.commit()
return jsonify({'success': True})
if __name__ == '__main__':
with app.app_context():
db.create_all()
app.run(debug=True, host='0.0.0.0', port=5001)

Xet Storage Details

Size:
3.07 kB
·
Xet hash:
54ce56e02da7aa8fcba3db412bdca2f63e10b2490660bb2ac271e6616437071e

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.