Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
C
cmt120-2
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package Registry
Container Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Felix Chadwick-Smith
cmt120-2
Commits
975a4490
Commit
975a4490
authored
1 year ago
by
Felix Chadwick-Smith
Browse files
Options
Downloads
Patches
Plain Diff
changed back to original
parent
940312f9
No related branches found
No related tags found
1 merge request
!18
changed back to original
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
my_flask_app.py
+16
-20
16 additions, 20 deletions
my_flask_app.py
with
16 additions
and
20 deletions
my_flask_app.py
+
16
−
20
View file @
975a4490
...
...
@@ -2,9 +2,7 @@ import os
import
secrets
from
flask
import
Flask
,
render_template
,
request
,
redirect
,
url_for
,
send_from_directory
,
abort
from
flask_sqlalchemy
import
SQLAlchemy
from
flask_wtf
import
FlaskForm
from
wtforms
import
StringField
,
TextAreaField
,
SubmitField
from
wtforms.validators
import
DataRequired
from
models
import
db
,
Project
app
=
Flask
(
__name__
,
static_folder
=
'
static
'
)
app
.
config
[
'
SEND_FILE_MAX_AGE_DEFAULT
'
]
=
0
...
...
@@ -12,17 +10,11 @@ app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///' + os.path.abspath(os.path.j
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
(
'
Submit
'
)
@app.route
(
'
/
'
)
def
home
():
try
:
...
...
@@ -54,20 +46,23 @@ def portfolio():
def
contact
():
return
render_template
(
'
contact.html
'
)
# Updated route for adding a project with Flask-WTF form
# Updated route for adding a project with
out
Flask-WTF form
@app.route
(
'
/add_project
'
,
methods
=
[
'
GET
'
,
'
POST
'
])
def
add_project
():
form
=
ProjectForm
()
if
form
.
validate_on_submit
():
title
=
form
.
title
.
data
description
=
form
.
description
.
data
if
request
.
method
==
'
POST
'
:
# 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
print
(
f
"
Received form data - Title:
{
title
}
, Description:
{
description
}
"
)
new_project
=
Project
(
title
=
title
,
description
=
description
)
db
.
session
.
add
(
new_project
)
db
.
session
.
commit
()
return
redirect
(
url_for
(
'
home
'
))
return
render_template
(
'
add_project.html
'
,
form
=
form
)
return
render_template
(
'
add_project.html
'
)
# Updated route for serving the 'my-cv.docx' file
@app.route
(
'
/download_cv
'
)
...
...
@@ -80,15 +75,16 @@ def download_cv():
@app.route
(
'
/download_assessment/<filename>
'
)
def
download_assessment
(
filename
):
try
:
file_path
=
os
.
path
.
join
(
'
static
'
,
filename
)
app
.
logger
.
debug
(
f
"
Attempting to serve file:
{
file_path
}
"
)
file_path
=
f
'
static
/
{
filename
}
'
print
(
f
"
Attempting to serve file:
{
file_path
}
"
)
return
send_from_directory
(
'
static
'
,
filename
,
as_attachment
=
True
)
except
FileNotFoundError
:
app
.
logger
.
error
(
f
"
File not found:
{
file_path
}
"
)
print
(
f
"
File not found:
{
file_path
}
"
)
abort
(
404
)
# Return a 404 Not Found error
except
Exception
as
e
:
app
.
logger
.
error
(
f
"
Error serving assessment file:
{
str
(
e
)
}
"
)
abort
(
500
)
print
(
f
"
Error serving assessment file:
{
str
(
e
)
}
"
)
app
.
logger
.
exception
(
f
"
Error serving assessment file:
{
str
(
e
)
}
"
)
abort
(
500
)
if
__name__
==
'
__main__
'
:
with
app
.
app_context
():
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment