1
1
from flask import flash , render_template , redirect , url_for , request
2
2
from flask_login import current_user , login_user , logout_user
3
+ from pymysql import IntegrityError
3
4
4
5
from app .modules .auth import auth_bp
5
6
from app .modules .auth .decorators import guest_required
8
9
from app .modules .profile .services import UserProfileService
9
10
from app .modules .captcha .services import CaptchaService
10
11
12
+ from app import db
13
+
11
14
authentication_service = AuthenticationService ()
12
15
user_profile_service = UserProfileService ()
13
16
captcha_service = CaptchaService ()
@@ -20,22 +23,29 @@ def show_signup_form():
20
23
21
24
form = SignupForm ()
22
25
if form .validate_on_submit ():
23
-
24
26
user_input = request .form ['captcha' ]
25
27
if not captcha_service .validate_captcha (user_input ):
26
28
flash ('Please complete the reCAPTCHA' , 'danger' )
27
29
return render_template ('auth/signup_form.html' , form = form )
28
30
29
31
email = form .email .data
30
32
if not authentication_service .is_email_available (email ):
31
- return render_template ("auth/signup_form.html" , form = form , error = f'Email { email } in use' )
33
+ flash (f'Email { email } is already in use' , 'danger' )
34
+ return render_template ("auth/signup_form.html" , form = form )
32
35
33
36
try :
37
+ # Intentamos crear el usuario
34
38
user = authentication_service .create_with_profile (** form .data )
35
39
authentication_service .send_confirmation_email (user .email )
36
40
flash ("Please confirm your email" , "info" )
37
- except Exception as exc :
38
- return render_template ("auth/signup_form.html" , form = form , error = f'Error creating user: { exc } ' )
41
+ except IntegrityError as exc :
42
+ # Manejar el caso de duplicado en la base de datos
43
+ db .session .rollback () # Hacer rollback para limpiar la sesión
44
+ if 'Duplicate entry' in str (exc ):
45
+ flash (f'Email { email } is already in use' , 'danger' )
46
+ else :
47
+ flash (f'Error creating user: { exc } ' , 'danger' )
48
+ return render_template ("auth/signup_form.html" , form = form )
39
49
40
50
return redirect (url_for ("public.index" ))
41
51
0 commit comments