Commit 6aab5288 authored by Tom van Bussel's avatar Tom van Bussel
Browse files

Renamed Member to Profile and added User proxy

parent e9324074
...@@ -5,6 +5,8 @@ from django.contrib.auth.models import Permission ...@@ -5,6 +5,8 @@ from django.contrib.auth.models import Permission
from django.db.models import Q from django.db.models import Q
from django.utils import timezone from django.utils import timezone
from members.models import Member
class CommitteeBackend(object): class CommitteeBackend(object):
"""Check permissions against committees""" """Check permissions against committees"""
...@@ -20,11 +22,17 @@ class CommitteeBackend(object): ...@@ -20,11 +22,17 @@ class CommitteeBackend(object):
def _get_permissions(self, user, obj): def _get_permissions(self, user, obj):
if not user.is_active or user.is_anonymous or obj is not None: if not user.is_active or user.is_anonymous or obj is not None:
return set() return set()
perm_cache_name = '_committee_perm_cache' try:
committees = user.committee_set.filter( member = Member.objects.get(pk=user.pk)
except Member.DoesNotExist:
return set()
committees = member.committee_set.filter(
Q(committeemembership__until=None) | Q(committeemembership__until=None) |
Q(committeemembership__until__gte=timezone.now()) Q(committeemembership__until__gte=timezone.now())
) )
perm_cache_name = '_committee_perm_cache'
if not hasattr(user, perm_cache_name): if not hasattr(user, perm_cache_name):
perms = (Permission.objects perms = (Permission.objects
.filter(committee__in=committees) .filter(committee__in=committees)
......
from django import forms from django import forms
from django.contrib.auth.models import User
from activemembers.models import CommitteeMembership from activemembers.models import CommitteeMembership
from members.models import Member
class CommitteeMembershipForm(forms.ModelForm): class CommitteeMembershipForm(forms.ModelForm):
member = forms.ModelChoiceField( member = forms.ModelChoiceField(
queryset=User.objects.order_by('first_name', queryset=Member.objects.order_by('first_name',
'last_name')) 'last_name'))
class Meta: class Meta:
model = CommitteeMembership model = CommitteeMembership
......
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2017-10-13 12:16
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('members', '0018_create_proxy_member'),
('activemembers', '0022_4_user_foreign_keys'),
]
operations = [
migrations.AlterField(
model_name='committee',
name='members',
field=models.ManyToManyField(through='activemembers.CommitteeMembership', to='members.Member'),
),
migrations.AlterField(
model_name='committeemembership',
name='member',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='members.Member', verbose_name='Member'),
),
migrations.AlterField(
model_name='mentorship',
name='member',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='members.Member', verbose_name='Member'),
),
]
import datetime import datetime
import logging import logging
from django.contrib.auth.models import Permission, User from django.contrib.auth.models import Permission
from django.core.exceptions import NON_FIELD_ERRORS, ValidationError from django.core.exceptions import NON_FIELD_ERRORS, ValidationError
from django.core.validators import MinValueValidator from django.core.validators import MinValueValidator
from django.db import models from django.db import models
...@@ -69,7 +69,7 @@ class Committee(models.Model, metaclass=ModelTranslateMeta): ...@@ -69,7 +69,7 @@ class Committee(models.Model, metaclass=ModelTranslateMeta):
) )
members = models.ManyToManyField( members = models.ManyToManyField(
User, 'members.Member',
through='CommitteeMembership' through='CommitteeMembership'
) )
...@@ -206,7 +206,7 @@ class CommitteeMembership(models.Model, metaclass=ModelTranslateMeta): ...@@ -206,7 +206,7 @@ class CommitteeMembership(models.Model, metaclass=ModelTranslateMeta):
active_memberships = ActiveMembershipManager() active_memberships = ActiveMembershipManager()
member = models.ForeignKey( member = models.ForeignKey(
User, 'members.Member',
on_delete=models.CASCADE, on_delete=models.CASCADE,
verbose_name=_('Member'), verbose_name=_('Member'),
) )
...@@ -348,7 +348,7 @@ class CommitteeMembership(models.Model, metaclass=ModelTranslateMeta): ...@@ -348,7 +348,7 @@ class CommitteeMembership(models.Model, metaclass=ModelTranslateMeta):
class Mentorship(models.Model): class Mentorship(models.Model):
member = models.ForeignKey( member = models.ForeignKey(
User, 'members.Member',
on_delete=models.CASCADE, on_delete=models.CASCADE,
verbose_name=_('Member'), verbose_name=_('Member'),
) )
......
...@@ -34,13 +34,13 @@ ...@@ -34,13 +34,13 @@
<ul class="row" id="commissieleden"> <ul class="row" id="commissieleden">
{% for member in members %} {% for member in members %}
<li class="post member-item span3 has-overlay"> <li class="post member-item span3 has-overlay">
<a href="{{ member.member.get_absolute_url }}"> <a href="{{ member.get_absolute_url }}">
<div class="post-inner"> <div class="post-inner">
<div class="inner-img"> <div class="inner-img">
{% if not member.member.photo %} {% if not member.profile.photo %}
<img alt="{{ member.member.display_name }}" src="{% static 'members/images/default-avatar.jpg' %}" height="220" width="220" /> <img alt="{{ member.profile.display_name }}" src="{% static 'members/images/default-avatar.jpg' %}" height="220" width="220" />
{% else %} {% else %}
<img alt="{{ member.member.display_name }}" src="{% thumbnail member.member.photo '220x220' %}" width="220" height="220" /> <img alt="{{ member.profile.display_name }}" src="{% thumbnail member.profile.photo '220x220' %}" width="220" height="220" />
{% endif %} {% endif %}
</div> </div>
{% if member.chair %} {% if member.chair %}
...@@ -52,12 +52,12 @@ ...@@ -52,12 +52,12 @@
{% endif %} {% endif %}
<div class="post-overlay"> <div class="post-overlay">
<div class="post-overlay-meta"> <div class="post-overlay-meta">
<h2>{{ member.member.display_name }}</h2> <h2>{{ member.profile.display_name }}</h2>
<p>{{ member.role }}</p> <p>{{ member.role }}</p>
</div> </div>
</div> </div>
<div class="post-body avatar-subtitle"> <div class="post-body avatar-subtitle">
{{ member.member.display_name }} {{ member.profile.display_name }}
</div> </div>
</div> </div>
</a> </a>
......
...@@ -30,13 +30,13 @@ ...@@ -30,13 +30,13 @@
<ul class="row" id="commissieleden"> <ul class="row" id="commissieleden">
{% for member in members %} {% for member in members %}
<li class="post member-item span3 has-overlay"> <li class="post member-item span3 has-overlay">
<a href="{{ member.member.get_absolute_url }}"> <a href="{{ member.get_absolute_url }}">
<div class="post-inner"> <div class="post-inner">
<div class="inner-img"> <div class="inner-img">
{% if not member.member.photo %} {% if not member.profile.photo %}
<img alt="{{ member.member.display_name }}" src="{% static 'members/images/default-avatar.jpg' %}" height="220" width="220" /> <img alt="{{ member.profile.display_name }}" src="{% static 'members/images/default-avatar.jpg' %}" height="220" width="220" />
{% else %} {% else %}
<img alt="{{ member.member.display_name }}" src="{% thumbnail member.member.photo '220x220' %}" width="220" height="220" /> <img alt="{{ member.profile.display_name }}" src="{% thumbnail member.profile.photo '220x220' %}" width="220" height="220" />
{% endif %} {% endif %}
</div> </div>
{% if member.chair %} {% if member.chair %}
...@@ -48,7 +48,7 @@ ...@@ -48,7 +48,7 @@
{% endif %} {% endif %}
<div class="post-overlay"> <div class="post-overlay">
<div class="post-overlay-meta"> <div class="post-overlay-meta">
<h2>{{ member.member.display_name }}</h2> <h2>{{ member.profile.display_name }}</h2>
<p> <p>
{% if member.committee_since.year == 1970 %} {% if member.committee_since.year == 1970 %}
{% blocktrans %}Committee member since: ?{% endblocktrans %} {% blocktrans %}Committee member since: ?{% endblocktrans %}
...@@ -59,7 +59,7 @@ ...@@ -59,7 +59,7 @@
</div> </div>
</div> </div>
<div class="post-body avatar-subtitle"> <div class="post-body avatar-subtitle">
{{ member.member.display_name }} {{ member.profile.display_name }}
</div> </div>
</div> </div>
</a> </a>
......
from django.contrib.auth import get_user_model
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db.utils import IntegrityError from django.db.utils import IntegrityError
...@@ -7,6 +6,7 @@ from django.utils import timezone ...@@ -7,6 +6,7 @@ from django.utils import timezone
from activemembers.models import Committee, CommitteeMembership, Board from activemembers.models import Committee, CommitteeMembership, Board
from mailinglists.models import MailingList from mailinglists.models import MailingList
from members.models import Member
class CommitteeMembersTest(TestCase): class CommitteeMembersTest(TestCase):
...@@ -15,7 +15,7 @@ class CommitteeMembersTest(TestCase): ...@@ -15,7 +15,7 @@ class CommitteeMembersTest(TestCase):
@classmethod @classmethod
def setUpTestData(cls): def setUpTestData(cls):
cls.testcie = Committee.objects.get(pk=1) cls.testcie = Committee.objects.get(pk=1)
cls.testuser = User.objects.get(pk=1) cls.testuser = Member.objects.get(pk=1)
cls.m = CommitteeMembership.objects.create( cls.m = CommitteeMembership.objects.create(
committee=cls.testcie, committee=cls.testcie,
member=cls.testuser, member=cls.testuser,
...@@ -35,7 +35,7 @@ class CommitteeMembersTest(TestCase): ...@@ -35,7 +35,7 @@ class CommitteeMembersTest(TestCase):
photo="") photo="")
def test_join(self): def test_join(self):
testuser2 = User.objects.get(pk=2) testuser2 = Member.objects.get(pk=2)
m = CommitteeMembership(committee=self.testcie, m = CommitteeMembership(committee=self.testcie,
member=testuser2) member=testuser2)
m.full_clean() m.full_clean()
...@@ -95,8 +95,8 @@ class CommitteeMembersChairTest(TestCase): ...@@ -95,8 +95,8 @@ class CommitteeMembersChairTest(TestCase):
@classmethod @classmethod
def setUpTestData(cls): def setUpTestData(cls):
cls.testcie = Committee.objects.get(pk=1) cls.testcie = Committee.objects.get(pk=1)
cls.testuser = User.objects.get(pk=1) cls.testuser = Member.objects.get(pk=1)
cls.testuser2 = User.objects.get(pk=2) cls.testuser2 = Member.objects.get(pk=2)
def setUp(self): def setUp(self):
self.m1 = CommitteeMembership(committee=self.testcie, self.m1 = CommitteeMembership(committee=self.testcie,
...@@ -134,11 +134,11 @@ class PermissionsBackendTest(TestCase): ...@@ -134,11 +134,11 @@ class PermissionsBackendTest(TestCase):
@classmethod @classmethod
def setUpTestData(cls): def setUpTestData(cls):
cls.u1 = User.objects.get(pk=1) cls.u1 = Member.objects.get(pk=1)
cls.u1.is_superuser = False cls.u1.is_superuser = False
cls.u1.save() cls.u1.save()
cls.u2 = User.objects.get(pk=2) cls.u2 = Member.objects.get(pk=2)
cls.u3 = User.objects.get(pk=3) cls.u3 = Member.objects.get(pk=3)
cls.c1 = Committee.objects.get(pk=1) cls.c1 = Committee.objects.get(pk=1)
cls.c2 = Committee.objects.get(pk=2) cls.c2 = Committee.objects.get(pk=2)
cls.m1 = CommitteeMembership.objects.create(committee=cls.c1, cls.m1 = CommitteeMembership.objects.create(committee=cls.c1,
...@@ -152,7 +152,7 @@ class PermissionsBackendTest(TestCase): ...@@ -152,7 +152,7 @@ class PermissionsBackendTest(TestCase):
self.assertEqual(set(), self.u3.get_all_permissions()) self.assertEqual(set(), self.u3.get_all_permissions())
def test_nonmember_user(self): def test_nonmember_user(self):
u = get_user_model().objects.create(username='foo') u = User.objects.create(username='foo')
self.assertEqual(set(), u.get_all_permissions()) self.assertEqual(set(), u.get_all_permissions())
......
# -*- coding: utf-8 -*-
# Generated by Django 1.11.6 on 2017-10-13 13:35
from __future__ import unicode_literals
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('members', '0018_create_proxy_member'),
('education', '0005_auto_20170906_2109'),
]
operations = [
migrations.AlterField(
model_name='exam',
name='uploader',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='members.Member', verbose_name='uploader'),
),
migrations.AlterField(
model_name='summary',
name='uploader',
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='members.Member', verbose_name='uploader'),
),
]
from django.conf import settings
from django.db import models from django.db import models
from django.urls import reverse from django.urls import reverse
from django.utils import timezone from django.utils import timezone
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from members.models import Member
from utils.snippets import datetime_to_lectureyear from utils.snippets import datetime_to_lectureyear
from utils.translation import ModelTranslateMeta, MultilingualField from utils.translation import ModelTranslateMeta, MultilingualField
...@@ -103,7 +103,7 @@ class Exam(models.Model, metaclass=ModelTranslateMeta): ...@@ -103,7 +103,7 @@ class Exam(models.Model, metaclass=ModelTranslateMeta):
) )
uploader = models.ForeignKey( uploader = models.ForeignKey(
settings.AUTH_USER_MODEL, Member,
verbose_name=_('uploader'), verbose_name=_('uploader'),
on_delete=models.CASCADE, on_delete=models.CASCADE,
) )
...@@ -159,7 +159,7 @@ class Summary(models.Model, metaclass=ModelTranslateMeta): ...@@ -159,7 +159,7 @@ class Summary(models.Model, metaclass=ModelTranslateMeta):
) )
uploader = models.ForeignKey( uploader = models.ForeignKey(
settings.AUTH_USER_MODEL, Member,
verbose_name=_('uploader'), verbose_name=_('uploader'),
on_delete=models.CASCADE, on_delete=models.CASCADE,
) )
......
...@@ -76,7 +76,7 @@ def submit_exam(request, id=None): ...@@ -76,7 +76,7 @@ def submit_exam(request, id=None):
if form.is_valid(): if form.is_valid():
saved = True saved = True
obj = form.save(commit=False) obj = form.save(commit=False)
obj.uploader = request.user obj.uploader = request.member
obj.uploader_date = datetime.now() obj.uploader_date = datetime.now()
obj.save() obj.save()
...@@ -101,18 +101,18 @@ def submit_summary(request, id=None): ...@@ -101,18 +101,18 @@ def submit_summary(request, id=None):
if form.is_valid(): if form.is_valid():
saved = True saved = True
obj = form.save(commit=False) obj = form.save(commit=False)
obj.uploader = request.user obj.uploader = request.member
obj.uploader_date = datetime.now() obj.uploader_date = datetime.now()
obj.save() obj.save()
obj = Summary() obj = Summary()
obj.author = request.user.get_full_name() obj.author = request.member.get_full_name()
form = AddSummaryForm(instance=obj) form = AddSummaryForm(instance=obj)
else: else:
obj = Summary() obj = Summary()
if id is not None: if id is not None:
obj.course = Course.objects.get(id=id) obj.course = Course.objects.get(id=id)
obj.author = request.user.get_full_name() obj.author = request.member.get_full_name()
form = AddSummaryForm(instance=obj) form = AddSummaryForm(instance=obj)
return render(request, 'education/add_summary.html', return render(request, 'education/add_summary.html',
......
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
from django.contrib import admin from django.contrib import admin
from django.contrib.auth.models import User
from django.http import HttpResponseRedirect from django.http import HttpResponseRedirect
from django.template.defaultfilters import date as _date from django.template.defaultfilters import date as _date
from django.urls import reverse from django.urls import reverse
...@@ -74,7 +73,7 @@ class EventAdmin(DoNextModelAdmin): ...@@ -74,7 +73,7 @@ class EventAdmin(DoNextModelAdmin):
def has_change_permission(self, request, event=None): def has_change_permission(self, request, event=None):
if (event is not None and if (event is not None and
not services.is_organiser(request.user, event)): not services.is_organiser(request.member, event)):
return False return False
return super().has_change_permission(request, event) return super().has_change_permission(request, event)
...@@ -111,13 +110,10 @@ class EventAdmin(DoNextModelAdmin): ...@@ -111,13 +110,10 @@ class EventAdmin(DoNextModelAdmin):
@staticmethod @staticmethod
def _change_published(request, queryset, published): def _change_published(request, queryset, published):
try: if not request.user.is_superuser:
if not request.user.is_superuser: queryset = queryset.filter(
queryset = queryset.filter( organiser__in=request.member.get_committees())
organiser__in=request.user.member.get_committees()) queryset.update(published=published)
queryset.update(published=published)
except Member.DoesNotExist:
pass
def save_formset(self, request, form, formset, change): def save_formset(self, request, form, formset, change):
"""Save formsets with their order""" """Save formsets with their order"""
...@@ -144,13 +140,9 @@ class EventAdmin(DoNextModelAdmin): ...@@ -144,13 +140,9 @@ class EventAdmin(DoNextModelAdmin):
if db_field.name == 'organiser': if db_field.name == 'organiser':
# Use custom queryset for organiser field # Use custom queryset for organiser field
# Only get the current active committees the user is a member of # Only get the current active committees the user is a member of
try: if not (request.user.is_superuser or
if not (request.user.is_superuser or request.user.has_perm('events.override_organiser')):
request.user.has_perm('events.override_organiser')): kwargs['queryset'] = request.member.get_committees()
kwargs['queryset'] = request.user.member.get_committees()
except Member.DoesNotExist:
pass
return super().formfield_for_foreignkey(db_field, request, **kwargs) return super().formfield_for_foreignkey(db_field, request, **kwargs)
def get_actions(self, request): def get_actions(self, request):
...@@ -179,6 +171,5 @@ class RegistrationAdmin(DoNextModelAdmin): ...@@ -179,6 +171,5 @@ class RegistrationAdmin(DoNextModelAdmin):
kwargs['queryset'] = models.Event.objects.filter( kwargs['queryset'] = models.Event.objects.filter(
pk=int(request.GET['event_pk'])) pk=int(request.GET['event_pk']))
elif db_field.name == 'member': elif db_field.name == 'member':
# TODO: Member.active_members.all() kwargs['queryset'] = Member.active_members.all()
kwargs['queryset'] = User.objects.all()
return super().formfield_for_foreignkey(db_field, request, **kwargs) return super().formfield_for_foreignkey(db_field, request, **kwargs)
...@@ -95,7 +95,7 @@ def export(request, event_id): ...@@ -95,7 +95,7 @@ def export(request, event_id):
_('Date'): timezone.localtime( _('Date'): timezone.localtime(
registration.date).strftime("%Y-%m-%d %H:%m"), registration.date).strftime("%Y-%m-%d %H:%m"),
_('Present'): _('Yes') if registration.present else '', _('Present'): _('Yes') if registration.present else '',
_('Phone number'): (registration.member.member.phone_number _('Phone number'): (registration.member.profile.phone_number
if registration.member if registration.member
else ''), else ''),
_('Email'): (registration.member.email _('Email'): (registration.member.email
......
...@@ -77,7 +77,8 @@ class EventCalenderJSSerializer(CalenderJSSerializer): ...@@ -77,7 +77,8 @@ class EventCalenderJSSerializer(CalenderJSSerializer):
def _registered(self, instance): def _registered(self, instance):
try: try:
return services.is_user_registered(instance, self.context['user']) return services.is_user_registered(instance,
self.context['member'])
except AttributeError: except AttributeError:
return None return None
...@@ -155,16 +156,15 @@ class EventRetrieveSerializer(serializers.ModelSerializer): ...@@ -155,16 +156,15 @@ class EventRetrieveSerializer(serializers.ModelSerializer):
def _user_registration(self, instance): def _user_registration(self, instance):
try: try:
reg = instance.registration_set.get( reg = instance.registration_set.get(
member=self.context['request'].user) member=self.context['request'].member)
return RegistrationListSerializer(reg, context=self.context).data return RegistrationListSerializer(reg, context=self.context).data
except Registration.DoesNotExist: except Registration.DoesNotExist:
return None return None
def _registration_allowed(self, instance): def _registration_allowed(self, instance):
user = self.context['request'].user member = self.context['request'].member
return (user.member is not None and return (member.has_active_membership and
user.member.current_membership is not None and member.can_attend_events)