add app.py
This commit is contained in:
parent
7126351d1e
commit
2aa8bfe78d
1 changed files with 90 additions and 0 deletions
90
app.py
Normal file
90
app.py
Normal file
|
|
@ -0,0 +1,90 @@
|
||||||
|
import json
|
||||||
|
import os
|
||||||
|
import uuid
|
||||||
|
from datetime import datetime
|
||||||
|
from flask import Flask, render_template, request, jsonify
|
||||||
|
|
||||||
|
app = Flask(__name__)
|
||||||
|
DATA_DIR = os.path.join(os.path.dirname(__file__), 'data')
|
||||||
|
os.makedirs(DATA_DIR, exist_ok=True)
|
||||||
|
|
||||||
|
def list_projects():
|
||||||
|
projects = []
|
||||||
|
for f in os.listdir(DATA_DIR):
|
||||||
|
if f.endswith('.json'):
|
||||||
|
path = os.path.join(DATA_DIR, f)
|
||||||
|
with open(path) as fp:
|
||||||
|
data = json.load(fp)
|
||||||
|
projects.append({
|
||||||
|
'id': f.replace('.json', ''),
|
||||||
|
'name': data.get('name', 'Unbenannt'),
|
||||||
|
'updated': data.get('updated', ''),
|
||||||
|
'rooms': len(data.get('rooms', []))
|
||||||
|
})
|
||||||
|
projects.sort(key=lambda p: p['updated'], reverse=True)
|
||||||
|
return projects
|
||||||
|
|
||||||
|
def load_project(pid):
|
||||||
|
path = os.path.join(DATA_DIR, f'{pid}.json')
|
||||||
|
if not os.path.exists(path):
|
||||||
|
return None
|
||||||
|
with open(path) as fp:
|
||||||
|
return json.load(fp)
|
||||||
|
|
||||||
|
def save_project(pid, data):
|
||||||
|
data['updated'] = datetime.now().isoformat()
|
||||||
|
path = os.path.join(DATA_DIR, f'{pid}.json')
|
||||||
|
with open(path, 'w') as fp:
|
||||||
|
json.dump(data, fp, indent=2, ensure_ascii=False)
|
||||||
|
|
||||||
|
@app.route('/')
|
||||||
|
def index():
|
||||||
|
return render_template('index.html')
|
||||||
|
|
||||||
|
@app.route('/api/projects')
|
||||||
|
def api_projects():
|
||||||
|
return jsonify(list_projects())
|
||||||
|
|
||||||
|
@app.route('/api/projects', methods=['POST'])
|
||||||
|
def api_create_project():
|
||||||
|
body = request.get_json() or {}
|
||||||
|
pid = str(uuid.uuid4())[:8]
|
||||||
|
data = {
|
||||||
|
'name': body.get('name', 'Neuer Grundriss'),
|
||||||
|
'created': datetime.now().isoformat(),
|
||||||
|
'updated': datetime.now().isoformat(),
|
||||||
|
'rooms': [],
|
||||||
|
'grid_size': 20 # cm per grid cell
|
||||||
|
}
|
||||||
|
save_project(pid, data)
|
||||||
|
return jsonify({'id': pid, **data})
|
||||||
|
|
||||||
|
@app.route('/api/projects/<pid>')
|
||||||
|
def api_get_project(pid):
|
||||||
|
data = load_project(pid)
|
||||||
|
if not data:
|
||||||
|
return jsonify({'error': 'Projekt nicht gefunden'}), 404
|
||||||
|
return jsonify({'id': pid, **data})
|
||||||
|
|
||||||
|
@app.route('/api/projects/<pid>', methods=['PUT'])
|
||||||
|
def api_update_project(pid):
|
||||||
|
data = load_project(pid)
|
||||||
|
if not data:
|
||||||
|
return jsonify({'error': 'Projekt nicht gefunden'}), 404
|
||||||
|
body = request.get_json() or {}
|
||||||
|
if 'name' in body:
|
||||||
|
data['name'] = body['name']
|
||||||
|
if 'rooms' in body:
|
||||||
|
data['rooms'] = body['rooms']
|
||||||
|
save_project(pid, data)
|
||||||
|
return jsonify({'id': pid, **data})
|
||||||
|
|
||||||
|
@app.route('/api/projects/<pid>', methods=['DELETE'])
|
||||||
|
def api_delete_project(pid):
|
||||||
|
path = os.path.join(DATA_DIR, f'{pid}.json')
|
||||||
|
if os.path.exists(path):
|
||||||
|
os.remove(path)
|
||||||
|
return jsonify({'status': 'ok'})
|
||||||
|
|
||||||
|
if __name__ == '__main__':
|
||||||
|
app.run(host='0.0.0.0', port=5000, debug=True)
|
||||||
Loading…
Add table
Add a link
Reference in a new issue