# Path: remo/base/tasks.py
# @app.task
# def send_remo_mail(subject, recipients_list, sender=None,
#                    message=None, email_template=None, data=None,
#                    headers=None):
#     """Send email from /sender/ to /recipients_list/ with /subject/ and
#     /message/ as body.
# 
#     """
#     # Make sure that there is either a message or a template
#     if not data:
#         data = {}
#     # If there is no body for the email and not a template
#     # to render, then do nothing.
#     if not message and not email_template:
#         return
#     # If there is both an email body, submitted by the user,
#     # and a template to render as email body, then add
#     # user's input to extra_content key
#     elif message and email_template:
#         data.update({'extra_content': message})
#     # Make sure that there is a recipient
#     if not recipients_list:
#         return
#     if not headers:
#         headers = {}
#     data.update({'SITE_URL': settings.SITE_URL,
#                  'FROM_EMAIL': settings.FROM_EMAIL})
# 
#     # Make sure subject is one line.
#     subject = subject.replace('\n', ' ')
# 
#     for recipient in recipients_list:
#         to = ''
#         if (isinstance(recipient, long)
#                 and User.objects.filter(pk=recipient).exists()):
#             user = User.objects.get(pk=recipient)
#             to = '%s <%s>' % (user.get_full_name(), user.email)
#             ctx_data = {'user': user,
#                         'userprofile': user.userprofile}
#             data.update(ctx_data)
#         else:
#             try:
#                 validate_email(recipient)
#                 to = recipient
#             except forms.ValidationError:
#                 return
# 
#         if email_template:
#             message = render_to_string(email_template, data)
# 
#         email_data = {
#             'subject': subject,
#             'body': message,
#             'from_email': settings.FROM_EMAIL,
#             'to': [to],
#         }
# 
#         if sender:
#             # If there is a sender, add a Reply-To header and send a copy to the sender
#             headers.update({'Reply-To': sender})
#             email_data.update({'cc': [sender]})
# 
#         # Add the headers to the mail data
#         email_data.update({'headers': headers})
#         # Send the email
#         EmailMessage(**email_data).send()
# 
# Path: remo/base/utils.py
# def get_date(days=0, weeks=0):
#     """Return a date in UTC timezone, given an offset in days and or weeks.
# 
#     The calculation is based on the current date and the
#     offset can be either positive or negative.
#     """
# 
#     return (timezone.now().date() + datetime.timedelta(days=days, weeks=weeks))
# 
# def number2month(month, full_name=True):
#     """Convert to month name to number."""
#     if full_name:
#         format = '%B'
#     else:
#         format = '%b'
#     return datetime.datetime(year=2000, day=1, month=month).strftime(format)
# 
# Path: remo/celery.py
# RUN_DAILY = 60 * 60 * 24
# RUN_HOURLY = 60 * 60
# class Celery(BaseCelery):
#     def on_configure(self):
# def setup_periodic_tasks(sender, **kwargs):


from datetime import date, datetime, timedelta
from operator import or_
from django.conf import settings
from django.contrib.auth.models import Group, User
from django.contrib.contenttypes.models import ContentType
from django.core.mail import send_mail
from django.db import transaction
from django.template.loader import render_to_string
from django.utils.timezone import make_aware, now
from remo.base.tasks import send_remo_mail
from remo.base.utils import get_date, number2month
from remo.celery import app
from remo.dashboard.models import ActionItem
    from remo.voting.models import Poll
    from remo.voting.models import Poll
    from remo.voting.models import Poll
    from remo.voting.models import Poll, RangePoll, RangePollChoice
import pytz
import waffle
    # avoid circular dependencies

    poll = Poll.objects.get(pk=voting_id)
    data = {'SITE_URL': settings.SITE_URL,
            'FROM_EMAIL': settings.FROM_EMAIL,
            'poll': poll}

    if poll.automated_poll:
        message = render_to_string(email_template, data)
        send_mail(subject, message, settings.FROM_EMAIL, [settings.REPS_REVIEW_ALIAS])
    else:
        user_list = User.objects.filter(groups=poll.valid_groups).exclude(username='remobot')

        for user in user_list:
            ctx_data = {'user': user,
                        'userprofile': user.userprofile}
            ctx_data.update(data)
            message = render_to_string(email_template, ctx_data)
            send_mail(subject, message, settings.FROM_EMAIL, [user.email])


@app.task
def extend_voting_period():
    """Extend voting period by EXTEND_VOTING_PERIOD if there is no
    majority decision.

    """

    # avoid circular dependencies


# Path: remo/base/tasks.py
# @app.task
# def send_remo_mail(subject, recipients_list, sender=None,
#                    message=None, email_template=None, data=None,
#                    headers=None):
#     """Send email from /sender/ to /recipients_list/ with /subject/ and
#     /message/ as body.
# 
#     """
#     # Make sure that there is either a message or a template
#     if not data:
#         data = {}
#     # If there is no body for the email and not a template
#     # to render, then do nothing.
#     if not message and not email_template:
#         return
#     # If there is both an email body, submitted by the user,
#     # and a template to render as email body, then add
#     # user's input to extra_content key
#     elif message and email_template:
#         data.update({'extra_content': message})
#     # Make sure that there is a recipient
#     if not recipients_list:
#         return
#     if not headers:
#         headers = {}
#     data.update({'SITE_URL': settings.SITE_URL,
#                  'FROM_EMAIL': settings.FROM_EMAIL})
# 
#     # Make sure subject is one line.
#     subject = subject.replace('\n', ' ')
# 
#     for recipient in recipients_list:
#         to = ''
#         if (isinstance(recipient, long)
#                 and User.objects.filter(pk=recipient).exists()):
#             user = User.objects.get(pk=recipient)
#             to = '%s <%s>' % (user.get_full_name(), user.email)
#             ctx_data = {'user': user,
#                         'userprofile': user.userprofile}
#             data.update(ctx_data)
#         else:
#             try:
#                 validate_email(recipient)
#                 to = recipient
#             except forms.ValidationError:
#                 return
# 
#         if email_template:
#             message = render_to_string(email_template, data)
# 
#         email_data = {
#             'subject': subject,
#             'body': message,
#             'from_email': settings.FROM_EMAIL,
#             'to': [to],
#         }
# 
#         if sender:
#             # If there is a sender, add a Reply-To header and send a copy to the sender
#             headers.update({'Reply-To': sender})
#             email_data.update({'cc': [sender]})
# 
#         # Add the headers to the mail data
#         email_data.update({'headers': headers})
#         # Send the email
#         EmailMessage(**email_data).send()
# 
# Path: remo/base/utils.py
# def get_date(days=0, weeks=0):
#     """Return a date in UTC timezone, given an offset in days and or weeks.
# 
#     The calculation is based on the current date and the
#     offset can be either positive or negative.
#     """
# 
#     return (timezone.now().date() + datetime.timedelta(days=days, weeks=weeks))
# 
# def number2month(month, full_name=True):
#     """Convert to month name to number."""
#     if full_name:
#         format = '%B'
#     else:
#         format = '%b'
#     return datetime.datetime(year=2000, day=1, month=month).strftime(format)
# 
# Path: remo/celery.py
# RUN_DAILY = 60 * 60 * 24
# RUN_HOURLY = 60 * 60
# class Celery(BaseCelery):
#     def on_configure(self):
# def setup_periodic_tasks(sender, **kwargs):


# Path: remo/voting/tasks.py
from datetime import date, datetime, timedelta
from operator import or_
from django.conf import settings
from django.contrib.auth.models import Group, User
from django.contrib.contenttypes.models import ContentType
from django.core.mail import send_mail
from django.db import transaction
from django.template.loader import render_to_string
from django.utils.timezone import make_aware, now
from remo.base.tasks import send_remo_mail
from remo.base.utils import get_date, number2month
from remo.celery import app
from remo.dashboard.models import ActionItem
    from remo.voting.models import Poll
    from remo.voting.models import Poll
    from remo.voting.models import Poll
    from remo.voting.models import Poll, RangePoll, RangePollChoice
import pytz
import waffle

    # avoid circular dependencies

    poll = Poll.objects.get(pk=voting_id)
    data = {'SITE_URL': settings.SITE_URL,
            'FROM_EMAIL': settings.FROM_EMAIL,
            'poll': poll}

    if poll.automated_poll:
        message = render_to_string(email_template, data)
        send_mail(subject, message, settings.FROM_EMAIL, [settings.REPS_REVIEW_ALIAS])
    else:
        user_list = User.objects.filter(groups=poll.valid_groups).exclude(username='remobot')

        for user in user_list:
            ctx_data = {'user': user,
                        'userprofile': user.userprofile}
            ctx_data.update(data)
            message = render_to_string(email_template, ctx_data)
            send_mail(subject, message, settings.FROM_EMAIL, [user.email])


@app.task
def extend_voting_period():
    """Extend voting period by EXTEND_VOTING_PERIOD if there is no
    majority decision.

    """

    # avoid circular dependencies


    tomorrow = get_date(days=1)