Skip to content

Commit

Permalink
Merge pull request #48 from matklad/custom_user_support
Browse files Browse the repository at this point in the history
Custom user support
  • Loading branch information
treyhunner committed May 18, 2013
2 parents 482ccb9 + 0b3175e commit cda8882
Show file tree
Hide file tree
Showing 7 changed files with 62 additions and 15 deletions.
27 changes: 18 additions & 9 deletions runtests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from shutil import rmtree
from os.path import abspath, dirname, join

import django
from django.conf import settings


Expand All @@ -12,24 +13,32 @@
if not settings.configured:
media_root = join(abspath(dirname(__file__)), 'test_files')
rmtree(media_root, ignore_errors=True)

installed_apps = (
'django.contrib.contenttypes',
'django.contrib.auth',
'django.contrib.sessions',
'django.contrib.admin',
'simple_history',
'simple_history.tests',
'simple_history.tests.external',
)
auth_user_model = 'auth.User'
if django.VERSION >= (1, 5):
installed_apps += ('simple_history.tests.custom_user', )
auth_user_model = 'custom_user.CustomUser'

settings.configure(
ROOT_URLCONF='simple_history.tests.urls',
MEDIA_ROOT=media_root,
STATIC_URL='/static/',
INSTALLED_APPS=(
'django.contrib.contenttypes',
'django.contrib.auth',
'django.contrib.sessions',
'django.contrib.admin',
'simple_history',
'simple_history.tests',
'simple_history.tests.external'
),
INSTALLED_APPS=installed_apps,
DATABASES={
'default': {
'ENGINE': 'django.db.backends.sqlite3',
}
},
AUTH_USER_MODEL=auth_user_model
)


Expand Down
11 changes: 10 additions & 1 deletion simple_history/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@
except ImportError: # django 1.3 compatibility
from django.utils.encoding import force_unicode as force_text

try:
from django.contrib.auth import get_user_model
User = get_user_model()
except ImportError: # django 1.4 compatibility
from django.contrib.auth.models import User


class SimpleHistoryAdmin(admin.ModelAdmin):
object_history_template = "simple_history/object_history.html"
Expand Down Expand Up @@ -47,14 +53,17 @@ def history_view(self, request, object_id, extra_context=None):
action_list = history.filter(**{pk_name: object_id})
# If no history was found, see whether this object even exists.
obj = get_object_or_404(model, pk=unquote(object_id))
content_type = ContentType.objects.get_for_model(User)
admin_user_view = 'admin:%s_%s_change' % (content_type.app_label, content_type.model)
context = {
'title': _('Change history: %s') % force_text(obj),
'action_list': action_list,
'module_name': capfirst(force_text(opts.verbose_name_plural)),
'object': obj,
'root_path': getattr(self.admin_site, 'root_path', None),
'app_label': app_label,
'opts': opts
'opts': opts,
'admin_user_view': admin_user_view
}
context.update(extra_context or {})
context_instance = template.RequestContext(request, current_app=self.admin_site.name)
Expand Down
8 changes: 7 additions & 1 deletion simple_history/templates/simple_history/object_history.html
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@
<td><a href="{{ action.revert_url }}">{{ action.history_object }}</a></td>
<td>{{ action.history_date }}</td>
<td>{{ action.get_history_type_display }}</td>
<td><a href="{% url "admin:auth_user_change" action.history_user_id %}">{{ action.history_user }}</a></td>
<td>
{% if action.history_user %}
<a href="{% url admin_user_view action.history_user_id %}">{{ action.history_user }}</a>
{% else %}
None
{% endif %}
</td>
</tr>
{% endfor %}
</tbody>
Expand Down
Empty file.
11 changes: 11 additions & 0 deletions simple_history/tests/custom_user/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
from __future__ import unicode_literals
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin

from django.contrib.auth.models import AbstractUser


class CustomUser(AbstractUser):
pass

admin.site.register(CustomUser, UserAdmin)
7 changes: 6 additions & 1 deletion simple_history/tests/models.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
from __future__ import unicode_literals

from django.db import models
from django.contrib.auth.models import User
try:
from django.contrib.auth import get_user_model
User = get_user_model()
except ImportError: # django 1.4 compatibility
from django.contrib.auth.models import User

from simple_history.models import HistoricalRecords
from simple_history import register

Expand Down
13 changes: 10 additions & 3 deletions simple_history/tests/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@
from django_webtest import WebTest
from django.core.files.base import ContentFile
from django.core.urlresolvers import reverse
from django.contrib.auth.models import User
try:
from django.contrib.auth import get_user_model
User = get_user_model()
except ImportError: # django 1.4 compatibility
from django.contrib.auth.models import User

from .models import Poll, Choice, Restaurant, Person, FileModel, Document
from .models import ExternalModel1, ExternalModel3
Expand Down Expand Up @@ -315,7 +319,7 @@ def get_history_url(model, history_index=None):

class AdminSiteTest(WebTest):
def setUp(self):
self.user = User.objects.create_superuser('u', 'u@example.com', 'pass')
self.user = User.objects.create_superuser('user_login', 'u@example.com', 'pass')

def login(self, user=None):
if user is None:
Expand All @@ -327,11 +331,14 @@ def login(self, user=None):

def test_history_list(self):
self.login()
poll = Poll.objects.create(question="why?", pub_date=today)
poll = Poll(question="why?", pub_date=today)
poll._history_user = self.user
poll.save()
response = self.app.get(get_history_url(poll))
self.assertIn(get_history_url(poll, 0), response.unicode_normal_body)
self.assertIn("Poll object", response.unicode_normal_body)
self.assertIn("Created", response.unicode_normal_body)
self.assertIn(self.user.username, response.unicode_normal_body)

def test_history_form_permission(self):
self.login(self.user)
Expand Down

0 comments on commit cda8882

Please sign in to comment.