Commits

benoitc  committed 8fea515

add context processor

  • Participants
  • Parent commits 6adb41c

Comments (0)

Files changed (4)

File django_authopenid/context_processors.py

+# -*- coding: utf-8 -*-
+# Copyright 2007, 2008,2009 by Benoît Chesneau <benoitc@e-engura.org>
+# 
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+
+
+def authopenid(request):
+    """
+    Returns context variables required by apps that use django-authopenid.
+    """
+    if hasattr(request, 'openid'):
+        openid = request.openid
+    else:
+        openid = None
+        
+    if hasattr(request, 'openids'):
+        openids = request.openids
+    else:
+        openids = []
+        
+    if hasattr(request, 'associated_openids'):
+        associated_openids = request.associated_openids
+    else:
+        associated_openids = []
+        
+    return {
+        "openid": openid,
+        "openids": openids,
+        "associated_openids": associated_openids,
+        "signin_with_openid": (openid is not None),
+        "has_openids": (len(associated_openids) > 0)
+    }

File django_authopenid/middleware.py

 from django.http import HttpResponseRedirect
 from django.core.urlresolvers import reverse
 
+from django_authopenid.models import UserAssociation
+
 __all__ = ["OpenIDMiddleware"]
 
 class OpenIDMiddleware(object):
     """
     def process_request(self, request):
         request.openid = request.session.get('openid', None)
+        request.openids = request.session.get('openids', [])
+        
+        rels = UserAssociation.objects.filter(user__id=request.user.id)
+        request.associated_openids = [rel.openid_url for rel in rels]
     
     def process_response(self, request, response):
         if response.status_code != 200 or len(response.content) < 200:

File example/settings.py

     'django.template.loaders.app_directories.load_template_source',
 )
 
+TEMPLATE_CONTEXT_PROCESSORS = (
+    'django.core.context_processors.auth',
+    'django.core.context_processors.debug',
+    'django.core.context_processors.i18n',
+    'django.core.context_processors.media',
+    'django_authopenid.context_processors.authopenid',
+)
+
 MIDDLEWARE_CLASSES = (
     'django.middleware.common.CommonMiddleware',
     'django.contrib.sessions.middleware.SessionMiddleware',

File example/templates/home.html

 <p>Welcome {{ user }}</p>
 
 {% if user.is_authenticated%}
+
+{% if has_openids %}
+<h2>Associated openids</h2>
+<ul>
+	{% for oid_url in associated_openids %}
+		<li>{{ oid_url }}</li>
+	{% endfor %}
+</ul>
+
+{% endif %}
+
+
+
+<h2>Actions:</h2>
 <ul>
 	<li><a href="{% url auth_password_change %}">Change password</a></li>
   	<li><a href="{% url user_signout %}">signout</a></li>