Updated newsletter subscription system

This commit is contained in:
Zakary Timson 2018-01-14 16:48:55 -05:00
parent 00fcc9725c
commit 667a470f41
3 changed files with 34 additions and 58 deletions

View File

@ -37,7 +37,6 @@ urlpatterns = [
url(r'^logout/', main.views.logout, name='logout'), url(r'^logout/', main.views.logout, name='logout'),
url(r'^media/secure/(?P<path>.*)$', protected_serve, {'document_root': os.path.join(settings.MEDIA_ROOT, 'secure')}, name='secure media'), url(r'^media/secure/(?P<path>.*)$', protected_serve, {'document_root': os.path.join(settings.MEDIA_ROOT, 'secure')}, name='secure media'),
url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}, name='media'), url(r'^media/(?P<path>.*)$', serve, {'document_root': settings.MEDIA_ROOT}, name='media'),
url(r'^newsletter/subscribe', newsletters.views.subscribe, name='subscribe'),
url(r'^newsletter/', newsletters.views.newsletters, name='newsletters'), url(r'^newsletter/', newsletters.views.newsletters, name='newsletters'),
url(r'^tinymce/', include('tinymce.urls')) url(r'^tinymce/', include('tinymce.urls'))
] ]

View File

@ -15,16 +15,20 @@
<div class="container-fluid bg-dark-primary"> <div class="container-fluid bg-dark-primary">
<div class="container py-3"> <div class="container py-3">
{% if request.user.is_anonymous %} {% if request.user.is_anonymous %}
<button class="btn btn-secondary mx-auto d-block mb-3" data-toggle="modal" data-target="#newsletter-modal">Subscribe To The Newsletter</button> <button class="btn btn-secondary mx-auto d-block mb-3" data-toggle="modal"
data-target="#subscribeModal">Subscribe To The Newsletter
</button>
{% endif %} {% endif %}
{% for newsletter in newsletters %} {% for newsletter in newsletters %}
<div class="col-12 p-0 mb-3 bg-white newsletter"> <div class="col-12 p-0 mb-3 bg-white newsletter">
<div class="p-3 newsletter-header" data-toggle="collapse" data-target="#newsletter-body-{{ newsletter.id }}" aria-expanded="false" aria-controls="newsletter-body-{{ newsletter.id }}"> <div class="p-3 newsletter-header" data-toggle="collapse"
data-target="#newsletter-body-{{ newsletter.id }}" aria-expanded="false"
aria-controls="newsletter-body-{{ newsletter.id }}">
<h2>{{ newsletter.subject }}</h2> <h2>{{ newsletter.subject }}</h2>
<span class="text-muted">By: {{ newsletter.creator }}</span> <span class="text-muted">By: {{ newsletter.creator }}</span>
<span class="text-muted float-right">{{ newsletter.publish }}</span> <span class="text-muted float-right">{{ newsletter.publish }}</span>
</div> </div>
<div id="newsletter-body-{{ newsletter.id }}" class="bg-light-blue collapse"> <div id="newsletter-body-{{ newsletter.id }}" class="bg-light-blue collapse">
<div class="p-3" style="overflow:hidden;"> <div class="p-3" style="overflow:hidden;">
{{ newsletter.body | safe }} {{ newsletter.body | safe }}
</div> </div>
@ -34,55 +38,34 @@
</div> </div>
</div> </div>
<div id="newsletter-modal" class="modal fade"> <div id="subscribeModal" class="modal fade" tabindex="-1" role="dialog">
<div class="modal-dialog" role="document"> <div class="modal-dialog" role="document">
<div class="modal-content"> <div class="modal-content">
<div class="modal-body"> <div class="modal-header">
<div class="col-12"> <h5 class="modal-title text-dark-primary">Newsletter Sign Up</h5>
<h3 class="modal-title text-dark-primary"><i class="fa fa-envelope-o"></i> Newsletter Sign Up</h3> <button type="button" class="close" data-dismiss="modal" aria-label="Close">
<div class="my-3"> <span aria-hidden="true">&times;</span>
<p> </button>
Want to keep up to date with all our latest news? </div>
<br> <div class="modal-body">
Enter your email below to be added to our mailing list. Want to keep up to date with all our latest news?
</p> <br>
<div id="emailInputGroup" class="input-group"> Enter your email below to be added to our mailing list.
<span class="input-group-addon"><i class="fa fa-envelope-o"></i></span> <form id="subscribeForm" method="post">
<input id="email" class="form-control" type="email"> {% csrf_token %}
</div> <div class="input-group pt-2">
</div> <div class="input-group-prepend">
<div class="float-right"> <span class="input-group-text"><i class="fa fa-envelope-o"></i></span>
<button id="subscribe" type="button" class="btn btn-primary"><i class="fa fa-check-circle"></i> Subscribe</button> </div>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button> <input name="email" class="form-control" type="email">
</div> </div>
</div> </form>
</div>
<div class="modal-footer">
<button id="subscribe" type="submit" form="subscribeForm" class="btn btn-primary">Subscribe</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
<script>
$('#subscribe').click(function() {
var email = $('#email').val();
if(email && email.match(/.+@.+\..+/g)) {
var csrfToken = $("[name='csrfmiddlewaretoken']").val();
$.post('/newsletter/subscribe', {csrfmiddlewaretoken: csrfToken, email: email}, function(resp) {
if(resp.success) {
$('#email').val('');
$('#newsletter-modal').modal('hide');
} else {
$('#emailInputGroup').effect('shake')
}
}, 'json');
} else {
$('#emailInputGroup').effect('shake')
}
});
if(!sessionStorage.getItem('newsletter') && {% if request.user.is_anonymous %} true {% else %} false {% endif %}) {
$('#newsletter-modal').modal('show');
sessionStorage.setItem('newsletter', true);
}
</script>
{% endblock %} {% endblock %}

View File

@ -1,20 +1,14 @@
import json import json
from django.db.models import Q from django.db.models import Q
from django.shortcuts import render, HttpResponse from django.shortcuts import render
from django.utils import timezone from django.utils import timezone
from .models import Newsletter, Subscriber from .models import Newsletter, Subscriber
def newsletters(request): def newsletters(request):
if request.method == 'POST':
Subscriber.objects.create(email=request.POST.get('email'))
newsletters = Newsletter.objects.filter(Q(publish__lte=timezone.now())).order_by('-publish') newsletters = Newsletter.objects.filter(Q(publish__lte=timezone.now())).order_by('-publish')
return render(request, 'newsletters.html', {'newsletters': newsletters}) return render(request, 'newsletters.html', {'newsletters': newsletters})
def subscribe(request):
try:
Subscriber.objects.create(email=request.POST.get('email'))
return HttpResponse(json.dumps({'success': True}), content_type='json')
except Exception:
return HttpResponse(json.dumps({'success': False}), content_type='json')