From 4d6ab8b3080924531f07ff35385b781fdbf36804 Mon Sep 17 00:00:00 2001 From: Joseph Kocherhans Date: Fri, 6 Jan 2006 23:42:07 +0000 Subject: [PATCH] magic-removal: removed django.models.auth and updated dependencies. django.models.auth is now django.contrib.auth.models. git-svn-id: http://code.djangoproject.com/svn/django/branches/magic-removal@1847 bcc190cf-cafb-0310-a4f2-bffc1f526a37 --- django/contrib/admin/models.py | 5 +- django/contrib/admin/views/decorators.py | 2 +- django/contrib/auth/handlers/modpython.py | 2 +- django/contrib/comments/models.py | 11 +- django/contrib/comments/views/comments.py | 2 +- django/core/handlers/modpython.py | 2 +- django/core/handlers/wsgi.py | 2 +- django/models/auth.py | 222 ---------------------- 8 files changed, 14 insertions(+), 234 deletions(-) delete mode 100644 django/models/auth.py diff --git a/django/contrib/admin/models.py b/django/contrib/admin/models.py index 3163ae5204..5e1fbc655c 100644 --- a/django/contrib/admin/models.py +++ b/django/contrib/admin/models.py @@ -1,5 +1,6 @@ from django.db import models -from django.models import auth, core +from django.models import core +from django.contrib.auth.models import User from django.utils.translation import gettext_lazy as _ ADDITION = 1 @@ -13,7 +14,7 @@ class LogEntryManager(models.Manager): class LogEntry(models.Model): action_time = models.DateTimeField(_('action time'), auto_now=True) - user = models.ForeignKey(auth.User) + user = models.ForeignKey(User) content_type = models.ForeignKey(core.ContentType, blank=True, null=True) object_id = models.TextField(_('object id'), blank=True, null=True) object_repr = models.CharField(_('object repr'), maxlength=200) diff --git a/django/contrib/admin/views/decorators.py b/django/contrib/admin/views/decorators.py index d2253cd430..f0541ffb24 100644 --- a/django/contrib/admin/views/decorators.py +++ b/django/contrib/admin/views/decorators.py @@ -1,6 +1,6 @@ from django.core.extensions import DjangoContext, render_to_response from django.conf.settings import SECRET_KEY -from django.models.auth import User, SESSION_KEY +from django.contrib.auth.models import User, SESSION_KEY from django.utils import httpwrappers from django.utils.translation import gettext_lazy import base64, md5 diff --git a/django/contrib/auth/handlers/modpython.py b/django/contrib/auth/handlers/modpython.py index 5e5aac4183..2694a7cfdc 100644 --- a/django/contrib/auth/handlers/modpython.py +++ b/django/contrib/auth/handlers/modpython.py @@ -10,7 +10,7 @@ def authenhandler(req, **kwargs): # that so that the following import works os.environ.update(req.subprocess_env) - from django.models.auth import User + from django.contrib.auth.models import User # check for PythonOptions _str_to_bool = lambda s: s.lower() in '1', 'true', 'on', 'yes' diff --git a/django/contrib/comments/models.py b/django/contrib/comments/models.py index b1034cc4ea..d526bc9d91 100644 --- a/django/contrib/comments/models.py +++ b/django/contrib/comments/models.py @@ -1,5 +1,6 @@ from django.db import models -from django.models import auth, core +from django.models import core +from django.contrib.auth.models import User from django.utils.translation import gettext_lazy as _ import datetime @@ -60,7 +61,7 @@ class CommentManager(models.Manager): return False class Comment(models.Model): - user = models.ForeignKey(auth.User, raw_id_admin=True) + user = models.ForeignKey(User, raw_id_admin=True) content_type = models.ForeignKey(core.ContentType) object_id = models.IntegerField(_('object ID')) headline = models.CharField(_('headline'), maxlength=255, blank=True) @@ -230,7 +231,7 @@ class KarmaScoreManager(models.Manager): return int(round((4.5 * score) + 5.5)) class KarmaScore(models.Model): - user = models.ForeignKey(auth.User) + user = models.ForeignKey(User) comment = models.ForeignKey(Comment) score = models.SmallIntegerField(_('score'), db_index=True) scored_date = models.DateTimeField(_('score date'), auto_now=True) @@ -262,7 +263,7 @@ class UserFlagManager(models.Manager): f.save() class UserFlag(models.Model): - user = models.ForeignKey(auth.User) + user = models.ForeignKey(User) comment = models.ForeignKey(Comment) flag_date = models.DateTimeField(_('flag date'), auto_now_add=True) objects = UserFlagManager() @@ -276,7 +277,7 @@ class UserFlag(models.Model): return _("Flag by %r") % self.get_user() class ModeratorDeletion(models.Model): - user = models.ForeignKey(auth.User, verbose_name='moderator') + user = models.ForeignKey(User, verbose_name='moderator') comment = models.ForeignKey(Comment) deletion_date = models.DateTimeField(_('deletion date'), auto_now_add=True) class Meta: diff --git a/django/contrib/comments/views/comments.py b/django/contrib/comments/views/comments.py index 589cf0fbb0..159362be6e 100644 --- a/django/contrib/comments/views/comments.py +++ b/django/contrib/comments/views/comments.py @@ -2,7 +2,7 @@ from django.core import formfields, validators from django.core.mail import mail_admins, mail_managers from django.core.exceptions import Http404, ObjectDoesNotExist from django.core.extensions import DjangoContext, render_to_response -from django.models.auth import SESSION_KEY +from django.contrib.auth.models import SESSION_KEY from django.contrib.comments.models import Comment, FreeComment, PHOTOS_REQUIRED, PHOTOS_OPTIONAL, RATINGS_REQUIRED, RATINGS_OPTIONAL, IS_PUBLIC from django.models.core import ContentType from django.parts.auth.formfields import AuthenticationForm diff --git a/django/core/handlers/modpython.py b/django/core/handlers/modpython.py index 62c6d73a2d..2d210c903d 100644 --- a/django/core/handlers/modpython.py +++ b/django/core/handlers/modpython.py @@ -99,7 +99,7 @@ class ModPythonRequest(httpwrappers.HttpRequest): def _get_user(self): if not hasattr(self, '_user'): - from django.models.auth import User, SESSION_KEY + from django.contrib.auth.models import User, SESSION_KEY try: user_id = self.session[SESSION_KEY] if not user_id: diff --git a/django/core/handlers/wsgi.py b/django/core/handlers/wsgi.py index 19eec253d6..fd79805d40 100644 --- a/django/core/handlers/wsgi.py +++ b/django/core/handlers/wsgi.py @@ -120,7 +120,7 @@ class WSGIRequest(httpwrappers.HttpRequest): def _get_user(self): if not hasattr(self, '_user'): - from django.models.auth import User, SESSION_KEY + from django.contrib.auth.models import User, SESSION_KEY try: user_id = self.session[SESSION_KEY] if not user_id: diff --git a/django/models/auth.py b/django/models/auth.py deleted file mode 100644 index 29149bce3f..0000000000 --- a/django/models/auth.py +++ /dev/null @@ -1,222 +0,0 @@ -from django.core import validators -from django.db import backend, connection, models -from django.models import core -from django.utils.translation import gettext_lazy as _ -import datetime - -SESSION_KEY = '_auth_user_id' - -class Permission(models.Model): - name = models.CharField(_('name'), maxlength=50) - package = models.ForeignKey(core.Package, db_column='package') - codename = models.CharField(_('codename'), maxlength=100) - class Meta: - verbose_name = _('Permission') - verbose_name_plural = _('Permissions') - unique_together = (('package', 'codename'),) - ordering = ('package', 'codename') - - def __repr__(self): - return "%s | %s" % (self.package_id, self.name) - -class Group(models.Model): - name = models.CharField(_('name'), maxlength=80, unique=True) - permissions = models.ManyToManyField(Permission, blank=True, filter_interface=models.HORIZONTAL) - class Meta: - verbose_name = _('Group') - verbose_name_plural = _('Groups') - ordering = ('name',) - admin = models.Admin( - search_fields = ('name',), - ) - - def __repr__(self): - return self.name - -class UserManager(models.Manager): - def create_user(self, username, email, password): - "Creates and saves a User with the given username, e-mail and password." - now = datetime.datetime.now() - user = self.klass(None, username, '', '', email.strip().lower(), 'placeholder', False, True, False, now, now) - user.set_password(password) - user.save() - return user - - def make_random_password(self, length=10, allowed_chars='abcdefghjkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ23456789'): - "Generates a random password with the given length and given allowed_chars" - # Note that default value of allowed_chars does not have "I" or letters - # that look like it -- just to avoid confusion. - from random import choice - return ''.join([choice(allowed_chars) for i in range(length)]) - -class User(models.Model): - username = models.CharField(_('username'), maxlength=30, unique=True, validator_list=[validators.isAlphaNumeric]) - first_name = models.CharField(_('first name'), maxlength=30, blank=True) - last_name = models.CharField(_('last name'), maxlength=30, blank=True) - email = models.EmailField(_('e-mail address'), blank=True) - password = models.CharField(_('password'), maxlength=128, help_text=_("Use '[algo]$[salt]$[hexdigest]'")) - is_staff = models.BooleanField(_('staff status'), help_text=_("Designates whether the user can log into this admin site.")) - is_active = models.BooleanField(_('active'), default=True) - is_superuser = models.BooleanField(_('superuser status')) - last_login = models.DateTimeField(_('last login'), default=models.LazyDate()) - date_joined = models.DateTimeField(_('date joined'), default=models.LazyDate()) - groups = models.ManyToManyField(Group, blank=True, - help_text=_("In addition to the permissions manually assigned, this user will also get all permissions granted to each group he/she is in.")) - user_permissions = models.ManyToManyField(Permission, blank=True, filter_interface=models.HORIZONTAL) - objects = UserManager() - class Meta: - verbose_name = _('User') - verbose_name_plural = _('Users') - ordering = ('username',) - exceptions = ('SiteProfileNotAvailable',) - admin = models.Admin( - fields = ( - (None, {'fields': ('username', 'password')}), - (_('Personal info'), {'fields': ('first_name', 'last_name', 'email')}), - (_('Permissions'), {'fields': ('is_staff', 'is_active', 'is_superuser', 'user_permissions')}), - (_('Important dates'), {'fields': ('last_login', 'date_joined')}), - (_('Groups'), {'fields': ('groups',)}), - ), - list_display = ('username', 'email', 'first_name', 'last_name', 'is_staff'), - list_filter = ('is_staff', 'is_superuser'), - search_fields = ('username', 'first_name', 'last_name', 'email'), - ) - - def __repr__(self): - return self.username - - def get_absolute_url(self): - return "/users/%s/" % self.username - - def is_anonymous(self): - return False - - def get_full_name(self): - full_name = '%s %s' % (self.first_name, self.last_name) - return full_name.strip() - - def set_password(self, raw_password): - import sha, random - algo = 'sha1' - salt = sha.new(str(random.random())).hexdigest()[:5] - hsh = sha.new(salt+raw_password).hexdigest() - self.password = '%s$%s$%s' % (algo, salt, hsh) - - def check_password(self, raw_password): - """ - Returns a boolean of whether the raw_password was correct. Handles - encryption formats behind the scenes. - """ - # Backwards-compatibility check. Older passwords won't include the - # algorithm or salt. - if '$' not in self.password: - import md5 - is_correct = (self.password == md5.new(raw_password).hexdigest()) - if is_correct: - # Convert the password to the new, more secure format. - self.set_password(raw_password) - self.save() - return is_correct - algo, salt, hsh = self.password.split('$') - if algo == 'md5': - import md5 - return hsh == md5.new(salt+raw_password).hexdigest() - elif algo == 'sha1': - import sha - return hsh == sha.new(salt+raw_password).hexdigest() - raise ValueError, "Got unknown password algorithm type in password." - - def get_group_permissions(self): - "Returns a list of permission strings that this user has through his/her groups." - if not hasattr(self, '_group_perm_cache'): - import sets - cursor = connection.cursor() - # The SQL below works out to the following, after DB quoting: - # cursor.execute(""" - # SELECT p.package, p.codename - # FROM auth_permissions p, auth_groups_permissions gp, auth_users_groups ug - # WHERE p.id = gp.permission_id - # AND gp.group_id = ug.group_id - # AND ug.user_id = %s""", [self.id]) - sql = """ - SELECT p.%s, p.%s - FROM %s p, %s gp, %s ug - WHERE p.%s = gp.%s - AND gp.%s = ug.%s - AND ug.%s = %%s""" % ( - backend.quote_name('package'), backend.quote_name('codename'), - backend.quote_name('auth_permissions'), backend.quote_name('auth_groups_permissions'), - backend.quote_name('auth_users_groups'), backend.quote_name('id'), - backend.quote_name('permission_id'), backend.quote_name('group_id'), - backend.quote_name('group_id'), backend.quote_name('user_id')) - cursor.execute(sql, [self.id]) - self._group_perm_cache = sets.Set(["%s.%s" % (row[0], row[1]) for row in cursor.fetchall()]) - return self._group_perm_cache - - def get_all_permissions(self): - if not hasattr(self, '_perm_cache'): - import sets - self._perm_cache = sets.Set(["%s.%s" % (p.package_id, p.codename) for p in self.get_permission_list()]) - self._perm_cache.update(self.get_group_permissions()) - return self._perm_cache - - def has_perm(self, perm): - "Returns True if the user has the specified permission." - if not self.is_active: - return False - if self.is_superuser: - return True - return perm in self.get_all_permissions() - - def has_perms(self, perm_list): - "Returns True if the user has each of the specified permissions." - for perm in perm_list: - if not self.has_perm(perm): - return False - return True - - def has_module_perms(self, package_name): - "Returns True if the user has any permissions in the given package." - if self.is_superuser: - return True - return bool(len([p for p in self.get_all_permissions() if p[:p.index('.')] == package_name])) - - def get_and_delete_messages(self): - messages = [] - for m in self.get_message_list(): - messages.append(m.message) - m.delete() - return messages - - def email_user(self, subject, message, from_email=None): - "Sends an e-mail to this User." - from django.core.mail import send_mail - send_mail(subject, message, from_email, [self.email]) - - def get_profile(self): - """ - Returns site-specific profile for this user. Raises - SiteProfileNotAvailable if this site does not allow profiles. - """ - if not hasattr(self, '_profile_cache'): - from django.conf.settings import AUTH_PROFILE_MODULE - if not AUTH_PROFILE_MODULE: - raise SiteProfileNotAvailable - try: - app, mod = AUTH_PROFILE_MODULE.split('.') - module = __import__('ellington.%s.apps.%s' % (app, mod), [], [], ['']) - self._profile_cache = module.get_object(user_id=self.id) - except ImportError: - try: - module = __import__('django.models.%s' % AUTH_PROFILE_MODULE, [], [], ['']) - self._profile_cache = module.get_object(user__id__exact=self.id) - except ImportError: - raise SiteProfileNotAvailable - return self._profile_cache - -class Message(models.Model): - user = models.ForeignKey(User) - message = models.TextField(_('Message')) - - def __repr__(self): - return self.message