Migrate mentorships and concatenated memberships

parent aeeecaf7
......@@ -26,7 +26,7 @@ class BoardAdmin(TranslatedModelAdmin):
@admin.register(models.CommitteeMembership)
class CommitteeMembershipAdmin(TranslatedModelAdmin):
pass
list_display = ('member', 'committee', 'since', 'until', 'chair', 'role')
@admin.register(models.Mentorship)
......
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2016-10-05 19:40
from __future__ import unicode_literals
from django.db import migrations
class Migration(migrations.Migration):
dependencies = [
('activemembers', '0009_translations'),
]
operations = [
migrations.RemoveField(
model_name='mentorship',
name='members',
),
migrations.DeleteModel(
name='Mentorship',
),
]
# -*- coding: utf-8 -*-
# Generated by Django 1.10 on 2016-10-05 19:41
from __future__ import unicode_literals
import django.core.validators
from django.db import migrations, models
import django.db.models.deletion
class Migration(migrations.Migration):
dependencies = [
('members', '0010_merge_20160907_2042'),
('activemembers', '0010_auto_20161005_2140'),
]
operations = [
migrations.CreateModel(
name='Mentorship',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('year', models.IntegerField(validators=django.core.validators.MinValueValidator(1990))),
('member', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='members.Member', verbose_name='Member')),
],
),
migrations.AlterUniqueTogether(
name='mentorship',
unique_together=set([('member', 'year')]),
),
]
......@@ -3,6 +3,7 @@ import logging
from django.contrib.auth.models import Permission
from django.core.exceptions import ValidationError, NON_FIELD_ERRORS
from django.core.validators import MinValueValidator
from django.db import models
from django.urls import reverse
from django.utils import timezone
......@@ -211,14 +212,6 @@ class CommitteeMembership(models.Model, metaclass=ModelTranslateMeta):
'member': _('This member is already in the committee for '
'this period')})
def delete(self, *args, **kwargs):
"""Deactivates active memberships, deletes inactive ones"""
if self.is_active:
self.until = timezone.now().date()
self.save()
else:
super().delete(*args, **kwargs)
def __str__(self):
return "{} membership of {} since {}, until {}".format(self.member,
self.committee,
......@@ -231,8 +224,16 @@ class CommitteeMembership(models.Model, metaclass=ModelTranslateMeta):
class Mentorship(models.Model):
members = models.ManyToManyField(Member)
year = models.IntegerField(unique=True)
member = models.ForeignKey(
Member,
on_delete=models.CASCADE,
verbose_name=_('Member'),
)
year = models.IntegerField(validators=MinValueValidator(1990))
def __str__(self):
return _("Mentor introduction {year}").format(year=self.year)
return _("{name} mentor in {year}").format(name=self.member,
year=self.year)
class Meta:
unique_together = ('member', 'year')
......@@ -6,7 +6,8 @@ from django.core.management.base import BaseCommand
from django.contrib.auth.models import User
from django.utils.translation import activate
from activemembers.models import Board, Committee, CommitteeMembership
from activemembers.models import (Board, Committee, CommitteeMembership,
Mentorship)
from members.models import Member
from bs4 import BeautifulSoup
......@@ -33,11 +34,13 @@ class Command(BaseCommand):
data = json.loads(requests.get(url).text)
groups = {}
mentorgroups = {}
board_url = "https://thalia.nu/board/2015-2016"
soup = BeautifulSoup(requests.get(board_url).text, 'lxml')
default_board_photo = ("https://thalia.nu/application/files/"
"6614/3560/3446/site_logo_board.png")
print("Migrating boards..")
for board in data['boards']:
obj, cr = Board.objects.get_or_create(name_nl=board['name'])
obj.name_en = board['name']
......@@ -49,6 +52,10 @@ class Command(BaseCommand):
imagefield_from_url(obj.photo, default_board_photo)
obj.save()
for mentorgroup in data['mentors']:
mentorgroups[mentorgroup['gID']] = int(mentorgroup['name'][-4:])
print("Migrating committees..")
committee_url = "https://thalia.nu/committees"
soup = BeautifulSoup(requests.get(committee_url).text, 'lxml')
anchors = soup.find('ul', {'class': 'row committees'}).find_all('a')
......@@ -71,6 +78,7 @@ class Command(BaseCommand):
for member in data['members']:
user, cr = User.objects.get_or_create(username=member['username'])
print("Migrating {}".format(member['username']))
user.username = member['username']
user.email = member['email']
# Concrete5 uses bcrypt passwords, which django can rehash
......@@ -150,6 +158,11 @@ class Command(BaseCommand):
if not p['begindate'] or p['begindate'][:4] == '0000':
p['begindate'] = '1970-01-01'
if mdata['gID'] in mentorgroups:
m, cr = Mentorship.objects.get_or_create(
year=mentorgroups[mdata['gID']],
member=user.member)
m.save()
if mdata['gID'] not in groups:
continue # These are concrete5 groups (Admin, etc..)
group = groups[mdata['gID']]
......@@ -192,3 +205,45 @@ class Command(BaseCommand):
newmship.save()
else:
newmship.save()
for m in CommitteeMembership.objects.filter(member=user.member):
ms = (CommitteeMembership.objects
.filter(committee_id=m.committee_id,
member_id=m.member_id,
since=m.until,
chair=m.chair,
role_en=m.role_en,
role_nl=m.role_nl,
))
if not ms:
continue
if len(ms) > 1:
raise Exception("Could not merge more than one membership")
m.until = ms[0].until
m.save()
ms[0].delete()
print("Sanitizing board memberships")
for m in CommitteeMembership.objects.all():
try:
if m.committee.board:
m.since = parse_date(
'{}-09-01'.format(m.committee.name_nl[8:12]))
m.until = parse_date(
'{}-09-01'.format(m.committee.name_nl[13:17]))
m.save()
except Board.DoesNotExist:
pass
# remove duplicates to be sure
print("Cleaning up duplicates")
for m in CommitteeMembership.objects.all():
if (CommitteeMembership.objects
.filter(committee_id=m.committee_id,
member_id=m.member_id,
since=m.since,
chair=m.chair,
role_en=m.role_en,
role_nl=m.role_nl)
.count()) > 1:
m.delete()
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