Code

Ticket #3011: #3011-extendable auth_user.diff

File #3011-extendable auth_user.diff, 3.5 KB (added by Kronuz, 3 years ago)
Line 
1Index: django/conf/global_settings.py
2===================================================================
3--- django/conf/global_settings.py      (revision 10115)
4+++ django/conf/global_settings.py      (working copy)
5@@ -379,6 +379,9 @@
6 # The number of days a password reset link is valid for
7 PASSWORD_RESET_TIMEOUT_DAYS = 3
8 
9+# The class to use as the default AUTH_USER for the authentication system.
10+AUTH_USER_MODULE = None
11+
12 ###########
13 # TESTING #
14 ###########
15Index: django/contrib/auth/models.py
16===================================================================
17--- django/contrib/auth/models.py       (revision 10115)
18+++ django/contrib/auth/models.py       (working copy)
19@@ -1,6 +1,7 @@
20 import datetime
21 import urllib
22 
23+from django.conf import settings
24 from django.contrib import auth
25 from django.core.exceptions import ImproperlyConfigured
26 from django.db import models
27@@ -198,7 +199,7 @@
28     return False
29 
30 
31-class User(models.Model):
32+class UserTemplate(models.Model):
33     """
34     Users within the Django authentication system are represented by this model.
35 
36@@ -222,6 +223,7 @@
37     class Meta:
38         verbose_name = _('user')
39         verbose_name_plural = _('users')
40+        abstract = True
41 
42     def __unicode__(self):
43         return self.username
44@@ -287,6 +289,33 @@
45                 raise SiteProfileNotAvailable
46         return self._profile_cache
47 
48+if settings.AUTH_USER_MODULE is not None:
49+    # Grab the AUTH_USER_MODULE path and classname from the settings.
50+    # auth_user_module_parts[0] = module path
51+    # auth_user_module_parts[1] = class name
52+    auth_user_module_parts = settings.AUTH_USER_MODULE.rsplit('.', 1)
53+    auth_user_module = __import__(auth_user_module_parts[0], {}, {}, [auth_user_module_parts[0]])
54+    # Store the auth_user model so it is accessible with the standard
55+    # 'from django.contrib.auth.models import User'
56+    User = getattr(auth_user_module, auth_user_module_parts[1])
57+
58+    # Add te User model to the django models cache
59+    # These two lines allow the custom auth_user model to play nicely with syncdb
60+    # and other systems that rely on functions like
61+    # django.db.models.loading.get_model(...)
62+    from django.db.models.loading import cache
63+    cache.register_models('auth', User)
64+
65+    # We need to remove whatever we used as the AUTH_USER_MODUlE from the
66+    # db cache so apps like contenttypes don't think that it's part
67+    # of contrib.auth
68+    del cache.app_models['auth'][User.__name__.lower()]
69+else:
70+    class User(UserTemplate):
71+        class Meta:
72+            verbose_name = _('user')
73+            verbose_name_plural = _('users')
74+
75 class Message(models.Model):
76     """
77     The message system is a lightweight way to queue messages for given
78Index: django/contrib/auth/models.py
79===================================================================
80--- django/contrib/admin/sites.py       (revision 10115)
81+++ django/contrib/admin/sites.py       (working copy)
82@@ -1,7 +1,6 @@
83 import re
84 from django import http, template
85 from django.contrib.admin import ModelAdmin, actions
86-from django.contrib.admin.forms import AdminAuthenticationForm
87 from django.contrib.auth import REDIRECT_FIELD_NAME
88 from django.contrib.contenttypes import views as contenttype_views
89 from django.views.decorators.csrf import csrf_protect
90@@ -315,6 +314,7 @@
91         Displays the login form for the given HttpRequest.
92         """
93         from django.contrib.auth.views import login
94+        from django.contrib.admin.forms import AdminAuthenticationForm
95         context = {
96             'title': _('Log in'),
97             'root_path': self.root_path,