dev: fix user mapping

This commit is contained in:
pablohashescobar 2024-03-20 10:09:44 +05:30
parent 0487579f4e
commit cf75cb35b1

View File

@ -64,13 +64,299 @@ from plane.db.models import (
from plane.utils.exception_logger import log_exception
def replace_model_data(fields, model_data, user_maps):
for field in fields:
model_data[field] = user_maps.get(
model_data.get(field), model_data.get(field)
)
return model_data
def data_transformer(model, model_data, user_maps):
# model name mapper
mapper = {
"workspace": [
"created_by_id",
"updated_by_id",
"owner_id",
],
"workspacememberinvite": [
"created_by_id",
"updated_by_id",
],
"workspacetheme": [
"created_by_id",
"updated_by_id",
],
"workspacemember": [
"created_by_id",
"updated_by_id",
"member_id",
],
"workspaceuserproperties": [
"created_by_id",
"updated_by_id",
"user_id",
],
"project": [
"created_by_id",
"updated_by_id",
"default_assignee_id",
"project_lead_id",
],
"projectdeployboard": [
"created_by_id",
"updated_by_id",
],
"projectfavorite": [
"user_id",
"created_by_id",
"updated_by_id",
],
"projectmember": [
"created_by_id",
"updated_by_id",
"member_id",
],
"projectidentifier": [
"created_by_id",
"updated_by_id",
],
"projectmemberinvite": [
"created_by_id",
"updated_by_id",
],
"projectpublicmember": [
"created_by_id",
"updated_by_id",
"member_id",
],
"state": [
"created_by_id",
"updated_by_id",
],
"label": [
"created_by_id",
"updated_by_id",
],
"estimate": [
"created_by_id",
"updated_by_id",
],
"estimatepoint": [
"created_by_id",
"updated_by_id",
],
"issue": [
"created_by_id",
"updated_by_id",
],
"issuecomment": [
"created_by_id",
"updated_by_id",
"actor_id",
],
"issueassignee": [
"created_by_id",
"updated_by_id",
"assignee_id",
],
"issuelabel": [
"created_by_id",
"updated_by_id",
],
"issuelink": [
"created_by_id",
"updated_by_id",
],
"issuemention": [
"created_by_id",
"updated_by_id",
"mention_id",
],
"issuevote": [
"created_by_id",
"updated_by_id",
"actor_id",
],
"issuesubscriber": [
"created_by_id",
"updated_by_id",
"subscriber_id",
],
"issueproperty": [
"created_by_id",
"updated_by_id",
"user_id",
],
"issuesequence": [
"created_by_id",
"updated_by_id",
],
"issuereaction": [
"created_by_id",
"updated_by_id",
"actor_id",
],
"issuerelation": [
"created_by_id",
"updated_by_id",
],
"issueattachment": [
"created_by_id",
"updated_by_id",
],
"issueactivity": [
"created_by_id",
"updated_by_id",
"actor_id",
],
"apitoken": [
"created_by_id",
"updated_by_id",
"user_id",
],
"fileasset": [
"created_by_id",
"updated_by_id",
],
"commentreaction": [
"created_by_id",
"updated_by_id",
"actor_id",
],
"cycle": [
"created_by_id",
"updated_by_id",
"owned_by_id",
],
"cycleissue": [
"created_by_id",
"updated_by_id",
],
"cyclefavorite": [
"user_id",
"created_by_id",
"updated_by_id",
],
"cycleuserproperties": [
"user_id",
"created_by_id",
"updated_by_id",
],
"module": [
"created_by_id",
"updated_by_id",
"lead_id",
],
"moduleissue": [
"created_by_id",
"updated_by_id",
],
"modulefavorite": [
"user_id",
"created_by_id",
"updated_by_id",
],
"modulelink": [
"created_by_id",
"updated_by_id",
],
"modulemember": [
"created_by_id",
"updated_by_id",
"member_id",
],
"moduleuserproperties": [
"user_id",
"created_by_id",
"updated_by_id",
],
"page": [
"created_by_id",
"updated_by_id",
"owned_by_id",
],
"pagelog": [
"created_by_id",
"updated_by_id",
],
"pagelabel": [
"created_by_id",
"updated_by_id",
],
"pagefavorite": [
"user_id",
"created_by_id",
"updated_by_id",
],
"webhook": [
"created_by_id",
"updated_by_id",
],
"issueview": [
"created_by_id",
"updated_by_id",
],
"issueviewfavorite": [
"user_id",
"created_by_id",
"updated_by_id",
],
"notification": [
"created_by_id",
"updated_by_id",
"triggered_by_id",
"receiver_id",
],
"usernotificationpreference": [
"user_id",
"created_by_id",
"updated_by_id",
],
"inbox": [
"created_by_id",
"updated_by_id",
],
"inboxissue": [
"created_by_id",
"updated_by_id",
],
}
# Get all the fields for the current model
fields = mapper[model._meta.model_name]
if fields:
model_data = replace_model_data(
fields=fields, model_data=model_data, user_maps=user_maps
)
# return modified model data
return model_data
# return model data
return model_data
@shared_task
def workspace_import(workspace_data):
try:
# Create Users
users = workspace_data.get("users.json")
users = json.loads(workspace_data.get("users.json"))
# get user emails
user_emails = [user.get("email") for user in users]
imported_users = {user.get("email"): user.get("id") for user in users}
existing_users = User.objects.filter(email__in=user_emails).values(
"id", "email"
)
user_maps = {
imported_users.get(exuser.get("email")): str(exuser.get("id"))
for exuser in existing_users
if imported_users.get(exuser.get("email"))
}
User.objects.bulk_create(
[User(**user) for user in json.loads(users)],
[User(**user) for user in (users)],
batch_size=100,
ignore_conflicts=True,
)
@ -78,7 +364,16 @@ def workspace_import(workspace_data):
# Workspaces
workspaces = workspace_data.get("workspaces.json")
Workspace.objects.bulk_create(
[Workspace(**workspace) for workspace in json.loads(workspaces)],
[
Workspace(
**data_transformer(
model=Workspace,
model_data=workspace,
user_maps=user_maps,
)
)
for workspace in json.loads(workspaces)
],
batch_size=100,
ignore_conflicts=True,
)
@ -157,13 +452,24 @@ def workspace_import(workspace_data):
for model in models:
file_name = models[model]
data = workspace_data.get(file_name)
# Loop through all the models and create the records accordingly
model.objects.bulk_create(
[model(**model_data) for model_data in json.loads(data)],
[
model(
**data_transformer(
model=model,
model_data=model_data,
user_maps=user_maps,
)
)
for model_data in json.loads(data)
],
batch_size=100,
ignore_conflicts=True,
)
return
except Exception as e:
log_exception(e)
return