|
|
|
|
@ -7,6 +7,7 @@ from django.contrib.auth import get_user_model |
|
|
|
|
from django.utils import timezone |
|
|
|
|
|
|
|
|
|
from .ws_sender import websocket_sender |
|
|
|
|
from .registry import device_registry |
|
|
|
|
|
|
|
|
|
User = get_user_model() |
|
|
|
|
|
|
|
|
|
@ -43,11 +44,7 @@ def synchronization_prepare(sender, instance, **kwargs): |
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
if signal == pre_save: |
|
|
|
|
device_id = None |
|
|
|
|
if hasattr(instance, '_device_id'): |
|
|
|
|
device_id = instance._device_id |
|
|
|
|
|
|
|
|
|
detect_foreign_key_changes(sender, instance, device_id) |
|
|
|
|
detect_foreign_key_changes(sender, instance) |
|
|
|
|
|
|
|
|
|
@receiver([post_save, post_delete]) |
|
|
|
|
def synchronization_notifications(sender, instance, created=False, **kwargs): |
|
|
|
|
@ -61,20 +58,12 @@ def synchronization_notifications(sender, instance, created=False, **kwargs): |
|
|
|
|
if not isinstance(instance, BaseModel) and not isinstance(instance, User): |
|
|
|
|
return |
|
|
|
|
|
|
|
|
|
device_id = None |
|
|
|
|
if hasattr(instance, '_device_id'): |
|
|
|
|
device_id = instance._device_id |
|
|
|
|
|
|
|
|
|
process_foreign_key_changes(sender, instance, device_id, **kwargs) |
|
|
|
|
process_foreign_key_changes(sender, instance, **kwargs) |
|
|
|
|
|
|
|
|
|
signal = kwargs.get('signal') |
|
|
|
|
save_model_log_if_possible(instance, signal, created, device_id) |
|
|
|
|
|
|
|
|
|
save_model_log_if_possible(instance, signal, created) |
|
|
|
|
notify_impacted_users(instance) |
|
|
|
|
|
|
|
|
|
# print(f'*** instance._state.db: {instance._state.db}') |
|
|
|
|
# transaction.on_commit(lambda: notify_impacted_users(instance)) |
|
|
|
|
|
|
|
|
|
def notify_impacted_users(instance): |
|
|
|
|
user_ids = set() |
|
|
|
|
# add impacted users |
|
|
|
|
@ -91,16 +80,15 @@ def notify_impacted_users(instance): |
|
|
|
|
else: |
|
|
|
|
print('no users to notify') |
|
|
|
|
|
|
|
|
|
device_id = None |
|
|
|
|
if hasattr(instance, '_device_id'): |
|
|
|
|
device_id = instance._device_id |
|
|
|
|
device_id = device_registry.get_device_id(instance.id) |
|
|
|
|
|
|
|
|
|
# print(f'notify: {user_ids}') |
|
|
|
|
# print(f'notify: {device_id}') |
|
|
|
|
for user_id in user_ids: |
|
|
|
|
websocket_sender.send_user_message(user_id, device_id) |
|
|
|
|
# send_user_message(user_id) |
|
|
|
|
|
|
|
|
|
def save_model_log_if_possible(instance, signal, created, device_id): |
|
|
|
|
device_registry.unregister(instance.id) |
|
|
|
|
|
|
|
|
|
def save_model_log_if_possible(instance, signal, created): |
|
|
|
|
|
|
|
|
|
users = related_users(instance) |
|
|
|
|
# print(f'users = {len(users)}, instance = {instance}') |
|
|
|
|
@ -125,12 +113,14 @@ def save_model_log_if_possible(instance, signal, created, device_id): |
|
|
|
|
|
|
|
|
|
# print(f'users to notify: {user_ids}') |
|
|
|
|
instance._users_to_notify = user_ids # save this for the post_save signal |
|
|
|
|
save_model_log(users, operation, model_name, instance.id, store_id, device_id) |
|
|
|
|
save_model_log(users, operation, model_name, instance.id, store_id) |
|
|
|
|
|
|
|
|
|
else: |
|
|
|
|
print(f'>>> Model Log could not be created because no linked user could be found: {instance.__class__.__name__} {instance}, {signal}') |
|
|
|
|
|
|
|
|
|
def save_model_log(users, model_operation, model_name, model_id, store_id, device_id): |
|
|
|
|
def save_model_log(users, model_operation, model_name, model_id, store_id): |
|
|
|
|
|
|
|
|
|
device_id = device_registry.get_device_id(model_id) |
|
|
|
|
|
|
|
|
|
with transaction.atomic(): |
|
|
|
|
for user in users: |
|
|
|
|
@ -164,7 +154,7 @@ def save_model_log(users, model_operation, model_name, model_id, store_id, devic |
|
|
|
|
# model_log.save() |
|
|
|
|
# model_log.users.set(users) |
|
|
|
|
|
|
|
|
|
def detect_foreign_key_changes(sender, instance, device_id): |
|
|
|
|
def detect_foreign_key_changes(sender, instance): |
|
|
|
|
if not hasattr(instance, 'pk') or not instance.pk: |
|
|
|
|
return |
|
|
|
|
if not isinstance(instance, BaseModel): |
|
|
|
|
@ -190,7 +180,8 @@ def detect_foreign_key_changes(sender, instance, device_id): |
|
|
|
|
'new_value': new_value |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
def process_foreign_key_changes(sender, instance, device_id, **kwargs): |
|
|
|
|
def process_foreign_key_changes(sender, instance, **kwargs): |
|
|
|
|
|
|
|
|
|
if hasattr(instance, '_fk_changes'): |
|
|
|
|
for change in instance._fk_changes: |
|
|
|
|
for data_access in change['data_access_list']: |
|
|
|
|
@ -198,12 +189,12 @@ def process_foreign_key_changes(sender, instance, device_id, **kwargs): |
|
|
|
|
model_name = change['old_value'].__class__.__name__ |
|
|
|
|
save_model_log(data_access.concerned_users(), 'REVOKE_ACCESS', |
|
|
|
|
model_name, change['old_value'].id, |
|
|
|
|
change['old_value'].get_store_id(), device_id) |
|
|
|
|
change['old_value'].get_store_id()) |
|
|
|
|
if change['new_value']: |
|
|
|
|
model_name = change['new_value'].__class__.__name__ |
|
|
|
|
save_model_log(data_access.concerned_users(), 'GRANT_ACCESS', |
|
|
|
|
model_name, change['new_value'].id, |
|
|
|
|
change['new_value'].get_store_id(), device_id) |
|
|
|
|
change['new_value'].get_store_id()) |
|
|
|
|
|
|
|
|
|
@receiver(post_delete) |
|
|
|
|
def delete_data_access_if_necessary(sender, instance, **kwargs): |
|
|
|
|
@ -222,12 +213,11 @@ def handle_shared_with_changes(sender, instance, action, pk_set, **kwargs): |
|
|
|
|
elif action == "post_remove": |
|
|
|
|
instance.create_access_log(users, 'REVOKE_ACCESS') |
|
|
|
|
|
|
|
|
|
device_id = None |
|
|
|
|
if hasattr(instance, '_device_id'): |
|
|
|
|
device_id = instance._device_id |
|
|
|
|
|
|
|
|
|
device_id = device_registry.get_device_id(instance.id) |
|
|
|
|
for user_id in pk_set: |
|
|
|
|
websocket_sender.send_user_message(user_id, device_id) |
|
|
|
|
device_registry.unregister(instance.id) |
|
|
|
|
|
|
|
|
|
for user in users: |
|
|
|
|
evaluate_if_user_should_sync(user) |
|
|
|
|
|
|
|
|
|
|