mirror of
https://github.com/aljazceru/CTFd.git
synced 2025-12-17 22:14:25 +01:00
Format all the things (#991)
* Format Javascript and CSS files with `prettier`: `prettier --write 'CTFd/themes/**/*'` * Format Python with `black`: `black CTFd` & `black tests` * Travis now uses xenial instead of trusty.
This commit is contained in:
@@ -1,88 +1,77 @@
|
||||
from flask import session, request, abort
|
||||
from flask_restplus import Namespace, Resource
|
||||
from CTFd.models import db, Users, Solves, Awards, Tracking, Unlocks, Submissions, Notifications
|
||||
from CTFd.utils.decorators import (
|
||||
authed_only,
|
||||
admins_only,
|
||||
authed,
|
||||
ratelimit
|
||||
from CTFd.models import (
|
||||
db,
|
||||
Users,
|
||||
Solves,
|
||||
Awards,
|
||||
Tracking,
|
||||
Unlocks,
|
||||
Submissions,
|
||||
Notifications,
|
||||
)
|
||||
from CTFd.utils.decorators import authed_only, admins_only, authed, ratelimit
|
||||
from CTFd.cache import clear_standings
|
||||
from CTFd.utils.config import get_mail_provider
|
||||
from CTFd.utils.email import sendmail, user_created_notification
|
||||
from CTFd.utils.user import get_current_user, is_admin
|
||||
from CTFd.utils.decorators.visibility import check_account_visibility
|
||||
|
||||
from CTFd.utils.config.visibility import (
|
||||
accounts_visible,
|
||||
scores_visible
|
||||
)
|
||||
from CTFd.utils.config.visibility import accounts_visible, scores_visible
|
||||
|
||||
from CTFd.schemas.submissions import SubmissionSchema
|
||||
from CTFd.schemas.awards import AwardSchema
|
||||
from CTFd.schemas.users import UserSchema
|
||||
|
||||
|
||||
users_namespace = Namespace('users', description="Endpoint to retrieve Users")
|
||||
users_namespace = Namespace("users", description="Endpoint to retrieve Users")
|
||||
|
||||
|
||||
@users_namespace.route('')
|
||||
@users_namespace.route("")
|
||||
class UserList(Resource):
|
||||
@check_account_visibility
|
||||
def get(self):
|
||||
users = Users.query.filter_by(banned=False, hidden=False)
|
||||
response = UserSchema(view='user', many=True).dump(users)
|
||||
response = UserSchema(view="user", many=True).dump(users)
|
||||
|
||||
if response.errors:
|
||||
return {
|
||||
'success': False,
|
||||
'errors': response.errors
|
||||
}, 400
|
||||
return {"success": False, "errors": response.errors}, 400
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'data': response.data
|
||||
return {"success": True, "data": response.data}
|
||||
|
||||
@users_namespace.doc(
|
||||
params={
|
||||
"notify": "Whether to send the created user an email with their credentials"
|
||||
}
|
||||
|
||||
@users_namespace.doc(params={'notify': 'Whether to send the created user an email with their credentials'})
|
||||
)
|
||||
@admins_only
|
||||
def post(self):
|
||||
req = request.get_json()
|
||||
schema = UserSchema('admin')
|
||||
schema = UserSchema("admin")
|
||||
response = schema.load(req)
|
||||
|
||||
if response.errors:
|
||||
return {
|
||||
'success': False,
|
||||
'errors': response.errors
|
||||
}, 400
|
||||
return {"success": False, "errors": response.errors}, 400
|
||||
|
||||
db.session.add(response.data)
|
||||
db.session.commit()
|
||||
|
||||
if request.args.get('notify'):
|
||||
if request.args.get("notify"):
|
||||
name = response.data.name
|
||||
email = response.data.email
|
||||
password = req.get('password')
|
||||
password = req.get("password")
|
||||
|
||||
user_created_notification(
|
||||
addr=email,
|
||||
name=name,
|
||||
password=password
|
||||
)
|
||||
user_created_notification(addr=email, name=name, password=password)
|
||||
|
||||
clear_standings()
|
||||
|
||||
response = schema.dump(response.data)
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'data': response.data
|
||||
}
|
||||
return {"success": True, "data": response.data}
|
||||
|
||||
|
||||
@users_namespace.route('/<int:user_id>')
|
||||
@users_namespace.param('user_id', "User ID")
|
||||
@users_namespace.route("/<int:user_id>")
|
||||
@users_namespace.param("user_id", "User ID")
|
||||
class UserPublic(Resource):
|
||||
@check_account_visibility
|
||||
def get(self, user_id):
|
||||
@@ -91,36 +80,25 @@ class UserPublic(Resource):
|
||||
if (user.banned or user.hidden) and is_admin() is False:
|
||||
abort(404)
|
||||
|
||||
response = UserSchema(
|
||||
view=session.get('type', 'user')
|
||||
).dump(user)
|
||||
response = UserSchema(view=session.get("type", "user")).dump(user)
|
||||
|
||||
if response.errors:
|
||||
return {
|
||||
'success': False,
|
||||
'errors': response.errors
|
||||
}, 400
|
||||
return {"success": False, "errors": response.errors}, 400
|
||||
|
||||
response.data['place'] = user.place
|
||||
response.data['score'] = user.score
|
||||
response.data["place"] = user.place
|
||||
response.data["score"] = user.score
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'data': response.data
|
||||
}
|
||||
return {"success": True, "data": response.data}
|
||||
|
||||
@admins_only
|
||||
def patch(self, user_id):
|
||||
user = Users.query.filter_by(id=user_id).first_or_404()
|
||||
data = request.get_json()
|
||||
data['id'] = user_id
|
||||
schema = UserSchema(view='admin', instance=user, partial=True)
|
||||
data["id"] = user_id
|
||||
schema = UserSchema(view="admin", instance=user, partial=True)
|
||||
response = schema.load(data)
|
||||
if response.errors:
|
||||
return {
|
||||
'success': False,
|
||||
'errors': response.errors
|
||||
}, 400
|
||||
return {"success": False, "errors": response.errors}, 400
|
||||
|
||||
db.session.commit()
|
||||
|
||||
@@ -130,10 +108,7 @@ class UserPublic(Resource):
|
||||
|
||||
clear_standings()
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'data': response
|
||||
}
|
||||
return {"success": True, "data": response}
|
||||
|
||||
@admins_only
|
||||
def delete(self, user_id):
|
||||
@@ -149,35 +124,27 @@ class UserPublic(Resource):
|
||||
|
||||
clear_standings()
|
||||
|
||||
return {
|
||||
'success': True
|
||||
}
|
||||
return {"success": True}
|
||||
|
||||
|
||||
@users_namespace.route('/me')
|
||||
@users_namespace.route("/me")
|
||||
class UserPrivate(Resource):
|
||||
@authed_only
|
||||
def get(self):
|
||||
user = get_current_user()
|
||||
response = UserSchema('self').dump(user).data
|
||||
response['place'] = user.place
|
||||
response['score'] = user.score
|
||||
return {
|
||||
'success': True,
|
||||
'data': response
|
||||
}
|
||||
response = UserSchema("self").dump(user).data
|
||||
response["place"] = user.place
|
||||
response["score"] = user.score
|
||||
return {"success": True, "data": response}
|
||||
|
||||
@authed_only
|
||||
def patch(self):
|
||||
user = get_current_user()
|
||||
data = request.get_json()
|
||||
schema = UserSchema(view='self', instance=user, partial=True)
|
||||
schema = UserSchema(view="self", instance=user, partial=True)
|
||||
response = schema.load(data)
|
||||
if response.errors:
|
||||
return {
|
||||
'success': False,
|
||||
'errors': response.errors
|
||||
}, 400
|
||||
return {"success": False, "errors": response.errors}, 400
|
||||
|
||||
db.session.commit()
|
||||
|
||||
@@ -186,17 +153,14 @@ class UserPrivate(Resource):
|
||||
|
||||
clear_standings()
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'data': response.data
|
||||
}
|
||||
return {"success": True, "data": response.data}
|
||||
|
||||
|
||||
@users_namespace.route('/<user_id>/solves')
|
||||
@users_namespace.param('user_id', "User ID or 'me'")
|
||||
@users_namespace.route("/<user_id>/solves")
|
||||
@users_namespace.param("user_id", "User ID or 'me'")
|
||||
class UserSolves(Resource):
|
||||
def get(self, user_id):
|
||||
if user_id == 'me':
|
||||
if user_id == "me":
|
||||
if not authed():
|
||||
abort(403)
|
||||
user = get_current_user()
|
||||
@@ -210,26 +174,20 @@ class UserSolves(Resource):
|
||||
abort(404)
|
||||
solves = user.get_solves(admin=is_admin())
|
||||
|
||||
view = 'user' if not is_admin() else 'admin'
|
||||
view = "user" if not is_admin() else "admin"
|
||||
response = SubmissionSchema(view=view, many=True).dump(solves)
|
||||
|
||||
if response.errors:
|
||||
return {
|
||||
'success': False,
|
||||
'errors': response.errors
|
||||
}, 400
|
||||
return {"success": False, "errors": response.errors}, 400
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'data': response.data
|
||||
}
|
||||
return {"success": True, "data": response.data}
|
||||
|
||||
|
||||
@users_namespace.route('/<user_id>/fails')
|
||||
@users_namespace.param('user_id', "User ID or 'me'")
|
||||
@users_namespace.route("/<user_id>/fails")
|
||||
@users_namespace.param("user_id", "User ID or 'me'")
|
||||
class UserFails(Resource):
|
||||
def get(self, user_id):
|
||||
if user_id == 'me':
|
||||
if user_id == "me":
|
||||
if not authed():
|
||||
abort(403)
|
||||
user = get_current_user()
|
||||
@@ -243,13 +201,10 @@ class UserFails(Resource):
|
||||
abort(404)
|
||||
fails = user.get_fails(admin=is_admin())
|
||||
|
||||
view = 'user' if not is_admin() else 'admin'
|
||||
view = "user" if not is_admin() else "admin"
|
||||
response = SubmissionSchema(view=view, many=True).dump(fails)
|
||||
if response.errors:
|
||||
return {
|
||||
'success': False,
|
||||
'errors': response.errors
|
||||
}, 400
|
||||
return {"success": False, "errors": response.errors}, 400
|
||||
|
||||
if is_admin():
|
||||
data = response.data
|
||||
@@ -257,20 +212,14 @@ class UserFails(Resource):
|
||||
data = []
|
||||
count = len(response.data)
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'data': data,
|
||||
'meta': {
|
||||
'count': count
|
||||
}
|
||||
}
|
||||
return {"success": True, "data": data, "meta": {"count": count}}
|
||||
|
||||
|
||||
@users_namespace.route('/<user_id>/awards')
|
||||
@users_namespace.param('user_id', "User ID or 'me'")
|
||||
@users_namespace.route("/<user_id>/awards")
|
||||
@users_namespace.param("user_id", "User ID or 'me'")
|
||||
class UserAwards(Resource):
|
||||
def get(self, user_id):
|
||||
if user_id == 'me':
|
||||
if user_id == "me":
|
||||
if not authed():
|
||||
abort(403)
|
||||
user = get_current_user()
|
||||
@@ -284,57 +233,37 @@ class UserAwards(Resource):
|
||||
abort(404)
|
||||
awards = user.get_awards(admin=is_admin())
|
||||
|
||||
view = 'user' if not is_admin() else 'admin'
|
||||
view = "user" if not is_admin() else "admin"
|
||||
response = AwardSchema(view=view, many=True).dump(awards)
|
||||
|
||||
if response.errors:
|
||||
return {
|
||||
'success': False,
|
||||
'errors': response.errors
|
||||
}, 400
|
||||
return {"success": False, "errors": response.errors}, 400
|
||||
|
||||
return {
|
||||
'success': True,
|
||||
'data': response.data
|
||||
}
|
||||
return {"success": True, "data": response.data}
|
||||
|
||||
|
||||
@users_namespace.route('/<int:user_id>/email')
|
||||
@users_namespace.param('user_id', "User ID")
|
||||
@users_namespace.route("/<int:user_id>/email")
|
||||
@users_namespace.param("user_id", "User ID")
|
||||
class UserEmails(Resource):
|
||||
@admins_only
|
||||
@ratelimit(method="POST", limit=10, interval=60)
|
||||
def post(self, user_id):
|
||||
req = request.get_json()
|
||||
text = req.get('text', '').strip()
|
||||
text = req.get("text", "").strip()
|
||||
user = Users.query.filter_by(id=user_id).first_or_404()
|
||||
|
||||
if get_mail_provider() is None:
|
||||
return {
|
||||
'success': False,
|
||||
'errors': {
|
||||
"": [
|
||||
"Email settings not configured"
|
||||
]
|
||||
}
|
||||
}, 400
|
||||
return (
|
||||
{"success": False, "errors": {"": ["Email settings not configured"]}},
|
||||
400,
|
||||
)
|
||||
|
||||
if not text:
|
||||
return {
|
||||
'success': False,
|
||||
'errors': {
|
||||
"text": [
|
||||
"Email text cannot be empty"
|
||||
]
|
||||
}
|
||||
}, 400
|
||||
return (
|
||||
{"success": False, "errors": {"text": ["Email text cannot be empty"]}},
|
||||
400,
|
||||
)
|
||||
|
||||
result, response = sendmail(
|
||||
addr=user.email,
|
||||
text=text
|
||||
)
|
||||
result, response = sendmail(addr=user.email, text=text)
|
||||
|
||||
return {
|
||||
'success': result,
|
||||
'data': {}
|
||||
}
|
||||
return {"success": result, "data": {}}
|
||||
|
||||
Reference in New Issue
Block a user