telegram_bots/feeds/views.py

71 lines
2.3 KiB
Python
Raw Normal View History

2019-01-11 19:16:01 +00:00
from django.contrib import messages
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, FEED_MODULES
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_form.save()
config_form.save()
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(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')