Skip to content

Commit

Permalink
Add newsletter signup example
Browse files Browse the repository at this point in the history
  • Loading branch information
hidalgopl committed Apr 25, 2021
1 parent 3426f23 commit 31cad50
Show file tree
Hide file tree
Showing 6 changed files with 99 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import core.views.book_search
import core.views.chat
import core.views.calendar
import core.views.newsletter_signup


urlpatterns = [
Expand All @@ -17,4 +18,5 @@
path('example/', core.views.example.example, name='example'),
path('chat/', core.views.chat.chat, name='chat'),
path('calendar/', core.views.calendar.calendar, name="calendar"),
path('newsletter-signup', core.views.newsletter_signup.view, name='newsletter-signup')
] + staticfiles_urlpatterns()
22 changes: 22 additions & 0 deletions core/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.2 on 2021-04-25 17:06

from django.db import migrations, models


class Migration(migrations.Migration):

initial = True

dependencies = [
]

operations = [
migrations.CreateModel(
name='NewsletterSubscription',
fields=[
('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('email', models.EmailField(max_length=254)),
('name', models.CharField(max_length=256)),
],
),
]
5 changes: 4 additions & 1 deletion core/models.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
from django.db import models

# Create your models here.

class NewsletterSubscription(models.Model):
email = models.EmailField()
name = models.CharField(max_length=256)
15 changes: 15 additions & 0 deletions core/reflexes/newsletter_signup_reflex.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
from sockpuppet.reflex import Reflex

from core.models import NewsletterSubscription


class SubscriptionReflex(Reflex):
def add_person(self):
o = NewsletterSubscription.objects.update_or_create(
name=self.params['name'],
email=self.params['email']
)

def remove_person(self):
id = self.element.dataset['id']
NewsletterSubscription.objects.filter(id=id).delete()
33 changes: 33 additions & 0 deletions core/templates/_newsletter_signup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{% load static %}
<body xmlns="http://www.w3.org/1999/html">


<script src="{% static 'sockpuppet/sockpuppet.js' %}"></script>
<h2>Sign up for newsletter!</h2>
<form id="morph-form">
{{ form }}
<input type="submit" data-reflex="click->Subscription_Reflex#add_person" data-reflex-root="#morph">
<div id="morph">
<h2>Signed:</h2>
<table>
<thead>
<tr>
<th scope="col">Name</th>
<th scope="col">Email</th>
<th scope="col">Remove</th>
</tr>
</thead>
<tbody>
{% for p in people %}
<tr>
<td>{{ p.name }} </td>
<td>{{ p.email }}</td>
<td><a href="#" data-reflex="click->Subscription_Reflex#remove_person"
data-id="{{ p.id }}">Remove</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</form>
</body>
23 changes: 23 additions & 0 deletions core/views/newsletter_signup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from django.forms import ModelForm
from django.views.generic.edit import FormView

from core.models import NewsletterSubscription


class NewsletterSubscriptionForm(ModelForm):
class Meta:
model = NewsletterSubscription
fields = ['name', 'email']


class YourReflexNameView(FormView):
template_name = '_newsletter_signup.html'
form_class = NewsletterSubscriptionForm

def get_context_data(self, *args, **kwargs):
context = super().get_context_data(*args, **kwargs)
context['people'] = NewsletterSubscription.objects.all()
return context


view = YourReflexNameView.as_view()

0 comments on commit 31cad50

Please sign in to comment.