From 350e183375938da903640fa756762a7e952d7960 Mon Sep 17 00:00:00 2001 From: pablohashescobar <118773738+pablohashescobar@users.noreply.github.com> Date: Fri, 17 Mar 2023 22:57:10 +0530 Subject: [PATCH] dev: update importer task to create user automatically and adding it to project and workspace (#467) --- apiserver/plane/bgtasks/importer_task.py | 58 +++++++++++++----------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/apiserver/plane/bgtasks/importer_task.py b/apiserver/plane/bgtasks/importer_task.py index fdb32a81b..82050e950 100644 --- a/apiserver/plane/bgtasks/importer_task.py +++ b/apiserver/plane/bgtasks/importer_task.py @@ -1,12 +1,14 @@ # Python imports import json import requests +import uuid import jwt from datetime import datetime # Django imports from django.conf import settings from django.core.serializers.json import DjangoJSONEncoder +from django.contrib.auth.hashers import make_password # Third Party imports from django_rq import job @@ -16,12 +18,13 @@ from sentry_sdk import capture_exception from plane.api.serializers import ImporterSerializer from plane.db.models import ( Importer, - WorkspaceMemberInvite, + WorkspaceMember, GithubRepositorySync, GithubRepository, ProjectMember, WorkspaceIntegration, Label, + User, ) from .workspace_invitation_task import workspace_invitation @@ -35,40 +38,42 @@ def service_importer(service, importer_id): users = importer.data.get("users", []) - workspace_invitations = WorkspaceMemberInvite.objects.bulk_create( + # For all invited users create the uers + new_users = User.objects.bulk_create( [ - WorkspaceMemberInvite( + User( email=user.get("email").strip().lower(), - workspace_id=importer.workspace_id, - token=jwt.encode( - { - "email": user.get("email").strip().lower(), - "timestamp": datetime.now().timestamp(), - }, - settings.SECRET_KEY, - algorithm="HS256", - ), - role=10, + username=uuid.uuid4().hex, + password=make_password(uuid.uuid4().hex), + is_password_autoset=True, ) for user in users if user.get("import", False) == "invite" - or user.get("import", False) == "map" + ], + batch_size=10, + ) + + # Add new users to Workspace and project automatically + WorkspaceMember.objects.bulk_create( + [ + WorkspaceMember(member=user, workspace_id=importer.workspace_id) + for user in new_users + ], + batch_size=100, + ignore_conflicts=True, + ) + ProjectMember.objects.bulk_create( + [ + ProjectMember( + project_id=importer.project_id, + workspace_id=importer.workspace_id, + member=user, + ) + for user in new_users ], batch_size=100, ignore_conflicts=True, ) - - # Send the invites - [ - workspace_invitation.delay( - invitation.email, - importer.workspace_id, - invitation.token, - settings.WEB_URL, - importer.initiated_by.email, - ) - for invitation in workspace_invitations - ] # Check if sync config is on for github importers if service == "github" and importer.config.get("sync", False): @@ -85,7 +90,6 @@ def service_importer(service, importer_id): # Delete the old repository object GithubRepositorySync.objects.filter(project_id=importer.project_id).delete() GithubRepository.objects.filter(project_id=importer.project_id).delete() - # Project member delete # Create a Label for github label = Label.objects.filter(