[Plugin] Schedule: Identify EventTypes by ID not name
This commit is contained in:
parent
d2854315c1
commit
30305c26cc
|
@ -32,20 +32,26 @@ class SchedulePlugin(Plugin):
|
||||||
|
|
||||||
|
|
||||||
@schedule_bp.route("/event-types", methods=["GET"])
|
@schedule_bp.route("/event-types", methods=["GET"])
|
||||||
|
@schedule_bp.route("/event-types/<identifier:int>", methods=["GET"])
|
||||||
@login_required()
|
@login_required()
|
||||||
def get_event_types(current_session):
|
def get_event_types(current_session, identifier=None):
|
||||||
"""Get all EventTypes
|
"""Get EventType(s)
|
||||||
|
|
||||||
Route: ``/schedule/event-types`` | Method: ``GET``
|
Route: ``/schedule/event-types`` | Method: ``GET``
|
||||||
|
Route: ``/schedule/event-types/<identifier>`` | Method: ``GET``
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
current_session: Session sent with Authorization Header
|
current_session: Session sent with Authorization Header
|
||||||
|
identifier: If querying a specific EventType
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
JSON encoded list of EventTypes HTTP-error
|
JSON encoded (list of) EventType(s) or HTTP-error
|
||||||
"""
|
"""
|
||||||
types = event_controller.get_event_types()
|
if identifier:
|
||||||
return jsonify(types)
|
result = event_controller.get_event_type(identifier)
|
||||||
|
else:
|
||||||
|
result = event_controller.get_event_types()
|
||||||
|
return jsonify(result)
|
||||||
|
|
||||||
|
|
||||||
@schedule_bp.route("/event-types", methods=["POST"])
|
@schedule_bp.route("/event-types", methods=["POST"])
|
||||||
|
@ -66,33 +72,33 @@ def new_event_type(current_session):
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
if "name" not in data:
|
if "name" not in data:
|
||||||
raise BadRequest
|
raise BadRequest
|
||||||
event = event_controller.create_event_type(data["name"])
|
event_type = event_controller.create_event_type(data["name"])
|
||||||
return jsonify(event)
|
return jsonify(event_type)
|
||||||
|
|
||||||
|
|
||||||
@schedule_bp.route("/event-types/<name>", methods=["PUT", "DELETE"])
|
@schedule_bp.route("/event-types/<identifier:int>", methods=["PUT", "DELETE"])
|
||||||
@login_required(permission=permissions.EVENT_TYPE)
|
@login_required(permission=permissions.EVENT_TYPE)
|
||||||
def modify_event_type(name, current_session):
|
def modify_event_type(identifier, current_session):
|
||||||
"""Rename or delete an event type
|
"""Rename or delete an event type
|
||||||
|
|
||||||
Route: ``/schedule/event-types/<name>`` | Method: ``PUT`` or ``DELETE``
|
Route: ``/schedule/event-types/<id>`` | Method: ``PUT`` or ``DELETE``
|
||||||
|
|
||||||
POST-data: (if renaming) ``{name: string}``
|
POST-data: (if renaming) ``{name: string}``
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
name: Name identifying the EventType
|
identifier: Identifier of the EventType
|
||||||
current_session: Session sent with Authorization Header
|
current_session: Session sent with Authorization Header
|
||||||
|
|
||||||
Returns:
|
Returns:
|
||||||
HTTP-NoContent or HTTP-error
|
HTTP-NoContent or HTTP-error
|
||||||
"""
|
"""
|
||||||
if request.method == "DELETE":
|
if request.method == "DELETE":
|
||||||
event_controller.delete_event_type(name)
|
event_controller.delete_event_type(identifier)
|
||||||
else:
|
else:
|
||||||
data = request.get_json()
|
data = request.get_json()
|
||||||
if "name" not in data:
|
if "name" not in data:
|
||||||
raise BadRequest("Parameter missing in data")
|
raise BadRequest("Parameter missing in data")
|
||||||
event_controller.rename_event_type(name, data["name"])
|
event_controller.rename_event_type(identifier, data["name"])
|
||||||
return "", NO_CONTENT
|
return "", NO_CONTENT
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -14,8 +14,13 @@ def get_event_types():
|
||||||
return EventType.query.all()
|
return EventType.query.all()
|
||||||
|
|
||||||
|
|
||||||
def get_event_type(name):
|
def get_event_type(identifier):
|
||||||
et = EventType.query.filter(EventType.name == name).one_or_none()
|
"""Get EventType by ID (int) or name (string)"""
|
||||||
|
|
||||||
|
if isinstance(identifier, int):
|
||||||
|
et = EventType.query.get(identifier)
|
||||||
|
else:
|
||||||
|
et = EventType.query.filter(EventType.name == identifier).one_or_none()
|
||||||
if not et:
|
if not et:
|
||||||
raise NotFound
|
raise NotFound
|
||||||
return et
|
return et
|
||||||
|
@ -31,8 +36,8 @@ def create_event_type(name):
|
||||||
raise BadRequest("Name already exists")
|
raise BadRequest("Name already exists")
|
||||||
|
|
||||||
|
|
||||||
def rename_event_type(name, new_name):
|
def rename_event_type(identifier, new_name):
|
||||||
event_type = get_event_type(name)
|
event_type = get_event_type(identifier)
|
||||||
event_type.name = new_name
|
event_type.name = new_name
|
||||||
try:
|
try:
|
||||||
db.session.commit()
|
db.session.commit()
|
||||||
|
|
|
@ -14,13 +14,13 @@ from flaschengeist.database import db
|
||||||
|
|
||||||
class EventType(db.Model, ModelSerializeMixin):
|
class EventType(db.Model, ModelSerializeMixin):
|
||||||
__tablename__ = "schedule_event_type"
|
__tablename__ = "schedule_event_type"
|
||||||
id: int = db.Column("id", db.Integer, primary_key=True)
|
id: int = db.Column(db.Integer, primary_key=True)
|
||||||
name: str = db.Column(db.String(30), nullable=False, unique=True)
|
name: str = db.Column(db.String(30), nullable=False, unique=True)
|
||||||
|
|
||||||
|
|
||||||
class JobType(db.Model, ModelSerializeMixin):
|
class JobType(db.Model, ModelSerializeMixin):
|
||||||
__tablename__ = "schedule_job_type"
|
__tablename__ = "schedule_job_type"
|
||||||
id: int = db.Column("id", db.Integer, primary_key=True)
|
id: int = db.Column(db.Integer, primary_key=True)
|
||||||
name: str = db.Column(db.String(30), nullable=False, unique=True)
|
name: str = db.Column(db.String(30), nullable=False, unique=True)
|
||||||
|
|
||||||
|
|
||||||
|
@ -50,7 +50,7 @@ class Job(db.Model, ModelSerializeMixin):
|
||||||
_type_id = db.Column("type_id", db.Integer, db.ForeignKey("schedule_job_type.id"), nullable=False)
|
_type_id = db.Column("type_id", db.Integer, db.ForeignKey("schedule_job_type.id"), nullable=False)
|
||||||
_event_id = db.Column("event_id", db.Integer, db.ForeignKey("schedule_event.id"), nullable=False)
|
_event_id = db.Column("event_id", db.Integer, db.ForeignKey("schedule_event.id"), nullable=False)
|
||||||
|
|
||||||
id: int = db.Column("id", db.Integer, primary_key=True)
|
id: int = db.Column(db.Integer, primary_key=True)
|
||||||
start: datetime = db.Column(UtcDateTime, nullable=False)
|
start: datetime = db.Column(UtcDateTime, nullable=False)
|
||||||
end: Optional[datetime] = db.Column(UtcDateTime)
|
end: Optional[datetime] = db.Column(UtcDateTime)
|
||||||
comment: str = db.Column(db.String(256))
|
comment: str = db.Column(db.String(256))
|
||||||
|
|
Loading…
Reference in New Issue