Commit 787fd2f4 authored by Skia's avatar Skia
Browse files

Settingize some stuff and add custom AnonymousUser

parent a6edfcc0
import os
from import BaseCommand, CommandError
from import call_command
from django.conf import settings
from core.models import Group, User
class Command(BaseCommand):
......@@ -21,11 +22,8 @@ class Command(BaseCommand):
is_superuser=True, is_staff=True)
# Just some example groups, only root is truly mandatory
for g in settings.AE_GROUPS.values():
Group(id=g['id'], name=g['name']).save()
if not options['prod']:
print("Dev mode, adding some test data")
u = User(username='skia', last_name="Kia", first_name="S'",
......@@ -34,4 +32,10 @@ class Command(BaseCommand):
is_superuser=True, is_staff=True)
u = User(username='guy', last_name="Carlier", first_name="Guy",
is_superuser=False, is_staff=False)
import importlib
from django.conf import settings
from django.utils.functional import SimpleLazyObject
from django.contrib.auth import get_user
from django.contrib.auth.middleware import AuthenticationMiddleware as DjangoAuthenticationMiddleware
module, klass = settings.AUTH_ANONYMOUS_MODEL.rsplit('.', 1)
AnonymousUser = getattr(importlib.import_module(module), klass)
def get_cached_user(request):
if not hasattr(request, '_cached_user'):
user = get_user(request)
if user.is_anonymous():
user = AnonymousUser(request)
request._cached_user = user
return request._cached_user
class AuthenticationMiddleware(DjangoAuthenticationMiddleware):
def process_request(self, request):
assert hasattr(request, 'session'), (
"The Django authentication middleware requires session middleware "
"to be installed. Edit your MIDDLEWARE_CLASSES setting to insert "
"'django.contrib.sessions.middleware.SessionMiddleware' before "
request.user = SimpleLazyObject(lambda: get_cached_user(request))
from django.db import models
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, UserManager, Group as AuthGroup
from django.contrib.auth.models import AbstractBaseUser, PermissionsMixin, UserManager, Group as AuthGroup, AnonymousUser as AuthAnonymousUser
from django.utils.translation import ugettext_lazy as _
from django.utils import timezone
from django.core import validators
from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
from django.conf import settings
from datetime import datetime, timedelta
class Group(AuthGroup):
......@@ -60,14 +61,15 @@ class User(AbstractBaseUser, PermissionsMixin):
date_joined = models.DateTimeField(_('date joined'),
owner_group = models.ForeignKey(Group, related_name="owned_user", default=1)
owner_group = models.ForeignKey(Group, related_name="owned_user",
edit_group = models.ManyToManyField(Group, related_name="editable_user", blank=True)
view_group = models.ManyToManyField(Group, related_name="viewable_user", blank=True)
objects = UserManager()
USERNAME_FIELD = 'username'
REQUIRED_FIELDS = ['email', 'first_name', 'last_name', 'date_of_birth']
REQUIRED_FIELDS = ['email', 'date_of_birth']
class Meta:
verbose_name = _('user')
......@@ -149,7 +151,8 @@ class User(AbstractBaseUser, PermissionsMixin):
if not hasattr(obj, "owner_group"):
return False
if (self.is_superuser or self.groups.filter( or
self.has_perm(obj.__class__.__module__.split('.')[0]+".change_prop_"+obj.__class__.__name__.lower()) or
return True
return False
......@@ -185,6 +188,21 @@ class User(AbstractBaseUser, PermissionsMixin):
return True
return False
class AnonymousUser(AuthAnonymousUser):
def __init__(self, request):
super(AnonymousUser, self).__init__()
def is_owner(self, obj):
return False
def can_edit(self, obj):
return False
def can_view(self, obj):
if obj.view_group.filter(pk=settings.AE_GROUPS['public']['id']).exists():
return True
return False
class LockError(Exception):
"""There was a lock error on the object"""
......@@ -213,7 +231,8 @@ class Page(models.Model):
# Attention: this field may not be valid until you call save(). It's made for fast query, but don't rely on it when
# playing with a Page object, use get_full_name() instead!
full_name = models.CharField(_('page name'), max_length=255, blank=True)
owner_group = models.ForeignKey(Group, related_name="owned_page", default=1)
owner_group = models.ForeignKey(Group, related_name="owned_page",
edit_group = models.ManyToManyField(Group, related_name="editable_page", blank=True)
view_group = models.ManyToManyField(Group, related_name="viewable_page", blank=True)
lock_mutex = {}
......@@ -49,6 +49,7 @@ MIDDLEWARE_CLASSES = (
ROOT_URLCONF = 'sith.urls'
......@@ -105,6 +106,7 @@ STATIC_URL = '/static/'
# Auth configuration
AUTH_USER_MODEL = 'core.User'
AUTH_ANONYMOUS_MODEL = 'core.models.AnonymousUser'
LOGIN_URL = '/login'
LOGOUT_URL = '/logout'
......@@ -113,3 +115,24 @@ DEFAULT_FROM_EMAIL=""
# Email
# AE configuration
'root': {
'id': 1,
'name': "root",
'board': {
'id': 2,
'name': "ae_bureau",
'members': {
'id': 3,
'name': "ae_membres",
'public': {
'id': 4,
'name': "not_registered_users",
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment