telegram_bots/feeds/views.py

77 lines
2.6 KiB
Python

from django.contrib import messages
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.contenttypes.models import ContentType
from django.http import HttpResponseRedirect
from django.shortcuts import get_object_or_404, redirect
from django.views import View
from django.views.generic import ListView
from django.views.generic.detail import SingleObjectMixin
from cabinet.utils import CabinetViewMixin
from feeds.models import Feed
from feeds.modules import *
from feeds.tasks import execute_feed
from feeds.utils import BaseFeedConfigView
class FeedListView(CabinetViewMixin, ListView):
template_name = 'cabinet/feeds/feed_list.html'
title = 'Feed list'
sidebar_section = 'feeds'
def get_queryset(self):
return Feed.objects.filter(owner=self.request.user)
def get_context_data(self, *args, **kwargs):
ctx = super(FeedListView, self).get_context_data(*args, **kwargs)
ctx['feed_modules'] = FEED_MODULES
return ctx
class FeedConfigEditView(BaseFeedConfigView, SingleObjectMixin):
title = 'Configure feed'
sidebar_section = 'feeds'
def form_valid(self, feed_form, config_form):
feed = feed_form.save()
config_form.save()
if 'run' in self.request.POST:
execute_feed.apply_async(args=(feed.pk,), shadow=str(feed))
else:
messages.success(self.request, 'Config was successfully saved')
return HttpResponseRedirect('')
def get_content_type(self):
return self.get_object().config_type
class FeedConfigCreateView(BaseFeedConfigView):
title = 'Create feed'
sidebar_section = 'feeds'
def get_object(self):
return None
def form_valid(self, feed_form, config_form):
config_form.save()
feed_form.instance.owner = self.request.user
feed_form.instance.config_type = self.get_content_type()
feed_form.instance.config_id = config_form.instance.pk
feed_form.save()
messages.success(self.request, 'Config was successfully saved')
return redirect('cabinet:feeds:edit', pk=feed_form.instance.pk)
def get_content_type(self):
return get_object_or_404(ContentType, model=self.kwargs['content_type'])
class FeedConfigDeleteView(LoginRequiredMixin, SingleObjectMixin, View):
def get_queryset(self):
return Feed.objects.filter(owner=self.request.user)
def post(self, request, *args, **kwargs):
feed = self.get_object()
messages.success(self.request, 'Feed "{}" was successfully deleted'.format(feed.title))
feed.delete()
return redirect('cabinet:feeds:index')