Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
Flask
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
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
Ethan2.Clay@live.uwe.ac.uk
Flask
Commits
5f344aca
Commit
5f344aca
authored
3 months ago
by
Ethan Clay
Browse files
Options
Downloads
Patches
Plain Diff
Add SQL on inital load, depending on init.sql file
parent
c427be82
Branches
main
No related tags found
No related merge requests found
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
app/__init__.py
+54
-7
54 additions, 7 deletions
app/__init__.py
app/admin/routes.py
+19
-0
19 additions, 0 deletions
app/admin/routes.py
app/models/listings.py
+3
-0
3 additions, 0 deletions
app/models/listings.py
docker-compose.yml
+0
-1
0 additions, 1 deletion
docker-compose.yml
with
76 additions
and
8 deletions
app/__init__.py
+
54
−
7
View file @
5f344aca
...
...
@@ -9,6 +9,8 @@ from dotenv import load_dotenv
from
app.logger
import
auth_logger
from
functools
import
wraps
import
os
import
pymysql
from
sqlalchemy.sql
import
text
# Initialize extensions
db
=
SQLAlchemy
()
...
...
@@ -28,11 +30,55 @@ def permission_required(permission):
return
decorated_function
return
decorator
super_admin_permission
=
Permission
(
RoleNeed
(
'
super-admin
'
))
admin_permission
=
Permission
(
RoleNeed
(
'
admin
'
))
user_permission
=
Permission
(
RoleNeed
(
'
user
'
))
def
create_database_if_not_exists
(
db_host
,
db_user
,
db_password
,
db_name
):
# Connect using Root to create schema if doesn't exist and then create user for that schema
connection
=
pymysql
.
connect
(
host
=
db_host
,
user
=
'
root
'
,
password
=
db_password
)
try
:
with
connection
.
cursor
()
as
cursor
:
cursor
.
execute
(
f
"
CREATE DATABASE IF NOT EXISTS
{
db_name
}
"
)
# Create the user from .env details
cursor
.
execute
(
f
"
CREATE USER IF NOT EXISTS
'
{
db_user
}
'
@
'
%
'
IDENTIFIED BY
'
{
db_password
}
'"
)
cursor
.
execute
(
f
"
GRANT ALL PRIVILEGES ON
{
db_name
}
.* TO
'
{
db_user
}
'
@
'
%
'"
)
cursor
.
execute
(
f
"
FLUSH PRIVILEGES
"
)
connection
.
commit
()
finally
:
connection
.
close
()
# Reconnect using user in .env to prevent permission issues
connection
=
pymysql
.
connect
(
host
=
db_host
,
user
=
db_user
,
password
=
db_password
,
database
=
db_name
)
try
:
with
connection
.
cursor
()
as
cursor
:
# Check if tables exist
cursor
.
execute
(
"
SHOW TABLES;
"
)
tables
=
cursor
.
fetchall
()
if
not
tables
:
with
open
(
'
sql-setup/init.sql
'
,
'
r
'
)
as
file
:
sql_commands
=
file
.
read
().
split
(
'
;
'
)
for
command
in
sql_commands
:
if
command
.
strip
():
cursor
.
execute
(
command
)
connection
.
commit
()
finally
:
connection
.
close
()
def
create_app
(
config_class
=
Config
):
app
=
Flask
(
__name__
)
app
.
config
.
from_object
(
config_class
)
...
...
@@ -48,9 +94,12 @@ def create_app(config_class=Config):
db_password
=
os
.
getenv
(
"
DATABASE_PASSWORD
"
)
db_name
=
os
.
getenv
(
"
DATABASE_NAME
"
)
create_database_if_not_exists
(
db_host
,
db_user
,
db_password
,
db_name
)
app
.
config
[
'
SECRET_KEY
'
]
=
os
.
getenv
(
'
SECRET_KEY
'
)
app
.
config
[
'
SQLALCHEMY_DATABASE_URI
'
]
=
f
"
mysql+pymysql://
{
db_user
}
:
{
db_password
}
@
{
db_host
}
/
{
db_name
}
"
# Initialize extensions with the app
db
.
init_app
(
app
)
migrate
.
init_app
(
app
,
db
)
...
...
@@ -75,7 +124,6 @@ def create_app(config_class=Config):
else
:
auth_logger
.
debug
(
f
'
No role found for user
{
identity
.
user
.
username
}
.
'
)
# Add global template variables
@app.context_processor
def
set_global_html_variable_values
():
...
...
@@ -137,7 +185,6 @@ def create_app(config_class=Config):
return
app
@login_manager.user_loader
def
load_user
(
user_id
):
from
app.models
import
User
...
...
This diff is collapsed.
Click to expand it.
app/admin/routes.py
+
19
−
0
View file @
5f344aca
...
...
@@ -4,6 +4,7 @@ from app import admin_permission, permission_required, super_admin_permission
from
app.models
import
Listings
,
ListingImages
,
User
from
app.admin
import
bp
from
app.main.utils
import
generate_time_options
from
sqlalchemy.sql
import
text
@bp.route
(
'
/home
'
)
...
...
@@ -296,3 +297,21 @@ def delete_image(image_id):
except
Exception
as
e
:
db
.
session
.
rollback
()
return
jsonify
({
'
success
'
:
False
,
'
error
'
:
str
(
e
)}),
500
@bp.route
(
'
/init/database
'
,
methods
=
[
'
GET
'
])
def
check_database_exists
():
try
:
if
Listings
.
check_table_exists
():
flash
(
"
Database already exists,
'
error
"
)
else
:
raise
Exception
(
'
Schema exists but database does not
'
)
except
:
with
open
(
'
sql-setup/init.sql
'
,
'
r
'
)
as
file
:
sql_commands
=
file
.
read
().
split
(
'
;
'
)
for
command
in
sql_commands
:
if
command
.
strip
():
db
.
session
.
execute
(
text
(
command
))
db
.
session
.
commit
()
flash
(
"
Database initialised
"
,
'
success
'
)
This diff is collapsed.
Click to expand it.
app/models/listings.py
+
3
−
0
View file @
5f344aca
...
...
@@ -80,3 +80,6 @@ class Listings(db.Model):
'
business_fair_cost
'
:
self
.
business_fair_cost
}
@classmethod
def
check_table_exists
(
cls
):
return
cls
.
get_top_listings
(
1
)
This diff is collapsed.
Click to expand it.
docker-compose.yml
+
0
−
1
View file @
5f344aca
...
...
@@ -31,7 +31,6 @@ services:
MYSQL_PASSWORD
:
${DATABASE_PASSWORD}
volumes
:
-
mysql_data:/var/lib/mysql
-
./sql-setup/init.sql:/docker-entrypoint-initdb.d/init.sql
networks
:
-
network
...
...
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