Skip to content

Commit

Permalink
add asynchronous task async_validate_email_invite
Browse files Browse the repository at this point in the history
  • Loading branch information
gustavomm19 committed Nov 10, 2023
1 parent 7132e27 commit b470ef2
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 5 deletions.
6 changes: 2 additions & 4 deletions breathecode/authenticate/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
from breathecode.events.models import Event
from breathecode.registry.models import Asset
import breathecode.activity.tasks as tasks_activity
import breathecode.authenticate.tasks as tasks_authenticate
from breathecode.marketing.actions import validate_email

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -1477,10 +1478,7 @@ def get_access_token(self, obj: UserInvite):
if obj.status != 'ACCEPTED':
return None

email_status = validate_email(obj.email, lang)
obj.email_quality = email_status['score']
obj.email_status = email_status
obj.save()
tasks_authenticate.validate_email.delay(obj.id)

if not self.user:
self.user = User(email=obj.email,
Expand Down
24 changes: 24 additions & 0 deletions breathecode/authenticate/tasks.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import logging, os
from celery import shared_task, Task
from django.contrib.auth.models import User
from breathecode.authenticate.models import UserInvite
from breathecode.marketing.actions import validate_email
from breathecode.utils.decorators import task, RetryTask

from breathecode.utils.decorators.task import task
from .actions import set_gitpod_user_expiration, add_to_organization, remove_from_organization
Expand All @@ -18,6 +21,27 @@ class BaseTaskWithRetry(Task):
retry_backoff = True


@task(bind=True)
def async_validate_email_invite(self, invite_id):
logger.debug(f'Validating email for invite {invite_id}')
user_invite = UserInvite.objects.filter(id=invite_id).first()

if user_invite is None:
logger.error(f'UserInvite {invite_id} not found')
return

try:
email_status = validate_email(user_invite.email, 'en')
except:
raise RetryTask(f'Retrying email validation for invite {invite_id}')

user_invite.email_quality = email_status['score']
user_invite.email_status = email_status
user_invite.save()

return True


@shared_task
def async_set_gitpod_user_expiration(gitpoduser_id):
logger.debug(f'Recalculate gitpoduser expiration for {gitpoduser_id}')
Expand Down
2 changes: 1 addition & 1 deletion breathecode/marketing/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ def validate_email(email, lang):
'El correo electrónico que haz especificado parece inválido, por favor corrige tu correo electronico',
slug='invalid-email'))

if 'quality_score' in data and float(data['quality_score']) < 0.60:
if 'quality_score' in data and float(data['quality_score']) <= 0.60:
raise ValidationException(translation(
lang,
en=
Expand Down

0 comments on commit b470ef2

Please sign in to comment.