[Plugin] Schedule: Identify EventTypes by ID not name

This commit is contained in:
Ferdinand Thiessen 2021-01-24 16:20:25 +01:00
parent d2854315c1
commit 30305c26cc
3 changed files with 31 additions and 20 deletions

View File

@ -32,20 +32,26 @@ class SchedulePlugin(Plugin):
@schedule_bp.route("/event-types", methods=["GET"])
@schedule_bp.route("/event-types/<identifier:int>", methods=["GET"])
@login_required()
def get_event_types(current_session):
"""Get all EventTypes
def get_event_types(current_session, identifier=None):
"""Get EventType(s)
Route: ``/schedule/event-types`` | Method: ``GET``
Route: ``/schedule/event-types/<identifier>`` | Method: ``GET``
Args:
current_session: Session sent with Authorization Header
identifier: If querying a specific EventType
Returns:
JSON encoded list of EventTypes HTTP-error
JSON encoded (list of) EventType(s) or HTTP-error
"""
types = event_controller.get_event_types()
return jsonify(types)
if identifier:
result = event_controller.get_event_type(identifier)
else:
result = event_controller.get_event_types()
return jsonify(result)
@schedule_bp.route("/event-types", methods=["POST"])
@ -66,33 +72,33 @@ def new_event_type(current_session):
data = request.get_json()
if "name" not in data:
raise BadRequest
event = event_controller.create_event_type(data["name"])
return jsonify(event)
event_type = event_controller.create_event_type(data["name"])
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)
def modify_event_type(name, current_session):
def modify_event_type(identifier, current_session):
"""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}``
Args:
name: Name identifying the EventType
identifier: Identifier of the EventType
current_session: Session sent with Authorization Header
Returns:
HTTP-NoContent or HTTP-error
"""
if request.method == "DELETE":
event_controller.delete_event_type(name)
event_controller.delete_event_type(identifier)
else:
data = request.get_json()
if "name" not 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

View File

@ -14,8 +14,13 @@ def get_event_types():
return EventType.query.all()
def get_event_type(name):
et = EventType.query.filter(EventType.name == name).one_or_none()
def get_event_type(identifier):
"""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:
raise NotFound
return et
@ -31,8 +36,8 @@ def create_event_type(name):
raise BadRequest("Name already exists")
def rename_event_type(name, new_name):
event_type = get_event_type(name)
def rename_event_type(identifier, new_name):
event_type = get_event_type(identifier)
event_type.name = new_name
try:
db.session.commit()

View File

@ -14,13 +14,13 @@ from flaschengeist.database import db
class EventType(db.Model, ModelSerializeMixin):
__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)
class JobType(db.Model, ModelSerializeMixin):
__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)
@ -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)
_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)
end: Optional[datetime] = db.Column(UtcDateTime)
comment: str = db.Column(db.String(256))