|
| 1 | +import os |
| 2 | +from flask import Flask, render_template, request, jsonify, session, redirect, url_for |
| 3 | +from flask_sqlalchemy import SQLAlchemy |
| 4 | +from sqlalchemy.orm import DeclarativeBase |
| 5 | +import logging |
| 6 | + |
| 7 | +# Configure logging for debugging |
| 8 | +logging.basicConfig(level=logging.DEBUG) |
| 9 | + |
| 10 | +class Base(DeclarativeBase): |
| 11 | + pass |
| 12 | + |
| 13 | +db = SQLAlchemy(model_class=Base) |
| 14 | + |
| 15 | +# create the app |
| 16 | +app = Flask(__name__) |
| 17 | +app.secret_key = os.environ.get("SESSION_SECRET", "dev-secret-key") |
| 18 | + |
| 19 | +# configure the database |
| 20 | +app.config["SQLALCHEMY_DATABASE_URI"] = os.environ.get("DATABASE_URL") |
| 21 | +app.config["SQLALCHEMY_ENGINE_OPTIONS"] = { |
| 22 | + "pool_recycle": 300, |
| 23 | + "pool_pre_ping": True, |
| 24 | +} |
| 25 | + |
| 26 | +# initialize the app with the extension |
| 27 | +db.init_app(app) |
| 28 | + |
| 29 | +with app.app_context(): |
| 30 | + # Import models here so they are registered with SQLAlchemy |
| 31 | + import models |
| 32 | + db.create_all() |
| 33 | + |
| 34 | +@app.route('/') |
| 35 | +def index(): |
| 36 | + return render_template('index.html') |
| 37 | + |
| 38 | +@app.route('/api/profiles', methods=['GET', 'POST']) |
| 39 | +def profiles(): |
| 40 | + if request.method == 'POST': |
| 41 | + # Save profile data |
| 42 | + data = request.get_json() |
| 43 | + profile = models.Profile( |
| 44 | + name=data.get('name'), |
| 45 | + last_name=data.get('lastName'), |
| 46 | + role=data.get('role'), |
| 47 | + city=data.get('city'), |
| 48 | + country=data.get('country'), |
| 49 | + avatar_url=data.get('avatarURL'), |
| 50 | + background_url=data.get('background'), |
| 51 | + include_cover=data.get('includeCover', True), |
| 52 | + social_media_position=data.get('socialMediaPosition', 'right'), |
| 53 | + twitter=data.get('twitter'), |
| 54 | + github=data.get('github'), |
| 55 | + linkedin=data.get('linkedin'), |
| 56 | + instagram=data.get('instagram') |
| 57 | + ) |
| 58 | + db.session.add(profile) |
| 59 | + db.session.commit() |
| 60 | + |
| 61 | + return jsonify({'id': profile.id, 'message': 'Profile saved successfully'}) |
| 62 | + |
| 63 | + else: |
| 64 | + # Get all profiles |
| 65 | + profiles = models.Profile.query.all() |
| 66 | + return jsonify([profile.to_dict() for profile in profiles]) |
| 67 | + |
| 68 | +@app.route('/api/profiles/<int:profile_id>', methods=['GET', 'PUT', 'DELETE']) |
| 69 | +def profile_detail(profile_id): |
| 70 | + profile = models.Profile.query.get_or_404(profile_id) |
| 71 | + |
| 72 | + if request.method == 'GET': |
| 73 | + return jsonify(profile.to_dict()) |
| 74 | + |
| 75 | + elif request.method == 'PUT': |
| 76 | + # Update profile |
| 77 | + data = request.get_json() |
| 78 | + profile.name = data.get('name', profile.name) |
| 79 | + profile.last_name = data.get('lastName', profile.last_name) |
| 80 | + profile.role = data.get('role', profile.role) |
| 81 | + profile.city = data.get('city', profile.city) |
| 82 | + profile.country = data.get('country', profile.country) |
| 83 | + profile.avatar_url = data.get('avatarURL', profile.avatar_url) |
| 84 | + profile.background_url = data.get('background', profile.background_url) |
| 85 | + profile.include_cover = data.get('includeCover', profile.include_cover) |
| 86 | + profile.social_media_position = data.get('socialMediaPosition', profile.social_media_position) |
| 87 | + profile.twitter = data.get('twitter', profile.twitter) |
| 88 | + profile.github = data.get('github', profile.github) |
| 89 | + profile.linkedin = data.get('linkedin', profile.linkedin) |
| 90 | + profile.instagram = data.get('instagram', profile.instagram) |
| 91 | + |
| 92 | + db.session.commit() |
| 93 | + return jsonify({'message': 'Profile updated successfully'}) |
| 94 | + |
| 95 | + elif request.method == 'DELETE': |
| 96 | + db.session.delete(profile) |
| 97 | + db.session.commit() |
| 98 | + return jsonify({'message': 'Profile deleted successfully'}) |
| 99 | + |
| 100 | +if __name__ == '__main__': |
| 101 | + app.run(host='0.0.0.0', port=5000, debug=True) |
0 commit comments