Skip to content

Commit

Permalink
feature: creating user if doesnot exist in project creation
Browse files Browse the repository at this point in the history
  • Loading branch information
AlanJaeger committed Sep 21, 2023
1 parent 90b03ca commit 5d82e4e
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 20 deletions.
6 changes: 4 additions & 2 deletions chats/apps/event_driven/consumers.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
from .signals import message_finished, message_started


def pyamqp_call_dlx_when_error(routing_key: str, default_exchange: str):
def pyamqp_call_dlx_when_error(
routing_key: str, default_exchange: str, consumer_name: str
):
def decorator(consumer):
def consumer_wrapper(*args, **kw):
message = args[0]
Expand All @@ -18,7 +20,7 @@ def consumer_wrapper(*args, **kw):
except Exception as err:
capture_exception(err)
channel.basic_reject(message.delivery_tag, requeue=False)
print(f"[TemplateTypeConsumer] - Message rejected by: {err}")
print(f"[{consumer_name}] - Message rejected by: {err}")
callback_body = {
"original_message": message.body.decode("utf-8"),
"error_type": str(type(err)),
Expand Down
1 change: 1 addition & 0 deletions chats/apps/projects/consumers/project_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class ProjectConsumer(EDAConsumer):
@pyamqp_call_dlx_when_error(
default_exchange=settings.CONNECT_DEFAULT_DEAD_LETTER_EXCHANGE,
routing_key="",
consumer_name="ProjectConsumer",
)
def consume(message: amqp.Message):
channel = message.channel
Expand Down
1 change: 1 addition & 0 deletions chats/apps/projects/consumers/template_type_consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class TemplateTypeConsumer(EDAConsumer):
@pyamqp_call_dlx_when_error(
default_exchange=settings.CONNECT_DEFAULT_DEAD_LETTER_EXCHANGE,
routing_key="",
consumer_name="TemplateTypeConsumer",
)
def consume(message: amqp.Message):
print(f"[TemplateTypeConsumer] - Consuming a message. Body: {message.body}")
Expand Down
33 changes: 15 additions & 18 deletions chats/apps/projects/usecases/project_creation.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,30 @@ class ProjectCreationUseCase:
def __init__(self, sector_setup_handler):
self.__sector_setup_handler = sector_setup_handler

def get_or_create_user_by_email(self, email: str) -> tuple:
return User.objects.get_or_create(email=email)

def create_project(self, project_dto: ProjectCreationDTO):
project: Project = None
template_type: TemplateType = None
user: User = None

if project_dto.is_template and project_dto.template_type_uuid is None:
raise InvalidProjectData(
"'template_type_uuid' cannot be empty when 'is_template' is True!"
)

try:
template_type = TemplateType.objects.get(
uuid=project_dto.template_type_uuid
)
except TemplateType.DoesNotExist:
raise InvalidProjectData(
f"Template Type with uuid `{project_dto.template_type_uuid}` does not exists!"
)
if project_dto.is_template:
try:
template_type = TemplateType.objects.get(
uuid=project_dto.template_type_uuid
)
except TemplateType.DoesNotExist:
raise InvalidProjectData(
f"Template Type with uuid `{project_dto.template_type_uuid}` does not exists!"
)

user, _ = self.get_or_create_user_by_email(project_dto.user_email)

try:
user = User.objects.get(email=project_dto.user_email)
except User.DoesNotExist:
raise InvalidProjectData(
f"User with email `{project_dto.user_email}` does not exist!"
)
if Project.objects.filter(uuid=project_dto.uuid).exists():
raise InvalidProjectData(f"The project `{project_dto.uuid}` already exist!")
else:
Expand All @@ -69,9 +68,7 @@ def create_project(self, project_dto: ProjectCreationDTO):
role=1 if permission.get("role") == 3 else 2,
)

creator_permission = ProjectPermission.objects.get(
user=project_dto.user_email, project=project
)
creator_permission = ProjectPermission.objects.get(user=user, project=project)

if project_dto.is_template:
self.__sector_setup_handler.setup_sectors_in_project(
Expand Down

0 comments on commit 5d82e4e

Please sign in to comment.