import os import secrets from flask import Flask, render_template, request, redirect, flash from flask import Flask, render_template, request, redirect, url_for, send_from_directory, abort, current_app from flask_sqlalchemy import SQLAlchemy from flask_wtf import FlaskForm from forms import ProjectForm from wtforms import StringField, TextAreaField, SubmitField from wtforms.validators import DataRequired app = Flask(__name__, static_folder='static') app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0 app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.abspath(os.path.join(os.path.dirname(__file__), 'instance/site.db')) app.config['SECRET_KEY'] = secrets.token_hex(16) db = SQLAlchemy(app) class Project(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(100), nullable=False) description = db.Column(db.Text, nullable=False) class ProjectForm(FlaskForm): title = StringField('Title', validators=[DataRequired()]) description = TextAreaField('Description', validators=[DataRequired()]) submit = SubmitField('Add Project') @app.route('/') def home(): try: projects = Project.query.all() return render_template('index.html', projects=projects) except Exception as e: print(f"Error fetching projects: {str(e)}") return render_template('error.html') # New route for the 'about' page @app.route('/about') def about(): return render_template('about.html') # New route for the 'experience' page @app.route('/experience') def experience(): # Add logic to fetch data related to the Experience section if needed return render_template('experience.html') # New route for the 'portfolio' page @app.route('/portfolio') def portfolio(): # Add logic to fetch data related to the Portfolio section if needed return render_template('portfolio.html') # New route for the 'contact' page @app.route('/contact') def contact(): return render_template('contact.html') # Updated route for adding a project using Flask-WTF form @app.route('/add_project', methods=['GET', 'POST']) def add_project(): form = ProjectForm() if request.method == 'POST' and form.validate_on_submit(): title = form.title.data description = form.description.data new_project = Project(title=title, description=description) db.session.add(new_project) db.session.commit() flash('Project added successfully!', 'success') # Flash success message return redirect(url_for('home')) return render_template('add_project.html', form=form) # Updated route for serving the 'my-cv.docx' file @app.route('/download_cv') def download_cv(): file_path = 'static/my-cv.docx' print(f"Attempting to serve file: {file_path}") return send_from_directory('static', 'my-cv.docx', as_attachment=True, mimetype='application/docx') # Updated route for serving assessment files @app.route('/download_assessment/<filename>') def download_assessment(filename): try: file_path = os.path.join('static', filename) print(f"Attempting to serve file: {file_path}") return send_from_directory(app.static_folder, filename, as_attachment=True) except FileNotFoundError: print(f"File not found: {file_path}") abort(404) # Return a 404 Not Found error except Exception as e: print(f"Error serving assessment file: {str(e)}") current_app.logger.exception(f"Error serving assessment file: {str(e)}") abort(500) if __name__ == '__main__': with app.app_context(): db.create_all() app.run(debug=True, port=int(os.environ.get('PORT', 8080)))