Skip to content

Commit

Permalink
feature: implement step image clear
Browse files Browse the repository at this point in the history
  • Loading branch information
mutantsan committed Sep 11, 2023
1 parent 55d94d5 commit 2a63ef3
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 23 deletions.
30 changes: 23 additions & 7 deletions ckanext/tour/logic/action.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,14 +149,18 @@ def tour_step_update(context, data_dict):
tour_step.intro = data_dict["intro"]
tour_step.position = data_dict["position"]

if data_dict.get("image"):
if data_dict["clear"] and tour_step.image:
tk.get_action("tour_step_image_remove")(
{"ignore_auth": True}, {"id": tour_step.image.id}
)
elif data_dict.get("image"):
data_dict["image"][0]["tour_step_id"] = tour_step.id
action = "tour_step_image_update" if tour_step.image else "tour_step_image_upload"
action = (
"tour_step_image_update" if tour_step.image else "tour_step_image_upload"
)

try:
tk.get_action(action)(
{"ignore_auth": True}, data_dict["image"][0]
)
tk.get_action(action)({"ignore_auth": True}, data_dict["image"][0])
except TourStepFileError as e:
raise tk.ValidationError(f"Error while uploading step image: {e}")

Expand All @@ -167,9 +171,9 @@ def tour_step_update(context, data_dict):

@validate(schema.tour_step_remove)
def tour_step_remove(context, data_dict):
study_request = cast(tour_model.Tour, tour_model.TourStep.get(data_dict["id"]))
tour_step = cast(tour_model.TourStep, tour_model.TourStep.get(data_dict["id"]))

study_request.delete()
tour_step.delete()
model.Session.commit()

return True
Expand Down Expand Up @@ -248,3 +252,15 @@ def tour_step_image_update(context, data_dict):
model.Session.commit()

return tour_step_image.dictize(context)


@validate(schema.tour_step_image_remove_schema)
def tour_step_image_remove(context, data_dict):
tour_step_image = cast(
tour_model.TourStepImage, tour_model.TourStepImage.get(data_dict["id"])
)

tour_step_image.delete(with_file=bool(tour_step_image.file_id))
model.Session.commit()

return True
12 changes: 9 additions & 3 deletions ckanext/tour/logic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,12 @@ def tour_step_schema(

@validator_args
def tour_step_update(
ignore_empty,
unicode_safe,
tour_tour_step_exist,
ignore_empty, unicode_safe, tour_tour_step_exist, default, boolean_validator
) -> Schema:
step_schema = tour_step_schema()
step_schema.pop("tour_id")
step_schema["id"] = [ignore_empty, unicode_safe, tour_tour_step_exist]
step_schema["clear"] = [default("false"), boolean_validator]

return step_schema

Expand Down Expand Up @@ -131,3 +130,10 @@ def tour_step_remove(not_empty, unicode_safe, tour_tour_step_exist) -> Schema:
@validator_args
def tour_step_image_update_schema() -> Schema:
return tour_step_image_schema()


@validator_args
def tour_step_image_remove_schema(
not_empty, unicode_safe, tour_tour_step_image_exist
) -> Schema:
return {"id": [tour_tour_step_image_exist]}
8 changes: 4 additions & 4 deletions ckanext/tour/logic/validators.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,18 @@ def tour_tour_step_exist(v: str, context) -> Any:
result = tour_model.TourStep.get(v)

if not result:
raise tk.Invalid(f"The tour with an id {v} doesn't exist.")
raise tk.Invalid(f"The tour step with an id {v} doesn't exist.")

return v


def tour_tour_step_image_exist(v: str, context) -> Any:
"""Ensures that the tour step image exists for a specific tour step"""
"""Ensures that the tour step image with a given id exists"""

result = tour_model.TourStepImage.get_by_step(v)
result = tour_model.TourStepImage.get(v)

if not result:
raise tk.Invalid(f"The tour image for tour step {v} doesn't exists.")
raise tk.Invalid(f"The tour step image with an id {v} doesn't exist.")

return v

Expand Down
19 changes: 10 additions & 9 deletions ckanext/tour/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ def create(cls, data_dict) -> Self:
return tour_step_image

@classmethod
def get(cls, file_id: str) -> Self | None:
query: Query = model.Session.query(cls).filter(cls.file_id == file_id)
def get(cls, image_id: str) -> Self | None:
query: Query = model.Session.query(cls).filter(cls.id == image_id)

return query.one_or_none()

Expand All @@ -189,14 +189,15 @@ def dictize(self, context):
"url": uploaded_file["url"] if uploaded_file else self.url,
}

def delete(self) -> None:
def delete(self, with_file: bool = False) -> None:
"""Drop step image and related file from file system"""
try:
tk.get_action("files_file_delete")(
{"ignore_auth": True}, {"id": self.file_id}
)
except tk.ObjectNotFound:
raise TourStepFileError("Related file not found.")
if with_file:
try:
tk.get_action("files_file_delete")(
{"ignore_auth": True}, {"id": self.file_id}
)
except tk.ObjectNotFound:
raise TourStepFileError("Related file not found.")

model.Session().autoflush = False
model.Session.delete(self)
Expand Down
1 change: 1 addition & 0 deletions ckanext/tour/views/tour_update.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ def _prepare_payload(self, tour_id: str):
"step_element",
"step_intro",
"step_position",
"step_clear",
)

steps = {}
Expand Down

0 comments on commit 2a63ef3

Please sign in to comment.