subscribe to digest

This commit is contained in:
j 2025-01-23 14:03:55 +05:30
parent 743276298d
commit bee761e0c4
8 changed files with 95 additions and 6 deletions

View file

@ -57,12 +57,16 @@ def email2name(email):
name = " ".join([part.capitalize() for part in name.split(" ")])
return name
def is_subscribed(email):
url = settings.LISTMONK_API + 'subscribers'
auth = (settings.LISTMONK_USER, settings.LISTMONK_PASSWORD)
exists = url + '?' + "list_id=&query=email='%s'&page=1&order_by=id&order=desc" % email
return bool(len(requests.get(exists, auth=auth).json()['data']['results']))
def add_email(email):
url = settings.LISTMONK_API + 'subscribers'
auth = (settings.LISTMONK_USER, settings.LISTMONK_PASSWORD)
exists = url + '?' + "list_id=&query=email='%s'&page=1&order_by=id&order=desc" % email
if not len(requests.get(exists, auth=auth).json()['data']['results']):
if not is_subscribed(email):
data = {
"email": email,
"name": email2name(email),

View file

@ -1,13 +1,26 @@
import json
from brake.decorators import ratelimit
from django.conf import settings
from django.contrib.auth import get_user_model
from django.db.models.functions import ExtractWeek, ExtractYear
from django.shortcuts import render
from django.shortcuts import redirect, render
from django.template.loader import render_to_string
from django.utils import timezone
from django.utils.timezone import datetime, timedelta
from django.core.validators import validate_email
import django.contrib.auth
from ..item import models
from ..item.views import get_weeks, format_week, get_monday, get_byline
from ..item.utils import render_to_json
from ..utils import default_context
from . import utils
User = get_user_model()
def week(year, month, day):
@ -32,3 +45,40 @@ def week(year, month, day):
context['week_link'] = settings.URL + '/_' + monday
context['items'] = archive_week
return render_to_string("weekly_email.html", context)
@ratelimit(method="POST", block=True, rate="5/m")
def subscribe(request):
context = default_context(request)
response = {}
request_type = 'json'
if request.method == "POST":
if "email" in request.POST:
data = request.POST
request_type = 'html'
else:
data = json.loads(request.body)
if not response:
try:
validate_email(data["email"])
except ValidationError as e:
response['error'] = 'please enter a valid email address.'
else:
try:
if utils.is_subscribed(data["email"]):
pass
elif not utils.add_email(data["email"]):
response['error'] = 'failed to add email to weekly digest.'
except:
response['error'] = 'failed to add your email, please try again later.'
if request_type == 'html':
if 'error' in response:
context['error'] = response['error']
return render(request, 'subscribe.html', context)
else:
context["subscribed"] = True
return render(request, 'subscribe.html', context)
return render_to_json(response)
else:
return render(request, 'subscribe.html', context)

View file

@ -250,7 +250,7 @@ nav.overlay {
}
.login, .register {
.login, .register, .subscribe, .digest{
margin-top: 16px;
margin-left: 24px;
textarea,
@ -276,6 +276,9 @@ nav.overlay {
border: solid 1px lightgreen;
cursor: pointer;
}
.error {
color: red;
}
}
.content {

View file

@ -39,6 +39,9 @@
</header>
<div class="about">
{{ overlay.content | safe }}
<div class="digest">
{% include "subscribe_form.html" %}
</div>
</div>
{% if now %}
<div class="now">

View file

@ -1,6 +1,6 @@
{% extends "base.html" %}
{% block head %}
<title>Login - {{ settings.SITENAME }}</title>
<title>Register - {{ settings.SITENAME }}</title>
{% endblock %}
{% block content %}
<div class="register">

View file

@ -0,0 +1,21 @@
{% extends "base.html" %}
{% block head %}
<title>Subscribe - {{ settings.SITENAME }}</title>
{% endblock %}
{% block content %}
<div class="subscribe">
{% if subscribed %}
Thank you for subscribing to our weekly email digest.
<script>
setTimeout(function() {
document.location.href = '/'
}, 3000)
</script>
{% else %}
You can subscribe to a weekly email digest with all clips, edits, sirens and phantoms published in the week.
{% include "subscribe_form.html" %}
{% endif %}
</div>
{% endblock %}

View file

@ -0,0 +1,6 @@
<form method="POST" action="{% url 'subscribe' %}">
{% csrf_token %}
<input name="email" type="email" placeholder="your email" required></input>
<button id="subscribe">Subscribe to weekly digest</button>
<div class="error">{{ error }}</div>
</form>

View file

@ -20,6 +20,7 @@ from django.urls import path, re_path
from .item import views as item_views
from .user import views as user_views
from .page import views as page_views
from .listmonk import views as email_views
from . import views
urlpatterns = [
@ -31,6 +32,7 @@ urlpatterns = [
path('login/', user_views.login, name='login'),
path('logout/', user_views.logout, name='logout'),
path('register/', user_views.register, name='register'),
path('subscribe/', email_views.subscribe, name='subscribe'),
path('comment/publish/', item_views.publish_comment, name='publish-comment'),
path('comment/', item_views.comment, name='comment'),
path('_<int:year>-<int:month>-<int:day>/', item_views.archive, name='archive'),