telegram_bots/feeds/utils.py

75 lines
2.6 KiB
Python
Raw Normal View History

2019-02-08 14:04:37 +00:00
from io import BytesIO
import requests
2019-01-25 16:33:22 +00:00
import yaml
2019-01-11 19:16:01 +00:00
from django.views.generic import TemplateView
2019-01-25 16:33:22 +00:00
from djconfig import config
from jconfig.base import BaseConfig
2019-02-08 14:04:37 +00:00
from python_anticaptcha import AnticaptchaClient, ImageToTextTask
2019-01-25 16:33:22 +00:00
from yaml.parser import ParserError
2019-01-11 19:16:01 +00:00
from cabinet.utils import CabinetViewMixin
2019-11-12 17:13:56 +00:00
from feeds.forms import FeedForm, FeedsAppConfigForm
from config.utils import get_config_form
2019-01-11 19:16:01 +00:00
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
2019-01-25 16:33:22 +00:00
class DatabaseConfig(BaseConfig):
__slots__ = ()
def load(self, **kwargs):
config._reload_maybe()
try:
2022-04-09 21:37:52 +00:00
settings = yaml.load(config.vk_auth, yaml.Loader)
2021-03-11 23:45:10 +00:00
if not settings:
settings = {}
2019-01-25 16:33:22 +00:00
except (ParserError, AttributeError):
settings = {}
settings.setdefault(self.section_name, {})
return settings
def save(self):
2019-01-25 16:39:17 +00:00
form = FeedsAppConfigForm(data={'vk_auth': yaml.dump(self._settings)})
form.is_valid()
form.save()
2019-02-08 14:04:37 +00:00
def captcha_handler(captcha):
client = AnticaptchaClient(config.anticaptcha_token)
job = client.createTask(ImageToTextTask(BytesIO(requests.get(captcha.get_url()).content)))
job.join()
2019-02-08 14:22:17 +00:00
return captcha.try_again(job.get_captcha_text())