Skip to content

Commit

Permalink
use import_preview-collection for import instead of action_worker
Browse files Browse the repository at this point in the history
  • Loading branch information
r-peschke committed Sep 21, 2023
1 parent 5b59166 commit 5d4c6c0
Show file tree
Hide file tree
Showing 11 changed files with 165 additions and 129 deletions.
27 changes: 24 additions & 3 deletions global/meta/models.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3825,9 +3825,6 @@ action_worker:
- running
- end
- aborted
- warning
- error
- done
restriction_mode: A
created:
type: timestamp
Expand All @@ -3840,3 +3837,27 @@ action_worker:
result:
type: JSON
restriction_mode: A

import_preview:
id:
type: number
restriction_mode: A
name:
type: string
required: true
restriction_mode: A
state:
type: string
required: true
enum:
- warning
- error
- done
restriction_mode: A
created:
type: timestamp
required: true
restriction_mode: A
result:
type: JSON
restriction_mode: A
10 changes: 5 additions & 5 deletions openslides_backend/action/actions/topic/import_.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
@register_action("topic.import")
class TopicImport(DuplicateCheckMixin, ImportMixin):
"""
Action to import a result from the action_worker.
Action to import a result from the import_preview.
"""

model = ActionWorker()
Expand Down Expand Up @@ -50,10 +50,10 @@ def update_instance(self, instance: Dict[str, Any]) -> Dict[str, Any]:
def get_meeting_id(self, instance: Dict[str, Any]) -> int:
store_id = instance["id"]
worker = self.datastore.get(
fqid_from_collection_and_id("action_worker", store_id),
["result"],
fqid_from_collection_and_id("import_preview", store_id),
["name", "result"],
lock_result=False,
)
if worker.get("result", {}).get("import") == TopicImport.import_name:
return next(iter(worker["result"]["rows"]))["data"]["meeting_id"]
if worker.get("name") == TopicImport.import_name:
return next(iter(worker.get("result", {})["rows"]))["data"]["meeting_id"]
raise ActionException("Import data cannot be found.")
2 changes: 1 addition & 1 deletion openslides_backend/action/actions/topic/json_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ def update_instance(self, instance: Dict[str, Any]) -> Dict[str, Any]:
self.set_state(
state_to_count[ImportState.ERROR], state_to_count[ImportState.WARNING]
)
self.store_rows_in_the_action_worker("topic")
self.store_rows_in_the_import_preview("topic")
return {}

def validate_entry(self, entry: Dict[str, Any]) -> Dict[str, Any]:
Expand Down
8 changes: 4 additions & 4 deletions openslides_backend/action/actions/user/account_import.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from typing import Any, Dict, List, cast

from ....models.models import ActionWorker
from ....models.models import ImportPreview
from ....permissions.management_levels import OrganizationManagementLevel
from ....shared.exceptions import ActionException
from ....shared.schema import required_id_schema
Expand All @@ -20,11 +20,11 @@
@register_action("account.import")
class AccountImport(ImportMixin):
"""
Action to import a result from the action_worker.
Action to import a result from the import_preview.
"""

model = ActionWorker()
schema = DefaultSchema(ActionWorker()).get_default_schema(
model = ImportPreview()
schema = DefaultSchema(ImportPreview()).get_default_schema(
additional_required_fields={
"id": required_id_schema,
"import": {"type": "boolean"},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ def update_instance(self, instance: Dict[str, Any]) -> Dict[str, Any]:
self.set_state(
state_to_count[ImportState.ERROR], state_to_count[ImportState.WARNING]
)
self.store_rows_in_the_action_worker("account")
self.store_rows_in_the_import_preview("account")
return {}

def validate_entry(self, entry: Dict[str, Any]) -> Dict[str, Any]:
Expand Down
31 changes: 16 additions & 15 deletions openslides_backend/action/mixins/import_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from enum import Enum
from time import mktime, strptime, time
from typing import Any, Callable, Dict, List, Optional, Tuple, Union, cast

from typing_extensions import NotRequired, TypedDict

from ...shared.exceptions import ActionException
Expand Down Expand Up @@ -184,22 +185,22 @@ def prepare_action_data(self, action_data: ActionData) -> ActionData:

def update_instance(self, instance: Dict[str, Any]) -> Dict[str, Any]:
store_id = instance["id"]
worker = self.datastore.get(
fqid_from_collection_and_id("action_worker", store_id),
["result", "state"],
import_preview = self.datastore.get(
fqid_from_collection_and_id("import_preview", store_id),
["result", "state", "name"],
lock_result=False,
)
if (worker.get("result") or {}).get("import") != self.import_name:
if import_preview.get("name") != self.import_name:
raise ActionException(
f"Wrong id doesn't point on {self.import_name} import data."
)
if worker.get("state") not in list(ImportState):
if import_preview.get("state") not in list(ImportState):
raise ActionException(
"Error in import: Missing valid state in stored worker."
)
if worker.get("state") == ImportState.ERROR:
if import_preview.get("state") == ImportState.ERROR:
raise ActionException("Error in import. Data will not be imported.")
self.result = worker["result"]
self.result = import_preview.get("result", {})
return instance

def handle_relation_updates(self, instance: Dict[str, Any]) -> Any:
Expand Down Expand Up @@ -231,13 +232,13 @@ def on_success() -> None:
store_id = instance["id"]
if store_id in self.error_store_ids:
continue
self.datastore.write_action_worker(
self.datastore.write(
WriteRequest(
events=[
Event(
type=EventType.Delete,
fqid=fqid_from_collection_and_id(
"action_worker", store_id
"import_preview", store_id
),
)
],
Expand Down Expand Up @@ -277,21 +278,21 @@ def set_state(self, number_errors: int, number_warnings: int) -> None:
else:
self.import_state = ImportState.DONE

def store_rows_in_the_action_worker(self, import_name: str) -> None:
self.new_store_id = self.datastore.reserve_id(collection="action_worker")
fqid = fqid_from_collection_and_id("action_worker", self.new_store_id)
def store_rows_in_the_import_preview(self, import_name: str) -> None:
self.new_store_id = self.datastore.reserve_id(collection="import_preview")
fqid = fqid_from_collection_and_id("import_preview", self.new_store_id)
time_created = int(time())
self.datastore.write_action_worker(
self.datastore.write(
WriteRequest(
events=[
Event(
type=EventType.Create,
fqid=fqid,
fields={
"id": self.new_store_id,
"result": {"import": import_name, "rows": self.rows},
"name": import_name,
"result": {"rows": self.rows},
"created": time_created,
"timestamp": time_created,
"state": self.import_state,
},
)
Expand Down
18 changes: 15 additions & 3 deletions openslides_backend/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from .base import Model
from .mixins import AgendaItemModelMixin, MeetingModelMixin, PollModelMixin

MODELS_YML_CHECKSUM = "583c60c2e2ead61990169ab080bc9173"
MODELS_YML_CHECKSUM = "566944b1e2fa1b4b216537f3acbb3e5f"


class Organization(Model):
Expand Down Expand Up @@ -2104,9 +2104,21 @@ class ActionWorker(Model):
id = fields.IntegerField()
name = fields.CharField(required=True)
state = fields.CharField(
required=True,
constraints={"enum": ["running", "end", "aborted", "warning", "error", "done"]},
required=True, constraints={"enum": ["running", "end", "aborted"]}
)
created = fields.TimestampField(required=True)
timestamp = fields.TimestampField(required=True)
result = fields.JSONField()


class ImportPreview(Model):
collection = "import_preview"
verbose_name = "import preview"

id = fields.IntegerField()
name = fields.CharField(required=True)
state = fields.CharField(
required=True, constraints={"enum": ["warning", "error", "done"]}
)
created = fields.TimestampField(required=True)
result = fields.JSONField()
16 changes: 8 additions & 8 deletions tests/system/action/topic/test_import.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,10 @@ def setUp(self) -> None:
self.set_models(
{
"meeting/22": {"name": "test", "is_active_in_organization_id": 1},
"action_worker/2": {
"import_preview/2": {
"state": ImportState.DONE,
"name": "topic",
"result": {
"import": "topic",
"rows": [
{
"state": ImportState.NEW,
Expand All @@ -35,13 +35,13 @@ def test_import_correct(self) -> None:
self.assert_status_code(response, 200)
self.assert_model_exists("topic/1", {"title": "test", "meeting_id": 22})
self.assert_model_exists("meeting/22", {"topic_ids": [1]})
self.assert_model_not_exists("action_worker/2")
self.assert_model_deleted("import_preview/2")

def test_import_abort(self) -> None:
response = self.request("topic.import", {"id": 2, "import": False})
self.assert_status_code(response, 200)
self.assert_model_not_exists("topic/1")
self.assert_model_not_exists("action_worker/2")
self.assert_model_deleted("import_preview/2")

def test_import_duplicate_in_db(self) -> None:
self.set_models(
Expand Down Expand Up @@ -73,7 +73,7 @@ def test_import_duplicate_and_topic_deleted_so_imported(self) -> None:
},
)
self.assert_status_code(response, 200)
self.assert_model_exists("action_worker/3")
self.assert_model_exists("import_preview/3")
response = self.request("topic.delete", {"id": 1})
self.assert_status_code(response, 200)
self.assert_model_deleted("topic/1")
Expand All @@ -100,7 +100,7 @@ def test_import_duplicate_so_not_imported(self) -> None:
},
)
self.assert_status_code(response, 200)
self.assert_model_exists("action_worker/3")
self.assert_model_exists("import_preview/3")
response = self.request("topic.import", {"id": 3, "import": True})
self.assert_status_code(response, 200)
self.assert_model_not_exists("topic/2")
Expand All @@ -118,11 +118,11 @@ def test_import_with_upload(self) -> None:
},
)
self.assert_status_code(response, 200)
self.assert_model_exists("action_worker/3")
self.assert_model_exists("import_preview/3")
response = self.request("topic.import", {"id": 3, "import": True})
self.assert_status_code(response, 200)
self.assert_model_exists(
"topic/1", {"title": "another title", "meeting_id": 22}
)
self.assert_model_exists("meeting/22", {"topic_ids": [1]})
self.assert_model_not_exists("action_worker/3")
self.assert_model_deleted("import_preview/3")
15 changes: 7 additions & 8 deletions tests/system/action/topic/test_json_upload.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,9 @@ def test_json_upload_agenda_data(self) -> None:
},
}
worker = self.assert_model_exists(
"action_worker/1", {"state": ImportState.DONE}
"import_preview/1", {"state": ImportState.DONE}
)
assert start_time <= worker.get("created", -1) <= end_time
assert start_time <= worker.get("timestamp", -1) <= end_time

def test_json_upload_empty_data(self) -> None:
response = self.request(
Expand Down Expand Up @@ -84,18 +83,18 @@ def test_json_upload_results(self) -> None:
)
self.assert_status_code(response, 200)
self.assert_model_exists(
"action_worker/1",
"import_preview/1",
{
"name": "topic",
"result": {
"import": "topic",
"rows": [
{
"state": ImportState.NEW,
"messages": [],
"data": {"title": "test", "meeting_id": 22},
}
],
}
},
},
)
result = response.json["results"][0][0]
Expand Down Expand Up @@ -159,10 +158,10 @@ def test_json_upload_duplicate_in_data(self) -> None:
assert result["rows"][2]["messages"] == ["Duplicate"]
assert result["rows"][2]["state"] == ImportState.WARNING
self.assert_model_exists(
"action_worker/1",
"import_preview/1",
{
"name": "topic",
"result": {
"import": "topic",
"rows": [
{
"state": ImportState.NEW,
Expand All @@ -180,7 +179,7 @@ def test_json_upload_duplicate_in_data(self) -> None:
"data": {"title": "test", "meeting_id": 22},
},
],
}
},
},
)

Expand Down
Loading

0 comments on commit 5d4c6c0

Please sign in to comment.