Inventario provides a secure authentication system with multiple login methods, email verification, and password recovery features. The system is built on Django’s authentication framework with custom extensions.
Users can log in using either their username or email address with their password.
def login_usuario(request): if request.method == 'POST': # Allow login with email or username data = request.POST.copy() username_input = data.get('username', '') User = get_user_model() user_obj = None if '@' in username_input: try: user_obj = User.objects.get(email__iexact=username_input) data['username'] = user_obj.username except User.DoesNotExist: user_obj = None form = LoginForm(request, data=data) username = data.get('username') password = request.POST.get('password')
Flexible Login: Users can authenticate using either their username or email address. The system automatically detects email addresses by checking for the @ symbol.
When users log in with Google, their profile photo is automatically saved:
social = SocialAccount.objects.get(user=request.user, provider='google')foto_url = social.extra_data.get('picture')if foto_url and not request.user.foto_perfil: # Download and save profile photo
3
Redirect After Login
Users are redirected based on their role:
Superusers: /admin/
Admin: /dashboard/
Vendedor: Sales list view
Google OAuth requires proper configuration of GOOGLE_CLIENT_ID and GOOGLE_CLIENT_SECRET environment variables.
Before allowing login, the system checks account status:
applications/cuentas/views.py:241-244
try: if user_obj is None: user_obj = User.objects.get(username=username) if not user_obj.is_active: messages.error(request, '❌ La cuenta no ha sido activada por el administrador.') return render(request, 'login.html', {'form': form})except User.DoesNotExist: user_obj = None
Inactive accounts receive a clear error message directing them to contact an administrator.