Skip to content

Commit

Permalink
feat: add template_type to ProjectSerializer
Browse files Browse the repository at this point in the history
  • Loading branch information
kallilsouza committed Jan 7, 2025
1 parent 8794b02 commit 59d48aa
Showing 1 changed file with 54 additions and 42 deletions.
96 changes: 54 additions & 42 deletions connect/api/v1/project/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class Meta:
"wa_demo_token",
"redirect_url",
"description",
"template_type",
]
ref_name = None

Expand Down Expand Up @@ -98,7 +99,11 @@ class Meta:

def get_flow_uuid(self, obj):
if obj.is_template and obj.template_project.exists():
template = obj.template_project.filter(flow_uuid__isnull=False, wa_demo_token__isnull=False, redirect_url__isnull=False).first()
template = obj.template_project.filter(
flow_uuid__isnull=False,
wa_demo_token__isnull=False,
redirect_url__isnull=False,
).first()
if template:
return template.flow_uuid
...
Expand All @@ -114,25 +119,35 @@ def get_first_access(self, obj):
template = obj.template_project.get(authorization__user__email=email)
return template.first_access
except TemplateProject.DoesNotExist:
template_project = obj.template_project.filter(wa_demo_token__isnull=False, redirect_url__isnull=False).first()
template_project = obj.template_project.filter(
wa_demo_token__isnull=False, redirect_url__isnull=False
).first()
template = obj.template_project.create(
wa_demo_token=template_project.wa_demo_token,
redirect_url=template_project.redirect_url,
authorization=authorization
authorization=authorization,
)
...

def get_wa_demo_token(self, obj):
if obj.is_template and obj.template_project.exists():
template = obj.template_project.filter(flow_uuid__isnull=False, wa_demo_token__isnull=False, redirect_url__isnull=False).first()
template = obj.template_project.filter(
flow_uuid__isnull=False,
wa_demo_token__isnull=False,
redirect_url__isnull=False,
).first()
if template:
return template.wa_demo_token
...
...

def get_redirect_url(self, obj):
if obj.is_template and obj.template_project.exists():
template = obj.template_project.filter(flow_uuid__isnull=False, wa_demo_token__isnull=False, redirect_url__isnull=False).first()
template = obj.template_project.filter(
flow_uuid__isnull=False,
wa_demo_token__isnull=False,
redirect_url__isnull=False,
).first()
if template:
return template.redirect_url
...
Expand Down Expand Up @@ -164,10 +179,7 @@ def create(self, validated_data):
)

validated_data.update(
{
"flow_organization": project.get("uuid"),
"created_by": user
}
{"flow_organization": project.get("uuid"), "created_by": user}
)
instance = super().create(validated_data)

Expand All @@ -182,17 +194,14 @@ def update(self, instance, validated_data):
def get_authorizations(self, obj):
exclude_roles = [ProjectRole.SUPPORT.value]
queryset = obj.project_authorizations.exclude(role__in=exclude_roles)
response = dict(
count=queryset.count(),
users=[]
)
response = dict(count=queryset.count(), users=[])
for i in queryset:
chats_role = None
if i.rocket_authorization:
chats_role = i.rocket_authorization.role
elif i.chats_authorization:
chats_role = i.chats_authorization.role
response['users'].append(
response["users"].append(
dict(
username=i.user.username,
email=i.user.email,
Expand All @@ -214,7 +223,7 @@ def get_pending_authorizations(self, obj):
rocket_authorization = RequestRocketPermission.objects.filter(email=i.email)
chats_authorization = RequestChatsPermission.objects.filter(email=i.email)
chats_role = None
if(len(rocket_authorization) > 0):
if len(rocket_authorization) > 0:
rocket_authorization = rocket_authorization.first()
chats_role = rocket_authorization.role

Expand All @@ -227,7 +236,7 @@ def get_pending_authorizations(self, obj):
email=i.email,
project_role=i.role,
created_by=i.created_by.email,
chats_role=chats_role
chats_role=chats_role,
)
)
return response
Expand All @@ -246,7 +255,9 @@ def get_last_opened_on(self, obj):
request = self.context.get("request")
if not request or not request.user.is_authenticated:
return None
opened = OpenedProject.objects.filter(user__email=request.user, project=obj.uuid)
opened = OpenedProject.objects.filter(
user__email=request.user, project=obj.uuid
)
response = None
if opened.exists():
opened = opened.first()
Expand Down Expand Up @@ -297,15 +308,11 @@ def validate(self, attrs):

email = attrs.get("email")

if ' ' in email:
raise ValidationError(
_("Email field cannot have spaces")
)
if " " in email:
raise ValidationError(_("Email field cannot have spaces"))

if bool(re.match('[A-Z]', email)):
raise ValidationError(
_("Email field cannot have uppercase characters")
)
if bool(re.match("[A-Z]", email)):
raise ValidationError(_("Email field cannot have uppercase characters"))

return attrs

Expand Down Expand Up @@ -496,7 +503,7 @@ def create(self, validated_data, request):
data.update(
{
"data": {"message": "Project authorization not setted"},
"status": "FAILED"
"status": "FAILED",
}
)
return data
Expand All @@ -509,14 +516,16 @@ def create(self, validated_data, request):
intelligence_client = IntelligenceRESTClient()
try:
repository_uuid = settings.REPOSITORY_IDS.get(project.template_type)
access_token = intelligence_client.get_access_token(request.user.email, repository_uuid)
access_token = intelligence_client.get_access_token(
request.user.email, repository_uuid
)
except Exception as error:
logger.error(f" REPOSITORY IDS {error}")
template.delete()
data.update(
{
"data": {"message": "Could not get access token"},
"status": "FAILED"
"status": "FAILED",
}
)
return data
Expand All @@ -529,23 +538,24 @@ def create(self, validated_data, request):
classifier_uuid = tasks.create_classifier(
project_uuid=str(project.flow_organization),
user_email=request.user.email,
classifier_name="Farewell & Greetings" if project.template_type == Project.TYPE_LEAD_CAPTURE else "Binary Answers",
classifier_name=(
"Farewell & Greetings"
if project.template_type == Project.TYPE_LEAD_CAPTURE
else "Binary Answers"
),
access_token=access_token,
).get("uuid")
except Exception as error:
logger.error(f"CREATE CLASSIFIER {error}")
template.delete()
data.update(
{
"message": "Could not create classifier",
"status": "FAILED"
}
{"message": "Could not create classifier", "status": "FAILED"}
)
return data
else:
classifier_uuid = uuid.uuid4()

# Create Flow and Ticketer
# Create Flow and Ticketer
if not settings.TESTING and not settings.USE_EDA:
rest_client = FlowsRESTClient()
try:
Expand All @@ -558,7 +568,7 @@ def create(self, validated_data, request):
date_format=project.date_format,
timezone=str(project.timezone),
is_template=True,
user_email=project.created_by.email
user_email=project.created_by.email,
)
chats_response = json.loads(chats_response.text)
flows = rest_client.create_flows(
Expand All @@ -573,12 +583,7 @@ def create(self, validated_data, request):
except Exception as error:
logger.error(error)
template.delete()
data.update(
{
"message": "Could not create flow",
"status": "FAILED"
}
)
data.update({"message": "Could not create flow", "status": "FAILED"})
return data
else:
flows = {"uuid": uuid.uuid4()}
Expand All @@ -592,7 +597,14 @@ def create(self, validated_data, request):
redirect_url = "https://wa.me/5582123456?text=wa-demo-12345"
template.wa_demo_token = wa_demo_token
template.redirect_url = redirect_url
template.save(update_fields=["classifier_uuid", "flow_uuid", "wa_demo_token", "redirect_url"])
template.save(
update_fields=[
"classifier_uuid",
"flow_uuid",
"wa_demo_token",
"redirect_url",
]
)

data = {
"first_access": template.first_access,
Expand Down

0 comments on commit 59d48aa

Please sign in to comment.