Skip to content

Commit 66ff9c0

Browse files
authoredNov 17, 2020
Add import export commands to manage.py (CTFd#1723)
* Add `import_ctf` and `export_ctf` commands to `manage.py` * Deprecate `import.py` and `export.py` * Works on CTFd#1629
1 parent 1e9c0b4 commit 66ff9c0

File tree

3 files changed

+41
-5
lines changed

3 files changed

+41
-5
lines changed
 

‎export.py

+3
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99

1010
app = create_app()
1111
with app.app_context():
12+
print(
13+
"This file will be deleted in CTFd v4.0. Switch to using `python manage.py export_ctf`"
14+
)
1215
backup = export_ctf()
1316

1417
if len(sys.argv) > 1:

‎import.py

+3
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,7 @@
88

99
app = create_app()
1010
with app.app_context():
11+
print(
12+
"This file will be deleted in CTFd v4.0. Switch to using `python manage.py import_ctf`"
13+
)
1114
import_ctf(sys.argv[1])

‎manage.py

+35-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1-
from flask import Flask
2-
from flask_sqlalchemy import SQLAlchemy
1+
import datetime
2+
import shutil
3+
4+
from flask_migrate import MigrateCommand
35
from flask_script import Manager
4-
from flask_migrate import Migrate, MigrateCommand
6+
57
from CTFd import create_app
6-
from CTFd.utils import get_config as get_config_util, set_config as set_config_util
7-
from CTFd.models import *
8+
from CTFd.utils import get_config as get_config_util
9+
from CTFd.utils import set_config as set_config_util
10+
from CTFd.utils.config import ctf_name
11+
from CTFd.utils.exports import export_ctf as export_ctf_util
12+
from CTFd.utils.exports import import_ctf as import_ctf_util
813

914
app = create_app()
1015

@@ -46,5 +51,30 @@ def build(cmd):
4651
cmd()
4752

4853

54+
@manager.command
55+
def export_ctf(path=None):
56+
with app.app_context():
57+
backup = export_ctf_util()
58+
59+
if path:
60+
with open(path, "wb") as target:
61+
shutil.copyfileobj(backup, target)
62+
else:
63+
name = ctf_name()
64+
day = datetime.datetime.now().strftime("%Y-%m-%d")
65+
full_name = f"{name}.{day}.zip"
66+
67+
with open(full_name, "wb") as target:
68+
shutil.copyfileobj(backup, target)
69+
70+
print(f"Exported {full_name}")
71+
72+
73+
@manager.command
74+
def import_ctf(path):
75+
with app.app_context():
76+
import_ctf_util(path)
77+
78+
4979
if __name__ == "__main__":
5080
manager.run()

0 commit comments

Comments
 (0)