airavata-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From machris...@apache.org
Subject [airavata-django-portal] 03/07: AIRAVATA-2859 Create default project for new user
Date Sat, 21 Jul 2018 20:12:57 GMT
This is an automated email from the ASF dual-hosted git repository.

machristie pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/airavata-django-portal.git

commit 3dea7d81a53cc4d1c1dadedeaa41b82b2c3c3111
Author: Marcus Christie <machrist@iu.edu>
AuthorDate: Fri Jul 20 13:59:46 2018 -0400

    AIRAVATA-2859 Create default project for new user
---
 django_airavata/apps/workspace/apps.py    |  3 +++
 django_airavata/apps/workspace/signals.py | 35 +++++++++++++++++++++++++++++++
 2 files changed, 38 insertions(+)

diff --git a/django_airavata/apps/workspace/apps.py b/django_airavata/apps/workspace/apps.py
index e95c1d7..f3dc5a9 100644
--- a/django_airavata/apps/workspace/apps.py
+++ b/django_airavata/apps/workspace/apps.py
@@ -12,3 +12,6 @@ class WorkspaceConfig(AiravataAppConfig):
     app_description = """
         Launch applications and manage your experiments and projects.
     """
+
+    def ready(self):
+        import django_airavata.apps.workspace.signals  # noqa
diff --git a/django_airavata/apps/workspace/signals.py b/django_airavata/apps/workspace/signals.py
new file mode 100644
index 0000000..e99f03d
--- /dev/null
+++ b/django_airavata/apps/workspace/signals.py
@@ -0,0 +1,35 @@
+"""Signal receivers for the workspace app."""
+
+import logging
+
+from django.conf import settings
+from django.contrib.auth.signals import user_logged_in
+from django.dispatch import receiver
+
+from airavata.model.workspace.ttypes import Project
+from django_airavata.apps.auth.utils import get_authz_token
+from django_airavata.utils import airavata_client
+
+log = logging.getLogger(__name__)
+
+
+@receiver(user_logged_in)
+def create_default_project_if_not_exists(sender, request, user, **kwargs):
+    # auth middleware hasn't run yet so authz_token attribute is not available
+    # on request, so need to create the authz_token manually
+    authz_token = get_authz_token(request)
+    with airavata_client() as client:
+        # Just retrieve the first project
+        projects = client.getUserProjects(authz_token, settings.GATEWAY_ID, request.user.username,
1, 0)
+        if len(projects) == 0:
+            log.info("Creating default project for user {}".format(
+                user.username))
+            default_project = Project()
+            default_project.owner = request.user.username
+            default_project.name = "Default Project"
+            default_project.gatewayId = settings.GATEWAY_ID
+            default_project.description = ("This is the default project for "
+                                           "user {owner}".format(
+                                               owner=default_project.owner))
+            client.createProject(authz_token, settings.GATEWAY_ID,
+                                 default_project)


Mime
View raw message