41 lines
1.8 KiB
Python
41 lines
1.8 KiB
Python
from django.views.generic import TemplateView
|
|
|
|
from bots.forms import BotForm
|
|
from bots.models import TelegramBot
|
|
from cabinet.utils import CabinetViewMixin
|
|
from config.utils import get_config_form
|
|
|
|
|
|
class BaseBotConfigView(CabinetViewMixin, TemplateView):
|
|
template_name = 'cabinet/bots/bot_form.html'
|
|
context_object_name = 'feed'
|
|
|
|
def get_queryset(self):
|
|
return TelegramBot.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()
|
|
bot_form, config_form = self.get_forms()
|
|
if bot_form.is_valid() and config_form.is_valid():
|
|
return self.form_valid(bot_form, config_form)
|
|
else:
|
|
context = self.get_context_data(forms=(bot_form, config_form))
|
|
return self.render_to_response(context)
|
|
|
|
def get_forms(self):
|
|
bot = self.get_object()
|
|
data = self.request.POST if self.request.method == 'POST' else None
|
|
files = self.request.FILES if self.request.method == 'POST' else None
|
|
return BotForm(data=data, files=files, instance=bot, module=self.get_content_type().model_class()), \
|
|
get_config_form(self.get_content_type().model_class())(data=data, files=files,
|
|
instance=bot.config if bot else None)
|
|
|
|
def get_context_data(self, forms=None, **kwargs):
|
|
ctx = super(BaseBotConfigView, self).get_context_data(**kwargs)
|
|
ctx['bot_form'], ctx['config_form'] = self.get_forms() if forms is None else forms
|
|
ctx['bot_module'] = self.get_content_type().model_class()
|
|
return ctx |