Compare commits
5 Commits
main
...
feature/su
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
15c9d589fe | ||
|
|
fa116c08c1 | ||
|
|
4f9129e718 | ||
|
|
df45f3a704 | ||
|
|
90ef093fca |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -414,4 +414,5 @@ fabric.properties
|
|||||||
|
|
||||||
# Android studio 3.1+ serialized cache file
|
# Android studio 3.1+ serialized cache file
|
||||||
.idea/caches/build_file_checksums.ser
|
.idea/caches/build_file_checksums.ser
|
||||||
|
celerybeat-schedule.db
|
||||||
|
|
||||||
|
|||||||
3
README.md
Normal file
3
README.md
Normal file
@@ -0,0 +1,3 @@
|
|||||||
|
celery -A TWB.celery:app worker -l info
|
||||||
|
|
||||||
|
celery -A TWB.celery:app beat -l info
|
||||||
7
SubscribesApp/serializers.py
Normal file
7
SubscribesApp/serializers.py
Normal file
@@ -0,0 +1,7 @@
|
|||||||
|
from rest_framework import serializers
|
||||||
|
|
||||||
|
|
||||||
|
class SubscribersSerializer(serializers.Serializer):
|
||||||
|
email = serializers.EmailField(error_messages={'invalid': 'Invalid email'})
|
||||||
|
email_notification = serializers.BooleanField()
|
||||||
|
auto_subscribe = serializers.BooleanField()
|
||||||
9
SubscribesApp/urls.py
Normal file
9
SubscribesApp/urls.py
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
from django.urls import path
|
||||||
|
|
||||||
|
from SubscribesApp.views import SubscribersView
|
||||||
|
|
||||||
|
urlpatterns = [
|
||||||
|
|
||||||
|
path('auto-subscribe/', SubscribersView.as_view(), name='auto_subscribe'),
|
||||||
|
|
||||||
|
]
|
||||||
@@ -1,3 +1,43 @@
|
|||||||
from django.shortcuts import render
|
from django.contrib.auth.models import User
|
||||||
|
from django.http import JsonResponse
|
||||||
|
from rest_framework import status
|
||||||
|
from rest_framework.permissions import IsAuthenticated
|
||||||
|
from rest_framework.views import APIView
|
||||||
|
|
||||||
# Create your views here.
|
from SubscribesApp.models import SubscribeForUser
|
||||||
|
from SubscribesApp.serializers import SubscribersSerializer
|
||||||
|
|
||||||
|
|
||||||
|
class SubscribersView(APIView):
|
||||||
|
# permission_classes = [IsAuthenticated]
|
||||||
|
|
||||||
|
def post(self, request):
|
||||||
|
serializer = SubscribersSerializer(data=request.data)
|
||||||
|
|
||||||
|
if serializer.is_valid():
|
||||||
|
validated_data = serializer.validated_data
|
||||||
|
email = validated_data['email']
|
||||||
|
email_notification = validated_data['email_notification']
|
||||||
|
auto_subscribe = validated_data['auto_subscribe']
|
||||||
|
|
||||||
|
user = User.objects.filter(email=email)
|
||||||
|
|
||||||
|
if user:
|
||||||
|
|
||||||
|
subscribe_for_user = SubscribeForUser.objects.filter(user_id=user[0].id)
|
||||||
|
|
||||||
|
if email_notification:
|
||||||
|
subscribe_for_user.update(receive_finish_subscribe_msg=True)
|
||||||
|
else:
|
||||||
|
subscribe_for_user.update(receive_finish_subscribe_msg=False)
|
||||||
|
|
||||||
|
if auto_subscribe:
|
||||||
|
subscribe_for_user.update(auto_continue=True)
|
||||||
|
else:
|
||||||
|
subscribe_for_user.update(auto_continue=False)
|
||||||
|
|
||||||
|
return JsonResponse({"message": "Subscriptions updated successfully"}, status=status.HTTP_200_OK)
|
||||||
|
else:
|
||||||
|
return JsonResponse({"message": "User not found"}, status=status.HTTP_404_NOT_FOUND)
|
||||||
|
else:
|
||||||
|
return JsonResponse(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
||||||
27
TWB/celery.py
Executable file
27
TWB/celery.py
Executable file
@@ -0,0 +1,27 @@
|
|||||||
|
from __future__ import absolute_import
|
||||||
|
|
||||||
|
import os
|
||||||
|
|
||||||
|
from celery import Celery
|
||||||
|
from celery.schedules import crontab
|
||||||
|
from django.conf import settings
|
||||||
|
|
||||||
|
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "TWB.settings")
|
||||||
|
|
||||||
|
app = Celery('bo', include=['TWB.tasks'])
|
||||||
|
app.config_from_object('django.conf:settings', namespace='CELERY')
|
||||||
|
app.autodiscover_tasks(lambda: settings.INSTALLED_APPS)
|
||||||
|
|
||||||
|
app.conf.beat_schedule = {
|
||||||
|
'update-currency-rates': {
|
||||||
|
'task': 'TWB.tasks.check_auto_subscribe',
|
||||||
|
'schedule': crontab(minute=0, hour='*/1'),
|
||||||
|
},
|
||||||
|
'subscription_expiration_check': {
|
||||||
|
'task': 'TWB.tasks.subscription_expiration_check',
|
||||||
|
'schedule': crontab(hour=0, minute=0),
|
||||||
|
# 'schedule': crontab(minute='*', hour='*'),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
app.conf.broker_url = settings.CELERY_BROKER_URL
|
||||||
@@ -11,6 +11,7 @@ https://docs.djangoproject.com/en/4.2/ref/settings/
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
from decouple import config
|
||||||
|
|
||||||
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
# Build paths inside the project like this: BASE_DIR / 'subdir'.
|
||||||
BASE_DIR = Path(__file__).resolve().parent.parent
|
BASE_DIR = Path(__file__).resolve().parent.parent
|
||||||
@@ -60,7 +61,6 @@ AUTHENTICATION_BACKENDS = [
|
|||||||
]
|
]
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
SOCIALACCOUNT_PROVIDERS = {
|
SOCIALACCOUNT_PROVIDERS = {
|
||||||
'google': {
|
'google': {
|
||||||
'SCOPE': [
|
'SCOPE': [
|
||||||
@@ -172,11 +172,8 @@ CHANNEL_LAYERS = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
POSTGRES_DB = config('POSTGRES_DB')
|
||||||
|
POSTGRES_USER = config('POSTGRES_USER')
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
# Database
|
# Database
|
||||||
@@ -185,8 +182,8 @@ CHANNEL_LAYERS = {
|
|||||||
DATABASES = {
|
DATABASES = {
|
||||||
'default': {
|
'default': {
|
||||||
'ENGINE': 'django.db.backends.postgresql_psycopg2',
|
'ENGINE': 'django.db.backends.postgresql_psycopg2',
|
||||||
'NAME': 'twbDB',
|
'NAME': POSTGRES_DB,
|
||||||
'USER': 'test_user',
|
'USER': POSTGRES_USER,
|
||||||
'PASSWORD': 'test_db_pass',
|
'PASSWORD': 'test_db_pass',
|
||||||
'HOST': '127.0.0.1',
|
'HOST': '127.0.0.1',
|
||||||
'PORT': '5432',
|
'PORT': '5432',
|
||||||
@@ -336,6 +333,16 @@ CKEDITOR_CONFIGS = {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CELERY_BROKER_URL = config('CELERY_BROKER_URL')
|
||||||
|
CELERY_RESULT_BACKEND = config('CELERY_RESULT_BACKEND')
|
||||||
|
|
||||||
|
EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend'
|
||||||
|
EMAIL_HOST = 'smtp.gmail.com'
|
||||||
|
EMAIL_USE_TLS = True
|
||||||
|
EMAIL_PORT = 587
|
||||||
|
EMAIL_HOST_USER = config('EMAIL_HOST_USER')
|
||||||
|
EMAIL_HOST_PASSWORD = config('EMAIL_HOST_PASSWORD')
|
||||||
|
|
||||||
|
|
||||||
# CKEDITOR_OPTIONS = {
|
# CKEDITOR_OPTIONS = {
|
||||||
# 'height': 291,
|
# 'height': 291,
|
||||||
|
|||||||
40
TWB/tasks.py
Normal file
40
TWB/tasks.py
Normal file
@@ -0,0 +1,40 @@
|
|||||||
|
from datetime import datetime, timedelta, timezone
|
||||||
|
|
||||||
|
from SubscribesApp.models import SubscribeForUser
|
||||||
|
from TWB import settings
|
||||||
|
from TWB.celery import app
|
||||||
|
from django.core.mail import send_mail
|
||||||
|
|
||||||
|
|
||||||
|
@app.task
|
||||||
|
def check_auto_subscribe():
|
||||||
|
current_time = datetime.now()
|
||||||
|
subscribes = SubscribeForUser.objects.filter(auto_continue=True)
|
||||||
|
if subscribes:
|
||||||
|
for subscribe in subscribes:
|
||||||
|
if subscribe.paid_period_to_DT and subscribe.paid_period_to_DT <= current_time + timedelta(hours=1):
|
||||||
|
user_email = subscribe.user.email
|
||||||
|
subject = 'Подписка продлена!'
|
||||||
|
message = 'Ваша подписка успешно продлена!'
|
||||||
|
send_mail(subject, message, settings.EMAIL_HOST_USER, [user_email], fail_silently=False)
|
||||||
|
else:
|
||||||
|
print('Нету подписок')
|
||||||
|
|
||||||
|
|
||||||
|
@app.task
|
||||||
|
def subscription_expiration_check():
|
||||||
|
current_time = datetime.now()
|
||||||
|
subscribes = SubscribeForUser.objects.filter(paid_period_to_DT__gte=current_time)
|
||||||
|
if subscribes:
|
||||||
|
for subscribe in subscribes:
|
||||||
|
expiration_date = subscribe.paid_period_to_DT
|
||||||
|
remaining_days = (expiration_date - current_time).days
|
||||||
|
|
||||||
|
if remaining_days <= 7:
|
||||||
|
message = f'Ваша подписка заканчивается через {remaining_days} дня. Пожалуйста, продлите её.'
|
||||||
|
|
||||||
|
user_email = subscribe.user.email
|
||||||
|
subject = 'Подписка истекает!'
|
||||||
|
send_mail(subject, message, settings.EMAIL_HOST_USER, [user_email], fail_silently=False)
|
||||||
|
else:
|
||||||
|
print('Нету подписок')
|
||||||
@@ -1,4 +1,3 @@
|
|||||||
|
|
||||||
from django.contrib import admin
|
from django.contrib import admin
|
||||||
from django.urls import path, include
|
from django.urls import path, include
|
||||||
from django.conf.urls.static import static
|
from django.conf.urls.static import static
|
||||||
@@ -9,7 +8,6 @@ from AuthApp.views import login_View
|
|||||||
handler404 = Page404
|
handler404 = Page404
|
||||||
|
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
# path('admin/', admin.site.urls),
|
|
||||||
path('ckeditor/', include('ckeditor_uploader.urls')),
|
path('ckeditor/', include('ckeditor_uploader.urls')),
|
||||||
path('i18n/', include('django.conf.urls.i18n')),
|
path('i18n/', include('django.conf.urls.i18n')),
|
||||||
|
|
||||||
@@ -39,7 +37,9 @@ urlpatterns = [
|
|||||||
|
|
||||||
path('test_404', Page404, name='page_404'),
|
path('test_404', Page404, name='page_404'),
|
||||||
|
|
||||||
path('', include('PushMessages.urls'))
|
path('', include('PushMessages.urls')),
|
||||||
|
path('', include('SubscribesApp.urls')),
|
||||||
|
|
||||||
]
|
]
|
||||||
|
|
||||||
from django.conf.urls.i18n import i18n_patterns
|
from django.conf.urls.i18n import i18n_patterns
|
||||||
|
|||||||
8
env.sample
Normal file
8
env.sample
Normal file
@@ -0,0 +1,8 @@
|
|||||||
|
EMAIL_HOST_USER=
|
||||||
|
EMAIL_HOST_PASSWORD=
|
||||||
|
|
||||||
|
POSTGRES_DB=
|
||||||
|
POSTGRES_USER=
|
||||||
|
|
||||||
|
CELERY_BROKER_URL=
|
||||||
|
CELERY_RESULT_BACKEND=
|
||||||
@@ -2,7 +2,6 @@ Django==4.2.2
|
|||||||
django-ckeditor==6.5.1
|
django-ckeditor==6.5.1
|
||||||
psycopg2-binary==2.9.6
|
psycopg2-binary==2.9.6
|
||||||
requests
|
requests
|
||||||
Pillow==9.5.0
|
|
||||||
django-modeltranslation==0.18.10
|
django-modeltranslation==0.18.10
|
||||||
overpass
|
overpass
|
||||||
geopy
|
geopy
|
||||||
@@ -13,5 +12,6 @@ django-colorfield
|
|||||||
django-webpush==0.3.5
|
django-webpush==0.3.5
|
||||||
django-allauth==0.60.0
|
django-allauth==0.60.0
|
||||||
pytz==2024.1
|
pytz==2024.1
|
||||||
#django-tz-detect==0.4.0
|
celery==5.3.6
|
||||||
|
djangorestframework==3.14.0
|
||||||
|
python-decouple==3.8
|
||||||
|
|||||||
Reference in New Issue
Block a user