login logout
authorcavaliet
Tue, 03 Sep 2013 17:44:17 +0200
changeset 24 3b1b0a9309d6
parent 23 cd4055479ba3
child 27 d2a40f44cbb9
login logout
src/p4l/settings.py
src/p4l/templates/p4l/p4l_base.html
src/p4l/templates/registration/login.html
src/p4l/urls.py
src/p4l/views.py
--- a/src/p4l/settings.py	Tue Sep 03 15:23:43 2013 +0200
+++ b/src/p4l/settings.py	Tue Sep 03 17:44:17 2013 +0200
@@ -187,4 +187,12 @@
 from config import *  # @UnusedWildImport
 
 if not "SRC_BASE_URL" in locals():
-    SRC_BASE_URL = BASE_URL + __name__.split('.')[0] + '/'     
+    SRC_BASE_URL = BASE_URL + __name__.split('.')[0] + '/'
+if not "LOGIN_URL" in locals():
+    LOGIN_URL = SRC_BASE_URL + 'auth/login/'
+if not "LOGOUT_URL" in locals():
+    LOGOUT_URL = SRC_BASE_URL + 'auth/disconnect/'
+if not "LOGIN_REDIRECT_URL" in locals():
+    LOGIN_REDIRECT_URL = SRC_BASE_URL
+if not "LOGOUT_REDIRECT_URL" in locals():
+    LOGOUT_REDIRECT_URL = SRC_BASE_URL + 'auth/login'     
--- a/src/p4l/templates/p4l/p4l_base.html	Tue Sep 03 15:23:43 2013 +0200
+++ b/src/p4l/templates/p4l/p4l_base.html	Tue Sep 03 17:44:17 2013 +0200
@@ -24,6 +24,15 @@
 	          <a href="{% url 'p4l_home' %}">Plan 4 Learning</a>
 	        </li>
 	      </ul>
+          <ul class="nav navbar-nav" style="float:right;">
+            {% if user.is_authenticated %}
+              <li><a>{{ user.username }}</a></li>
+              {% if user.is_staff %}<li><a href="{% url 'admin:index' %}" >admin</a></li>{% endif %}
+              <li><a href="{% url 'logout' %}?next={% url 'p4l_home' %}" >{% trans 'logout' %}</a></li>
+            {% else %}
+              <li><a href="{% url 'login' %}" >{% trans 'login' %}</a></li>
+            {% endif %}
+          </ul>
 	    </nav>
 	  </div>
 	  {% endblock %}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/p4l/templates/registration/login.html	Tue Sep 03 17:44:17 2013 +0200
@@ -0,0 +1,31 @@
+{% extends "p4l/p4l_base.html" %}
+{% load i18n %}
+
+{% block page_title %}{% trans 'login' %}{% endblock %}
+
+{% block content %}
+{% if form.errors %}
+    <p class="error">{% trans "Sorry, that's not a valid username or password." %}</p>
+{% endif %}
+    <form action="{% url 'login' %}" method='post' class="form-horizontal" role="form">
+        {% csrf_token %}
+        <input type="hidden" name="next" value="{% if next %}{{ next }}{% else %}{% url 'p4l_home' %}{% endif %}" />
+        <div class="form-group">
+    		<label class="col-lg-2 control-label" for="id_username">{% trans 'Username' %} :</label>
+    		<div class="col-lg-10">
+                <input id="id_username" type="text" name="username" maxlength="254" class="form-control" placeholder="{% trans 'Username' %}" required="required">
+			</div>
+    	</div>
+		<div class="form-group">
+			<label class="col-lg-2 control-label" for="id_password">{% trans 'Password' %} :</label>
+			<div class="col-lg-10">
+                <input id="id_password" type="password" name="password" class="form-control" placeholder="{% trans 'Password' %}" required="required">
+            </div>
+		</div>
+        <div class="form-group">
+           <div class="col-lg-offset-2 col-lg-10">
+               <button type="submit" class="btn btn-default">{% trans "login" %}</button>
+           </div>
+        </div>
+    </form>
+{% endblock %}
--- a/src/p4l/urls.py	Tue Sep 03 15:23:43 2013 +0200
+++ b/src/p4l/urls.py	Tue Sep 03 17:44:17 2013 +0200
@@ -1,12 +1,13 @@
 from django.conf.urls import patterns, include, url
 from django.contrib import admin
+from django.contrib.auth import urls as auth_urls
 from p4l.views import RecordListView, RecordDetailView
 
 admin.autodiscover()
 
 urlpatterns = patterns('',
-    # Examples:
     url(r'^$', RecordListView.as_view(), name='p4l_home'),
+    url(r'^auth/', include(auth_urls)),
     url(r'^record/view$', RecordDetailView.as_view(), name='p4l_record_view'),
     url(r'^api/', include('p4l.api.urls')),
 
--- a/src/p4l/views.py	Tue Sep 03 15:23:43 2013 +0200
+++ b/src/p4l/views.py	Tue Sep 03 17:44:17 2013 +0200
@@ -6,12 +6,10 @@
 '''
 
 from django.conf import settings
-#from django.core.paginator import Paginator, InvalidPage, EmptyPage
-#from django.db.models import Count
-#from django.http.response import HttpResponse, HttpResponseForbidden
+from django.contrib.auth.decorators import login_required
 from django.shortcuts import get_object_or_404
-from django.views.generic import ListView, DetailView#, View
-#from django.views.generic.list import MultipleObjectMixin
+from django.utils.decorators import method_decorator
+from django.views.generic import ListView, DetailView
 from .models import Record
 from .forms import RecordFilterForm
 from .utils import get_labels_for_uris
@@ -27,6 +25,9 @@
     template_name = "p4l/p4l_home.html"
     form_class = RecordFilterForm
     
+    @method_decorator(login_required)
+    def dispatch(self, *args, **kwargs):
+        return super(RecordListView, self).dispatch(*args, **kwargs)
     
     def get_context_data(self, **kwargs):
         context = ListView.get_context_data(self, **kwargs)
@@ -53,6 +54,10 @@
     template_name = "p4l/p4l_record_view.html"
     slug_field = "uri" # Even if it is useless because we override get_objet
     
+    @method_decorator(login_required)
+    def dispatch(self, *args, **kwargs):
+        return super(RecordDetailView, self).dispatch(*args, **kwargs)
+    
     def get_object(self, queryset=None):
         if "uri" not in self.request.GET:
             raise AttributeError(u"Record view must be called uri GET parameter")