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):
migrations.AlterModelManagers(
name='board',
managers=[
('objects', activemembers.models.BoardManager()),
('objects', models.manager.Manager()),
],
),
migrations.RunPython(remove_enddates),
......
# Generated by Django 2.0.1 on 2018-01-08 11:53
import activemembers.models
from django.db import migrations
from django.db import migrations, models
class Migration(migrations.Migration):
......@@ -14,7 +13,7 @@ class Migration(migrations.Migration):
migrations.AlterModelManagers(
name='board',
managers=[
('objects', activemembers.models.BoardManager()),
('objects', models.manager.Manager()),
],
),
]
......@@ -9,6 +9,10 @@ class Migration(migrations.Migration):
dependencies = [
('members', '0023_auto_20180819_1542'),
('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 = [
......
# 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,
logger = logging.getLogger(__name__)
class UnfilteredSortedManager(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):
class ActiveMemberGroupsManager(models.Manager):
"""Returns active committees only"""
def get_queryset(self):
return (super().get_queryset()
.filter(board=None)
.exclude(active=False)
.order_by(localize_attr_name('name')))
class Committee(models.Model, metaclass=ModelTranslateMeta):
class MemberGroup(models.Model, metaclass=ModelTranslateMeta):
"""Describes a committee"""
unfiltered_objects = UnfilteredSortedManager()
objects = CommitteeManager()
active_committees = ActiveCommitteeManager()
objects = models.Manager()
active_objects = ActiveMemberGroupsManager()
name = MultilingualField(
models.CharField,
......@@ -109,12 +90,6 @@ class Committee(models.Model, metaclass=ModelTranslateMeta):
on_delete=models.SET_NULL,
)
wiki_namespace = models.CharField(
_('Wiki namespace'),
null=True,
blank=True,
max_length=50)
def clean(self):
if ((self.contact_email is not None and
self.contact_mailinglist is not None) or
......@@ -136,40 +111,35 @@ class Committee(models.Model, metaclass=ModelTranslateMeta):
return reverse('activemembers:committee', args=[str(self.pk)])
class Meta:
verbose_name = _('committee')
verbose_name_plural = _('committees')
verbose_name = _('member group')
verbose_name_plural = _('member groups')
# ordering is done in the manager, to sort on a translated field
class BoardManager(models.Manager):
"""
Custom manager that filters out
instances of Committee that are not boards
"""
use_in_migrations = True
class Committee(MemberGroup):
wiki_namespace = models.CharField(
_('Wiki namespace'),
null=True,
blank=True,
max_length=50)
def get_queryset(self):
# sorting by descending order by default makes more sense for boards
return (super().get_queryset()
.filter(is_board=True)
.order_by(localize_attr_name('-name')))
class Meta:
verbose_name = _('committee')
verbose_name_plural = _('committees')
# ordering is done in the manager, to sort on a translated field
class Board(Committee):
""" Because Board inherits from Committee, Django creates a OneToOneField
linking the two models together. This can be accessed as usual;
given a Committee or Board b, one can access b.board, which will either
return the object b if b is a Board, or a Board.DoesNotExist exception.
"""
objects = BoardManager()
class Society(MemberGroup):
class Meta:
verbose_name = _('society')
verbose_name_plural = _('societies')
# ordering is done in the manager, to sort on a translated field
is_board = models.BooleanField(
verbose_name=_('Is this a board'),
default=True,
)
class Board(MemberGroup):
class Meta:
verbose_name = _('board')
verbose_name_plural = _('boards')
ordering = ['-since']
permissions = (
('board_wiki', _("Access the board wiki")),
......@@ -222,7 +192,7 @@ class MemberGroupMembership(models.Model, metaclass=ModelTranslateMeta):
)
group = models.ForeignKey(
Committee,
MemberGroup,
on_delete=models.CASCADE,
verbose_name=_('Committee'),
)
......
......@@ -2,7 +2,7 @@ from django.shortcuts import get_object_or_404, render, redirect, reverse
import datetime
from utils.snippets import datetime_to_lectureyear
from utils.translation import localize_attr_name
from .models import Board, Committee, MemberGroupMembership
from .models import Board, MemberGroup, MemberGroupMembership
def committee_index(request):
......@@ -12,7 +12,7 @@ def committee_index(request):
:param request: the request object
:return: response containing the HTML
"""
committees = Committee.active_committees.all().order_by(
committees = MemberGroup.active_objects.all().order_by(
localize_attr_name('name'))
return render(request, 'activemembers/committee_index.html',
......@@ -27,7 +27,7 @@ def committee_detail(request, pk):
:param pk: pk of the selected committee
:return:
"""
committee = get_object_or_404(Committee, pk=pk)
committee = get_object_or_404(MemberGroup, pk=pk)
members = []
memberships = (MemberGroupMembership
......
......@@ -11,7 +11,7 @@ from django.utils.html import format_html
from django.utils.http import is_safe_url
from django.utils.translation import ugettext_lazy as _
from activemembers.models import Committee
from activemembers.models import MemberGroup
from events import services
from members.models import Member
from pizzas.models import PizzaEvent
......@@ -209,8 +209,8 @@ class EventAdmin(DoNextModelAdmin):
# Hide old boards and inactive committees for new events
if 'add' in request.path:
kwargs['queryset'] = (
Committee.active_committees.all() |
Committee.unfiltered_objects
MemberGroup.active_objects.all() |
MemberGroup.unfiltered_objects
.filter(board=None)
.exclude(until__lt=(timezone.now() -
timezone.timedelta(weeks=1)))
......
......@@ -45,7 +45,7 @@ class Event(models.Model, metaclass=ModelTranslateMeta):
end = models.DateTimeField(_("end time"))
organiser = models.ForeignKey(
'activemembers.Committee',
'activemembers.MemberGroup',
models.PROTECT,
verbose_name=_("organiser")
)
......
......@@ -4,7 +4,7 @@ from django.db import models
from django.utils import timezone
from django.utils.translation import ugettext_lazy as _
from activemembers.models import Committee
from activemembers.models import MemberGroup
from members.models import Member
......@@ -48,7 +48,7 @@ class MailingList(models.Model):
)
committees = models.ManyToManyField(
Committee,
MemberGroup,
verbose_name=_("Committees"),
help_text=_('Select entire committees to include in the list.'),
blank=True,
......
......@@ -17,7 +17,7 @@ from functools import reduce
from localflavor.generic.countries.sepa import IBAN_SEPA_COUNTRIES
from localflavor.generic.models import IBANField
from activemembers.models import Committee, MemberGroupMembership
from activemembers.models import MemberGroup, MemberGroupMembership
logger = logging.getLogger(__name__)
......@@ -177,7 +177,7 @@ class Member(User):
def get_committees(self):
"""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__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