from django.contrib import messages
from django.shortcuts import render, redirect
from web.models import MailingList, SiteSection, LinkTree


def index(request):
    synopsis = SiteSection.objects.filter(section="Synopsis").first()

    context = {
        "synopsis": synopsis.content
    }

    return render(request, 'index.html', context=context)


def album(request):
    return render(request, 'album.html')


def socials(request):
    links = LinkTree.objects.filter(active=True).order_by("order").all()
    context = {
        "links": links
    }
    return render(request, 'socials.html', context=context)


def mailing_list(request):
    if request.method == 'POST':
        referer = request.META['HTTP_REFERER'][:-1].split('/')[-1].split('.')[0]
        email = request.POST.get('email')

        if email:
            exists = MailingList.objects.filter(email=email).first()
            if not exists:
                m = MailingList(
                    email=email
                )
                m.save()
                messages.add_message(request, messages.INFO, "Nice! Thanks for joining our mailing list!")
    return redirect('index')
