from django.shortcuts import render, redirect
from django.contrib import messages
from visitors_app.models import  auto_email
from visitors_app.context_processors import my_constants
from visitors_app.views import date_time
from visitors_app.check_subscription import check_subscription

@check_subscription
def auto_email_view(request):
    constants = my_constants(request)

    username = request.session.get('admin')
    if not username:
        return redirect('admin')

    user_data = constants.get('admin_data', {})
    email = username['user_email']
    user_name = username['user_name']

    admin_auto_email = auto_email.objects.get(id=1)

    if request.method == "POST":
        admin_auto_email.email = request.POST['email']
        admin_auto_email.updated_at = date_time()
        admin_auto_email.save()
        messages.success(request, "Successfully Updated", extra_tags="success")

    return render(request, 'dashboard/admin_dashboard/admin_auto_email/auto_email.html',
                  {'admin_auto_email': admin_auto_email,'function_name': 'Admin','username':username})
