Commit 604e93df authored by Joren Vrancken's avatar Joren Vrancken Committed by Sébastiaan Versteeg
Browse files

Some bugs left

parent 03fe4cf7
import os
import sys
import re
import json
import requests
import events.models as events_models
import pytz
from django.contrib.auth.models import User
from datetime import datetime
from django.core.management.base import BaseCommand
import events.models as events_models
import members.models as members_models
FIELD_DATA_TYPES = {
'0': 'charfield',
'1': 'intfield',
'2': 'checkbox',
}
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')
# TODO: I'm 50% sure this is not the right way
return pytz.timezone('Europe/Amsterdam').localize(naive_datetime)
class Command(BaseCommand):
help = 'Make the events database great.'
......@@ -15,13 +32,13 @@ class Command(BaseCommand):
def handle(self, *args, **options):
api_key = 'fcd7ad95a6ae9d70bc844e0f3dcd9b6518c7dd78'
events_api_url = 'https://thalia.nu/events/api/?apikey={}'.format(api_key)
events_api_url = 'https://thalia.nu/events/api/?apikey={}'.format(
api_key)
print('[*]Getting events json data')
try:
response = requests.get(events_api_url,
timeout=2,
headers={'User-Agent': 'The Donald'})
except requests.RequestException:
......@@ -37,7 +54,6 @@ class Command(BaseCommand):
event_fields_translations = {
# name in data : name in model
'id': 'id',
'title': 'title',
'description': 'description',
'location': 'location',
......@@ -56,10 +72,20 @@ class Command(BaseCommand):
# Not in data: map_location, organiser
# In data but not Model: needs_registration
print('[*]Removing all test data')
events_models.Event.objects.all().delete()
events_models.RegistrationInformationField.objects.all().delete()
events_models.Registration.objects.all().delete()
print('[*]Parsing API data.')
print('[*]Parsing event data.')
# Event
new_events = []
for event_data in data['events']:
new_event = events_models.Event()
# TODO: Is this the right way?
new_event = events_models.Event(pk=event_data['id'])
for concrete_field in event_fields_translations:
......@@ -68,19 +94,28 @@ class Command(BaseCommand):
concrete_data = event_data[concrete_field]
# MultilingualField
if django_field in ('title', 'description', 'location', 'no_registration_message'):
if django_field in (
'title', 'description', 'location',
'no_registration_message'):
for language_code in ('en', 'nl'):
django_multilangiualfield = '{}_{}'.format(django_field, language_code)
django_multilangiualfield = '{}_{}'.format(
django_field, language_code)
if not hasattr(new_event, django_multilangiualfield):
print('[!]Could neither find {} nor {}'.format(django_field, django_multilangiualfield))
print('[!]Could neither find {} nor {}'.format(
django_field, django_multilangiualfield))
return
setattr(new_event, django_multilangiualfield, concrete_data)
setattr(new_event, django_multilangiualfield,
concrete_data)
# DateTimeField
elif django_field in ('start', 'end', 'begin_registration', 'end_registration', 'end_cancel'):
setattr(new_event, django_field, datetime.strptime(concrete_data, '%Y-%m-%d %H:%M:%S'))
elif django_field in (
'start', 'end', 'begin_registration',
'end_registration',
'end_cancel'):
setattr(new_event, django_field,
naive_to_aware(concrete_data))
# DecimalField
elif django_field in ('price', 'cost'):
......@@ -88,6 +123,7 @@ class Command(BaseCommand):
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)
# PositiveSmallIntegerField
......@@ -97,28 +133,89 @@ class Command(BaseCommand):
elif django_field == 'published':
setattr(new_event, django_field, bool(int(concrete_data)))
# TODO: new_event.save()
new_events.append(new_event)
print('Creating the events')
events_models.Event.objects.bulk_create(new_events)
print('[*]Parsing registration field data.')
# RegistrationInformationField
# TODO: RegistrationInformationField and MultiLingualField
new_registration_information_fields = []
for field_data in data['extra_fields']:
new_registration_information_field = events_models.RegistrationInformationField(
pk=int(field_data['field_id']),
name=field_data['field_name'],
description=field_data['field_explanation'])
new_registration_information_field = events_models.RegistrationInformationField()
new_registration_information_field.event = events_models.Event.objects.get(
pk=int(field_data['activity_id']))
new_registration_information_field.type = FIELD_DATA_TYPES[
field_data['data_type']]
event_id = field_data['activity_id']
new_registration_information_fields.append(
new_registration_information_field)
new_registration_information_field.event = events_models.Event().get(id=event_id)
print('Creating the registration information fields')
events_models.RegistrationInformationField.objects.bulk_create(
new_registration_information_fields)
data_types = {
'0': 'charfield',
'1': 'intfield',
'2': 'checkbox',
}
new_registration_information_field.type = data_types[field_data['data_type']]
print('[*]Parsing registration data.')
# Registration
new_registrations = []
for registration_data in data['registrations']:
new_registration_information_field.name = field_data['field_name']
new_registration_information_field.description = field_data['field_explanation']
new_registration = events_models.Registration()
event_id = int(registration_data['activity_id'])
new_registration.event = events_models.Event.objects.get(
pk=event_id)
# Registration
# for event_data in data['registrations']
username = registration_data['username']
if registration_data['username'] and not User.objects.filter(
username=username).exists():
# TODO: Seems like there is an easier way to do this
registration_user = User.objects.get(username=username)
new_registration.member = members_models.Member.objects.get(
user=registration_user)
new_registration.name = registration_data['name']
new_registration.date = naive_to_aware(registration_data['date'])
cancelled_date = registration_data['canceled']
if cancelled_date:
new_registration.cancelled_date = naive_to_aware(
cancelled_date)
new_registration.paid = bool(registration_data['paid'])
new_registrations.append(new_registration)
print('Creating the registrations')
events_models.Registration.objects.bulk_create(new_registrations)
print('[*]Parsing registration field info data.')
# fields info
for field_info_data in data['extra_info']:
registration_field = events_models.RegistrationInformationField.\
objects.get(pk=int(field_info_data['field_id']))
parameters = {
'registration': events_models.Registration.objects.get(
pk=int(field_info_data['registration_id'])),
'field': registration_field,
'value': field_info_data['value'],
}
if registration_field.type == 'charfield':
new_registration_information = \
events_models.textregistrationinformation(**parameters)
elif registration_field.type == 'checkbox':
new_registration_information = \
events_models.booleanregistrationinformation(**parameters)
else:
new_registration_information = \
events_models.IntegerRegistrationInformation(**parameters)
new_registration_information.save()
Supports Markdown
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