· 5 years ago · Jun 05, 2020, 08:50 AM
1----------------tasks.py
2
3from testbot.celery import app
4from .models import User
5import telebot
6
7TOKEN = ''
8
9bot = telebot.TeleBot(TOKEN)
10
11
12@app.task
13def send_daily_cryptocurrency():
14 all_users = User.objects.all()
15 for user in all_users:
16 if not user.send_daily_prices:
17 bot.send_message(user.user_id, 'У не вас стоит напоминание')
18
19--------------celery.py
20
21from __future__ import absolute_import, unicode_literals
22import os
23from celery import Celery
24from celery.schedules import crontab
25from django.conf import settings
26
27os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'testbot.settings')
28app = Celery('testbot')
29app.config_from_object('django.conf:settings')
30app.conf.timezone = 'UTC'
31app.conf.enable_utc = True
32app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
33
34------------views.py
35import telebot
36from telebot import types
37from collections import defaultdict
38import datetime
39from .models import User
40from . import market
41
42TOKEN = ''
43bot = telebot.TeleBot(TOKEN)
44from .tasks import send_daily_cryptocurrency
45
46send_daily_cryptocurrency.delay()
47
48----------settings.py
49"""
50Django settings for testbot project.
51
52Generated by 'django-admin startproject' using Django 3.0.3.
53
54For more information on this file, see
55https://docs.djangoproject.com/en/3.0/topics/settings/
56
57For the full list of settings and their values, see
58https://docs.djangoproject.com/en/3.0/ref/settings/
59"""
60
61import os
62import redis
63import urllib.parse as urlparse
64from celery.schedules import crontab
65
66# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
67BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
68
69# Quick-start development settings - unsuitable for production
70# See https://docs.djangoproject.com/en/3.0/howto/deployment/checklist/
71
72# SECURITY WARNING: keep the secret key used in production secret!
73
74SECRET_KEY = ''
75
76# SECURITY WARNING: don't run with debug turned on in production!
77DEBUG = True
78
79ALLOWED_HOSTS = ['*']
80
81# Application definition
82
83INSTALLED_APPS = [
84 'django.contrib.admin',
85 'django.contrib.auth',
86 'django.contrib.contenttypes',
87 'django.contrib.sessions',
88 'django.contrib.messages',
89 'django.contrib.staticfiles',
90 'bot',
91 'django_celery_beat',
92]
93MIDDLEWARE = [
94 'django.middleware.security.SecurityMiddleware',
95 'django.contrib.sessions.middleware.SessionMiddleware',
96 'django.middleware.common.CommonMiddleware',
97 'django.middleware.csrf.CsrfViewMiddleware',
98 'django.contrib.auth.middleware.AuthenticationMiddleware',
99 'django.contrib.messages.middleware.MessageMiddleware',
100 'django.middleware.clickjacking.XFrameOptionsMiddleware',
101]
102
103ROOT_URLCONF = 'testbot.urls'
104
105TEMPLATES = [
106 {
107 'BACKEND': 'django.template.backends.django.DjangoTemplates',
108 'DIRS': [],
109 'APP_DIRS': True,
110 'OPTIONS': {
111 'context_processors': [
112 'django.template.context_processors.debug',
113 'django.template.context_processors.request',
114 'django.contrib.auth.context_processors.auth',
115 'django.contrib.messages.context_processors.messages',
116 ],
117 },
118 },
119]
120
121WSGI_APPLICATION = 'testbot.wsgi.application'
122
123# Database
124# https://docs.djangoproject.com/en/3.0/ref/settings/#databases
125
126DATABASES = {
127 'default': {
128 'ENGINE': 'django.db.backends.postgresql_psycopg2',
129 'NAME': 'd8mcfajta6ksr',
130 'USER': 'bbutkkljbayvxw',
131 'PASSWORD': '78352d9a393074248f043e4538c7867e6a860f3957265f2c1836058fb9ce4022',
132 'HOST': 'ec2-54-81-37-115.compute-1.amazonaws.com',
133 'PORT': '5432'
134 }
135}
136
137# Password validation
138# https://docs.djangoproject.com/en/3.0/ref/settings/#auth-password-validators
139
140AUTH_PASSWORD_VALIDATORS = [
141 {
142 'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
143 },
144 {
145 'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
146 },
147 {
148 'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
149 },
150 {
151 'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
152 },
153]
154
155# Internationalization
156# https://docs.djangoproject.com/en/3.0/topics/i18n/
157
158LANGUAGE_CODE = 'en-us'
159
160TIME_ZONE = 'UTC'
161
162USE_I18N = True
163
164USE_L10N = True
165
166USE_TZ = True
167
168# AUTH_USER_MODEL = 'core.User'
169# Static files (CSS, JavaScript, Images)
170# https://docs.djangoproject.com/en/3.0/howto/static-files/
171
172STATIC_URL = '/static/'
173
174# redis settings for celery
175os.environ.setdefault('REDIS_URL',
176 'redis://h:p5067e3205757872a84ea31d841e6cf3ce88f7fcb568d463ff4dc1708d8f8c792@ec2-3-220-244-30.compute-1.amazonaws.com:14059')
177
178r = redis.from_url(os.environ.get('REDIS_URL'))
179# redis_url = urlparse.urlparse(os.environ.get(REDIS_URL))
180
181CACHES = {
182 "default": {
183 "BACKEND": "redis_cache.RedisCache",
184 "LOCATION": os.environ.get('REDIS_URL'),
185 }
186}
187
188BROKER_URL = os.environ['REDIS_URL']
189CELERY_RESULT_BACKEND = os.environ['REDIS_URL']
190CELERY_BROKER_TRANSPORT_OPTIONS={'visibility_timeout': 3600}
191CELERY_TIMEZONE = 'UTC'
192CELERY_ENABLE_UTC = True
193CELERY_ACCEPT_CONTENT = ['json']
194CELERY_TASK_SERIALIZER = 'json'
195CELERY_RESULT_SERIALIZER = 'json'
196CELERY_BEAT_SCHEDULER = 'django_celery_beat.schedulers:DatabaseScheduler'
197CELERY_BEAT_SCHEDULE = {
198 'send-daily-crypto': {
199 'task': 'bot.tasks.send_daily_cryptocurrency',
200 'schedule': crontab(minute='*/2'),
201 },
202}