Skip to content

Commit

Permalink
new release 0.5.4
Browse files Browse the repository at this point in the history
  • Loading branch information
areski committed Feb 6, 2014
1 parent c99def3 commit b7e15d6
Show file tree
Hide file tree
Showing 3 changed files with 172 additions and 0 deletions.
79 changes: 79 additions & 0 deletions admin_tools_stats/app_label_renamer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (C) 2011-2014 Star2Billing S.L.
#
# The Initial Developer of the Original Code is
# Arezqui Belaid <[email protected]>
#
from django.contrib import admin
from django.db.models.base import ModelBase
from django.core.urlresolvers import resolve


#TODO: Follow evolution of https://code.djangoproject.com/ticket/3591

# Source link : http://django-notes.blogspot.in/2011/07/django-app-name-breadcrumbs-l10n.html
class AppLabelRenamer(object):
"""
Rename app label and app breadcrumbs in admin
"""
def __init__(self, native_app_label, app_label):
self.native_app_label = native_app_label
self.app_label = app_label
self.module = '.'.join([native_app_label, 'models'])

class string_with_realoaded_title(str):
''' thanks to Ionel Maries Cristian for http://ionelmc.wordpress.com/2011/06/24/custom-app-names-in-the-django-admin/'''
def __new__(cls, value, title):
instance = str.__new__(cls, value)
instance._title = title
return instance

def title(self):
return self._title

__copy__ = lambda self: self
__deepcopy__ = lambda self, memodict: self

def rename_app_label(self, f):
app_label = self.app_label

def rename_breadcrumbs(f):
def wrap(self, *args, **kwargs):
extra_context = kwargs.get('extra_context', {})
extra_context['app_label'] = app_label
kwargs['extra_context'] = extra_context
return f(self, *args, **kwargs)
return wrap

def wrap(model_or_iterable, admin_class=None, **option):
if isinstance(model_or_iterable, ModelBase):
model_or_iterable = [model_or_iterable]
for model in model_or_iterable:
if model.__module__ != self.module:
continue
if admin_class is None:
admin_class = type(model.__name__ + 'Admin', (admin.ModelAdmin,), {})
admin_class.add_view = rename_breadcrumbs(admin_class.add_view)
admin_class.change_view = rename_breadcrumbs(admin_class.change_view)
admin_class.changelist_view = rename_breadcrumbs(admin_class.changelist_view)
model._meta.app_label = self.string_with_realoaded_title(self.native_app_label, self.app_label)
return f(model, admin_class, **option)
return wrap

def rename_app_index(self, f):
def wrap(request, app_label, extra_context=None):
requested_app_label = resolve(request.path).kwargs.get('app_label', '')
if requested_app_label and requested_app_label == self.native_app_label:
app_label = self.string_with_realoaded_title(self.native_app_label, self.app_label)
else:
app_label = requested_app_label
return f(request, app_label, extra_context=None)
return wrap

def main(self):
admin.site.register = self.rename_app_label(admin.site.register)
admin.site.app_index = self.rename_app_index(admin.site.app_index)
86 changes: 86 additions & 0 deletions admin_tools_stats/utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http://mozilla.org/MPL/2.0/.
#
# Copyright (C) 2011-2014 Star2Billing S.L.
#
# The Initial Developer of the Original Code is
# Arezqui Belaid <[email protected]>
#

from django.contrib.auth.models import User
from django.test import TestCase, Client
from django.test.client import RequestFactory
import base64
import unittest
import inspect


def build_test_suite_from(test_cases):
"""Returns a single or group of unittest test suite(s) that's ready to be
run. The function expects a list of classes that are subclasses of
TestCase.
The function will search the module where each class resides and
build a test suite from that class and all subclasses of it.
"""
test_suites = []
for test_case in test_cases:
mod = __import__(test_case.__module__)
components = test_case.__module__.split('.')
for comp in components[1:]:
mod = getattr(mod, comp)
tests = []
for item in mod.__dict__.values():
if type(item) is type and issubclass(item, test_case):
tests.append(item)
test_suites.append(unittest.TestSuite(
map(unittest.TestLoader().loadTestsFromTestCase, tests)))

return unittest.TestSuite(test_suites)


class BaseAuthenticatedClient(TestCase):
"""Common Authentication"""
fixtures = ['auth_user.json']

def setUp(self):
"""To create admin user"""
self.client = Client()
self.user = User.objects.get(username='admin')
auth = '%s:%s' % ('admin', 'admin')
auth = 'Basic %s' % base64.encodestring(auth)
auth = auth.strip()
self.extra = {
'HTTP_AUTHORIZATION': auth,
}
login = self.client.login(username='admin', password='admin')
self.assertTrue(login)
self.factory = RequestFactory()


class Choice(object):

class __metaclass__(type):
def __init__(self, *args, **kwargs):
self._data = []
for name, value in inspect.getmembers(self):
if not name.startswith('_') and not inspect.ismethod(value):
if isinstance(value, tuple) and len(value) > 1:
data = value
else:
pieces = [x.capitalize() for x in name.split('_')]
data = (value, ' '.join(pieces))
self._data.append(data)
setattr(self, name, data[0])

self._hash = dict(self._data)

def __iter__(self):
for value, data in self._data:
yield value, data

@classmethod
def get_value(self, key):
return self._hash[key]
7 changes: 7 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
python-dateutil>=2.0
django-jsonfield>=0.9.2
django-qsstats-magic>=0.6.1
python-memcached>=1.47
django-cache-utils>=0.7.2
django-admin-tools>=0.5.1
django-nvd3>=0.5.0

0 comments on commit b7e15d6

Please sign in to comment.