Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/apps/profiles/backends.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from django.contrib.auth.backends import ModelBackend
from django.core.exceptions import ObjectDoesNotExist
from django.contrib.auth import get_user_model

User = get_user_model()


class EmailAuthenticationBackend(ModelBackend):
def authenticate(self, request, username=None, password=None, **kwargs):
try:
user = User.objects.get(email=username)
if user.check_password(password):
return user
else:
return None
except ObjectDoesNotExist:
return None

def get_user(self, user_id):
try:
user = User.objects.get(id=user_id)
return user
except ObjectDoesNotExist:
return None
6 changes: 6 additions & 0 deletions src/apps/profiles/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,9 @@ class Meta:

model = User
fields = ("username", "email", "password1", "password2")


class LoginForm(forms.Form):

username = forms.CharField(max_length=150)
password = forms.CharField(max_length=150, widget=forms.PasswordInput)
3 changes: 2 additions & 1 deletion src/apps/profiles/urls_accounts.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,10 @@

urlpatterns = [
url(r'^signup', views.sign_up, name="signup"),
path('login/', views.log_in, name='login'),
# url(r'^user_profile', views.user_profile, name="user_profile"),
# path('login/', auth_views.LoginView.as_view(extra_context=extra_context), name='login'),
path('login/', views.LoginView.as_view(), name='login'),
# path('login/', views.LoginView.as_view(), name='login'),
# path('logout/', auth_views.LogoutView.as_view(), name='logout'),
path('logout/', views.LogoutView.as_view(), name='logout'),
path('password_reset/', views.CustomPasswordResetView.as_view(), name='password_reset'),
Expand Down
31 changes: 29 additions & 2 deletions src/apps/profiles/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from django.conf import settings
from django.contrib import messages
from django.contrib.auth import authenticate
from django.contrib.auth import authenticate, login
from django.contrib.sites.shortcuts import get_current_site
from django.core.mail import EmailMessage, EmailMultiAlternatives
from django.http import Http404
Expand All @@ -18,7 +18,7 @@

from api.serializers.profiles import UserSerializer, OrganizationDetailSerializer, OrganizationEditSerializer, \
UserNotificationSerializer
from .forms import SignUpForm
from .forms import SignUpForm, LoginForm
from .models import User, Organization, Membership
from .tokens import account_activation_token

Expand Down Expand Up @@ -128,6 +128,33 @@ def sign_up(request):
return render(request, 'registration/signup.html', context)


def log_in(request):

context = {}
context['chahub_signup_url'] = "{}/profiles/signup?next={}/social/login/chahub".format(
settings.SOCIAL_AUTH_CHAHUB_BASE_URL,
settings.SITE_DOMAIN
)
if request.method == 'POST':
form = LoginForm(request.POST)

if form.is_valid():
username = form.cleaned_data.get('username')
password = form.cleaned_data.get('password')
user = authenticate(username=username, password=password)
if user:
login(request, user)
return redirect('pages:home')
else:
messages.error(request, "Wrong Credentials!")
else:
context['form'] = form

if not context.get('form'):
context['form'] = LoginForm()
return render(request, 'registration/login.html', context)


# Password Reset views/forms below
# auth_forms
class CustomPasswordResetForm(auth_forms.PasswordResetForm):
Expand Down
1 change: 1 addition & 0 deletions src/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@
'utils.oauth_backends.ChahubOAuth2',
'django.contrib.auth.backends.ModelBackend',
'django_su.backends.SuBackend',
'profiles.backends.EmailAuthenticationBackend',
)

SOCIAL_AUTH_PIPELINE = (
Expand Down
2 changes: 1 addition & 1 deletion src/templates/registration/login.html
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ <h2 class="ui blue centered header">
<i class="user icon"></i>
<input type="text"
name="username"
placeholder="username"
placeholder="username or email"
id="id_username"
value="{{ form.username.value|default:''}}"
maxlength="{{ form.username.field.max_length }}"
Expand Down