2005-07-13 09:25:57 +08:00
|
|
|
from django.core import meta, validators
|
|
|
|
from django.models import core
|
|
|
|
|
|
|
|
class Permission(meta.Model):
|
|
|
|
fields = (
|
2005-08-03 01:56:22 +08:00
|
|
|
meta.CharField('name', maxlength=50),
|
2005-07-13 09:25:57 +08:00
|
|
|
meta.ForeignKey(core.Package, name='package'),
|
2005-08-03 01:56:22 +08:00
|
|
|
meta.CharField('codename', maxlength=100),
|
2005-07-13 09:25:57 +08:00
|
|
|
)
|
|
|
|
unique_together = (('package', 'codename'),)
|
2005-07-22 21:02:27 +08:00
|
|
|
ordering = ('package', 'codename')
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "%s | %s" % (self.package, self.name)
|
|
|
|
|
|
|
|
class Group(meta.Model):
|
|
|
|
fields = (
|
2005-08-03 01:56:22 +08:00
|
|
|
meta.CharField('name', maxlength=80, unique=True),
|
2005-07-13 09:25:57 +08:00
|
|
|
meta.ManyToManyField(Permission, blank=True, filter_interface=meta.HORIZONTAL),
|
|
|
|
)
|
2005-07-22 21:02:27 +08:00
|
|
|
ordering = ('name',)
|
2005-07-13 09:25:57 +08:00
|
|
|
admin = meta.Admin(
|
|
|
|
search_fields = ('name',),
|
|
|
|
)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.name
|
|
|
|
|
|
|
|
class User(meta.Model):
|
|
|
|
fields = (
|
2005-08-03 01:56:22 +08:00
|
|
|
meta.CharField('username', maxlength=30, unique=True,
|
2005-07-13 09:25:57 +08:00
|
|
|
validator_list=[validators.isAlphaNumeric]),
|
2005-08-03 01:56:22 +08:00
|
|
|
meta.CharField('first_name', maxlength=30, blank=True),
|
|
|
|
meta.CharField('last_name', maxlength=30, blank=True),
|
2005-07-13 09:25:57 +08:00
|
|
|
meta.EmailField('email', 'e-mail address', blank=True),
|
2005-07-17 07:34:07 +08:00
|
|
|
meta.CharField('password_md5', 'password', maxlength=32, help_text="Use an MD5 hash -- not the raw password."),
|
2005-07-13 09:25:57 +08:00
|
|
|
meta.BooleanField('is_staff', 'staff status',
|
|
|
|
help_text="Designates whether the user can log into this admin site."),
|
|
|
|
meta.BooleanField('is_active', 'active', default=True),
|
|
|
|
meta.BooleanField('is_superuser', 'superuser status'),
|
2005-08-03 01:56:22 +08:00
|
|
|
meta.DateTimeField('last_login', default=meta.LazyDate()),
|
|
|
|
meta.DateTimeField('date_joined', default=meta.LazyDate()),
|
2005-07-13 09:25:57 +08:00
|
|
|
meta.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."),
|
|
|
|
meta.ManyToManyField(Permission, name='user_permissions', blank=True, filter_interface=meta.HORIZONTAL),
|
|
|
|
)
|
2005-08-17 06:54:05 +08:00
|
|
|
module_constants = {
|
|
|
|
'SESSION_KEY': '_auth_user_id',
|
|
|
|
}
|
2005-07-22 21:02:27 +08:00
|
|
|
ordering = ('username',)
|
2005-07-13 09:25:57 +08:00
|
|
|
exceptions = ('SiteProfileNotAvailable',)
|
|
|
|
admin = meta.Admin(
|
|
|
|
fields = (
|
|
|
|
(None, {'fields': ('username', 'password_md5')}),
|
|
|
|
('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 md5
|
|
|
|
self.password_md5 = md5.new(raw_password).hexdigest()
|
|
|
|
|
|
|
|
def check_password(self, raw_password):
|
|
|
|
"Returns a boolean of whether the raw_password was correct."
|
|
|
|
import md5
|
|
|
|
return self.password_md5 == md5.new(raw_password).hexdigest()
|
|
|
|
|
|
|
|
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 = db.cursor()
|
|
|
|
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])
|
|
|
|
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
|
2005-07-14 11:17:09 +08:00
|
|
|
self._perm_cache = sets.Set(["%s.%s" % (p.package, p.codename) for p in self.get_permission_list()])
|
2005-07-13 09:25:57 +08:00
|
|
|
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
|
|
|
|
|
|
|
|
def _module_create_user(username, email, password):
|
|
|
|
"Creates and saves a User with the given username, e-mail and password."
|
|
|
|
import md5
|
|
|
|
password_md5 = md5.new(password).hexdigest()
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
user = User(None, username, '', '', email.strip().lower(), password_md5, False, True, False, now, now)
|
|
|
|
user.save()
|
|
|
|
return user
|
|
|
|
|
|
|
|
def _module_make_random_password(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.
|
2005-07-21 10:20:40 +08:00
|
|
|
from random import choice
|
2005-07-13 09:25:57 +08:00
|
|
|
return ''.join([choice(allowed_chars) for i in range(length)])
|
|
|
|
|
|
|
|
class Message(meta.Model):
|
|
|
|
fields = (
|
|
|
|
meta.ForeignKey(User),
|
2005-08-03 01:56:22 +08:00
|
|
|
meta.TextField('message'),
|
2005-07-13 09:25:57 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return self.message
|
|
|
|
|
|
|
|
class LogEntry(meta.Model):
|
|
|
|
module_name = 'log'
|
|
|
|
verbose_name_plural = 'log entries'
|
|
|
|
db_table = 'auth_admin_log'
|
|
|
|
fields = (
|
2005-08-03 01:56:22 +08:00
|
|
|
meta.DateTimeField('action_time', auto_now=True),
|
2005-07-13 09:25:57 +08:00
|
|
|
meta.ForeignKey(User),
|
|
|
|
meta.ForeignKey(core.ContentType, name='content_type_id', rel_name='content_type', blank=True, null=True),
|
2005-08-11 03:32:38 +08:00
|
|
|
meta.TextField('object_id', blank=True, null=True),
|
2005-08-03 01:56:22 +08:00
|
|
|
meta.CharField('object_repr', maxlength=200),
|
|
|
|
meta.PositiveSmallIntegerField('action_flag'),
|
|
|
|
meta.TextField('change_message', blank=True),
|
2005-07-13 09:25:57 +08:00
|
|
|
)
|
2005-07-22 21:02:27 +08:00
|
|
|
ordering = ('-action_time',)
|
2005-07-13 09:25:57 +08:00
|
|
|
module_constants = {
|
|
|
|
'ADDITION': 1,
|
|
|
|
'CHANGE': 2,
|
|
|
|
'DELETION': 3,
|
|
|
|
}
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return str(self.action_time)
|
|
|
|
|
|
|
|
def is_addition(self):
|
|
|
|
return self.action_flag == ADDITION
|
|
|
|
|
|
|
|
def is_change(self):
|
|
|
|
return self.action_flag == CHANGE
|
|
|
|
|
|
|
|
def is_deletion(self):
|
|
|
|
return self.action_flag == DELETION
|
|
|
|
|
|
|
|
def get_edited_object(self):
|
|
|
|
"Returns the edited object represented by this log entry"
|
2005-07-27 00:11:43 +08:00
|
|
|
return self.get_content_type().get_object_for_this_type(pk=self.object_id)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def get_admin_url(self):
|
2005-07-18 01:44:57 +08:00
|
|
|
"""
|
|
|
|
Returns the admin URL to edit the object represented by this log entry.
|
|
|
|
This is relative to the Django admin index page.
|
|
|
|
"""
|
|
|
|
return "%s/%s/%s/" % (self.get_content_type().package, self.get_content_type().python_module_name, self.object_id)
|
2005-07-13 09:25:57 +08:00
|
|
|
|
|
|
|
def _module_log_action(user_id, content_type_id, object_id, object_repr, action_flag, change_message=''):
|
|
|
|
e = LogEntry(None, None, user_id, content_type_id, object_id, object_repr[:200], action_flag, change_message)
|
|
|
|
e.save()
|