Verified Commit be6109fb authored by Sébastiaan Versteeg's avatar Sébastiaan Versteeg
Browse files

Finish migrations to new database schemes for activemembers

parent ae2e15c4
...@@ -26,7 +26,7 @@ class Migration(migrations.Migration): ...@@ -26,7 +26,7 @@ class Migration(migrations.Migration):
migrations.AlterModelManagers( migrations.AlterModelManagers(
name='board', name='board',
managers=[ managers=[
('objects', activemembers.models.BoardManager()), ('objects', models.manager.Manager()),
], ],
), ),
migrations.RunPython(remove_enddates), migrations.RunPython(remove_enddates),
......
# Generated by Django 2.0.1 on 2018-01-08 11:53 # Generated by Django 2.0.1 on 2018-01-08 11:53
import activemembers.models from django.db import migrations, models
from django.db import migrations
class Migration(migrations.Migration): class Migration(migrations.Migration):
...@@ -14,7 +13,7 @@ class Migration(migrations.Migration): ...@@ -14,7 +13,7 @@ class Migration(migrations.Migration):
migrations.AlterModelManagers( migrations.AlterModelManagers(
name='board', name='board',
managers=[ managers=[
('objects', activemembers.models.BoardManager()), ('objects', models.manager.Manager()),
], ],
), ),
] ]
...@@ -9,6 +9,10 @@ class Migration(migrations.Migration): ...@@ -9,6 +9,10 @@ class Migration(migrations.Migration):
dependencies = [ dependencies = [
('members', '0023_auto_20180819_1542'), ('members', '0023_auto_20180819_1542'),
('activemembers', '0026_auto_20180203_2151'), ('activemembers', '0026_auto_20180203_2151'),
('mailinglists', '0012_auto_20180203_2304'),
('auth', '0009_alter_user_last_name_max_length'),
('events', '0027_merge_20180618_1438'),
('members', '0023_auto_20180819_1542'),
] ]
operations = [ operations = [
......
# Generated by Django 2.0.8 on 2018-09-01 15:09
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('activemembers', '0027_auto_20180901_1545'),
]
operations = [
migrations.CreateModel(
name='TemporaryBoard',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('parent', models.IntegerField(null=True)),
],
),
]
# Generated by Django 2.0.8 on 2018-09-01 15:13
from django.db import migrations
def migrate_to(apps, schema_editor):
Board = apps.get_model("activemembers", "Board")
TemporaryBoard = apps.get_model("activemembers", "TemporaryBoard")
boards = Board.objects.all()
for board in boards:
TemporaryBoard.objects.create(parent=board.committee_ptr_id)
def migrate_back(apps, schema_editor):
# Reverse is not imported, objects will be deleted
pass
class Migration(migrations.Migration):
dependencies = [
('activemembers', '0028_create_tmp_board_model'),
]
operations = [
migrations.RunPython(migrate_to, migrate_back),
]
# Generated by Django 2.0.8 on 2018-09-01 15:27
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('activemembers', '0029_save_board_pk_in_tmp'),
]
operations = [
migrations.DeleteModel(name='Board')
]
# Generated by Django 2.0.8 on 2018-09-01 15:30
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('activemembers', '0030_delete_original_board_model'),
]
operations = [
migrations.RenameModel(
old_name='Committee',
new_name='MemberGroup',
),
]
# Generated by Django 2.0.8 on 2018-09-01 15:38
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('activemembers', '0031_rename_base_model'),
]
operations = [
migrations.RenameField(
model_name='membergroup',
old_name='wiki_namespace',
new_name='wiki_namespace_old',
),
migrations.CreateModel(
name='Board',
fields=[
('membergroup_ptr', models.OneToOneField(auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to='activemembers.MemberGroup')),
],
options={'ordering': ['-since'],
'permissions': (('board_wiki', 'Access the board wiki'),),
'verbose_name': 'board', 'verbose_name_plural': 'boards'},
bases=('activemembers.membergroup',),
),
migrations.CreateModel(
name='Committee',
fields=[
('membergroup_ptr', models.OneToOneField(auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to='activemembers.MemberGroup')),
('wiki_namespace',
models.CharField(blank=True, max_length=50, null=True,
verbose_name='Wiki namespace')),
],
options={
'verbose_name': 'committee',
'verbose_name_plural': 'committees',
},
bases=('activemembers.membergroup',),
),
migrations.CreateModel(
name='Society',
fields=[
('membergroup_ptr', models.OneToOneField(auto_created=True,
on_delete=django.db.models.deletion.CASCADE,
parent_link=True,
primary_key=True,
serialize=False,
to='activemembers.MemberGroup')),
],
options={
'verbose_name': 'society',
'verbose_name_plural': 'societies',
},
bases=('activemembers.membergroup',),
),
migrations.AlterModelOptions(
name='membergroup',
options={'verbose_name': 'member group',
'verbose_name_plural': 'member groups'},
),
migrations.AlterModelManagers(
name='membergroup',
managers=[
],
),
]
# Generated by Django 2.0.8 on 2018-09-01 15:39
from django.db import migrations
def migrate_to(apps, schema_editor):
TemporaryBoard = apps.get_model("activemembers", "TemporaryBoard")
MemberGroup = apps.get_model("activemembers", "MemberGroup")
boards = TemporaryBoard.objects.all()
for temp_board in boards:
schema_editor.execute(
'INSERT INTO activemembers_board (membergroup_ptr_id) VALUES ({});'
.format(temp_board.parent))
groups = MemberGroup.objects.all()
for group in groups:
if not TemporaryBoard.objects.filter(parent=group.pk).exists():
schema_editor.execute(
'INSERT INTO activemembers_committee '
'(membergroup_ptr_id, wiki_namespace) VALUES ({}, {});'
.format(group.pk, group.wiki_namespace_old if group.wiki_namespace_old else 'NULL'))
def migrate_back(apps, schema_editor):
schema_editor.execute('DELETE FROM activemembers_board;')
Committee = apps.get_model("activemembers", "Committee")
committees = Committee.objects.all()
for committee in committees:
schema_editor.execute(
'UPDATE activemembers_membergroup '
'SET wiki_namespace_old={} WHERE id={}'
.format(committee.wiki_namespace if committee.wiki_namespace else 'NULL', committee.membergroup_ptr_id))
schema_editor.execute('DELETE FROM activemembers_committee;')
class Migration(migrations.Migration):
dependencies = [
('activemembers', '0032_create_submodels'),
]
operations = [
migrations.RunPython(migrate_to, migrate_back),
]
# Generated by Django 2.0.8 on 2018-09-01 16:09
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('activemembers', '0033_fill_submodel_schemas'),
]
operations = [
migrations.DeleteModel(
name='TemporaryBoard',
),
migrations.RemoveField(model_name='MemberGroup',
name='wiki_namespace_old')
]
...@@ -17,39 +17,20 @@ from utils.translation import (ModelTranslateMeta, MultilingualField, ...@@ -17,39 +17,20 @@ from utils.translation import (ModelTranslateMeta, MultilingualField,
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
class UnfilteredSortedManager(models.Manager): class ActiveMemberGroupsManager(models.Manager):
"""Returns committees and boards, sorted by name"""
def get_queryset(self):
return (super().get_queryset()
.order_by(localize_attr_name('name')))
class CommitteeManager(models.Manager):
"""Returns committees only"""
def get_queryset(self):
return (super().get_queryset()
.filter(board=None)
.order_by(localize_attr_name('name')))
class ActiveCommitteeManager(models.Manager):
"""Returns active committees only""" """Returns active committees only"""
def get_queryset(self): def get_queryset(self):
return (super().get_queryset() return (super().get_queryset()
.filter(board=None)
.exclude(active=False) .exclude(active=False)
.order_by(localize_attr_name('name'))) .order_by(localize_attr_name('name')))
class Committee(models.Model, metaclass=ModelTranslateMeta): class MemberGroup(models.Model, metaclass=ModelTranslateMeta):
"""Describes a committee""" """Describes a committee"""
unfiltered_objects = UnfilteredSortedManager() objects = models.Manager()
objects = CommitteeManager() active_objects = ActiveMemberGroupsManager()
active_committees = ActiveCommitteeManager()
name = MultilingualField( name = MultilingualField(
models.CharField, models.CharField,
...@@ -109,12 +90,6 @@ class Committee(models.Model, metaclass=ModelTranslateMeta): ...@@ -109,12 +90,6 @@ class Committee(models.Model, metaclass=ModelTranslateMeta):
on_delete=models.SET_NULL, on_delete=models.SET_NULL,
) )
wiki_namespace = models.CharField(
_('Wiki namespace'),
null=True,
blank=True,
max_length=50)
def clean(self): def clean(self):
if ((self.contact_email is not None and if ((self.contact_email is not None and
self.contact_mailinglist is not None) or self.contact_mailinglist is not None) or
...@@ -136,40 +111,35 @@ class Committee(models.Model, metaclass=ModelTranslateMeta): ...@@ -136,40 +111,35 @@ class Committee(models.Model, metaclass=ModelTranslateMeta):
return reverse('activemembers:committee', args=[str(self.pk)]) return reverse('activemembers:committee', args=[str(self.pk)])
class Meta: class Meta:
verbose_name = _('committee') verbose_name = _('member group')
verbose_name_plural = _('committees') verbose_name_plural = _('member groups')
# ordering is done in the manager, to sort on a translated field # ordering is done in the manager, to sort on a translated field
class BoardManager(models.Manager): class Committee(MemberGroup):
""" wiki_namespace = models.CharField(
Custom manager that filters out _('Wiki namespace'),
instances of Committee that are not boards null=True,
""" blank=True,
max_length=50)
use_in_migrations = True
def get_queryset(self): class Meta:
# sorting by descending order by default makes more sense for boards verbose_name = _('committee')
return (super().get_queryset() verbose_name_plural = _('committees')
.filter(is_board=True) # ordering is done in the manager, to sort on a translated field
.order_by(localize_attr_name('-name')))
class Board(Committee): class Society(MemberGroup):
""" Because Board inherits from Committee, Django creates a OneToOneField class Meta:
linking the two models together. This can be accessed as usual; verbose_name = _('society')
given a Committee or Board b, one can access b.board, which will either verbose_name_plural = _('societies')
return the object b if b is a Board, or a Board.DoesNotExist exception. # ordering is done in the manager, to sort on a translated field
"""
objects = BoardManager()
is_board = models.BooleanField(
verbose_name=_('Is this a board'),
default=True,
)
class Board(MemberGroup):
class Meta: class Meta:
verbose_name = _('board')
verbose_name_plural = _('boards')
ordering = ['-since'] ordering = ['-since']
permissions = ( permissions = (
('board_wiki', _("Access the board wiki")), ('board_wiki', _("Access the board wiki")),
...@@ -222,7 +192,7 @@ class MemberGroupMembership(models.Model, metaclass=ModelTranslateMeta): ...@@ -222,7 +192,7 @@ class MemberGroupMembership(models.Model, metaclass=ModelTranslateMeta):
) )
group = models.ForeignKey( group = models.ForeignKey(
Committee, MemberGroup,
on_delete=models.CASCADE, on_delete=models.CASCADE,
verbose_name=_('Committee'), verbose_name=_('Committee'),
) )
......
...@@ -2,7 +2,7 @@ from django.shortcuts import get_object_or_404, render, redirect, reverse ...@@ -2,7 +2,7 @@ from django.shortcuts import get_object_or_404, render, redirect, reverse
import datetime import datetime
from utils.snippets import datetime_to_lectureyear from utils.snippets import datetime_to_lectureyear
from utils.translation import localize_attr_name from utils.translation import localize_attr_name
from .models import Board, Committee, MemberGroupMembership from .models import Board, MemberGroup, MemberGroupMembership
def committee_index(request): def committee_index(request):
...@@ -12,7 +12,7 @@ def committee_index(request): ...@@ -12,7 +12,7 @@ def committee_index(request):
:param request: the request object :param request: the request object
:return: response containing the HTML :return: response containing the HTML
""" """
committees = Committee.active_committees.all().order_by( committees = MemberGroup.active_objects.all().order_by(
localize_attr_name('name')) localize_attr_name('name'))
return render(request, 'activemembers/committee_index.html', return render(request, 'activemembers/committee_index.html',
...@@ -27,7 +27,7 @@ def committee_detail(request, pk): ...@@ -27,7 +27,7 @@ def committee_detail(request, pk):
:param pk: pk of the selected committee :param pk: pk of the selected committee
:return: :return:
""" """
committee = get_object_or_404(Committee, pk=pk) committee = get_object_or_404(MemberGroup, pk=pk)
members = [] members = []
memberships = (MemberGroupMembership memberships = (MemberGroupMembership
......
...@@ -11,7 +11,7 @@ from django.utils.html import format_html ...@@ -11,7 +11,7 @@ from django.utils.html import format_html
from django.utils.http import is_safe_url from django.utils.http import is_safe_url
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from activemembers.models import Committee from activemembers.models import MemberGroup
from events import services from events import services
from members.models import Member from members.models import Member
from pizzas.models import PizzaEvent from pizzas.models import PizzaEvent
...@@ -209,8 +209,8 @@ class EventAdmin(DoNextModelAdmin): ...@@ -209,8 +209,8 @@ class EventAdmin(DoNextModelAdmin):
# Hide old boards and inactive committees for new events # Hide old boards and inactive committees for new events
if 'add' in request.path: if 'add' in request.path:
kwargs['queryset'] = ( kwargs['queryset'] = (
Committee.active_committees.all() | MemberGroup.active_objects.all() |
Committee.unfiltered_objects MemberGroup.unfiltered_objects
.filter(board=None) .filter(board=None)
.exclude(until__lt=(timezone.now() - .exclude(until__lt=(timezone.now() -
timezone.timedelta(weeks=1))) timezone.timedelta(weeks=1)))
......
...@@ -45,7 +45,7 @@ class Event(models.Model, metaclass=ModelTranslateMeta): ...@@ -45,7 +45,7 @@ class Event(models.Model, metaclass=ModelTranslateMeta):
end = models.DateTimeField(_("end time")) end = models.DateTimeField(_("end time"))
organiser = models.ForeignKey( organiser = models.ForeignKey(
'activemembers.Committee', 'activemembers.MemberGroup',
models.PROTECT, models.PROTECT,
verbose_name=_("organiser") verbose_name=_("organiser")
) )
......
...@@ -4,7 +4,7 @@ from django.db import models ...@@ -4,7 +4,7 @@ from django.db import models
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 activemembers.models import Committee from activemembers.models import MemberGroup
from members.models import Member from members.models import Member
...@@ -48,7 +48,7 @@ class MailingList(models.Model): ...@@ -48,7 +48,7 @@ class MailingList(models.Model):
) )
committees = models.ManyToManyField( committees = models.ManyToManyField(
Committee, MemberGroup,
verbose_name=_("Committees"), verbose_name=_("Committees"),
help_text=_('Select entire committees to include in the list.'), help_text=_('Select entire committees to include in the list.'),
blank=True, blank=True,
......
...@@ -17,7 +17,7 @@ from functools import reduce ...@@ -17,7 +17,7 @@ from functools import reduce
from localflavor.generic.countries.sepa import IBAN_SEPA_COUNTRIES from localflavor.generic.countries.sepa import IBAN_SEPA_COUNTRIES
from localflavor.generic.models import IBANField from localflavor.generic.models import IBANField
from activemembers.models import Committee, MemberGroupMembership from activemembers.models import MemberGroup, MemberGroupMembership
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
...@@ -177,7 +177,7 @@ class Member(User): ...@@ -177,7 +177,7 @@ class Member(User):
def get_committees(self): def get_committees(self):
"""Get the committees this user is a member of""" """Get the committees this user is a member of"""
return Committee.unfiltered_objects.filter( return MemberGroup.unfiltered_objects.filter(
Q(membergroupmembership__member=self) & Q(membergroupmembership__member=self) &
( (
Q(membergroupmembership__until=None) | Q(membergroupmembership__until=None) |
......
Markdown is supported
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