-
Notifications
You must be signed in to change notification settings - Fork 716
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Assignment handling within single-user syncing #8219
Changes from 3 commits
a7f8a73
c52ed7b
89d106e
22ae2f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
from kolibri.plugins.hooks import define_hook | ||
from kolibri.plugins.hooks import KolibriHook | ||
|
||
|
||
@define_hook | ||
class FacilityDataSyncHook(KolibriHook): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This could be separated out into a pre-hook and post-hook, which would then allow each method to be setup with an |
||
""" | ||
A hook to allow plugins to register callbacks for sync events they're interested in. | ||
""" | ||
|
||
def pre_transfer( | ||
self, | ||
dataset_id, | ||
local_is_single_user, | ||
remote_is_single_user, | ||
single_user_id, | ||
context, | ||
): | ||
pass | ||
|
||
def post_transfer( | ||
self, | ||
dataset_id, | ||
local_is_single_user, | ||
remote_is_single_user, | ||
single_user_id, | ||
context, | ||
): | ||
pass |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import json | ||
|
||
from morango.sync.context import LocalSessionContext | ||
|
||
from kolibri.core.auth.constants.morango_sync import ScopeDefinitions | ||
from kolibri.core.auth.hooks import FacilityDataSyncHook | ||
|
||
|
||
def _get_our_cert(context): | ||
ss = context.sync_session | ||
return ss.server_certificate if ss.is_server else ss.client_certificate | ||
|
||
|
||
def _get_their_cert(context): | ||
ss = context.sync_session | ||
return ss.client_certificate if ss.is_server else ss.server_certificate | ||
|
||
|
||
def _this_side_using_single_user_cert(context): | ||
return _get_our_cert(context).scope_definition_id == ScopeDefinitions.SINGLE_USER | ||
|
||
|
||
def _other_side_using_single_user_cert(context): | ||
return _get_their_cert(context).scope_definition_id == ScopeDefinitions.SINGLE_USER | ||
|
||
|
||
def _get_user_id_for_single_user_sync(context): | ||
if _other_side_using_single_user_cert(context): | ||
cert = _get_their_cert(context) | ||
elif _this_side_using_single_user_cert(context): | ||
cert = _get_our_cert(context) | ||
else: | ||
return None | ||
return json.loads(cert.scope_params)["user_id"] | ||
|
||
|
||
def _extract_kwargs_from_context(context): | ||
return { | ||
"dataset_id": _get_our_cert(context).get_root().id, | ||
"local_is_single_user": _this_side_using_single_user_cert(context), | ||
"remote_is_single_user": _other_side_using_single_user_cert(context), | ||
"single_user_id": _get_user_id_for_single_user_sync(context), | ||
"context": context, | ||
} | ||
|
||
|
||
def _pre_transfer_handler(context): | ||
assert context is not None | ||
|
||
kwargs = _extract_kwargs_from_context(context) | ||
|
||
if isinstance(context, LocalSessionContext): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is to avoid having the hooks be called twice, once for local and once for remote context. |
||
for hook in FacilityDataSyncHook.registered_hooks: | ||
hook.pre_transfer(**kwargs) | ||
|
||
|
||
def _post_transfer_handler(context): | ||
assert context is not None | ||
|
||
kwargs = _extract_kwargs_from_context(context) | ||
|
||
if isinstance(context, LocalSessionContext): | ||
for hook in FacilityDataSyncHook.registered_hooks: | ||
hook.post_transfer(**kwargs) | ||
|
||
|
||
def register_sync_event_handlers(session_controller): | ||
session_controller.signals.initializing.completed.connect(_pre_transfer_handler) | ||
session_controller.signals.cleanup.completed.connect(_post_transfer_handler) |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,7 +57,7 @@ def manage(self, *args): | |
|
||
def create_model(self, model, **kwargs): | ||
kwarg_text = ",".join( | ||
'{key}=\\"{value}\\"'.format(key=key, value=value) | ||
"{key}={value}".format(key=key, value=repr(value)) | ||
for key, value in kwargs.items() | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Was getting some format issues in the piping -- this helped. |
||
) | ||
self.pipe_shell( | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wasn't sure whether this was the best place to put this, but... I couldn't think of a better place. Suggestions welcome, or maybe this is ok.