parent
7508472216
commit
0752cd5ff4
|
@ -0,0 +1,38 @@
|
||||||
|
from django import forms
|
||||||
|
from django.contrib.auth import authenticate
|
||||||
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
|
||||||
|
|
||||||
|
class LoginForm(forms.Form):
|
||||||
|
email = forms.EmailField(max_length=75)
|
||||||
|
password = forms.CharField(
|
||||||
|
max_length=255, widget=forms.PasswordInput)
|
||||||
|
|
||||||
|
def authenticate(self):
|
||||||
|
result = None
|
||||||
|
|
||||||
|
if self.cleaned_data:
|
||||||
|
email = self.cleaned_data['email']
|
||||||
|
password = self.cleaned_data['password']
|
||||||
|
|
||||||
|
result = authenticate(username=email, password=password)
|
||||||
|
|
||||||
|
return result
|
||||||
|
|
||||||
|
def clean(self):
|
||||||
|
data = self.cleaned_data
|
||||||
|
|
||||||
|
if not self.errors:
|
||||||
|
user = self.authenticate()
|
||||||
|
|
||||||
|
if user is not None:
|
||||||
|
if not user.is_active:
|
||||||
|
raise forms.ValidationError(
|
||||||
|
_("This account is disabled.")
|
||||||
|
)
|
||||||
|
else:
|
||||||
|
raise forms.ValidationError(
|
||||||
|
_('User with those credentials was not found.')
|
||||||
|
)
|
||||||
|
|
||||||
|
return data
|
|
@ -1,8 +1,9 @@
|
||||||
from django.conf.urls import patterns, url
|
from django.conf.urls import patterns, url
|
||||||
|
|
||||||
from .views import LoginView
|
from .views import LoginView, LogoutView
|
||||||
|
|
||||||
urlpatterns = patterns(
|
urlpatterns = patterns(
|
||||||
'',
|
'',
|
||||||
url(r'^login/$', LoginView.as_view()),
|
url(r'^login/$', LoginView.as_view()),
|
||||||
|
url(r'^logout/$', LogoutView.as_view()),
|
||||||
)
|
)
|
||||||
|
|
|
@ -1,11 +1,55 @@
|
||||||
from django.views.generic import View
|
from django.views.generic import View
|
||||||
from django.template import RequestContext
|
from django.template import RequestContext
|
||||||
from django.shortcuts import render_to_response
|
from django.shortcuts import render_to_response
|
||||||
|
from django.contrib.auth import logout
|
||||||
|
from django.utils.translation import ugettext_lazy as _
|
||||||
|
from django.http import HttpResponseRedirect
|
||||||
|
from django.contrib import messages
|
||||||
|
from django.contrib.auth import login
|
||||||
|
|
||||||
|
from .forms import LoginForm
|
||||||
|
|
||||||
|
|
||||||
class LoginView(View):
|
class LoginView(View):
|
||||||
template = 'landing/landing.html'
|
template = 'users/login.html'
|
||||||
|
|
||||||
def get(self, request):
|
def get(self, request):
|
||||||
ctx = RequestContext(request, {})
|
context = {
|
||||||
|
'login_form': LoginForm()
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx = RequestContext(request, context)
|
||||||
return render_to_response(self.template, context_instance=ctx)
|
return render_to_response(self.template, context_instance=ctx)
|
||||||
|
|
||||||
|
def post(self, request):
|
||||||
|
login_form = LoginForm(request.POST)
|
||||||
|
|
||||||
|
if login_form.is_valid():
|
||||||
|
user = login_form.authenticate()
|
||||||
|
login(request, user)
|
||||||
|
|
||||||
|
messages.success(
|
||||||
|
request,
|
||||||
|
_('Logged in successfully.')
|
||||||
|
)
|
||||||
|
|
||||||
|
return HttpResponseRedirect('/')
|
||||||
|
|
||||||
|
context = {
|
||||||
|
'login_form': login_form,
|
||||||
|
}
|
||||||
|
|
||||||
|
ctx = RequestContext(request, context)
|
||||||
|
return render_to_response(self.template, context_instance=ctx)
|
||||||
|
|
||||||
|
|
||||||
|
class LogoutView(View):
|
||||||
|
def get(self, request):
|
||||||
|
logout(request)
|
||||||
|
|
||||||
|
messages.success(
|
||||||
|
request,
|
||||||
|
_('Logged out successfully')
|
||||||
|
)
|
||||||
|
|
||||||
|
return HttpResponseRedirect('/')
|
||||||
|
|
|
@ -0,0 +1,7 @@
|
||||||
|
{% block main_content %}
|
||||||
|
<form method="post">
|
||||||
|
{% csrf_token %}
|
||||||
|
{{ login_form.as_p }}
|
||||||
|
<button class="btn btn-primary" type="submit">Login</button>
|
||||||
|
</form>
|
||||||
|
{% endblock %}
|
|
@ -6,5 +6,6 @@ admin.autodiscover()
|
||||||
urlpatterns = patterns(
|
urlpatterns = patterns(
|
||||||
'',
|
'',
|
||||||
url(r'^', include('shelfzilla.apps.landing.urls')),
|
url(r'^', include('shelfzilla.apps.landing.urls')),
|
||||||
|
url(r'^', include('shelfzilla.apps.users.urls')),
|
||||||
url(r'^admin/', include(admin.site.urls)),
|
url(r'^admin/', include(admin.site.urls)),
|
||||||
)
|
)
|
||||||
|
|
Reference in New Issue