telegram_bots/feeds/utils.py

39 lines
1.5 KiB
Python
Raw Normal View History

2019-01-11 19:16:01 +00:00
from django.views.generic import TemplateView
from cabinet.utils import CabinetViewMixin
from feeds.forms import FeedForm, get_config_form
from feeds.models import Feed
class BaseFeedConfigView(CabinetViewMixin, TemplateView):
template_name = 'cabinet/feeds/feed_form.html'
context_object_name = 'feed'
def get_queryset(self):
return Feed.objects.filter(owner=self.request.user)
def get(self, request, *args, **kwargs):
self.object = self.get_object()
return self.render_to_response(self.get_context_data())
def post(self, request, *args, **kwargs):
self.object = self.get_object()
feed_form, config_form = self.get_forms()
if feed_form.is_valid() and config_form.is_valid():
return self.form_valid(feed_form, config_form)
else:
context = self.get_context_data(forms=(feed_form, config_form))
return self.render_to_response(context)
def get_forms(self):
feed = self.get_object()
data = self.request.POST if self.request.method == 'POST' else None
return FeedForm(data=data, instance=feed), \
get_config_form(self.get_content_type().model_class())(data=data, instance=feed.config if feed else None)
def get_context_data(self, forms=None, **kwargs):
ctx = super(BaseFeedConfigView, self).get_context_data(**kwargs)
ctx['feed_form'], ctx['config_form'] = self.get_forms() if forms is None else forms
ctx['feed_module'] = self.get_content_type().model_class()
return ctx