# Path: api/processors.py
# def events_pending_for_report():
#     return Event.objects.filter(
#             reported_at=None,
#             status='APPROVED',
#             start_date__lte=datetime.datetime.now(),
#             start_date__gte=datetime.datetime(2015, 1, 1, 0, 0 ,0))
# 
# Path: api/processors.py
# def events_pending_for_report_for(creator):
#     return events_pending_for_report().filter(creator=creator)
# 
# Path: api/models/users.py
# class UserProfile(models.Model):
#     class Meta:
# class SocialAccountList(models.Model):
#     class Meta:
#     def __unicode__(self):
#     def is_ambassador(self):
# def get_user_profile(self):
# def email_with_name(self):
# def full_name(self):
#     def __unicode__(self):
# 
# Path: mailer/event_report_mailer.py
# def send_reminder_for_event_report_and_certificate(
#         user,
#         unreported_events_count,
#         previous_emails_count=0,
#         max_emails_count=None,
#         test_mode=False
#     ):
#     template = loader.get_template("mailer/reminder_for_event_report_and_certificate.txt")
#     context = Context({
#         'name': user.full_name(),
#         'email': user.email,
#         'previous_emails_count': previous_emails_count,
#         'total_emails_count': previous_emails_count + 1,
#         'max_emails_count': max_emails_count,
#         'unreported_events_count': unreported_events_count,
#     })
# 
#     content   = template.render(context)
#     subject   = '[CodeWeekEU] your feedback and your certificate of recognition'
#     sender    = settings.EVENT_REPORT_REMINDERS_FROM_EMAIL
#     recipient = user.email_with_name()
# 
#     if test_mode:
#         print("------------------------------------------------------------")
#         print(u"To: " + recipient)
#         print(u"From: " + sender)
#         print(u"Subject: " + subject)
#         print(u"\n" + content)
#         print("------------------------------------------------------------")
#     else:
#         send_mail(subject, content, sender, [recipient])


from datetime import datetime, timedelta
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from django.db.models import Q
from api.processors import events_pending_for_report
from api.processors import events_pending_for_report_for
from api.models.users import User
from mailer.event_report_mailer import send_reminder_for_event_report_and_certificate
            default=21,
            help="If we're allowed to send more than one reminder email, notifications will be sent each X days."
        ),
    )

    def handle(self, *args, **options):
        last_reminder_sent_before = datetime.now() - timedelta(days=options['notifications_interval_in_days'])

        if options['emails_per_run'] == None:
            self.stderr.write(
                "Please specify the emails to send per run as a positional argument. E.g.:\n\n" +
                "manage.py remind_organizers_to_report_events --emails-per-run 100"
            )
            exit(1)

        events_to_report = events_pending_for_report().filter(
                Q(last_report_notification_sent_at=None) |
                Q(last_report_notification_sent_at__lte=last_reminder_sent_before)
            ).filter(
                report_notifications_count__lt=options['notifications_limit']
            ).order_by(
                'report_notifications_count', 'start_date'
            )

        organizer_ids = events_to_report.distinct().values_list('creator_id', flat=True)

        # The values above may not be unique as we're using ordering. See here for more info:
        # https://docs.djangoproject.com/en/1.6/ref/models/querysets/#django.db.models.query.QuerySet.distinct
        organizer_ids = list(set(organizer_ids))


# Path: api/processors.py
# def events_pending_for_report():
#     return Event.objects.filter(
#             reported_at=None,
#             status='APPROVED',
#             start_date__lte=datetime.datetime.now(),
#             start_date__gte=datetime.datetime(2015, 1, 1, 0, 0 ,0))
# 
# Path: api/processors.py
# def events_pending_for_report_for(creator):
#     return events_pending_for_report().filter(creator=creator)
# 
# Path: api/models/users.py
# class UserProfile(models.Model):
#     class Meta:
# class SocialAccountList(models.Model):
#     class Meta:
#     def __unicode__(self):
#     def is_ambassador(self):
# def get_user_profile(self):
# def email_with_name(self):
# def full_name(self):
#     def __unicode__(self):
# 
# Path: mailer/event_report_mailer.py
# def send_reminder_for_event_report_and_certificate(
#         user,
#         unreported_events_count,
#         previous_emails_count=0,
#         max_emails_count=None,
#         test_mode=False
#     ):
#     template = loader.get_template("mailer/reminder_for_event_report_and_certificate.txt")
#     context = Context({
#         'name': user.full_name(),
#         'email': user.email,
#         'previous_emails_count': previous_emails_count,
#         'total_emails_count': previous_emails_count + 1,
#         'max_emails_count': max_emails_count,
#         'unreported_events_count': unreported_events_count,
#     })
# 
#     content   = template.render(context)
#     subject   = '[CodeWeekEU] your feedback and your certificate of recognition'
#     sender    = settings.EVENT_REPORT_REMINDERS_FROM_EMAIL
#     recipient = user.email_with_name()
# 
#     if test_mode:
#         print("------------------------------------------------------------")
#         print(u"To: " + recipient)
#         print(u"From: " + sender)
#         print(u"Subject: " + subject)
#         print(u"\n" + content)
#         print("------------------------------------------------------------")
#     else:
#         send_mail(subject, content, sender, [recipient])


# Path: api/management/commands/remind_organizers_to_report_events.py
from datetime import datetime, timedelta
from optparse import make_option
from django.core.management.base import BaseCommand, CommandError
from django.db.models import Q
from api.processors import events_pending_for_report
from api.processors import events_pending_for_report_for
from api.models.users import User
from mailer.event_report_mailer import send_reminder_for_event_report_and_certificate

            default=21,
            help="If we're allowed to send more than one reminder email, notifications will be sent each X days."
        ),
    )

    def handle(self, *args, **options):
        last_reminder_sent_before = datetime.now() - timedelta(days=options['notifications_interval_in_days'])

        if options['emails_per_run'] == None:
            self.stderr.write(
                "Please specify the emails to send per run as a positional argument. E.g.:\n\n" +
                "manage.py remind_organizers_to_report_events --emails-per-run 100"
            )
            exit(1)

        events_to_report = events_pending_for_report().filter(
                Q(last_report_notification_sent_at=None) |
                Q(last_report_notification_sent_at__lte=last_reminder_sent_before)
            ).filter(
                report_notifications_count__lt=options['notifications_limit']
            ).order_by(
                'report_notifications_count', 'start_date'
            )

        organizer_ids = events_to_report.distinct().values_list('creator_id', flat=True)

        # The values above may not be unique as we're using ordering. See here for more info:
        # https://docs.djangoproject.com/en/1.6/ref/models/querysets/#django.db.models.query.QuerySet.distinct
        organizer_ids = list(set(organizer_ids))


        organizers = User.objects.filter(id__in=organizer_ids)