# -*- coding: utf-8 -*-
# Generated by Django 1.11.1 on 2019-02-14 16:46
from __future__ import unicode_literals

from django.db import migrations, models
from datetime import datetime

def set_default_for_new_column(apps, schema_editor):
    Post = apps.get_model('dashboard', 'Post')
    Comment = apps.get_model('dashboard', 'Comment')
    # get newest comment update
    # get post modified date
    # find newest, assign it
    
    for post in Post.objects.all():
        newest_date = post.date_modified
        for comment in post.comments.all():
            if(comment.date_modified > newest_date):
                newest_date = comment.date_modified

        Post.objects.filter(pk=post.id).update(newest_update=newest_date)


class Migration(migrations.Migration):

    dependencies = [
        ('dashboard', '0057_post_newest_update'),
    ]

    operations = [
        migrations.AlterField(
            model_name='post',
            name='newest_update',
            field=models.DateTimeField(default=datetime(2000, 1, 1)),
        ),

        migrations.RunPython(set_default_for_new_column),

        migrations.AlterField(
            model_name='post',
            name='newest_update',
            field=models.DateTimeField(auto_now=True),
        ),

    ]

