<gh_stars>1-10
from datetime import timedelta
from itertools import count
from celery.schedules import crontab
from django_celery_beat.models import CrontabSchedule, PeriodicTask

import factory
from django.utils import timezone
from faker import Factory

from devices.factories import DeviceFactory
from .models import AlarmStatus, AlarmSchedule

faker = Factory.create()

class AlarmStatusFactory(factory.DjangoModelFactory):
    class Meta:
        model = AlarmStatus

    running = True
    device = factory.SubFactory(DeviceFactory)


_ids = count(0)


def _task():
    schedule = crontab(minute='*/10')
    c = CrontabSchedule.from_schedule(schedule)
    c.save()
    return PeriodicTask.objects.create(name='t{0}'.format(next(_ids)), crontab=c)


class AlarmScheduleFactory(factory.DjangoModelFactory):
    class Meta:
        model = AlarmSchedule

    start_time = factory.LazyFunction(timezone.now)
    end_time = factory.LazyAttribute(lambda _: timezone.now() + timedelta(hours=9))

    monday    = factory.Faker('pybool')
    tuesday   = factory.Faker('pybool')
    wednesday = factory.Faker('pybool')
    thursday  = factory.Faker('pybool')
    friday    = factory.Faker('pybool')
    saturday  = factory.Faker('pybool')
    sunday    = factory.Faker('pybool')

    turn_on_task = factory.LazyFunction(_task)
    turn_off_task = factory.LazyFunction(_task)
