diff --git a/AuthApp/js_urls.py b/AuthApp/js_urls.py index 5d3ceb9..df4a165 100644 --- a/AuthApp/js_urls.py +++ b/AuthApp/js_urls.py @@ -28,6 +28,8 @@ urlpatterns = [ path('change_profile_confirm/', change_profile_confirm_ajax, name='change_profile_confirm_ajax'), path('change_avatar_confirm/', change_avatar_confirm_ajax, name='change_avatar_confirm_ajax'), - path('send_message/', send_message_ajax, name='send_message_ajax') + path('send_message/', send_message_ajax, name='send_message_ajax'), + + path('mailing_subscribe/', mailing_subscribe_ajax, name='mailing_subscribe_ajax') ] \ No newline at end of file diff --git a/AuthApp/js_views.py b/AuthApp/js_views.py index 351368b..88fa544 100644 --- a/AuthApp/js_views.py +++ b/AuthApp/js_views.py @@ -17,6 +17,7 @@ import json from django.core.files import File import base64 from django.core.validators import validate_email +from django.urls import reverse # @login_required(login_url='/profile/login/') @@ -31,6 +32,48 @@ from django.core.validators import validate_email # return JsonResponse({'html': html}, status=200) +def mailing_subscribe_ajax(request): + if request.method != 'POST': + raise Http404 + + try: + + email = request.POST['email'] + user = None + if request.user and request.user.is_authenticated(): + user = request.user + user.user_profile.mailing_on = True + user.user_profile.save(update_fields=['mailing_on']) + + return JsonResponse({ + 'status': 'sended', + 'del_form': True, + 'html': _('Подписка на рассылку для адреса ') + user.email + _(' одобрена') + }) + + if not user: + try: + user = User.objects.get(email=email) + except User.DoesNotExist: + user = None + + if user: + redirect_url = f"{reverse('login_profile')}?mailing_for_email={email}" + else: + redirect_url = f"{reverse('registration_page')}?mailing_for_email={email}" + + return JsonResponse({ + 'status': 'sended', + 'redirect_url': redirect_url + }) + + + except Exception as e: + return JsonResponse({ + 'status': 'error', + 'html': str(e) + }, status=400) + def send_message_ajax(request): if request.method != 'POST': diff --git a/AuthApp/migrations/0005_userprofile_mailing_on.py b/AuthApp/migrations/0005_userprofile_mailing_on.py new file mode 100644 index 0000000..73b19f4 --- /dev/null +++ b/AuthApp/migrations/0005_userprofile_mailing_on.py @@ -0,0 +1,18 @@ +# Generated by Django 4.2.2 on 2024-02-01 17:17 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('AuthApp', '0004_alter_userprofile_account_type'), + ] + + operations = [ + migrations.AddField( + model_name='userprofile', + name='mailing_on', + field=models.BooleanField(default=False, verbose_name='Рассылка'), + ), + ] diff --git a/AuthApp/models.py b/AuthApp/models.py index d8fdac9..3ed08e8 100644 --- a/AuthApp/models.py +++ b/AuthApp/models.py @@ -51,6 +51,8 @@ class UserProfile(BaseModel): on_delete=models.SET_NULL ) + mailing_on = models.BooleanField(default=False, verbose_name=_('Рассылка')) + def save_user_alerts_to_session(self, request): for_save_to_session = self.get_node_by_name('for_save_to_session') if for_save_to_session: