Refactor a bit right handling

This commit is contained in:
Skia 2016-03-22 17:46:26 +01:00
parent 18f856af24
commit c3fb581f97
5 changed files with 31 additions and 11 deletions

View File

@ -17,17 +17,26 @@ class Command(BaseCommand):
def handle(self, *args, **options): def handle(self, *args, **options):
root_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))) root_path = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
u = User(username='root', last_name="", first_name="Bibou", root = User(username='root', last_name="", first_name="Bibou",
email="ae.info@utbm.fr", email="ae.info@utbm.fr",
date_of_birth="1942-06-12", date_of_birth="1942-06-12",
is_superuser=True, is_staff=True) is_superuser=True, is_staff=True)
u.set_password("plop") root.set_password("plop")
u.save() root.save()
for g in settings.AE_GROUPS.values(): for g in settings.AE_GROUPS.values():
Group(id=g['id'], name=g['name']).save() Group(id=g['id'], name=g['name']).save()
ae = Club(name=settings.AE_MAIN_CLUB['name'], unix_name=settings.AE_MAIN_CLUB['unix_name'], ae = Club(name=settings.AE_MAIN_CLUB['name'], unix_name=settings.AE_MAIN_CLUB['unix_name'],
address=settings.AE_MAIN_CLUB['address']) address=settings.AE_MAIN_CLUB['address'])
ae.save() ae.save()
p = Page(name='Index')
p.set_lock(root)
p.save()
p.view_groups=[settings.AE_GROUPS['public']['id']]
p.set_lock(root)
p.save()
PageRev(page=p, title="Wiki index", author=root, content="""
Welcome to the wiki page!
""").save()
# Here we add a lot of test datas, that are not necessary for the Sith, but that provide a basic development environment # Here we add a lot of test datas, that are not necessary for the Sith, but that provide a basic development environment
if not options['prod']: if not options['prod']:
@ -53,14 +62,12 @@ class Command(BaseCommand):
r.save() r.save()
# Adding syntax help page # Adding syntax help page
p = Page(name='Aide_sur_la_syntaxe') p = Page(name='Aide_sur_la_syntaxe')
p.set_lock(s)
p.save() p.save()
PageRev(page=p, title="Aide sur la syntaxe", author=s, content=""" PageRev(page=p, title="Aide sur la syntaxe", author=s, content="""
Cette page vise à documenter la syntaxe *Markdown* utilisée sur le site. Cette page vise à documenter la syntaxe *Markdown* utilisée sur le site.
""").save() """).save()
# Adding README # Adding README
p = Page(name='README') p = Page(name='README')
p.set_lock(s)
p.save() p.save()
p.view_groups=[settings.AE_GROUPS['public']['id']] p.view_groups=[settings.AE_GROUPS['public']['id']]
p.set_lock(s) p.set_lock(s)

View File

@ -98,6 +98,18 @@ class User(AbstractBaseUser, PermissionsMixin):
def is_in_group(self, group_name): def is_in_group(self, group_name):
"""If the user is in the group passed in argument (as string)""" """If the user is in the group passed in argument (as string)"""
if group_name == settings.AE_GROUPS['public']['name']:
return True
if group_name == settings.AE_GROUPS['members']['name']: # We check the subscription if asked
try:
from subscription import Subscriber
s = Subscriber.objects.filter(pk=self.pk).first()
if s is not None and s.is_subscribed():
return True
else:
return False
except Exception as e:
print(e)
return self.groups.filter(name=group_name).exists() return self.groups.filter(name=group_name).exists()
def get_profile(self): def get_profile(self):
@ -159,7 +171,7 @@ class User(AbstractBaseUser, PermissionsMixin):
""" """
if not hasattr(obj, "owner_group"): if not hasattr(obj, "owner_group"):
return False return False
if (self.is_superuser or self.groups.filter(name=obj.owner_group.name).exists() or if (self.is_superuser or self.is_in_group(obj.owner_group.name) or
self.has_perm(obj.__class__.__module__.split('.')[0]+".change_prop_"+obj.__class__.__name__.lower()) or self.has_perm(obj.__class__.__module__.split('.')[0]+".change_prop_"+obj.__class__.__name__.lower()) or
self.groups.filter(id=settings.AE_GROUPS['root']['id']).exists()): self.groups.filter(id=settings.AE_GROUPS['root']['id']).exists()):
return True return True
@ -175,7 +187,7 @@ class User(AbstractBaseUser, PermissionsMixin):
return True return True
if hasattr(obj, "edit_groups"): if hasattr(obj, "edit_groups"):
for g in obj.edit_groups.all(): for g in obj.edit_groups.all():
if self.groups.filter(name=g.name).exists(): if self.is_in_group(g.name):
return True return True
if isinstance(obj, User) and obj == self: if isinstance(obj, User) and obj == self:
return True return True
@ -193,7 +205,7 @@ class User(AbstractBaseUser, PermissionsMixin):
return True return True
if hasattr(obj, "view_groups"): if hasattr(obj, "view_groups"):
for g in obj.view_groups.all(): for g in obj.view_groups.all():
if self.groups.filter(name=g.name).exists(): if self.is_in_group(g.name):
return True return True
if hasattr(obj, "can_be_viewed_by") and obj.can_be_viewed_by(self): if hasattr(obj, "can_be_viewed_by") and obj.can_be_viewed_by(self):
return True return True

View File

@ -25,7 +25,7 @@
<ul> <ul>
<li><a href="{{ url('core:user_profile', user_id=user.id) }}">Profile</a></li> <li><a href="{{ url('core:user_profile', user_id=user.id) }}">Profile</a></li>
<li><a href="{{ url('core:user_list') }}">Users</a></li> <li><a href="{{ url('core:user_list') }}">Users</a></li>
<li><a href="{{ url('core:page_list') }}">Pages</a></li> <li><a href="{{ url('core:page', page_name="Index") }}">Pages</a></li>
<li><a href="{{ url('club:club_list') }}">Clubs</a></li> <li><a href="{{ url('club:club_list') }}">Clubs</a></li>
</ul> </ul>
{% endif %} {% endif %}

View File

@ -12,10 +12,10 @@
{% if request.user.id == profile.id %} {% if request.user.id == profile.id %}
<li><a href="{{ url('core:user_tools') }}">Tools</a></li> <li><a href="{{ url('core:user_tools') }}">Tools</a></li>
{% endif %} {% endif %}
{% if user.has_perms('core.change_user') or user.id == profile.id %} {% if can_edit(profile, request.user) or user.id == profile.id %}
<li><a href="{{ url('core:user_edit', user_id=profile.id) }}">Edit</a></li> <li><a href="{{ url('core:user_edit', user_id=profile.id) }}">Edit</a></li>
{% endif %} {% endif %}
{% if user.has_perms('core.change_prop_user') %} {% if can_edit_prop(profile, request.user) %}
<li><a href="{{ url('core:user_prop', user_id=profile.id) }}">Props</a></li> <li><a href="{{ url('core:user_prop', user_id=profile.id) }}">Props</a></li>
{% endif %} {% endif %}
</ul> </ul>

View File

@ -72,6 +72,7 @@ class CanViewMixin(View):
""" """
def dispatch(self, request, *arg, **kwargs): def dispatch(self, request, *arg, **kwargs):
res = super(CanViewMixin, self).dispatch(request, *arg, **kwargs) res = super(CanViewMixin, self).dispatch(request, *arg, **kwargs)
print("GUYGUYGUYGUYGUY")
if hasattr(self, 'object'): if hasattr(self, 'object'):
obj = self.object obj = self.object
elif hasattr(self, 'object_list'): elif hasattr(self, 'object_list'):