Add files model to database

This commit is contained in:
James Wang 2016-01-02 15:45:05 -05:00
parent 53b4b10947
commit 25fb22bc5c
5 changed files with 35 additions and 3 deletions

1
.gitignore vendored
View file

@ -2,6 +2,7 @@
.secret_key
.bundle/config
logs/
uploads/
# Object files
*.o

View file

@ -53,6 +53,15 @@ class Problems(db.Model):
self.flag = flag
self.value = value
class Files(db.Model):
fid = db.Column(db.Integer, primary_key=True)
pid = db.Column(db.Integer)
location = db.Column(db.Text)
def __init__(self, pid, location):
self.pid = pid
self.location = location
class Solves(db.Model):
sid = db.Column(db.Integer, primary_key=True)
pid = db.Column(db.Integer)

View file

@ -1,9 +1,11 @@
import hashlib
import logger
from flask import Blueprint, session, request
from flask import current_app as app
from werkzeug import secure_filename
from models import db, Problems, Solves, Teams
from models import db, Files, Problems, Solves, Teams
from decorators import admins_only, api_wrapper, login_required
blueprint = Blueprint("problem", __name__)
@ -20,12 +22,29 @@ def problem_add():
value = request.form["value"]
name_exists = Problems.query.filter_by(name=name).first()
if name_exists:
return { "success":0, "message": "Problem name already taken." }
problem = Problems(name, category, description, hint, flag, value)
db.session.add(problem)
files = request.files["imp-files"]
for _file in files:
filename = secure_filename(_file.filename)
if len(filename) == 0:
continue
folder = problem.name.replace(" ", "-")
folder_path = os.path.join(os.path.normpath(app["UPLOAD_FOLDER"], folder))
if not folder_path:
os.makedirs(folder_path)
file_path = os.path.join(path, filename)
_file.save(file_path)
db_file = Files(problem.pid, file_path)
db.session.add(db_file)
db.session.commit()
return { "success": 1, "message": "Success!" }

View file

@ -9,9 +9,10 @@ app = Flask(__name__)
app.config["SQLALCHEMY_DATABASE_URI"] = config.SQLALCHEMY_DATABASE_URI
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = config.SQLALCHEMY_TRACK_MODIFICATIONS
app.config["UPLOAD_FOLDER"] = config.UPLOAD_FOLDER
with app.app_context():
from api.models import db, Teams, Problems, Solves, Users
from api.models import db, Files, Teams, Problems, Solves, Users
db.init_app(app)
db.create_all()

View file

@ -15,5 +15,7 @@ SECRET_KEY = key
SQLALCHEMY_DATABASE_URI = "mysql://root:i_hate_passwords@localhost/easyctf"
SQLALCHEMY_TRACK_MODIFICATIONS = False
UPLOAD_FOLDER = os.path.normpath("uploads")
CTF_BEGIN = 0 # To be used later
CTF_END = 0 # To be used later