Skip to content

Commit

Permalink
Add import export commands to manage.py (CTFd#1723)
Browse files Browse the repository at this point in the history
* Add `import_ctf` and `export_ctf` commands to `manage.py`
* Deprecate `import.py` and `export.py`
* Works on CTFd#1629
  • Loading branch information
ColdHeat authored Nov 17, 2020
1 parent 1e9c0b4 commit 66ff9c0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
3 changes: 3 additions & 0 deletions export.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

app = create_app()
with app.app_context():
print(
"This file will be deleted in CTFd v4.0. Switch to using `python manage.py export_ctf`"
)
backup = export_ctf()

if len(sys.argv) > 1:
Expand Down
3 changes: 3 additions & 0 deletions import.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,7 @@

app = create_app()
with app.app_context():
print(
"This file will be deleted in CTFd v4.0. Switch to using `python manage.py import_ctf`"
)
import_ctf(sys.argv[1])
40 changes: 35 additions & 5 deletions manage.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
import datetime
import shutil

from flask_migrate import MigrateCommand
from flask_script import Manager
from flask_migrate import Migrate, MigrateCommand

from CTFd import create_app
from CTFd.utils import get_config as get_config_util, set_config as set_config_util
from CTFd.models import *
from CTFd.utils import get_config as get_config_util
from CTFd.utils import set_config as set_config_util
from CTFd.utils.config import ctf_name
from CTFd.utils.exports import export_ctf as export_ctf_util
from CTFd.utils.exports import import_ctf as import_ctf_util

app = create_app()

Expand Down Expand Up @@ -46,5 +51,30 @@ def build(cmd):
cmd()


@manager.command
def export_ctf(path=None):
with app.app_context():
backup = export_ctf_util()

if path:
with open(path, "wb") as target:
shutil.copyfileobj(backup, target)
else:
name = ctf_name()
day = datetime.datetime.now().strftime("%Y-%m-%d")
full_name = f"{name}.{day}.zip"

with open(full_name, "wb") as target:
shutil.copyfileobj(backup, target)

print(f"Exported {full_name}")


@manager.command
def import_ctf(path):
with app.app_context():
import_ctf_util(path)


if __name__ == "__main__":
manager.run()

0 comments on commit 66ff9c0

Please sign in to comment.