Add user authentication routes: implement login, registration, and logout functionality, along with user profile and admin routes. Enhance mindmap API with error handling and default node creation.
This commit is contained in:
113
website/app.py
113
website/app.py
@@ -31,57 +31,11 @@ def create_app():
|
|||||||
except Exception as e:
|
except Exception as e:
|
||||||
print(f"Error initializing database: {str(e)}")
|
print(f"Error initializing database: {str(e)}")
|
||||||
|
|
||||||
return app
|
# Register all routes with the app
|
||||||
|
|
||||||
# Database Models
|
|
||||||
class User(UserMixin, db.Model):
|
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
|
||||||
username = db.Column(db.String(80), unique=True, nullable=False)
|
|
||||||
email = db.Column(db.String(120), unique=True, nullable=False)
|
|
||||||
password_hash = db.Column(db.String(128))
|
|
||||||
is_admin = db.Column(db.Boolean, default=False)
|
|
||||||
thoughts = db.relationship('Thought', backref='author', lazy=True)
|
|
||||||
|
|
||||||
def set_password(self, password):
|
|
||||||
self.password_hash = generate_password_hash(password)
|
|
||||||
|
|
||||||
def check_password(self, password):
|
|
||||||
return check_password_hash(self.password_hash, password)
|
|
||||||
|
|
||||||
class Thought(db.Model):
|
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
|
||||||
content = db.Column(db.Text, nullable=False)
|
|
||||||
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
|
|
||||||
branch = db.Column(db.String(100), nullable=False)
|
|
||||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
|
||||||
comments = db.relationship('Comment', backref='thought', lazy=True, cascade="all, delete-orphan")
|
|
||||||
|
|
||||||
class Comment(db.Model):
|
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
|
||||||
content = db.Column(db.Text, nullable=False)
|
|
||||||
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
|
|
||||||
thought_id = db.Column(db.Integer, db.ForeignKey('thought.id'), nullable=False)
|
|
||||||
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
|
||||||
author = db.relationship('User', backref='comments')
|
|
||||||
|
|
||||||
class MindMapNode(db.Model):
|
|
||||||
id = db.Column(db.Integer, primary_key=True)
|
|
||||||
name = db.Column(db.String(100), nullable=False)
|
|
||||||
parent_id = db.Column(db.Integer, db.ForeignKey('mind_map_node.id'), nullable=True)
|
|
||||||
children = db.relationship('MindMapNode', backref=db.backref('parent', remote_side=[id]))
|
|
||||||
thoughts = db.relationship('Thought', secondary='node_thought_association', backref='nodes')
|
|
||||||
|
|
||||||
# Association table for many-to-many relationship between MindMapNode and Thought
|
|
||||||
node_thought_association = db.Table('node_thought_association',
|
|
||||||
db.Column('node_id', db.Integer, db.ForeignKey('mind_map_node.id'), primary_key=True),
|
|
||||||
db.Column('thought_id', db.Integer, db.ForeignKey('thought.id'), primary_key=True)
|
|
||||||
)
|
|
||||||
|
|
||||||
@login_manager.user_loader
|
@login_manager.user_loader
|
||||||
def load_user(id):
|
def load_user(id):
|
||||||
return User.query.get(int(id))
|
return User.query.get(int(id))
|
||||||
|
|
||||||
# Routes for authentication
|
|
||||||
@app.route('/login', methods=['GET', 'POST'])
|
@app.route('/login', methods=['GET', 'POST'])
|
||||||
def login():
|
def login():
|
||||||
if request.method == 'POST':
|
if request.method == 'POST':
|
||||||
@@ -126,12 +80,10 @@ def logout():
|
|||||||
logout_user()
|
logout_user()
|
||||||
return redirect(url_for('index'))
|
return redirect(url_for('index'))
|
||||||
|
|
||||||
# Route for the homepage
|
|
||||||
@app.route('/')
|
@app.route('/')
|
||||||
def index():
|
def index():
|
||||||
return render_template('index.html')
|
return render_template('index.html')
|
||||||
|
|
||||||
# Route for the mindmap page
|
|
||||||
@app.route('/mindmap')
|
@app.route('/mindmap')
|
||||||
def mindmap():
|
def mindmap():
|
||||||
try:
|
try:
|
||||||
@@ -145,14 +97,12 @@ def mindmap():
|
|||||||
app.logger.error(f"Error loading mindmap: {str(e)}")
|
app.logger.error(f"Error loading mindmap: {str(e)}")
|
||||||
return render_template('mindmap.html', error="Fehler beim Laden der Mindmap. Bitte versuchen Sie es erneut.")
|
return render_template('mindmap.html', error="Fehler beim Laden der Mindmap. Bitte versuchen Sie es erneut.")
|
||||||
|
|
||||||
# Route for user profile
|
|
||||||
@app.route('/profile')
|
@app.route('/profile')
|
||||||
@login_required
|
@login_required
|
||||||
def profile():
|
def profile():
|
||||||
thoughts = Thought.query.filter_by(user_id=current_user.id).order_by(Thought.timestamp.desc()).all()
|
thoughts = Thought.query.filter_by(user_id=current_user.id).order_by(Thought.timestamp.desc()).all()
|
||||||
return render_template('profile.html', thoughts=thoughts)
|
return render_template('profile.html', thoughts=thoughts)
|
||||||
|
|
||||||
# API routes for mindmap and thoughts
|
|
||||||
@app.route('/api/mindmap')
|
@app.route('/api/mindmap')
|
||||||
def get_mindmap():
|
def get_mindmap():
|
||||||
try:
|
try:
|
||||||
@@ -167,7 +117,6 @@ def get_mindmap():
|
|||||||
|
|
||||||
result = [build_tree(node) for node in root_nodes]
|
result = [build_tree(node) for node in root_nodes]
|
||||||
if not result:
|
if not result:
|
||||||
# Create default root if no nodes exist
|
|
||||||
root = MindMapNode(name="Wissenschaft")
|
root = MindMapNode(name="Wissenschaft")
|
||||||
db.session.add(root)
|
db.session.add(root)
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
@@ -195,19 +144,6 @@ def get_thoughts(node_id):
|
|||||||
|
|
||||||
return jsonify(thoughts)
|
return jsonify(thoughts)
|
||||||
|
|
||||||
@app.route('/api/thought/<int:thought_id>', methods=['GET'])
|
|
||||||
def get_thought(thought_id):
|
|
||||||
thought = Thought.query.get_or_404(thought_id)
|
|
||||||
|
|
||||||
return jsonify({
|
|
||||||
'id': thought.id,
|
|
||||||
'content': thought.content,
|
|
||||||
'author': thought.author.username,
|
|
||||||
'timestamp': thought.timestamp.strftime('%d.%m.%Y, %H:%M'),
|
|
||||||
'branch': thought.branch,
|
|
||||||
'comments_count': len(thought.comments)
|
|
||||||
})
|
|
||||||
|
|
||||||
@app.route('/api/thoughts', methods=['POST'])
|
@app.route('/api/thoughts', methods=['POST'])
|
||||||
@login_required
|
@login_required
|
||||||
def add_thought():
|
def add_thought():
|
||||||
@@ -280,7 +216,6 @@ def add_comment():
|
|||||||
'timestamp': comment.timestamp.strftime('%d.%m.%Y, %H:%M')
|
'timestamp': comment.timestamp.strftime('%d.%m.%Y, %H:%M')
|
||||||
})
|
})
|
||||||
|
|
||||||
# Admin routes
|
|
||||||
@app.route('/admin')
|
@app.route('/admin')
|
||||||
@login_required
|
@login_required
|
||||||
def admin():
|
def admin():
|
||||||
@@ -294,6 +229,52 @@ def admin():
|
|||||||
|
|
||||||
return render_template('admin.html', users=users, nodes=nodes, thoughts=thoughts)
|
return render_template('admin.html', users=users, nodes=nodes, thoughts=thoughts)
|
||||||
|
|
||||||
|
return app
|
||||||
|
|
||||||
|
# Database Models
|
||||||
|
class User(UserMixin, db.Model):
|
||||||
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
|
username = db.Column(db.String(80), unique=True, nullable=False)
|
||||||
|
email = db.Column(db.String(120), unique=True, nullable=False)
|
||||||
|
password_hash = db.Column(db.String(128))
|
||||||
|
is_admin = db.Column(db.Boolean, default=False)
|
||||||
|
thoughts = db.relationship('Thought', backref='author', lazy=True)
|
||||||
|
|
||||||
|
def set_password(self, password):
|
||||||
|
self.password_hash = generate_password_hash(password)
|
||||||
|
|
||||||
|
def check_password(self, password):
|
||||||
|
return check_password_hash(self.password_hash, password)
|
||||||
|
|
||||||
|
class Thought(db.Model):
|
||||||
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
|
content = db.Column(db.Text, nullable=False)
|
||||||
|
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
|
||||||
|
branch = db.Column(db.String(100), nullable=False)
|
||||||
|
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
||||||
|
comments = db.relationship('Comment', backref='thought', lazy=True, cascade="all, delete-orphan")
|
||||||
|
|
||||||
|
class Comment(db.Model):
|
||||||
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
|
content = db.Column(db.Text, nullable=False)
|
||||||
|
timestamp = db.Column(db.DateTime, default=datetime.utcnow)
|
||||||
|
thought_id = db.Column(db.Integer, db.ForeignKey('thought.id'), nullable=False)
|
||||||
|
user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
|
||||||
|
author = db.relationship('User', backref='comments')
|
||||||
|
|
||||||
|
class MindMapNode(db.Model):
|
||||||
|
id = db.Column(db.Integer, primary_key=True)
|
||||||
|
name = db.Column(db.String(100), nullable=False)
|
||||||
|
parent_id = db.Column(db.Integer, db.ForeignKey('mind_map_node.id'), nullable=True)
|
||||||
|
children = db.relationship('MindMapNode', backref=db.backref('parent', remote_side=[id]))
|
||||||
|
thoughts = db.relationship('Thought', secondary='node_thought_association', backref='nodes')
|
||||||
|
|
||||||
|
# Association table for many-to-many relationship between MindMapNode and Thought
|
||||||
|
node_thought_association = db.Table('node_thought_association',
|
||||||
|
db.Column('node_id', db.Integer, db.ForeignKey('mind_map_node.id'), primary_key=True),
|
||||||
|
db.Column('thought_id', db.Integer, db.ForeignKey('thought.id'), primary_key=True)
|
||||||
|
)
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
app = create_app()
|
app = create_app()
|
||||||
app.run(host="0.0.0.0", port=6000, debug=True)
|
app.run(host="0.0.0.0", port=6000, debug=True)
|
||||||
Reference in New Issue
Block a user