Commit 1732593e authored by Joost Rijneveld's avatar Joost Rijneveld
Browse files

Merge branch 'remove_migrations' into 'master'

Remove old migrations

Closes #266

See merge request !296
parents b4c518a3 cb09e0c2
......@@ -29,8 +29,6 @@ RUN apk add --no-cache \
zlib \
freetype \
lcms2 \
libxml2 \
libxslt \
libffi \
ghostscript \
libjpeg-turbo
......@@ -44,8 +42,6 @@ RUN apk add --no-cache --virtual .builddeps \
freetype-dev \
lcms2-dev \
libwebp-dev \
libxml2-dev \
libxslt-dev \
libffi-dev \
linux-headers \
git \
......@@ -59,12 +55,10 @@ WORKDIR /usr/src/app
# install python requirements
COPY requirements.txt /usr/src/app/
COPY production-requirements.txt /usr/src/app/
COPY migration-requirements.txt /usr/src/app/
COPY dev-requirements.txt /usr/src/app/
RUN pip install --no-cache-dir \
-r requirements.txt \
-r production-requirements.txt \
-r migration-requirements.txt \
-r dev-requirements.txt
RUN apk del .builddeps
......
# Data migrations
The old new website of Study Association Thalia contained a lot of data.
Before we can launch the new website, all old data has to be migrated
to the new website.
The migration instructions for each part of the website are shown below.
## Active members
This is taken care of during member migration.
## Documents
Make sure you have valid login credentials for thalia.nu.
To migrate all documents execute `python manage.py migratedocuments`
and enter the required login information.
## Education
Make sure that the API key is available as `MIGRATION_KEY` in settings.py.
To migrate the events, make sure that membershave been migrated.
Then simply execute `python manage.py migrateeducation`.
## Events
Make sure that the API key is available as `MIGRATION_KEY` in settings.py.
To migrate the events, make sure that members, committees and boards
have been migrated. Then simply execute `python manage.py migrateevents`.
## Mailing lists
Make sure that the API key is available as `MIGRATION_KEY` in settings.py.
To migrate the mailinglists, make sure that members, committees and boards
have been migrated. Then simply execute `python manage.py migratelists`.
## Members
To migrate members, committees, boards, memberships of committees and boards,
as well as introductionmentorships, execute `python manage.py migratemembers`.
Unfortunately, not all the data is complete. Most notably, some begin-dates are
missing. This will need to be fixed manually. These have been set to 1970,
as start dates are not optional.
This migration typically takes a few minutes.
## Newsletters
Make sure that the API key is available as `MIGRATION_KEY` in settings.py.
To migrate the newsletters simply execute `python manage.py migratenewsletters`.
## Partners
To migrate partners, partner events, vacancy categories and vacancies,
execute `python manage.py migratepartners`.
## Photos
Make sure the album directories exist somewhere on the server. Then,
for each album, perform `python manage.py importphotos <album dir>`.
This needs to be done per album to avoid having to store two copies of all
albums at the same time. Consider doing several albums at once by using
your shell to loop over a part of the set (e.g. divide them over directories).
## Pizzas
## Thabloids
Simply execute `python manage.py migratethabloids` to migrate all Thabloids.
## Merchandise
Make sure you have valid login credentials for thalia.nu.
To migrate the merchandise execute `python manage.py migratemerchandise` and
enter the required login information.
## Wiki
Copy `/data` folder to new installation.
import os
import requests
from bs4 import BeautifulSoup
from django.core.files.base import ContentFile
from django.utils import timezone
from django.utils.timezone import datetime
from documents.models import (AssociationDocumentsYear, GeneralMeeting,
GeneralMeetingDocument, MiscellaneousDocument)
from utils.management.commands import legacylogin
def filefield_from_url(filefield, url):
file = ContentFile(requests.get(url).content)
filefield.save(os.path.basename(url), file)
class Command(legacylogin.Command):
help = "Scrapes the documents from the old Thalia website"
def handle(self, *args, **options):
super().handle(*args, **options)
print("Migrating the general meetings")
url = "https://thalia.nu/ajax/alvyearview?year={}"
for year in range(1990, 2017):
print("Migrating {}".format(year))
src = self.session.get(url.format(year)).text
soup = BeautifulSoup(src, 'lxml')
for alv in soup.div.find_all('div', recursive=False):
meeting = GeneralMeeting()
datetext = alv.find(attrs={'class': 'gw-go-coinb'}).text
date = datetime.strptime(datetext.strip() + ' ' + str(year),
'%d %b %Y')
if date.month < 9:
date = datetime(year+1, date.month, date.day)
date = timezone.make_aware(date,
timezone.get_current_timezone())
meeting.datetime = date
meeting.location = alv.find('p').text
meeting.save()
minutes = alv.find('div', {'class': 'gw-go-footer'}).find('a')
if minutes is not None:
minutes_url = 'https://thalia.nu' + minutes['href']
filefield_from_url(meeting.minutes, minutes_url)
for document in alv.find_all('li'):
doc_url = 'https://thalia.nu' + document.find('a')['href']
doc = GeneralMeetingDocument()
doc.meeting = meeting
filefield_from_url(doc.file, doc_url)
doc.save()
print("Migrating general meetings complete")
print("Migrating the policy documents")
url = "https://thalia.nu/association/documents"
documentpage = self.session.get(url)
soup = BeautifulSoup(documentpage.text, 'lxml')
wrapper = soup(attrs={'class': 'policywrapper'})[0]
uls = wrapper.find_all('ul', recursive=False)
policies = uls[0].find_all('li', recursive=False)
reports = uls[1].find_all('li', recursive=False)
for policy, report in zip(policies, reports):
year = policy.find('h2').find(text=True)
print("Migrating {}".format(year))
year = int(year.replace("Beleidsplan '", '')[:2])
year += (19 if year >= 90 else 20) * 100
obj, cr = AssociationDocumentsYear.objects.get_or_create(year=year)
obj.year = year
files = [(obj.policy_document,
policy.find(attrs={'class': 'overlay-icon-link'})),
(obj.annual_report,
report.find(attrs={'class': 'overlay-icon-link'})),
(obj.financial_report,
report.find(attrs={'class': 'overlay-icon-euro'}))]
for filefield, url in files:
if url is not None:
url = "https://thalia.nu" + url['href']
file = ContentFile(requests.get(url).content)
# File names are ignored when serving files anyway
filefield.save(os.path.basename(url), file)
print("Migrating policy documents complete")
print("Migrating the miscellaneous documents")
container = soup(attrs={'class': 'generalcontainer'})[0]
documents = container.find_all('li', recursive=False)
for document in documents:
name = document.find('h2').find(text=True)
print("Migrating {}".format(name))
obj, cr = MiscellaneousDocument.objects.get_or_create(name=name)
url = document.find(attrs={'class': 'overlay-icon-link'})
if url is not None:
url = "https://thalia.nu" + url['href']
filefield_from_url(obj.file, url)
print("Migrating miscellaneous documents complete")
import json
import os
import requests
from django.conf import settings
from django.contrib.auth.models import User
from django.core.exceptions import ImproperlyConfigured, PermissionDenied
from django.core.files.base import ContentFile
from django.utils.dateparse import parse_date
from django.utils.translation import activate
from education.models import Category, Course, Exam, Summary
from utils.management.commands import legacylogin
def filefield_from_url(filefield, url):
file = ContentFile(requests.get(url).content)
filefield.save(os.path.basename(url), file)
class Command(legacylogin.Command):
help = 'Scrapes the education data from the old Thalia website'
def handle(self, *args, **options):
activate('en')
if not settings.MIGRATION_KEY:
raise ImproperlyConfigured("MIGRATION_KEY not specified")
url = "https://thalia.nu/index.php/onderwijs/api?apikey={}".format(
settings.MIGRATION_KEY
)
input_val = input(
'Do you want to delete all existing objects? (type yes or no) ')
if input_val == 'yes':
Summary.objects.all().delete()
Exam.objects.all().delete()
Course.objects.all().delete()
Category.objects.all().delete()
session = requests.Session()
src = session.get(url).text
if 'invalid api key' in src:
raise PermissionDenied('Invalid API key')
data = json.loads(src)
category_map = {}
print('Importing categories')
for key in data['categories']:
name = data['categories'][key]
id = Category()
id.name_nl = name
id.name_en = name
id.save()
category_map[key] = id.pk
print('Importing categories complete')
course_map = {}
print('Importing courses')
for key in data['courses']:
src = data['courses'][key]
course = Course()
course.name_nl = src['name']
course.name_en = src['name']
course.course_code = src['course_code']
course.shorthand_nl = src['course_shorthand']
course.shorthand_en = src['course_shorthand']
course.ec = int(src['e_c_t_s'])
course.period = src['quarter'].replace(' en ', ' & ')
course.since = src['since']
if int(src['until']) != 0:
course.until = src['until']
course.save()
for id in src['categories']:
course.categories.add(Category.objects
.get(pk=category_map[str(id)]))
course_map[key] = course.pk
print('Combining courses with predecessors')
for key in data['courses']:
src = data['courses'][key]
course = Course.objects.get(pk=course_map[key])
try:
for id in src['predecessors']:
if id == 0:
continue
old_course = Course.objects.get(pk=course_map[str(id)])
course.old_courses.add(old_course)
except KeyError:
pass
course.save()
print('Importing courses complete')
print('Importing summaries')
for key in data['summaries']:
src = data['summaries'][key]
summary = Summary()
summary.name = src['name']
summary.author = '' if src['author'] is None else src['author']
summary.year = int(src['year'])
summary.uploader_date = parse_date(src['uploader_date'])
summary.accepted = src['accepted'] == '1'
course_id = str(src['course_id'])
summary.course = Course.objects.get(pk=course_map[course_id])
try:
summary.uploader = User.objects.get(username=src['uploader'])
except User.DoesNotExist:
summary.uploader = User.objects.get(pk=1)
filefield_from_url(summary.file, src['file_url'])
summary.save()
print('Importing summaries complete')
print('Importing exams')
for key in data['exams']:
src = data['exams'][key]
exam = Exam()
exam.name = '' if src['name'] is None else src['name']
exam.accepted = src['accepted'] == '1'
course_id = str(src['course_id'])
exam.course = Course.objects.get(pk=course_map[course_id])
type_map = {
0: 'document',
1: 'exam',
2: 'partial',
3: 'resit',
5: 'practice'
}
exam.type = type_map.get(int(src['type']), 'document')
exam.exam_date = parse_date(src['date'])
exam.uploader_date = parse_date(src['uploader_date'])
try:
exam.uploader = User.objects.get(username=src['uploader'])
except User.DoesNotExist:
exam.uploader = User.objects.get(pk=1)
filefield_from_url(exam.file, src['file_url'])
exam.save()
print('Importing exams complete')
import json
import re
from datetime import datetime
import requests
from django.conf import settings
from django.contrib.auth.models import User
from django.core.exceptions import ImproperlyConfigured
from django.core.management.base import BaseCommand
from django.utils import timezone
import events.models as events_models
import members.models as members_models
FIELD_DATA_TYPES = {
'0': events_models.RegistrationInformationField.TEXT_FIELD,
'1': events_models.RegistrationInformationField.INTEGER_FIELD,
'2': events_models.RegistrationInformationField.BOOLEAN_FIELD,
}
def naive_to_aware(date_string):
"""Convert string of form '%Y-%m-%d %H:%M:%S'
to timezone aware datetime object"""
naive_datetime = datetime.strptime(date_string, '%Y-%m-%d %H:%M:%S')
return timezone.get_current_timezone().localize(naive_datetime)
class Command(BaseCommand):
help = 'Migrate the events from the old website.'
def handle(self, *args, **options):
if not settings.MIGRATION_KEY:
raise ImproperlyConfigured('MIGRATION_KEY not specified')
events_api_url = 'https://thalia.nu/events/api/?apikey={}'.format(
settings.MIGRATION_KEY)
print('[*]Getting events json data')
try:
response = requests.get(events_api_url,
headers={'User-Agent': 'The Donald'})
except requests.RequestException:
print('[!]Could not get {}'.format(events_api_url))
return
try:
data = response.json()
except json.decoder.JSONDecodeError:
print('[!]No json data found')
return
event_fields_translations = {
# name in data : name in model
'title': 'title',
'description': 'description',
'location': 'location',
'start_date': 'start',
'end_date': 'end',
'member_price': 'price',
'thalia_costs': 'cost',
'begin_registration': 'registration_start',
'end_registration': 'registration_end',
'end_cancel': 'cancel_deadline',
'registration_not_needed_message': 'no_registration_message',
}
activity_map = {}
registration_map = {}
information_field_map = {}
print('[*]Parsing event data.')
# Event
for event_data in data['events']:
new_event = events_models.Event(
published=bool(int(event_data['public'])),
max_participants=int(event_data['registration_limit']),
)
for concrete_field in event_fields_translations:
django_field = event_fields_translations[concrete_field]
concrete_data = event_data[concrete_field]
# MultilingualField
if django_field in (
'title', 'description', 'location',
'no_registration_message'):
for language_code in ('en', 'nl'):
django_multilingualfield = '{}_{}'.format(
django_field, language_code)
if not hasattr(new_event, django_multilingualfield):
print('[!]Could neither find {} nor {}'.format(
django_field, django_multilingualfield))
return
if django_field == 'title':
django_field = django_field[:100]
setattr(new_event, django_multilingualfield,
concrete_data)
# DateTimeField
elif concrete_data and django_field in (
'start', 'end', 'registration_start',
'registration_end',
'cancel_deadline'):
setattr(new_event, django_field,
naive_to_aware(concrete_data))
# DecimalField
elif django_field in ('price', 'cost'):
if re.match(r'[-+]?\d*\.?\d+$', concrete_data):
setattr(new_event, django_field, float(concrete_data))
else:
# TODO: is 0 the right value?
setattr(new_event, django_field, 0)
new_event.save()
activity_map[event_data['id']] = new_event.pk
print('[*]Parsing registration field data.')
# RegistrationInformationField
for field_data in data['extra_fields']:
new_registration_information_field = \
events_models.RegistrationInformationField(
# TODO: UGLY AF
name_en=field_data['field_name'][:100],
name_nl=field_data['field_name'][:100],
description_en=field_data['field_explanation'],
description_nl=field_data['field_explanation'],
type=FIELD_DATA_TYPES[field_data['data_type']],
required=True,
event=events_models.Event.objects.get(
pk=activity_map[field_data['activity_id']]
),
)
new_registration_information_field.save()
information_field_map[field_data['field_id']] = \
new_registration_information_field.pk
print('[*]Parsing registration data.')
# Registration
for registration_data in data['registrations']:
new_registration = events_models.Registration(
name=registration_data['name'],
date=naive_to_aware(registration_data['date']),
paid=bool(registration_data['paid']),
event=events_models.Event.objects.get(
pk=activity_map[registration_data['activity_id']]
),
)
username = registration_data['username']
if registration_data['username'] and User.objects.filter(
username=username).exists():
registration_user = User.objects.get(username=username)
new_registration.member = members_models.Member.objects.get(
user=registration_user)
cancelled_date = registration_data['canceled']
if cancelled_date:
new_registration.date_cancelled = naive_to_aware(
cancelled_date)
new_registration.save()
registration_map[registration_data['id']] = new_registration.pk
print('[*]Parsing registration field info data.')
# fields info
for field_info_data in data['extra_info']:
registration_field = events_models.RegistrationInformationField.\
objects.get(
pk=information_field_map[field_info_data['field_id']]
)
parameters = {
'registration': events_models.Registration.objects.get(
pk=registration_map[field_info_data['registration_id']]),
'field': registration_field,
}
if registration_field.type == events_models.\
RegistrationInformationField.TEXT_FIELD:
new_registration_information = events_models. \
TextRegistrationInformation(
value=field_info_data['value'],
**parameters
)
elif registration_field.type == events_models.\
RegistrationInformationField.BOOLEAN_FIELD:
value = False
if value and bool(int(field_info_data['value'])):
value = True
new_registration_information = \
events_models.BooleanRegistrationInformation(
value=value,
**parameters)
# registration_field.type == INTEGER_FIELD:
else:
new_registration_information = \
events_models.IntegerRegistrationInformation(
value=field_info_data['value'] or 0,
**parameters)
new_registration_information.save()