Permalink
Browse files

added some smaller refactorings and other changes

git-svn-id: https://django-mobileadmin.googlecode.com/svn/trunk@32 4ed3a04f-053e-0410-b24d-b35526dee1fc
  • Loading branch information...
1 parent 9e84c7f commit cd9fc6d7f228b16f555a45024339f0be6fe597a2 leidel committed Sep 6, 2008
Showing with 104 additions and 13 deletions.
  1. +4 −0 .gitignore
  2. +5 −0 mobileadmin/conf/settings.py
  3. +40 −0 mobileadmin/media/js/base.js
  4. +54 −12 mobileadmin/middleware.py
  5. +1 −1 mobileadmin/templates/admin/base.html
View
@@ -0,0 +1,4 @@
+*.pyc
+.DS_Store
+dist/*
+MANIFEST
@@ -4,3 +4,8 @@
MEDIA_PATH = os.path.join(os.path.abspath(os.path.dirname(__file__)), '..', 'media')
MEDIA_PREFIX = getattr(settings, 'MOBILEADMIN_MEDIA_PREFIX', '/mobileadmin_media/')
MEDIA_REGEX = r'^%s(?P<path>.*)$' % MEDIA_PREFIX.lstrip('/')
+
+COOKIE_AGE = getattr(settings, 'MOBILEADMIN_COOKIE_AGE', settings.SESSION_COOKIE_AGE)
+
+# A string like ".lawrence.com", or None for standard domain cookie.
+COOKIE_DOMAIN = getattr(settings, 'MOBILEADMIN_COOKIE_DOMAIN', settings.SESSION_COOKIE_DOMAIN)
@@ -47,3 +47,43 @@ function tabSwitcher(tabLabels) {
$("_"+label).addEventListener("click", toggle, false);
}
}
+
+function getElementsByClassName(className, tag, elm){
+ var testClass = new RegExp("(^|\\s)" + className + "(\\s|$)");
+ var tag = tag || "*";
+ var elm = elm || document;
+ var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
+ var returnElements = [];
+ var current;
+ var length = elements.length;
+ for(var i=0; i<length; i++){
+ current = elements[i];
+ if(testClass.test(current.className)){
+ returnElements.push(current);
+ }
+ }
+ return returnElements;
+}
+
+function createCookie(value, age, path, secure) {
+ var cookie = "mobileadmin="+escape(value);
+ var date = new Date();
+ date.setTime(date.getTime()+(age*1000));
+ cookie += "; expires="+date.toGMTString();
+ cookie += "; path=/" + path;
+ if (secure)
+ cookie += "; secure";
+ document.cookie = cookie;
+}
+
+function toggle(age, path, secure) {
+ var toggle_link = $('mobileadmin_toggle');
+}
+
+var toggle_link = document.createElement('a');
+toggle_link.setAttribute('href','');
+var toggle_link_text = document.createTextNode("toggle")
+toggle_link.appendChild(toggle_link_text);
+var user_tools = getElementsByClassName('user-tools');
+alert(user_tools);
+user_tools[0].appendChild(toggle_link);
View
@@ -5,20 +5,44 @@
"""
-import os, re
-from django.core.urlresolvers import reverse, NoReverseMatch
+import os
+import re
+import time
from django.conf import settings
+from django.utils.http import cookie_date
from django.utils.cache import patch_vary_headers
+from django.core.urlresolvers import reverse, NoReverseMatch
+from django.template import Template, Context
+from django.utils.encoding import force_unicode
+from mobileadmin.conf.settings import COOKIE_AGE, COOKIE_DOMAIN
try:
from threading import local
except ImportError:
from django.utils._threading_local import local
_thread_locals = local()
-safari_regex = re.compile(r'AppleWebKit/.*Mobile/')
-blackbarry_regex = re.compile(r'^BlackBerry')
-opera_mini_regex = re.compile(r'[Oo]pera [Mm]ini')
+safari = re.compile(r'AppleWebKit/.*Mobile/')
+blackbarry = re.compile(r'^BlackBerry')
+opera_mini = re.compile(r'[Oo]pera [Mm]ini')
+
+TOGGLE_TEMPLATE = """\
+<script type="text/javascript" charset="utf-8">
+ $('toggle').addEventListener('click', function() {
+ toggle('on', {{ age }}, '{{ admin_url }}', {{ secure|yesno:"true,false" }});
+ }, false);
+</script>
+"""
+
+def is_valid_user_agent(user_agent):
+ """
+ Checks if the given user agent string matches one of the valid user agents.
+ """
+ valid_user_agents = (safari, blackbarry, opera_mini)
+ for regex in valid_user_agents:
+ if regex.search(user_agent) is not None:
+ return True
+ return False
def normalize_slashes(path):
"""
@@ -64,22 +88,40 @@ class MobileAdminMiddleware:
"""
def process_request(self, request):
user_agent = request.META.get('HTTP_USER_AGENT', '')
+ cookie = request.COOKIES.get('mobileadmin')
+ toggle = request.GET.get('mobileadmin')
+ use_mobileadmin = True
if 'django.contrib.admin' in settings.INSTALLED_APPS:
try:
- admin_url = normalize_slashes(reverse('django.contrib.admin.views.main.index'))
+ admin_url = normalize_slashes(
+ reverse('django.contrib.admin.views.main.index'))
except NoReverseMatch:
admin_url = None
- if admin_url is not None and \
- (safari_regex.search(user_agent) is not None or \
- opera_mini_regex.search(user_agent) is not None or \
- blackbarry_regex.search(user_agent) is not None ) and \
+ if use_mobileadmin and admin_url is not None and \
+ is_valid_user_agent(user_agent) and \
normalize_slashes(request.path).startswith(admin_url):
set_thread_var('use_mobile_templates', True)
else:
set_thread_var('use_mobile_templates', False)
return None
def process_response(self, request, response):
- if get_thread_var('use_mobile_templates') is not None:
- patch_vary_headers(response, ('User-Agent',))
+ try:
+ admin_url = normalize_slashes(
+ reverse('django.contrib.admin.views.main.index'))
+ except NoReverseMatch:
+ admin_url = None
+ if admin_url is None or \
+ 'text/html' not in response['Content-Type'] or \
+ request.is_ajax() or \
+ response.status_code != 200:
+ return response
+ content = Template(TOGGLE_TEMPLATE).render(Context({
+ 'age': COOKIE_AGE,
+ 'admin_url': admin_url,
+ 'secure': request.is_secure(),
+ 'path': request.get_full_path(),
+ }))
+ old_content = response.content
+ response.content = force_unicode(old_content).replace('</body>', content)
return response
@@ -15,7 +15,7 @@
<h1><a href="{% get_admin_root %}">Django</a></h1>
{% block tools %}
{% if user.is_authenticated and user.is_staff %}
- <div class="user-tools float-right">
+ <div class="user-tools">
<a href="{% get_admin_root %}logout/">{% trans "Logout" %}</a>
<a href="{% get_admin_root %}password_change/">{% trans 'Change password' %}</a>
</div>

0 comments on commit cd9fc6d

Please sign in to comment.