implement bots
This commit is contained in:
39
bots/utils.py
Normal file
39
bots/utils.py
Normal file
@@ -0,0 +1,39 @@
|
||||
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
|
||||
return BotForm(data=data, instance=bot, module=self.get_content_type().model_class()), \
|
||||
get_config_form(self.get_content_type().model_class())(data=data, 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
|
Reference in New Issue
Block a user