Skip to content
Snippets Groups Projects
Commit 414d0cbe authored by Felix Chadwick-Smith's avatar Felix Chadwick-Smith
Browse files

Merge branch 'master' into 'main'

Master

See merge request !21
parents 0f91fbca 491bd3b4
No related branches found
No related tags found
1 merge request!21Master
from flask_wtf import FlaskForm
from wtforms import StringField, TextAreaField, SubmitField
from wtforms.validators import DataRequired
class ProjectForm(FlaskForm):
title = StringField('Title', validators=[DataRequired()])
description = TextAreaField('Description', validators=[DataRequired()])
submit = SubmitField('Add Project')
...@@ -2,8 +2,10 @@ import os ...@@ -2,8 +2,10 @@ import os
import secrets import secrets
from flask import Flask, render_template, request, redirect, url_for, send_from_directory, abort, current_app from flask import Flask, render_template, request, redirect, url_for, send_from_directory, abort, current_app
from flask_sqlalchemy import SQLAlchemy from flask_sqlalchemy import SQLAlchemy
from models import db, Project 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 = Flask(__name__, static_folder='static')
app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0 app.config['SEND_FILE_MAX_AGE_DEFAULT'] = 0
...@@ -11,12 +13,18 @@ app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.abspath(os.path.j ...@@ -11,12 +13,18 @@ app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.abspath(os.path.j
app.config['SECRET_KEY'] = secrets.token_hex(16) app.config['SECRET_KEY'] = secrets.token_hex(16)
db = SQLAlchemy(app) db = SQLAlchemy(app)
class Project(db.Model): class Project(db.Model):
id = db.Column(db.Integer, primary_key=True) id = db.Column(db.Integer, primary_key=True)
title = db.Column(db.String(100), nullable=False) title = db.Column(db.String(100), nullable=False)
description = db.Column(db.Text, 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('/') @app.route('/')
def home(): def home():
try: try:
projects = Project.query.all() projects = Project.query.all()
...@@ -47,23 +55,22 @@ def portfolio(): ...@@ -47,23 +55,22 @@ def portfolio():
def contact(): def contact():
return render_template('contact.html') return render_template('contact.html')
# Updated route for adding a project without Flask-WTF form # Updated route for adding a project using Flask-WTF form
@app.route('/add_project', methods=['GET', 'POST']) @app.route('/add_project', methods=['GET', 'POST'])
def add_project(): def add_project():
if request.method == 'POST': form = ProjectForm()
# Retrieve form data directly from request
title = request.form.get('title')
description = request.form.get('description')
# Print or log the form data to check if it's received if request.method == 'POST' and form.validate_on_submit():
print(f"Received form data - Title: {title}, Description: {description}") title = form.title.data
description = form.description.data
new_project = Project(title=title, description=description) new_project = Project(title=title, description=description)
db.session.add(new_project) db.session.add(new_project)
db.session.commit() db.session.commit()
return redirect(url_for('home')) return redirect(url_for('home'))
return render_template('add_project.html') return render_template('add_project.html', form=form) # Pass the form to the template
# Updated route for serving the 'my-cv.docx' file # Updated route for serving the 'my-cv.docx' file
@app.route('/download_cv') @app.route('/download_cv')
...@@ -87,8 +94,8 @@ def download_assessment(filename): ...@@ -87,8 +94,8 @@ def download_assessment(filename):
print(f"Error serving assessment file: {str(e)}") print(f"Error serving assessment file: {str(e)}")
current_app.logger.exception(f"Error serving assessment file: {str(e)}") current_app.logger.exception(f"Error serving assessment file: {str(e)}")
abort(500) abort(500)
if __name__ == '__main__': if __name__ == '__main__':
with app.app_context(): with app.app_context():
db.create_all() db.create_all()
app.run(debug=True, port=int(os.environ.get('PORT', 8080))) app.run(debug=True, port=int(os.environ.get('PORT', 8080)))
\ No newline at end of file
...@@ -19,3 +19,6 @@ waitress==2.1.2 ...@@ -19,3 +19,6 @@ waitress==2.1.2
Werkzeug==3.0.1 Werkzeug==3.0.1
zope.event==5.0 zope.event==5.0
zope.interface==6.1 zope.interface==6.1
Flask-WTF==1.2.1
wtforms==3.1.2
...@@ -11,18 +11,20 @@ ...@@ -11,18 +11,20 @@
<body> <body>
<h2>Add Project</h2> <h2>Add Project</h2>
<form method="POST" action="{{ url_for('add_project') }}"> <form method="POST" action="{{ url_for('add_project') }}">
{{ form.csrf_token }} {% if form %}
<div class="form-group"> {{ form.csrf_token }}
{{ form.title.label }} <div class="form-group">
{{ form.title(class="form-control", placeholder="Enter project title") }} {{ form.title.label }}
</div> {{ form.title(class="form-control", placeholder="Enter project title") }}
<div class="form-group"> </div>
{{ form.description.label }} <div class="form-group">
{{ form.description(class="form-control", placeholder="Enter project description") }} {{ form.description.label }}
</div> {{ form.description(class="form-control", placeholder="Enter project description") }}
<div class="form-group"> </div>
{{ form.submit(class="btn btn-primary") }} <div class="form-group">
</div> {{ form.submit(class="btn btn-primary") }}
</div>
{% endif %}
</form> </form>
</body> </body>
</html> </html>
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment