Commits

Anonymous committed bcab1b4

Added support for simple authentication.

This patch decouples Transifex from the django_authopenid app,
in order to allow deployments with other auth backends.

Two reasons why: 1. django_authopenid does not do legacy
auth correctly (http://code.google.com/p/django-authopenid/issues/detail?id=51)
and 2. some functions like 'change password' do not make sense
with a backend auth system.

With a backend like Fedora Account System we probably only
need login and logout methods/views, which the application
'simpleauth' provides.

Comments (0)

Files changed (23)

     "django.core.context_processors.i18n",
     "django.core.context_processors.media",
     "django.core.context_processors.request",
-    # Following needs notification app too.
+    # Notification support (requires app below too)
     # "notification.context_processors.notification",
 )
 
     'django.middleware.doc.XViewMiddleware',
     'django.contrib.flatpages.middleware.FlatpageFallbackMiddleware',
     'pagination.middleware.PaginationMiddleware',
+    # OpenID support (requires app below too)
     'django_authopenid.middleware.OpenIDMiddleware',
 ]
 
     'django.contrib.sessions',
     'django.contrib.sites',
     'django.contrib.admindocs',
-    # Following needs notification context processor above too.
+    # Notification support (requires context processor above too)
     # 'notification',
+    # OpenId support (needs middleware class above too)
     'django_authopenid',
     'tagging',
     'pagination',

settings_local.py

 
 INSTALLED_APPS += ['django_evolution',]
 
+#Disable use of authopenid app and fall back to simple auth:
+#INSTALLED_APPS.remove('django_authopenid')
+#INSTALLED_APPS += ['simpleauth']
+
 # To enable the use of the notification app on your system:
 #ENABLE_NOTICES = True
 #TEMPLATE_CONTEXT_PROCESSORS += ["notification.context_processors.notification",]

simpleauth/__init__.py

Empty file added.

simpleauth/urls.py

+"""
+Simple URLConf for Django user authentication (no registration).
+"""
+
+from django.conf.urls.defaults import *
+from django.views.generic.simple import direct_to_template
+from django.contrib.auth import views as auth_views
+from django.utils.translation import ugettext as _
+from simpleauth.views import logout, account_settings
+
+urlpatterns = patterns('',
+    url(r'^signin/$',
+        auth_views.login,
+        {'template_name': 'simpleauth/signin.html',},
+        name='user_signin'),
+    url(r'^signout/$', logout, name='user_signout'),
+    url(r'^$', account_settings, name='user_account_settings'),
+)

simpleauth/util.py

+from urllib import unquote
+from django.conf import settings
+from django.http import HttpResponseRedirect, str_to_unicode
+
+DEFAULT_NEXT = getattr(settings, 'DEFAULT_REDIRECT_NEXT', '/')
+def clean_next(next):
+    """
+    Do necessary init and clean to the 'next' variable.
+
+    (Credits to django_authopenid.util)
+    """
+    if next is None:
+        return DEFAULT_NEXT
+    next = str_to_unicode(unquote(next), 'utf-8')
+    next = next.strip()
+    if next.startswith('/'):
+        return next
+    return DEFAULT_NEXT

simpleauth/views.py

+from django.http import HttpResponseRedirect
+from django.shortcuts import render_to_response
+from django.template import RequestContext
+from django.contrib.auth.decorators import login_required
+from django.contrib.auth.views import logout as auth_logout
+from simpleauth.util import clean_next
+
+@login_required
+def logout(request, template_name='simpleauth/logged_out.html'):
+    """Logout the user from the website and redirect back."""
+    next = clean_next(request.GET.get('next'))
+    auth_logout(request, next_page=next, template_name=template_name)
+    return HttpResponseRedirect(next)
+
+@login_required
+def account_settings(request, template_name='simpleauth/settings.html'):
+    """Account settings page."""
+    msg = request.GET.get('msg', '')
+    return render_to_response(template_name,
+                  {'msg': msg,},
+                  context_instance=RequestContext(request))

templates/authopenid/login_box.html

+{% load i18n %}
+<div id="login_index">
+   <div class="divright">
+   <fieldset>
+        <legend>{% trans "Sign in" %}</legend>
+        <div id="login-block">
+		      <p class="login-tagline">Add projects or modify existing ones. Wiki-style.</p>
+		      <form name="fauth" action="{% url user_signin %}" method="post">
+		      {{ form1.next }}
+		              <div class="form-row"><label for="id_username">{% trans "Username:" %}</label> <input id="id_username" class="required login" name="username" maxlength="30" size="12" type="text"></div>
+		              <div class="form-row"><label for="id_password">{% trans "Password:" %}</label> <input id="id_password" class="required login" name="password" maxlength="128" size="12" type="password"></div>
+		      <div class="submit-row"><input type="submit" name="blogin" value="{% trans "Sign in" %}"> </div>
+		      </form>
+		      <p>{% trans "Or, with your OpenID:" %}</p> 
+		      <form name="fopenid" action="{% url user_signin %}" method="post">
+		      <div class="form-row">
+		         <input id="id_openid_url" class="required openid withbgimage" name="openid_url" maxlength="255" type="text">
+		         <input name="bonus" type="text" class="hidden_field">
+		      </div>
+		      <div class="submit-row"><input type="submit" name="bsignin" value="{% trans "Sign in with OpenID" %}"></div>
+		      </form> 
+		      <div class="lost_passwd compact">&nbsp;&nbsp;<a href="{% url user_sendpw %}">{% trans "Lost your password?" %}</a></div>
+        </div>
+        </fieldset>     
+</div>
+</div>

templates/base.html

           {% if user.is_authenticated %}
               {% url user_account_settings as user_profile %} 
               {% blocktrans with user.username as user_username %}Welcome <a title="Profile settings" href="{{ user_profile }}">{{ user_username }}</a>!{% endblocktrans %} |
-              <a href="{% url user_signout %}">{% trans "Sign out" %}</a></span>
+              <a href="{% url user_signout %}?next={{request.path_info}}">{% trans "Sign out" %}</a></span>
           {% else %}
-              <a href="{% url user_signin %}?next={{request.path_info}}">{% trans "Sign in" %}</a> | <a href="{% url user_signup %}">{% trans "Register" %}</a></span>
+              <a href="{% url user_signin %}?next={{request.path_info}}">{% trans "Sign in" %}</a>
+  {% url user_changeopenid as user_changeopenid %}
+  {% if user_changeopenid %}{# hack to check if openid is enabled #} | <a href="{% url user_signup %}">{% trans "Register" %}</a>{% endif %}
+              </span>
           {% endif %}
       <select class="lang_switcher">
         <option selected>{% trans "English" %}</option>

templates/index.html

 {% block body %}
 
 {% if not request.user.is_authenticated %}
-    {% include "login.html" %}
+<div id="login_index">
+  <div class="divright">
+  {% url user_changeopenid as user_changeopenid %}
+  {% if user_changeopenid %}{# hack to check if openid is enabled #}
+    {% include "authopenid/login_box.html" %}
+  {% else %}
+    {% include "simpleauth/login_box.html" %}
+  {% endif %}
+  </div>
+</div>
 {% endif %}
 <div id="intro">
   <p class="promo">{% trans "Transifex is a service aimed at helping translators keep track of their favourite translation projects." %}</p>

templates/login.html

-{% load i18n %}
-<div id="login_index">
-   <div class="divright">
-   <fieldset>
-        <legend>{% trans "Sign in" %}</legend>
-        <div id="login-block">
-		      <p class="login-tagline">Add projects or modify existing ones. Wiki-style.</p>
-		      <form name="fauth" action="{% url user_signin %}" method="post">
-		      {{ form1.next }}
-		              <div class="form-row"><label for="id_username">{% trans "Username:" %}</label> <input id="id_username" class="required login" name="username" maxlength="30" size="12" type="text"></div>
-		              <div class="form-row"><label for="id_password">{% trans "Password:" %}</label> <input id="id_password" class="required login" name="password" maxlength="128" size="12" type="password"></div>
-		      <div class="submit-row"><input type="submit" name="blogin" value="{% trans "Sign in" %}"> </div>
-		      </form>
-		      <p>{% trans "Or, with your OpenID:" %}</p> 
-		      <form name="fopenid" action="{% url user_signin %}" method="post">
-		      <div class="form-row">
-		         <input id="id_openid_url" class="required openid withbgimage" name="openid_url" maxlength="255" type="text">
-		         <input name="bonus" type="text" class="hidden_field">
-		      </div>
-		      <div class="submit-row"><input type="submit" name="bsignin" value="{% trans "Sign in with OpenID" %}"></div>
-		      </form> 
-		      <div class="lost_passwd compact">&nbsp;&nbsp;<a href="{{ sendpw_url }}">{% trans "Lost your password?" %}</a></div>
-        </div>
-        </fieldset>     
-</div>  
-</div>

templates/registration/activate.html

-{% extends "base.html" %}
-
-{% block title %}{{ block.super }} | {% trans "Registration confirmation sent" %}{% endblock %}
-
-{% block content_title %}
-  <h2 class="pagetitle">{% trans "Check your email!" %}</h2>
-{% endblock %}
-
-{% block body %} 
-
-<p>{% blocktrans %}Hello {{ account }}!{% endblock %}</p>
-
-<p>{% trans "An email has been sent to you to confirm the account creation." %}</p>
-{% endblock %}

templates/registration/activation_email.txt

-{% blocktrans %}Please click here to activate your account: {{ site }}/accounts/activate/{{ activation_key }}/{% endblock %}

templates/registration/activation_email_subject.txt

-{% blocktrans %}Account Activation - {{ site }}{% endblock %}

templates/registration/login.html

-{% extends "base.html" %}
-
-{% block title %}{{ block.super }} | {% trans "Login" %} {% endblock %}
-
-{% block content_title %}
-  <h2 class="pagetitle">{% trans "Login" %}</h2>
-{% endblock %}
-
-{% block body %} 
-<form method="post" action="">
-<table>
-{% for field in form %}
-  <tr>
-    <th>{{ field.label_tag }}</th>
-    <td>{{ field }}</td>
-    {% if field.help_text %}<td>{{ field.help_text }}</td>{% endif %}
-    {% if field.errors %}<td class="myerrors">{{ field.errors }}</td>{% endif %}
-  </tr>
-{% endfor %}
-</table>
-<input type="submit" />
-</form>
- 
-{% endblock %}

templates/registration/registration_complete.html

-{% extends "base.html" %}
-
-{% block title %}{{ block.super }} | {% trans "Registration complete" %}{% endblock %}
-
-{% block content_title %}
-  <h2 class="pagetitle">{% trans "Registration complete!" %}</h2>
-{% endblock %}
-
-{% block body %} 
-<p>Your account is now activated.</p>
-{% endblock %}

templates/registration/registration_form.html

-{% extends "base.html" %}
-
-{% block title %}{{ block.super }} | {% trans "Register" %}{% endblock %}
-
-{% block content_title %}
-  <h2 class="pagetitle">{% trans "Register for an account" %}</h2>
-{% endblock %}
-
-{% block body %} 
-<form method="post" action="">
-<table>
-{% for field in form %}
-  <tr>
-    <th>{{ field.label_tag }}</th>
-    <td>{{ field }}</td>
-    {% if field.help_text %}<td>{{ field.help_text }}</td>{% endif %}
-    {% if field.errors %}<td class="myerrors">{{ field.errors }}</td>{% endif %}
-  </tr>
-{% endfor %}
-</table>
-<input type="submit" />
-</form>
- 
-{% endblock %}

templates/simpleauth/base.html

+{% extends "base.html" %}
+{% load i18n %}
+
+{% block body_class %}accounts{% endblock %}
+
+{% block breadcrumb %}{{ block.super }}{% endblock %}

templates/simpleauth/base_profile.html

+{% extends "simpleauth/base.html" %}
+{% load i18n %}
+
+{% block title %}{{ block.super }} | {% trans "Profile" %}{% endblock %}
+
+{% url user_account_settings as user_profile %}
+{% block breadcrumb %}{{ block.super }} &raquo; <a href="{{ user_profile }}">{% trans "Profile" %}</a>{% endblock %}
+
+{% block content_title %}
+  <h2 class="pagetitle">{% trans "Profile settings" %}</h2>
+{% endblock %}

templates/simpleauth/login_box.html

+{% load i18n %}
+   <fieldset>
+        <legend>{% trans "Sign in" %}</legend>
+        <div id="login-block">
+		      <p class="login-tagline">Add projects or modify existing ones. Wiki-style.</p>
+		      <form name="fauth" action="{% url user_signin %}?next={{request.path_info}}" method="post">
+		      {{ form1.next }}
+		              <div class="form-row"><label for="id_username">{% trans "Username:" %}</label> <input id="id_username" class="required login" name="username" maxlength="30" size="12" type="text"></div>
+		              <div class="form-row"><label for="id_password">{% trans "Password:" %}</label> <input id="id_password" class="required login" name="password" maxlength="128" size="12" type="password"></div>
+		      <div class="submit-row"><input type="submit" name="blogin" value="{% trans "Sign in" %}"> </div>
+		      </form>
+        </div>
+        </fieldset>

templates/simpleauth/logout.html

+{% extends "base.html" %}
+{% load i18n %}
+
+{% block title %}{{ block.super }} | {% trans "Sign in" %}{% endblock %}
+{% block breadcrumb %}{{ block.super }} &raquo; {% trans "Login" %}{% endblock %}
+{% block content_title %}<h2 class="pagetitle">{% trans "Sign in" %}</h2>{% endblock %}
+
+{% block body %}
+{% blocktrans %}<p>You have been logged out.{% endblocktrans %}
+{% endblock %}

templates/simpleauth/settings.html

+{% extends "simpleauth/base.html" %}
+{% load i18n %}
+
+{% block title %}{{ block.super }} | {% trans "Your Profile" %}{% endblock %}
+{% block breadcrumb %}{{ block.super }} &raquo; {% trans "Profile" %}{% endblock %}
+
+{% with user_username|capfirst as user %}
+{% block content_title %}
+  <h2 class="pagetitle">{% blocktrans %}Your profile, {{ user }}{% endblocktrans %}</h2>
+{% endblock %}
+{% endwith %}
+
+{% block body %}
+<div id="settings-intro">
+    <p>{% trans "Any settings associated with your account are presented and possibly modified here." %}
+</div>
+
+<div id="settings-options">
+	{% if msg %}<p class="error">{{ msg }}</p>{% endif %}
+	<dl>
+        {% url notification.views.notices as notices_url %}
+        {% if notices_url %}
+        <dt>» <a href="{{notices_url}}">{% trans "Change notifications" %}</a></dt>
+        <dd>{% trans "Change notifications associated to your account" %}</dd>
+        {% endif %}
+	</dl>
+</div>
+{% endblock %}

templates/simpleauth/signin.html

+{% extends "base.html" %}
+{% load i18n %}
+
+{% block title %}{{ block.super }} | {% trans "Sign in" %}{% endblock %}
+{% block breadcrumb %}{{ block.super }} &raquo; {% trans "Login" %}{% endblock %}
+{% block content_title %}<h2 class="pagetitle">{% trans "Sign in" %}</h2>{% endblock %}
+
+{% block body %}
+
+{% blocktrans %}
+<p>Please login in with your email or screen name.<br />
+{% endblocktrans %}
+<br />
+{% if msg %}
+<br />
+	<p class="warning">{{ msg }}</p>
+{% endif %}
+{% if form.errors %}
+<p class="errors">{% trans "Please correct errors below:" %}<br />
+	{% if form.username.errors %} 
+		<span class="error">{{ form.username.errors|join:", " }}</span>
+	{% endif %}
+	{% if form.password.errors %} 
+		<span class="error">{{ form.password.errors|join:", " }}</span>
+	{% endif %}
+</p>
+{% endif %}
+
+<div id="login_index">
+  <div class="divleft">
+      {% include "simpleauth/login_box.html" %}
+  </div>
+</div>
+<div class="clear"></div>
+{% endblock %}
+
     url(r'^admin/(.*)', admin.site.root),
     url(r'^contact/', include('contact_form.urls'), name='contact'),
     url(r'^languages/', include('languages.urls')),
-    url(r'^account/', include('django_authopenid.urls')),
 )
 
+# Choose auth app depending on whether authopenid was enabled or not
+if 'django_authopenid' in settings.INSTALLED_APPS:
+    urlpatterns += patterns('',
+        url(r'^account/', include('django_authopenid.urls')),)
+else:
+    urlpatterns += patterns('',
+        url(r'^account/', include('simpleauth.urls')),)
+
 if settings.STATIC_SERVE:
     urlpatterns += patterns('',
         (r'^site_media/(?P<path>.*)$', 'django.views.static.serve',