diff --git a/apps/user/views.py b/apps/user/views.py index 3cc75cc3..85414c27 100644 --- a/apps/user/views.py +++ b/apps/user/views.py @@ -10,6 +10,7 @@ from django.conf import settings from django.contrib.auth import login from django.core.exceptions import ValidationError from django.shortcuts import render, reverse, redirect +from django.views import View from django.views.generic import DetailView, UpdateView, TemplateView, FormView from django.contrib import messages from django.contrib.auth import get_user_model @@ -25,10 +26,9 @@ from apps.course.models import Course from apps.notification.utils import send_email from apps.school.models import SchoolSchedule from apps.payment.models import AuthorBalance, CoursePayment, SchoolPayment -from apps.user.models import SubscriptionCategory +from apps.user.models import AuthorRequest, EmailSubscription, SubscriptionCategory from .forms import AuthorRequesForm, UserEditForm, WithdrawalForm -from .models import AuthorRequest User = get_user_model() @@ -87,6 +87,27 @@ class UserView(DetailView): return context +class SubscribeView(View): + + def post(self, request, pk=None, **kwargs): + if request.user.is_authenticated: + messages.info(request, 'Вы уже подписаны на рассылки.') + return redirect(request.get_full_path()) + email = request.POST.get('email', None) + if email: + email_subscription = EmailSubscription.objects.create( + email=email, + ) + email_subscription.categories.set( + SubscriptionCategory.objects.filter(auto_add=True) + ) + messages.info(request, 'Вы подписаны на новости.') + return redirect(request.get_full_path()) + else: + messages.error(request, 'Введите адрес электронной почты.') + return redirect(request.get_full_path()) + + @method_decorator(login_required, name='dispatch') class NotificationEditView(TemplateView): template_name = 'user/notification-settings.html' diff --git a/project/urls.py b/project/urls.py index 57d948cd..b7f3dde3 100644 --- a/project/urls.py +++ b/project/urls.py @@ -28,6 +28,7 @@ from apps.user.views import ( AuthorRequestView, UserView, UserEditView, NotificationEditView, PaymentHistoryView, resend_email_verify, + SubscribeView, ) from apps.payment.views import ( CourseBuySuccessView, CourseBuyView, @@ -66,6 +67,7 @@ urlpatterns = [ path('user//notifications', NotificationEditView.as_view(), name='user-edit-notifications'), path('user//payments', PaymentHistoryView.as_view(), name='user-edit-payments'), path('user/resend-email-verify', resend_email_verify, name='resend-email-verify'), + path('subscribe', SubscribeView.as_view(), name='subscribe'), path('privacy', TemplateView.as_view(template_name='templates/lilcity/privacy_policy.html'), name='privacy'), path('terms', TemplateView.as_view(template_name='templates/lilcity/terms.html'), name='terms'), path('refund-policy', TemplateView.as_view(template_name='templates/lilcity/refund_policy.html'), name='refund_policy'),