Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions server/entities.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
group_admin = "admin"


class config:
class Config:
db_config = conf["db"]
ui_config = conf["ui"]
jwt_config = conf["jwt"]
Expand Down Expand Up @@ -61,13 +61,13 @@ def generate_token(username, group):
"group": group,
"exp": datetime.datetime.utcnow() + datetime.timedelta(days=5)
},
config.secret, algorithm="HS256"
Config.secret, algorithm="HS256"
)


def decode_token(token):
try:
obj = jwt.decode(token, config.secret, algorithms=["HS256"])
obj = jwt.decode(token, Config.secret, algorithms=["HS256"])
user = obj["username"]
group = obj["group"]
except:
Expand All @@ -78,7 +78,7 @@ def decode_token(token):

def filename_validation(fn):
fn = str(fn)
if fn.split(".")[-1] not in config.allow_file_type:
if fn.split(".")[-1] not in Config.allow_file_type:
return False
else:
return True
Expand Down Expand Up @@ -132,15 +132,15 @@ def _calculate_ranking():
from main import app

with app.app_context():
from model import Project, Score_weight, db
from model import Project, ScoreWeight, db

year = Project.query.with_entities(
Project.year).group_by(Project.year).all()
for y in year:
y = y[0]
projects = to_obj_list(Project.query.filter_by(year=y).all())
score_weight = to_obj_list(
Score_weight.query.filter_by(year=y).all())
ScoreWeight.query.filter_by(year=y).all())
score_weight = list(
map(
lambda x: (
Expand Down
4 changes: 2 additions & 2 deletions server/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
from flask import Flask, jsonify, Blueprint
from flask_restful import Api
import model
from entities import config
from entities import Config
from flask_cors import CORS

app = Flask(__name__)
app.config["SQLALCHEMY_TRACK_MODIFICATIONS"] = False
app.config["JSONIFY_PRETTYPRINT_REGULAR"] = False
app.config[
"SQLALCHEMY_DATABASE_URI"
] = f"{config.db_protocol}://{config.db_user}:{config.db_pass}@{config.db_host}:{config.db_port}/{config.db_name}"
] = f"{Config.db_protocol}://{Config.db_user}:{Config.db_pass}@{Config.db_host}:{Config.db_port}/{Config.db_name}"
app.config["MAX_CONTENT_LENGTH"] = 300 * 1024 * 1024
app.config["PROPAGATE_EXCEPTIONS"] = True
model.db.init_app(app)
Expand Down
6 changes: 3 additions & 3 deletions server/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ def to_obj(self):
}


class Project_score(db.Model):
class ProjectScore(db.Model):
__tablename__ = "project_score"
id = db.Column(db.Integer, primary_key=True, unique=True)
project_id = db.Column(db.Integer, db.ForeignKey("projects.id"))
Expand All @@ -92,7 +92,7 @@ def to_obj(self):
}


class Score_classification(db.Model):
class ScoreClassification(db.Model):
__tablename__ = "score_classification"
id = db.Column(db.Integer, primary_key=True, unique=True)
description = db.Column(db.String)
Expand All @@ -110,7 +110,7 @@ def to_obj(self):
}


class Score_weight(db.Model):
class ScoreWeight(db.Model):
__tablename__ = "score_weight"
id = db.Column(db.Integer, primary_key=True, unique=True)
year = db.Column(db.Integer)
Expand Down
16 changes: 8 additions & 8 deletions server/resources/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
api = Api(api_bp)


class login(Resource):
class Login(Resource):
def get(self):
res = entities.check_token(request.headers["Authorization"])
if res == None:
Expand Down Expand Up @@ -41,7 +41,7 @@ def post(self):
return err.account_error


class change_password(Resource):
class ChangePassword(Resource):
def post(self):
data = request.json
res = entities.check_token(request.headers["Authorization"])
Expand Down Expand Up @@ -71,7 +71,7 @@ def post(self):
return err.account_error


class upload(Resource):
class Upload(Resource):
def post(self):
res = entities.check_token(request.headers["Authorization"])
if res == None:
Expand Down Expand Up @@ -100,14 +100,14 @@ def post(self):
folder = os.path.join(
os.path.abspath(__file__ + "/.."),
"../..",
entities.config.upload_path,
entities.Config.upload_path,
uuid,
)
if not os.path.exists(folder):
os.mkdir(folder)
path = os.path.join(folder, fn)
f.save(path)
fn = os.path.join(entities.config.url_prefix, f"./{uuid}/", fn)
fn = os.path.join(entities.Config.url_prefix, f"./{uuid}/", fn)
db_f = None
if args["type"] == "file":
db_f = model.File(location=fn)
Expand All @@ -120,7 +120,7 @@ def post(self):
return {"status": "success", "link": fn}


api.add_resource(login, "/auth")
api.add_resource(upload, "/upload")
api.add_resource(change_password, "/change_password")
api.add_resource(Login, "/auth")
api.add_resource(Upload, "/upload")
api.add_resource(ChangePassword, "/change_password")
app.register_blueprint(api_bp)
8 changes: 4 additions & 4 deletions server/resources/projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
api = Api(api_bp)


class projects(Resource):
class Projects(Resource):
def get(self, id=None, uuid=None):
if id != None:
result = model.Project.query.get(id)
Expand Down Expand Up @@ -191,7 +191,7 @@ def put(self, id=None, uuid=None):
return err.not_allow_error


class get_students_by_project(Resource):
class GetStudentsByProject(Resource):
def get(self, uuid):
res = entities.check_token(request.headers["Authorization"])
if res == None:
Expand All @@ -208,6 +208,6 @@ def get(self, uuid):
return stu_obj


api.add_resource(projects, "/", "/<int:id>", "/<string:uuid>")
api.add_resource(get_students_by_project, "/<string:uuid>/students")
api.add_resource(Projects, "/", "/<int:id>", "/<string:uuid>")
api.add_resource(GetStudentsByProject, "/<string:uuid>/students")
app.register_blueprint(api_bp)
4 changes: 2 additions & 2 deletions server/resources/ranking.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
api = Api(api_bp)


class ranking(Resource):
class Ranking(Resource):
def get(self):
parser = reqparse.RequestParser()
parser.add_argument("year", type=int)
Expand Down Expand Up @@ -53,5 +53,5 @@ def get(self):
return res


api.add_resource(ranking, "/")
api.add_resource(Ranking, "/")
app.register_blueprint(api_bp)
46 changes: 23 additions & 23 deletions server/resources/scores.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@
api = Api(api_bp)


class scores_weight(Resource):
class ScoresWeight(Resource):
def get(self):
res = entities.check_token(request.headers["Authorization"])
if res == None:
raise Exception("invalid token")
_, group = res
if group != entities.group_admin:
return err.not_allow_error
weight_data = entities.to_obj_list(model.Score_weight.query.all())
weight_data = entities.to_obj_list(model.ScoreWeight.query.all())
return weight_data

def put(self):
Expand All @@ -37,11 +37,11 @@ def put(self):
year = int(c["year"])
classification_id = int(c["classification_id"])
weight = int(c["weight"])
score_obj = model.Score_weight.query.filter_by(
score_obj = model.ScoreWeight.query.filter_by(
year=year, score_classification_id=classification_id
).first()
if score_obj == None:
new_score_obj = model.Score_weight(
new_score_obj = model.ScoreWeight(
year=year, score_classification_id=classification_id, weight=weight
)
model.db.session.add(new_score_obj)
Expand All @@ -52,10 +52,10 @@ def put(self):
return {"status": "success"}


class scores_classification(Resource):
class ScoresClassification(Resource):
def get(self):
classification_data = entities.to_obj_list(
model.Score_classification.query.filter_by(enabled=True).all()
model.ScoreClassification.query.filter_by(enabled=True).all()
)
return classification_data

Expand All @@ -69,7 +69,7 @@ def post(self):
_, group = res
if group != entities.group_admin:
return err.not_allow_error
score_classification_obj = model.Score_classification(
score_classification_obj = model.ScoreClassification(
description=description, is_global=is_global, enabled=True
)
model.db.session.add(score_classification_obj)
Expand All @@ -87,7 +87,7 @@ def put(self):
_, group = res
if group != entities.group_admin:
return err.not_allow_error
score_classification_obj = model.Score_classification.query.get(id)
score_classification_obj = model.ScoreClassification.query.get(id)
score_classification_obj.description = description
score_classification_obj.is_global = is_global
model.db.session.commit()
Expand All @@ -102,12 +102,12 @@ def delete(self):
_, group = res
if group != entities.group_admin:
return err.not_allow_error
model.Score_classification.query.filter_by(id=id).first().enabled = False
model.ScoreClassification.query.filter_by(id=id).first().enabled = False
model.db.session.commit()
return {"status": "success"}


class set_scores(Resource):
class SetScores(Resource):
def post(self):
data = request.json
res = entities.check_token(request.headers["Authorization"])
Expand Down Expand Up @@ -137,10 +137,10 @@ def post(self):
classification_id = int(c["classification_id"])
score = float(c["score"])
project_id = model.Project.query.filter_by(uuid=uuid).first().id
model.Project_score.query.filter_by(
model.ProjectScore.query.filter_by(
project_id=project_id, score_classification_id=classification_id
).delete()
new_score = model.Project_score(
new_score = model.ProjectScore(
project_id=project_id,
score_classification_id=classification_id,
score=score,
Expand All @@ -152,7 +152,7 @@ def post(self):
return {"status": "success"}


class import_scores(Resource):
class ImportScores(Resource):
def post(self):
data = request.json
res = entities.check_token(request.headers["Authorization"])
Expand All @@ -169,10 +169,10 @@ def post(self):
if len(__group) * len(__score) == 0:
continue
project_id = model.Project.query.filter_by(uuid=__group).first().id
model.Project_score.query.filter_by(
model.ProjectScore.query.filter_by(
project_id=project_id, score_classification_id=classification_id
).delete()
new_score = model.Project_score(
new_score = model.ProjectScore(
project_id=project_id,
score_classification_id=classification_id,
score=__score,
Expand All @@ -183,7 +183,7 @@ def post(self):
return {"status": "success"}


class export_scores(Resource):
class ExportScores(Resource):
def post(self):
res = entities.check_token(request.headers["Authorization"])
if res == None:
Expand All @@ -202,10 +202,10 @@ def post(self):
list(map(lambda x: x.to_detail_scores(), students)),
key=lambda x: x["username"],
)
weight = entities.to_obj_list(model.Score_weight.query.filter_by(year=year))
weight = entities.to_obj_list(model.ScoreWeight.query.filter_by(year=year))

classification = entities.to_obj_list(
model.Score_classification.query.filter_by(enabled=True)
model.ScoreClassification.query.filter_by(enabled=True)
)

csv_header = (
Expand Down Expand Up @@ -293,9 +293,9 @@ def post(self):
return send_from_directory(csv_dir, csv_file, as_attachment=True)


api.add_resource(scores_weight, "/scores/weight")
api.add_resource(scores_classification, "/scores/classification")
api.add_resource(set_scores, "/scores")
api.add_resource(import_scores, "/import_scores")
api.add_resource(export_scores, "/export_scores")
api.add_resource(ScoresWeight, "/scores/weight")
api.add_resource(ScoresClassification, "/scores/classification")
api.add_resource(SetScores, "/scores")
api.add_resource(ImportScores, "/import_scores")
api.add_resource(ExportScores, "/export_scores")
app.register_blueprint(api_bp)
Loading