Skip to content

Commit 08bf52a

Browse files
committed
db: Handle mysql database.
1 parent 95c3218 commit 08bf52a

File tree

4 files changed

+50
-14
lines changed

4 files changed

+50
-14
lines changed

README.md

+30-2
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,14 @@ Ziho is a prototype for a better online interface for Anki than [Anki Web](https
2727
Ziho allows you to create, study and easily share and clone decks, as well as edit them online.
2828

2929
The backend is built with **Flask**, and the [fsrs](https://github.com/open-spaced-repetition) algorithm is used for spaced repetition.
30+
Following were also used:
31+
- Bootstrap
32+
- SQLAlchemy
33+
- Docker
3034

31-
<span><img src="https://shanukun.github.io/static/images/ziho/home.png" width="30%"></span>
32-
<span><img src="https://shanukun.github.io/static/images/ziho/study-deck.png" width="25%"></span>
35+
36+
<span><img src="https://shanukun.github.io/static/images/ziho/home.png" width="40%"></span>
37+
<span><img src="https://shanukun.github.io/static/images/ziho/study-deck.png" width="35%"></span>
3338

3439
You can find more screenshots [here](https://shanukun.github.io/ziho-ss/).
3540

@@ -58,7 +63,30 @@ You can find more screenshots [here](https://shanukun.github.io/ziho-ss/).
5863
./tools/setup
5964
```
6065
3. **Set up your environment variables in a `.env` file: To run without debug mode, set `DEBUG_MODE=off`.**
66+
- Set following for using MySQL
67+
- MYSQL_USER
68+
- MYSQL_PASS
69+
- MYSQL_HOST
70+
- MYSQL_PORT
71+
- MYSQL_DB
6172
4. **Start the webapp:**
6273
```bash
6374
./tools/run-ziho
6475
```
76+
77+
## Contributing
78+
79+
1. Follow the code style. Run the following to test the format.
80+
```bash
81+
./tools/test_lint
82+
```
83+
2. Format the code using:
84+
```bash
85+
./tools/lint_and_format
86+
```
87+
3. Make sure to add the tests for new routes
88+
```bash
89+
pytest ./tests
90+
```
91+
92+

config.py

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import os
2+
import sys
23
from pathlib import Path
34

45
from dotenv import load_dotenv
@@ -7,17 +8,24 @@
78
load_dotenv()
89

910

10-
DATABASE = "sqlite:///" + os.path.join(basedir, "app.db")
11+
def z_env(env_var: str) -> str | None:
12+
var_val = os.environ.get(env_var, None)
13+
return var_val
14+
15+
16+
DATABASE_URL = "sqlite:///" + os.path.join(basedir, "app.db")
1117
if os.environ.get("DATABASE") == "mysql":
12-
pass
18+
DATABASE_URL = f"mysql+pymysql://{z_env('MYSQL_USER')}:{z_env('MYSQL_PASS')}@{z_env('MYSQL_HOST')}:{z_env('MYSQL_PORT')}/{z_env('MYSQL_DB')}"
1319

1420

1521
class Config:
16-
SECRET_KEY = os.environ.get("SECRET_KEY") or "hardcoded-string"
22+
DATABASE = z_env("DATABASE")
23+
SECRET_KEY = z_env("SECRET_KEY") or "hardcoded-string"
1724

1825
# sqlalchemy
19-
SQLALCHEMY_DATABASE_URI = os.environ.get("DATABASE_URL") or DATABASE
26+
SQLALCHEMY_DATABASE_URI = DATABASE_URL
2027
SQLALCHEMY_TRACK_MODIFICATIONS = False
28+
2129
UPLOAD_FOLDER = Path("ziho_uploads")
2230
UPLOAD_PATH = Path(basedir) / "ziho" / UPLOAD_FOLDER
2331

tools/setup

-1
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,3 @@ curl -fLO --retry 3 "https://github.com/sass/dart-sass/releases/download/$versio
3232
tar -xf "$tarball" --strip-components=1 -C "$CWD/bin/sass"
3333

3434
"$CWD"/bin/sass/sass --no-source-map --style compressed "$CWD"/ziho/static/scss/main.scss "$CWD"/ziho/static/css/main.css
35-

ziho/__init__.py

+8-7
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ def create_app(config_class=Config):
2828

2929
db.init_app(app)
3030

31-
with app.app_context():
32-
33-
@event.listens_for(db.engine, "connect")
34-
def set_sqlite_pragma(dbapi_connection, connection_record):
35-
cursor = dbapi_connection.cursor()
36-
cursor.execute("PRAGMA foreign_keys=ON")
37-
cursor.close()
31+
if config_class.DATABASE != "mysql":
32+
with app.app_context():
33+
34+
@event.listens_for(db.engine, "connect")
35+
def set_sqlite_pragma(dbapi_connection, connection_record):
36+
cursor = dbapi_connection.cursor()
37+
cursor.execute("PRAGMA foreign_keys=ON")
38+
cursor.close()
3839

3940
migrate.init_app(app, db, render_as_batch=True)
4041
login_manager.init_app(app)

0 commit comments

Comments
 (0)