# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2017-10-02 19:38
from __future__ import unicode_literals

import logging

from django.db import migrations

from osf.models import OSFUser
from osf.models import NotificationSubscription
from website.notifications.utils import to_subscription_key

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def add_reviews_notification_setting(*args, **kwargs):
    active_users = OSFUser.objects.filter(date_confirmed__isnull=False).exclude(date_disabled__isnull=False).exclude(is_active=False).order_by('id')
    total_active_users = active_users.count()
    reviews_notification = 'global_reviews'

    logger.info('About to add a global_reviews setting for {} users.'.format(total_active_users))

    total_created = 0
    for user in active_users.iterator():
        user_subscription_id = to_subscription_key(user._id, reviews_notification)

        subscription = NotificationSubscription.load(user_subscription_id)
        if not subscription:
            logger.info('No {} subscription found for user {}. Subscribing...'.format(reviews_notification, user._id))
            subscription = NotificationSubscription(_id=user_subscription_id, owner=user, event_name=reviews_notification)
            subscription.save()  # Need to save in order to access m2m fields
            subscription.add_user_to_subscription(user, 'email_transactional')
        else:
            logger.info('User {} already has a {} subscription'.format(user._id, reviews_notification))
        total_created += 1

    logger.info('Added subscriptions for {}/{} users'.format(total_created, total_active_users))


class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0060_reviews'),
    ]

    operations = [
        migrations.RunPython(add_reviews_notification_setting),
    ]

# -*- coding: utf-8 -*-
# Generated by Django 1.11.4 on 2017-10-02 19:38
from __future__ import unicode_literals

import logging

from django.db import migrations

from osf.models import OSFUser
from osf.models import NotificationSubscription
from website.notifications.utils import to_subscription_key

logger = logging.getLogger(__name__)
logging.basicConfig(level=logging.INFO)


def add_reviews_notification_setting(*args, **kwargs):
    active_users = OSFUser.objects.filter(date_confirmed__isnull=False).exclude(date_disabled__isnull=False).exclude(is_active=False).order_by('id')
    total_active_users = active_users.count()
    reviews_notification = 'global_reviews'

    logger.info('About to add a global_reviews setting for {} users.'.format(total_active_users))

    total_created = 0
    for user in active_users.iterator():
        user_subscription_id = to_subscription_key(user._id, reviews_notification)

        subscription = NotificationSubscription.load(user_subscription_id)
        if not subscription:
            logger.info('No {} subscription found for user {}. Subscribing...'.format(reviews_notification, user._id))
            subscription = NotificationSubscription(_id=user_subscription_id, owner=user, event_name=reviews_notification)
            subscription.save()  # Need to save in order to access m2m fields
            subscription.add_user_to_subscription(user, 'email_transactional')
        else:
            logger.info('User {} already has a {} subscription'.format(user._id, reviews_notification))
        total_created += 1

    logger.info('Added subscriptions for {}/{} users'.format(total_created, total_active_users))


class Migration(migrations.Migration):

    dependencies = [
        ('osf', '0060_reviews'),
    ]

    operations = [
        migrations.RunPython(add_reviews_notification_setting),
    ]

# -*- coding: utf-8 -*- # Generated by Django 1.11.4 on 2017-10-02 19:38 # Need to save in order to access m2m fields