|
1 | 1 | import datetime |
2 | 2 | import logging |
3 | 3 | import os |
| 4 | +import shutil |
4 | 5 |
|
5 | 6 | from asgiref.sync import async_to_sync |
6 | 7 | from channels.layers import get_channel_layer |
7 | 8 | from django.apps import apps |
8 | 9 | from django.conf import settings |
9 | 10 | from django.db.models import Sum |
| 11 | +from django.template.loader import render_to_string |
10 | 12 | from django_q.brokers import get_broker |
11 | 13 | from django_q.tasks import async_task |
12 | 14 |
|
@@ -285,3 +287,56 @@ def update_user_quota_core_hours(user_quota_id): |
285 | 287 | workspace_id__user_id=user_quota.user_id |
286 | 288 | ).aggregate(Sum("core_hours"))["core_hours__sum"] |
287 | 289 | user_quota.save() |
| 290 | + |
| 291 | + |
| 292 | +def initialize_shared_workspace(shared_workspace_mapping_id: int): |
| 293 | + shared_workspace_mapping = models.SharedWorkspaceMapping.objects.get( |
| 294 | + pk=shared_workspace_mapping_id |
| 295 | + ) |
| 296 | + original_workspace = shared_workspace_mapping.original_workspace_id |
| 297 | + shared_workspace = shared_workspace_mapping.shared_workspace_id |
| 298 | + |
| 299 | + main_storage = apps.get_app_config("user_workspaces_server").main_storage |
| 300 | + external_user_mapping = main_storage.storage_user_authentication.has_permission( |
| 301 | + shared_workspace.user_id |
| 302 | + ) |
| 303 | + |
| 304 | + # Set the shared_workspace file path |
| 305 | + shared_workspace.file_path = os.path.join( |
| 306 | + external_user_mapping.external_username, str(shared_workspace.pk) |
| 307 | + ) |
| 308 | + |
| 309 | + try: |
| 310 | + # Copy non . directories |
| 311 | + shutil.copytree( |
| 312 | + os.path.join(main_storage.root_dir, original_workspace.file_path), |
| 313 | + os.path.join(main_storage.root_dir, shared_workspace.file_path), |
| 314 | + ignore=shutil.ignore_patterns(".*"), |
| 315 | + symlinks=True, |
| 316 | + ) |
| 317 | + main_storage.set_ownership( |
| 318 | + shared_workspace.file_path, external_user_mapping, recursive=True |
| 319 | + ) |
| 320 | + except Exception as e: |
| 321 | + logger.exception(f"Copying files for {shared_workspace_mapping} failed: {e}") |
| 322 | + |
| 323 | + async_update_workspace(shared_workspace.pk) |
| 324 | + |
| 325 | + message = render_to_string( |
| 326 | + "email_templates/share_email.txt", |
| 327 | + context={ |
| 328 | + "sharer": original_workspace.user_id, |
| 329 | + "receiver": shared_workspace.user_id, |
| 330 | + "mapping_details": shared_workspace_mapping, |
| 331 | + "original_workspace": original_workspace, |
| 332 | + }, |
| 333 | + ) |
| 334 | + async_task( |
| 335 | + "django.core.mail.send_mail", |
| 336 | + "Invitation to Share a Workspace", |
| 337 | + message, |
| 338 | + None, |
| 339 | + [shared_workspace.user_id.email], |
| 340 | + ) |
| 341 | + |
| 342 | + shared_workspace.save() |
0 commit comments