oarepo-workflows 1.0.0__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (27) hide show
  1. oarepo_workflows-1.0.0/LICENSE +21 -0
  2. oarepo_workflows-1.0.0/MANIFEST.in +10 -0
  3. oarepo_workflows-1.0.0/PKG-INFO +10 -0
  4. oarepo_workflows-1.0.0/README.md +1 -0
  5. oarepo_workflows-1.0.0/oarepo_workflows/__init__.py +0 -0
  6. oarepo_workflows-1.0.0/oarepo_workflows/ext.py +72 -0
  7. oarepo_workflows-1.0.0/oarepo_workflows/ext_config.py +5 -0
  8. oarepo_workflows-1.0.0/oarepo_workflows/permissions/__init__.py +0 -0
  9. oarepo_workflows-1.0.0/oarepo_workflows/permissions/generators.py +115 -0
  10. oarepo_workflows-1.0.0/oarepo_workflows/permissions/policy.py +73 -0
  11. oarepo_workflows-1.0.0/oarepo_workflows/proxies.py +6 -0
  12. oarepo_workflows-1.0.0/oarepo_workflows/records/__init__.py +0 -0
  13. oarepo_workflows-1.0.0/oarepo_workflows/records/models.py +18 -0
  14. oarepo_workflows-1.0.0/oarepo_workflows/records/systemfields/__init__.py +0 -0
  15. oarepo_workflows-1.0.0/oarepo_workflows/records/systemfields/state.py +20 -0
  16. oarepo_workflows-1.0.0/oarepo_workflows/records/systemfields/workflow.py +61 -0
  17. oarepo_workflows-1.0.0/oarepo_workflows/requests/__init__.py +0 -0
  18. oarepo_workflows-1.0.0/oarepo_workflows/requests/classes.py +12 -0
  19. oarepo_workflows-1.0.0/oarepo_workflows.egg-info/PKG-INFO +10 -0
  20. oarepo_workflows-1.0.0/oarepo_workflows.egg-info/SOURCES.txt +26 -0
  21. oarepo_workflows-1.0.0/oarepo_workflows.egg-info/dependency_links.txt +1 -0
  22. oarepo_workflows-1.0.0/oarepo_workflows.egg-info/entry_points.txt +5 -0
  23. oarepo_workflows-1.0.0/oarepo_workflows.egg-info/requires.txt +3 -0
  24. oarepo_workflows-1.0.0/oarepo_workflows.egg-info/top_level.txt +1 -0
  25. oarepo_workflows-1.0.0/pyproject.toml +3 -0
  26. oarepo_workflows-1.0.0/setup.cfg +36 -0
  27. oarepo_workflows-1.0.0/setup.py +3 -0
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (C) 2021 CESNET.
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of
6
+ this software and associated documentation files (the "Software"), to deal in
7
+ the Software without restriction, including without limitation the rights to
8
+ use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
9
+ of the Software, and to permit persons to whom the Software is furnished to do
10
+ so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,10 @@
1
+ recursive-exclude tests *
2
+ recursive-include oarepo_requests *.yaml
3
+ recursive-include oarepo_requests *.json
4
+ recursive-include oarepo_requests *.po
5
+ recursive-include oarepo_requests *.pot
6
+ recursive-include oarepo_requests *.mo
7
+ recursive-include oarepo_requests *.js
8
+ recursive-include oarepo_requests *.jsx
9
+ recursive-include oarepo_requests *.less
10
+ recursive-include oarepo_requests *.jinja
@@ -0,0 +1,10 @@
1
+ Metadata-Version: 2.1
2
+ Name: oarepo-workflows
3
+ Version: 1.0.0
4
+ Description-Content-Type: text/markdown
5
+ License-File: LICENSE
6
+ Requires-Dist: invenio-records-resources
7
+ Requires-Dist: invenio-requests
8
+ Requires-Dist: oarepo-runtime
9
+
10
+ # OARepo workflows
@@ -0,0 +1 @@
1
+ # OARepo workflows
File without changes
@@ -0,0 +1,72 @@
1
+ from functools import cached_property
2
+
3
+ import importlib_metadata
4
+
5
+
6
+ class OARepoWorkflows(object):
7
+
8
+ def __init__(self, app=None):
9
+ if app:
10
+ self.init_config(app)
11
+ self.init_app(app)
12
+
13
+ def init_config(self, app):
14
+ """Initialize configuration."""
15
+ from . import ext_config
16
+
17
+ if "OAREPO_PERMISSIONS_PRESETS" not in app.config:
18
+ app.config["OAREPO_PERMISSIONS_PRESETS"] = {}
19
+
20
+ for k in ext_config.OAREPO_PERMISSIONS_PRESETS:
21
+ if k not in app.config["OAREPO_PERMISSIONS_PRESETS"]:
22
+ app.config["OAREPO_PERMISSIONS_PRESETS"][k] = (
23
+ ext_config.OAREPO_PERMISSIONS_PRESETS[k]
24
+ )
25
+
26
+ @cached_property
27
+ def state_changed_notifiers(self):
28
+ group_name = "oarepo_workflows.state_changed_notifiers"
29
+ return importlib_metadata.entry_points().select(group=group_name)
30
+
31
+ @cached_property
32
+ def workflow_changed_notifiers(self):
33
+ group_name = "oarepo_workflows.workflow_changed_notifiers"
34
+ return importlib_metadata.entry_points().select(group=group_name)
35
+
36
+ @cached_property
37
+ def default_workflow_getters(self):
38
+ group_name = "oarepo_workflows.default_workflow_getters"
39
+ return importlib_metadata.entry_points().select(group=group_name)
40
+
41
+ def set_state(self, identity, record, value, *args, uow=None, **kwargs):
42
+ previous_value = record.state
43
+ record.state = value
44
+ for state_changed_notifier_ep in self.state_changed_notifiers:
45
+ state_changed_notifier = state_changed_notifier_ep.load()
46
+ state_changed_notifier(
47
+ identity, record, previous_value, value, *args, uow=uow, **kwargs
48
+ )
49
+
50
+ def get_default_workflow(self, default="default", *args, **kwargs):
51
+ for default_workflow_getter_ep in self.default_workflow_getters:
52
+ default_workflow_getter = default_workflow_getter_ep.load()
53
+ default = default_workflow_getter(default, *args, **kwargs)
54
+ return default
55
+
56
+ def set_workflow(self, identity, record, value, *args, uow=None, **kwargs):
57
+ previous_value = record.parent["workflow"]
58
+ record.parent.workflow = value
59
+ for workflow_changed_notifier_ep in self.workflow_changed_notifiers:
60
+ workflow_changed_notifier = workflow_changed_notifier_ep.load()
61
+ workflow_changed_notifier(
62
+ identity, record, previous_value, value, *args, uow=uow, **kwargs
63
+ )
64
+
65
+ @property
66
+ def record_workflows(self):
67
+ return self.app.config["RECORD_WORKFLOWS"]
68
+
69
+ def init_app(self, app):
70
+ """Flask application initialization."""
71
+ self.app = app
72
+ app.extensions["oarepo-workflows"] = self
@@ -0,0 +1,5 @@
1
+ from oarepo_workflows.permissions.policy import WorkflowPermissionPolicy
2
+
3
+ OAREPO_PERMISSIONS_PRESETS = {
4
+ "workflow": WorkflowPermissionPolicy,
5
+ }
@@ -0,0 +1,115 @@
1
+ import operator
2
+ from functools import reduce
3
+ from itertools import chain
4
+
5
+ from invenio_records.dictutils import dict_lookup
6
+ from invenio_records_permissions.generators import ConditionalGenerator, Generator
7
+ from invenio_search.engine import dsl
8
+
9
+ from oarepo_workflows.proxies import current_oarepo_workflows
10
+
11
+
12
+ def needs_from_generators(generators, **kwargs):
13
+ if not generators:
14
+ return []
15
+ needs = [
16
+ g.needs(
17
+ **kwargs,
18
+ )
19
+ for g in generators
20
+ ]
21
+ return set(chain.from_iterable(needs))
22
+
23
+
24
+ def reference_receiver_from_generators(generators, **kwargs):
25
+ if not generators:
26
+ return None
27
+ for generator in generators:
28
+ if hasattr(generator, 'reference_receiver'):
29
+ ref = generator.reference_receiver(**kwargs)
30
+ if ref:
31
+ return ref
32
+
33
+
34
+ def query_filters_from_generators(generators, **kwargs):
35
+ queries = [g.query_filter(**kwargs) for g in generators]
36
+ queries = [q for q in queries if q]
37
+ return reduce(operator.or_, queries) if queries else None
38
+
39
+
40
+ def _needs_from_workflow(workflow_id, action, record, **kwargs):
41
+ try:
42
+ generators = dict_lookup(
43
+ current_oarepo_workflows, f"{workflow_id}.permissions.{action}"
44
+ )
45
+ except KeyError:
46
+ return []
47
+ return needs_from_generators(generators, record, **kwargs)
48
+
49
+
50
+ def get_workflow_from_record(record, **kwargs):
51
+ if hasattr(record, "parent"):
52
+ record = record.parent
53
+ if hasattr(record, "workflow") and record.workflow:
54
+ return record.workflow
55
+ else:
56
+ if "record" not in kwargs:
57
+ return current_oarepo_workflows.get_default_workflow(
58
+ record=record, **kwargs
59
+ )
60
+ else:
61
+ return current_oarepo_workflows.get_default_workflow(**kwargs)
62
+
63
+
64
+ def get_permission_class_from_workflow(record=None, action_name=None, **kwargs):
65
+ if record:
66
+ workflow_id = get_workflow_from_record(record)
67
+ else:
68
+ workflow_id = current_oarepo_workflows.get_default_workflow(**kwargs)
69
+
70
+ policy = dict_lookup(
71
+ current_oarepo_workflows.record_workflows, f"{workflow_id}.permissions"
72
+ )
73
+ return policy(action_name, **kwargs)
74
+
75
+
76
+ class WorkflowPermission(Generator):
77
+ def __init__(self, action):
78
+ super().__init__()
79
+ self._action = action
80
+
81
+ def _get_generators(self, record, **kwargs):
82
+ permission_class = get_permission_class_from_workflow(
83
+ record, action_name=self._action, **kwargs
84
+ )
85
+ return getattr(permission_class, self._action, None)
86
+
87
+ def needs(self, record=None, **kwargs):
88
+ generators = self._get_generators(record, **kwargs)
89
+ return needs_from_generators(generators, record=record, **kwargs)
90
+
91
+ def query_filter(self, record=None, **kwargs):
92
+ generators = self._get_generators(record, **kwargs)
93
+ return query_filters_from_generators(generators, record=record, **kwargs)
94
+
95
+
96
+ class IfInState(ConditionalGenerator):
97
+ def __init__(self, state, then_):
98
+ super().__init__(then_, else_=[])
99
+ self.state = state
100
+
101
+ def _condition(self, record, **kwargs):
102
+ try:
103
+ state = record.state
104
+ except AttributeError:
105
+ return False
106
+ return state == self.state
107
+
108
+ def query_filter(self, **kwargs):
109
+ """Filters for queries."""
110
+ field = "state"
111
+
112
+ q_instate = dsl.Q("match", **{field: self.state})
113
+ then_query = self._make_query(self.then_, **kwargs)
114
+
115
+ return q_instate & then_query
@@ -0,0 +1,73 @@
1
+
2
+ from invenio_records_permissions import RecordPermissionPolicy
3
+ from invenio_records_permissions.generators import AuthenticatedUser, SystemProcess
4
+ from oarepo_runtime.services.generators import RecordOwners
5
+
6
+ from oarepo_workflows.permissions.generators import WorkflowPermission
7
+
8
+ from .generators import IfInState
9
+
10
+
11
+ class DefaultWorkflowPermissionPolicy(RecordPermissionPolicy):
12
+
13
+ PERMISSIONS_REMAP = {
14
+ "can_read_draft": "can_read",
15
+ "can_update_draft": "can_update",
16
+ "can_delete_draft": "can_delete",
17
+ "can_draft_create_files": "can_create_files",
18
+ "can_draft_set_content_files": "can_set_content_files",
19
+ "can_draft_get_content_files": "can_get_content_files",
20
+ "can_draft_commit_files": "can_commit_files",
21
+ "can_draft_read_files": "can_read_files",
22
+ "can_draft_update_files": "can_update_files",
23
+ "can_search_drafts": "can_search",
24
+ }
25
+
26
+ def __init__(self, action_name=None, **over):
27
+ action_name = DefaultWorkflowPermissionPolicy.PERMISSIONS_REMAP.get(
28
+ action_name, action_name
29
+ )
30
+ can = getattr(self, action_name)
31
+ can.append(SystemProcess())
32
+ super().__init__(action_name, **over)
33
+
34
+ can_search = [AuthenticatedUser()]
35
+ can_read = [
36
+ IfInState("draft", [RecordOwners()]),
37
+ IfInState("published", [AuthenticatedUser()]),
38
+ ]
39
+ can_update = [IfInState("draft", RecordOwners())]
40
+ can_delete = [
41
+ IfInState("draft", RecordOwners()),
42
+ # published record can not be deleted directly by anyone else than system
43
+ SystemProcess(),
44
+ ]
45
+ can_create = [AuthenticatedUser()]
46
+ can_publish = [AuthenticatedUser()]
47
+
48
+
49
+ class WorkflowPermissionPolicy(RecordPermissionPolicy):
50
+
51
+ can_create = [WorkflowPermission("can_create")]
52
+ can_publish = [WorkflowPermission("can_publish")]
53
+ can_search = [WorkflowPermission("can_search")]
54
+ can_read = [WorkflowPermission("can_read")]
55
+ can_update = [WorkflowPermission("can_update")]
56
+ can_delete = [WorkflowPermission("can_delete")]
57
+ can_create_files = [WorkflowPermission("can_create_files")]
58
+ can_set_content_files = [WorkflowPermission("can_set_content_files")]
59
+ can_get_content_files = [WorkflowPermission("can_get_content_files")]
60
+ can_commit_files = [WorkflowPermission("can_commit_files")]
61
+ can_read_files = [WorkflowPermission("can_read_files")]
62
+ can_update_files = [WorkflowPermission("can_update_files")]
63
+
64
+ can_search_drafts = [WorkflowPermission("can_search")]
65
+ can_read_draft = [WorkflowPermission("can_read")]
66
+ can_update_draft = [WorkflowPermission("can_update")]
67
+ can_delete_draft = [WorkflowPermission("can_delete")]
68
+ can_draft_create_files = [WorkflowPermission("can_create_files")]
69
+ can_draft_set_content_files = [WorkflowPermission("can_set_content_files")]
70
+ can_draft_get_content_files = [WorkflowPermission("can_get_content_files")]
71
+ can_draft_commit_files = [WorkflowPermission("can_commit_files")]
72
+ can_draft_read_files = [WorkflowPermission("can_read_files")]
73
+ can_draft_update_files = [WorkflowPermission("can_update_files")]
@@ -0,0 +1,6 @@
1
+ from flask import current_app
2
+ from werkzeug.local import LocalProxy
3
+
4
+ current_oarepo_workflows = LocalProxy(
5
+ lambda: current_app.extensions["oarepo-workflows"]
6
+ )
@@ -0,0 +1,18 @@
1
+ from invenio_db import db
2
+ from sqlalchemy import String
3
+ from sqlalchemy.ext.declarative import declared_attr
4
+ from sqlalchemy_utils.types import UUIDType
5
+
6
+
7
+ class RecordWorkflowModelMixin:
8
+ __record_model__ = None
9
+
10
+ @declared_attr
11
+ def record_id(cls):
12
+ return db.Column(
13
+ UUIDType,
14
+ db.ForeignKey(cls.__record_model__.id, ondelete="CASCADE"),
15
+ primary_key=True,
16
+ )
17
+
18
+ workflow = db.Column(String)
@@ -0,0 +1,20 @@
1
+ from invenio_records.systemfields.base import SystemField
2
+
3
+
4
+ class RecordStateField(SystemField):
5
+ def __init__(self, key="state", initial="draft", config=None):
6
+ self._config = config
7
+ self._initial = initial
8
+ super().__init__(key=key)
9
+
10
+ def post_create(self, record):
11
+ self.set_dictkey(record, self._initial)
12
+
13
+ def __get__(self, record, owner=None):
14
+ """Get the persistent identifier."""
15
+ if record is None:
16
+ return self
17
+ return self.get_dictkey(record)
18
+
19
+ def __set__(self, record, value):
20
+ self.set_dictkey(record, value)
@@ -0,0 +1,61 @@
1
+ from invenio_db import db
2
+ from invenio_records.systemfields.base import SystemField
3
+ from sqlalchemy.exc import NoResultFound
4
+
5
+ from ...proxies import current_oarepo_workflows
6
+
7
+
8
+ class WorkflowField(SystemField):
9
+
10
+ def __init__(self, record_workflow_model, key="workflow"):
11
+ self._workflow = None # added in db
12
+ self._record_workflow_model = record_workflow_model
13
+ super().__init__(key=key)
14
+
15
+ def _get_workflow_from_parent_db(self, record):
16
+ if record.id is None:
17
+ return
18
+ try:
19
+ res = (
20
+ db.session.query(self._record_workflow_model.workflow)
21
+ .filter(self._record_workflow_model.record_id == record.id)
22
+ .one()
23
+ )
24
+ return res[0]
25
+ except NoResultFound:
26
+ return None
27
+
28
+ def _get_workflow(self, record):
29
+ if "workflow" in record and record["workflow"]:
30
+ return record["workflow"]
31
+ workflow = self._get_workflow_from_parent_db(record)
32
+ if not workflow:
33
+ workflow = current_oarepo_workflows.get_default_workflow(record=record)
34
+ return workflow
35
+
36
+ def post_create(self, record):
37
+ workflow = self._get_workflow(record)
38
+ if workflow:
39
+ self.set_dictkey(record, workflow)
40
+
41
+ def pre_commit(self, record):
42
+ super().pre_commit(record)
43
+ saved_workflow = self._get_workflow_from_parent_db(record)
44
+ if not saved_workflow:
45
+ default = self._get_workflow(
46
+ record
47
+ )
48
+ if default:
49
+ new = self._record_workflow_model(
50
+ workflow=default, record_id=str(record.id)
51
+ )
52
+ db.session.add(new)
53
+
54
+ def __get__(self, record, owner=None):
55
+ """Get the persistent identifier."""
56
+ if record is None:
57
+ return self
58
+ return self._get_workflow(record)
59
+
60
+ def __set__(self, record, value):
61
+ self.set_dictkey(record, value)
@@ -0,0 +1,12 @@
1
+ class WorkflowRequest:
2
+ def __init__(self, requesters, recipients, transitions):
3
+ self.requesters = requesters
4
+ self.recipients = recipients
5
+ self.transitions = transitions
6
+
7
+
8
+ class WorkflowTransitions:
9
+ def __init__(self, submitted=None, approved=None, declined=None):
10
+ self.submitted = submitted
11
+ self.approved = approved
12
+ self.declined = declined
@@ -0,0 +1,10 @@
1
+ Metadata-Version: 2.1
2
+ Name: oarepo-workflows
3
+ Version: 1.0.0
4
+ Description-Content-Type: text/markdown
5
+ License-File: LICENSE
6
+ Requires-Dist: invenio-records-resources
7
+ Requires-Dist: invenio-requests
8
+ Requires-Dist: oarepo-runtime
9
+
10
+ # OARepo workflows
@@ -0,0 +1,26 @@
1
+ LICENSE
2
+ MANIFEST.in
3
+ README.md
4
+ pyproject.toml
5
+ setup.cfg
6
+ setup.py
7
+ oarepo_workflows/__init__.py
8
+ oarepo_workflows/ext.py
9
+ oarepo_workflows/ext_config.py
10
+ oarepo_workflows/proxies.py
11
+ oarepo_workflows.egg-info/PKG-INFO
12
+ oarepo_workflows.egg-info/SOURCES.txt
13
+ oarepo_workflows.egg-info/dependency_links.txt
14
+ oarepo_workflows.egg-info/entry_points.txt
15
+ oarepo_workflows.egg-info/requires.txt
16
+ oarepo_workflows.egg-info/top_level.txt
17
+ oarepo_workflows/permissions/__init__.py
18
+ oarepo_workflows/permissions/generators.py
19
+ oarepo_workflows/permissions/policy.py
20
+ oarepo_workflows/records/__init__.py
21
+ oarepo_workflows/records/models.py
22
+ oarepo_workflows/records/systemfields/__init__.py
23
+ oarepo_workflows/records/systemfields/state.py
24
+ oarepo_workflows/records/systemfields/workflow.py
25
+ oarepo_workflows/requests/__init__.py
26
+ oarepo_workflows/requests/classes.py
@@ -0,0 +1,5 @@
1
+ [invenio_base.api_apps]
2
+ oarepo_workflows = oarepo_workflows.ext:OARepoWorkflows
3
+
4
+ [invenio_base.apps]
5
+ oarepo_workflows = oarepo_workflows.ext:OARepoWorkflows
@@ -0,0 +1,3 @@
1
+ invenio-records-resources
2
+ invenio-requests
3
+ oarepo-runtime
@@ -0,0 +1 @@
1
+ oarepo_workflows
@@ -0,0 +1,3 @@
1
+ [build_system]
2
+ requires = ["setuptools", "wheel", "babel>2.8"]
3
+ build-backend = "setuptools.build_meta"
@@ -0,0 +1,36 @@
1
+ [metadata]
2
+ name = oarepo-workflows
3
+ version = 1.0.0
4
+ description =
5
+ authors = Ronald Krist <krist@cesnet.cz>
6
+ readme = README.md
7
+ long_description = file:README.md
8
+ long_description_content_type = text/markdown
9
+
10
+ [options]
11
+ python = >=3.9
12
+ install_requires =
13
+ invenio-records-resources
14
+ invenio-requests
15
+ oarepo-runtime
16
+ packages = find:
17
+ include_package_data = True
18
+
19
+ [options.package_data]
20
+ * = *.json, *.rst, *.md, *.json5, *.jinja2, *.po, *.mo, *.pot, *.js, *.jsx, *.less, *.jinja
21
+
22
+ [options.packages.find]
23
+ exclude =
24
+ tests
25
+ tests.*
26
+
27
+ [options.entry_points]
28
+ invenio_base.apps =
29
+ oarepo_workflows = oarepo_workflows.ext:OARepoWorkflows
30
+ invenio_base.api_apps =
31
+ oarepo_workflows = oarepo_workflows.ext:OARepoWorkflows
32
+
33
+ [egg_info]
34
+ tag_build =
35
+ tag_date = 0
36
+
@@ -0,0 +1,3 @@
1
+ from setuptools import setup
2
+
3
+ setup()