updated login mechanism

This commit is contained in:
Arthur Gerbaud
2016-11-16 20:04:00 +01:00
parent 96f013041b
commit 673c620be0
3 changed files with 23 additions and 20 deletions

View File

@@ -28,13 +28,23 @@
<li><a href="{% url 'logout' %}">{% bootstrap_icon "log-out" %} Déconnecter</a></li>
{% else %}
<li>
<form class="navbar-form navbar-left" method="post" action="">{% csrf_token %}
<form class="navbar-form navbar-left" method="post" action="/login/">{% csrf_token %}
{% if next %}
<p class="well-sm text-center"><strong style="color:#980300;">Vous devez vous connecter<br/> pour accéder à cette page.</strong></p>
<input name="next" value="{{next}}" hidden />
{% endif %}
<div class="form-group form-horizontal">
{% bootstrap_form form layout="horizontal" show_label=False %}
<div class="form-group">
<label class="col-md-2 sr-only control-label" for="id_username">Username</label>
<div class="col-md-10">
<input autofocus="" class="form-control" id="id_username" maxlength="254" name="username" placeholder="Username" title="" type="text" required />
</div>
</div>
<div class="form-group">
<label class="col-md-2 sr-only control-label" for="id_password">Password</label>
<div class="col-md-10"><input class="form-control" id="id_password" name="password" placeholder="Password" title="" type="password" required />
</div>
</div>
</div>
<div class="text-center"><button class="btn btn-primary navbar-button" type="submit">Connexion</button></div>
</form>

View File

@@ -2,14 +2,15 @@ from django.conf.urls import include, url
from django.contrib.auth import views as auth_views
from .views import Index
from .views import Index, login_view
from maraudes import urls as maraudes_urls
from suivi import urls as suivi_urls
from sujets import urls as sujets_urls
urlpatterns = [
# Authentification
url('^$', Index.as_view(), name="index"),
url(r'^$', Index.as_view(), name="index"),
url(r'^login/$', login_view),
url(r'^logout/$', auth_views.logout, {
'template_name': 'logout.html',
'next_page': 'index',

View File

@@ -4,40 +4,32 @@ from django import views
from .mixins import WebsiteTemplateMixin
from django.contrib.auth.views import login
from django.http import HttpResponseRedirect
from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect
class Index(WebsiteTemplateMixin, views.generic.TemplateView):
template_name = "main.html"
app_menu = None
login_response = None
class PageInfo:
title = "La maraude ALSA"
header = "La Maraude ALSA"
header_small = "accueil"
def dispatch(self, request, *args, **kwargs):
self.user = request.user
self.login_response = login(request)
return super().dispatch(request, *args, **kwargs)
def _get_user_entry_point(self):
# Should find best entry point according to user Group
return reverse('maraudes:index')
def post(self, request, *args, **kwargs):
if hasattr(self.login_response, 'url') and 'next' in self.request.POST:
return self.login_response
return self.get(request, *args, **kwargs)
def get(self, request, *args, **kwargs):
if request.user.is_authenticated():
return redirect(self._get_user_entry_point())
return super().get(request, *args, **kwargs)
def get_context_data(self, **kwargs):
context = super().get_context_data(**kwargs)
context.update(self.login_response.context_data)
return context
def login_view(request):
if request.method == 'GET':
return HttpResponsePermanentRedirect('/')
elif request.method == 'POST':
response = login(request)
return HttpResponseRedirect('/')