Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ----------------tasks.py
- from testbot.celery import app
- from .models import User
- import telebot
- TOKEN = ''
- bot = telebot.TeleBot(TOKEN)
- @app.task
- def send_daily_cryptocurrency():
- all_users = User.objects.all()
- for user in all_users:
- if not user.send_daily_prices:
- bot.send_message(user.user_id, 'У не вас стоит напоминание')
- --------------celery.py
- from __future__ import absolute_import, unicode_literals
- import os
- from celery import Celery
- from celery.schedules import crontab
- from django.conf import settings
- os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'testbot.settings')
- app = Celery('testbot')
- app.config_from_object('django.conf:settings')
- app.conf.timezone = 'UTC'
- app.conf.enable_utc = True
- app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
- ------------views.py
- import telebot
- from telebot import types
- from collections import defaultdict
- import datetime
- from .models import User
- from . import market
- TOKEN = ''
- bot = telebot.TeleBot(TOKEN)
- from .tasks import send_daily_cryptocurrency
- send_daily_cryptocurrency.delay()
- ----------settings.py
- """
- Django settings for testbot project.
- Generated by 'django-admin startproject' using Django 3.0.3.
- For more information on this file, see
- https://docs.djangoproject.com/en/3.0/topics/settings/
- For the full list of settings and their values, see
- https://docs.djangoproject.com/en/3.0/ref/settings/
- """
- import os
- import redis
- import urllib.parse as urlparse
- from celery.schedules import crontab
- # Build paths inside the project like this: os.path.join(BASE_DIR, ...)
- BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
- # Quick-start development settings - unsuitable for production
- # See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
- # SECURITY WARNING: keep the secret key used in production secret!
- SECRET_KEY = ''
- # SECURITY WARNING: don't run with debug turned on in production!
- DEBUG = True
- ALLOWED_HOSTS = ['*']
- # Application definition
- INSTALLED_APPS = [
- 'django.contrib.admin',
- 'django.contrib.auth',
- 'django.contrib.contenttypes',
- 'django.contrib.sessions',
- 'django.contrib.messages',
- 'django.contrib.staticfiles',
- 'bot',
- 'django_celery_beat',
- ]
- MIDDLEWARE = [
- 'django.middleware.security.SecurityMiddleware',
- 'django.contrib.sessions.middleware.SessionMiddleware',
- 'django.middleware.common.CommonMiddleware',
- 'django.middleware.csrf.CsrfViewMiddleware',
- 'django.contrib.auth.middleware.AuthenticationMiddleware',
- 'django.contrib.messages.middleware.MessageMiddleware',
- 'django.middleware.clickjacking.XFrameOptionsMiddleware',
- ]
- ROOT_URLCONF = 'testbot.urls'
- TEMPLATES = [
- {
- 'BACKEND': 'django.template.backends.django.DjangoTemplates',
- 'DIRS': [],
- 'APP_DIRS': True,
- 'OPTIONS': {
- 'context_processors': [
- 'django.template.context_processors.debug',
- 'django.template.context_processors.request',
- 'django.contrib.auth.context_processors.auth',
- 'django.contrib.messages.context_processors.messages',
- ],
- },
- },
- ]
- WSGI_APPLICATION = 'testbot.wsgi.application'
- # Database
- # https://docs.djangoproject.com/en/3.0/ref/settings/#databases
- DATABASES = {
- 'default': {
- 'ENGINE': 'django.db.backends.postgresql_psycopg2',
- 'NAME': 'd8mcfajta6ksr',
- 'USER': 'bbutkkljbayvxw',
- 'PASSWORD': '78352d9a393074248f043e4538c7867e6a860f3957265f2c1836058fb9ce4022',
- 'HOST': 'ec2-54-81-37-115.compute-1.amazonaws.com',
- 'PORT': '5432'
- }
- }
- # Password validation
- # https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
- AUTH_PASSWORD_VALIDATORS = [
- {
- 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
- },
- {
- 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
- },
- {
- 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
- },
- {
- 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
- },
- ]
- # Internationalization
- # https://docs.djangoproject.com/en/3.0/topics/i18n/
- LANGUAGE_CODE = 'en-us'
- TIME_ZONE = 'UTC'
- USE_I18N = True
- USE_L10N = True
- USE_TZ = True
- # AUTH_USER_MODEL = 'core.User'
- # Static files (CSS, JavaScript, Images)
- # https://docs.djangoproject.com/en/3.0/howto/static-files/
- STATIC_URL = '/static/'
- # redis settings for celery
- os.environ.setdefault('REDIS_URL',
- 'redis://h:p5067e3205757872a84ea31d841e6cf3ce88f7fcb568d463ff4dc1708d8f8c792@ec2-3-220-244-30.compute-1.amazonaws.com:14059')
- r = redis.from_url(os.environ.get('REDIS_URL'))
- # redis_url = urlparse.urlparse(os.environ.get(REDIS_URL))
- CACHES = {
- "default": {
- "BACKEND": "redis_cache.RedisCache",
- "LOCATION": os.environ.get('REDIS_URL'),
- }
- }
- BROKER_URL = os.environ['REDIS_URL']
- CELERY_RESULT_BACKEND = os.environ['REDIS_URL']
- CELERY_BROKER_TRANSPORT_OPTIONS={'visibility_timeout': 3600}
- CELERY_TIMEZONE = 'UTC'
- CELERY_ENABLE_UTC = True
- CELERY_ACCEPT_CONTENT = ['json']
- CELERY_TASK_SERIALIZER = 'json'
- CELERY_RESULT_SERIALIZER = 'json'
- CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'
- CELERY_BEAT_SCHEDULE = {
- 'send-daily-crypto': {
- 'task': 'bot.tasks.send_daily_cryptocurrency',
- 'schedule': crontab(minute='*/2'),
- },
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement