# -*- coding: utf-8 -*-
# Generated by Django 1.10.4 on 2017-06-05 19:15
from __future__ import unicode_literals

import json

from django.core import serializers
from django.db import migrations


def save_log_entry_data(apps, schema_editor):
    from django.contrib.auth import get_user_model
    log_model = apps.get_model('admin', 'LogEntry')
    custom_log_model = apps.get_model('logentry', 'CustomLogEntry')
    User = get_user_model()
    logs = log_model.objects.all()
    for log in logs:
        user = User.objects.get(id=log.user.id)
        user = user.get_full_name() if user.get_full_name() else user.get_username()
        data = serializers.serialize("json", [log])
        data = json.loads(data)[0]['fields']
        data['user'] = user
        custom_log_model.objects.create(log=log, log_dict=json.dumps(data))


class Migration(migrations.Migration):
    dependencies = [
        ('logentry', '0001_initial'),
    ]

    operations = [
        migrations.RunPython(save_log_entry_data)
    ]
