from django.views.generic.edit import CreateView, UpdateView

from django_celery_beat.models import IntervalSchedule

from newsreader.news.collection.choices import RuleTypeChoices
from newsreader.news.collection.forms import TwitterTimelineForm
from newsreader.news.collection.views.base import (
    CollectionRuleDetailMixin,
    CollectionRuleViewMixin,
    TaskCreationMixin,
)


class TwitterTimelineCreateView(
    CollectionRuleViewMixin, CollectionRuleDetailMixin, TaskCreationMixin, CreateView
):
    form_class = TwitterTimelineForm
    template_name = "news/collection/views/twitter/timeline-create.html"
    task_interval = (10, IntervalSchedule.MINUTES)
    task_name = "timeline"
    task_type = "TwitterTimelineTask"


class TwitterTimelineUpdateView(
    CollectionRuleViewMixin, CollectionRuleDetailMixin, UpdateView
):
    form_class = TwitterTimelineForm
    template_name = "news/collection/views/twitter/timeline-update.html"
    context_object_name = "timeline"

    def get_queryset(self):
        queryset = super().get_queryset()
        return queryset.filter(type=RuleTypeChoices.twitter_timeline)
