Skip to content

Commit

Permalink
Merge pull request #6 from hidalgopl/add-crud-newsletter-signup-example
Browse files Browse the repository at this point in the history
Add newsletter signup example
  • Loading branch information
zodman authored May 9, 2021
2 parents 3426f23 + 714f518 commit 6885187
Show file tree
Hide file tree
Showing 10 changed files with 175 additions and 4 deletions.
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()
30 changes: 30 additions & 0 deletions core/templates/_newsletter_signup.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{% load static %}


<h2>Sign up for newsletter!</h2>
<form id="morph-form" data-reflex-root="#morph">
{{ form }}
<input type="submit" data-reflex="click->Subscription_Reflex#add_person">
<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>
1 change: 1 addition & 0 deletions core/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ <h1 class="leading-none m-0 pl-1 self-center">
<li><a href="{% url 'example' %}">Example</a></li>
<li><a href="{% url 'book_search' %}">BookSearch</a></li>
<li><a href="{% url 'chat' %}">Chat</a></li>
<li><a href="{% url 'newsletter-signup' %}">Newsletter Signup</a> </li>
</ul>
</nav>
</aside>
Expand Down
12 changes: 10 additions & 2 deletions core/views/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class ExampleMixin(MixinBase):
('core/templates/_example_demo.html', 'html', 'htmldjango'),
)


class ChatMixin(MixinBase):
files = (
('core/views/chat.py', 'python', 'python3'),
Expand All @@ -59,12 +60,19 @@ class ChatMixin(MixinBase):
('core/templates/_chat_demo.html', 'html', 'htmldjango'),
)


class CalendarMixin(MixinBase):
files = (
('core/views/calendar.py', 'python', 'python3'),
# ('core/reflexes/calendar_reflex.py', 'python', 'python3'),
# ('core/javascript/controllers/calendar_controller.js', 'javascript', 'javascript'),
('core/templates/_calendar.html', 'html', 'htmldjango'),
('core/templates/_td_calendar.html', 'html', 'htmldjango'),

)


class NewsletterSignupMixin(MixinBase):
files = (
('core/views/newsletter_signup.py', 'python', 'python3'),
('core/reflexes/newsletter_signup_reflex.py', 'python', 'python3'),
('core/templates/_newsletter_signup.html', 'html', 'htmldjango')
)
25 changes: 25 additions & 0 deletions core/views/newsletter_signup.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from django.forms import ModelForm
from django.views.generic.edit import FormView

from core.models import NewsletterSubscription
from core.views.mixins import NewsletterSignupMixin


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


class NewsletterSignupView(NewsletterSignupMixin, FormView):
demo_template = '_newsletter_signup.html'
form_class = NewsletterSubscriptionForm
subtitle = 'Newsletter Signup'

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


view = NewsletterSignupView.as_view()
2 changes: 1 addition & 1 deletion requirements.in
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ django-pygments-renderer
django-widget-tweaks
django-redis

https://github.com/gilmrjc/django-webpack-loader/archive/webpack-bundle-tracker-1.zip
django-webpack-loader

# deployment
fabric
Expand Down
65 changes: 65 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#
# This file is autogenerated by pip-compile
# To update, run:
#
# pip-compile --output-file=requirements.txt requirements.in
#
aioredis==1.3.1 # via channels-redis
asgiref==3.3.4 # via channels, channels-redis, daphne, django
async-timeout==3.0.1 # via aioredis
attrs==21.2.0 # via automat, service-identity, twisted
autobahn==21.3.1 # via daphne
automat==20.2.0 # via twisted
bcrypt==3.2.0 # via paramiko
beautifulsoup4==4.9.3 # via bs4
bs4==0.0.1 # via django-sockpuppet
certifi==2020.12.5 # via requests
cffi==1.14.5 # via bcrypt, cryptography, pynacl
channels-redis==3.2.0 # via django-sockpuppet
channels==3.0.3 # via channels-redis, django-sockpuppet
chardet==4.0.0 # via requests
constantly==15.1.0 # via twisted
cryptography==3.4.7 # via autobahn, paramiko, pyopenssl, service-identity
daphne==3.0.2 # via channels
django-pygments-renderer==0.0.1 # via -r requirements.in
django-redis==4.12.1 # via -r requirements.in
django-seed==0.2.2 # via -r requirements.in
django-sockpuppet==0.6.0 # via -r requirements.in
django-webpack-loader==0.7.0 # via -r requirements.in
django-widget-tweaks==1.4.8 # via -r requirements.in
django==3.2.2 # via -r requirements.in, channels, django-pygments-renderer, django-redis, django-seed
fabric==2.6.0 # via -r requirements.in, patchwork
faker==8.1.2 # via django-seed
gunicorn==20.1.0 # via -r requirements.in
hiredis==2.0.0 # via aioredis
hyperlink==21.0.0 # via autobahn, twisted
idna==2.10 # via hyperlink, requests, twisted
incremental==21.3.0 # via twisted
invoke==1.5.0 # via fabric
msgpack==1.0.2 # via channels-redis
paramiko==2.7.2 # via fabric
patchwork==1.0.1 # via -r requirements.in
pathlib2==2.3.5 # via fabric
pyasn1-modules==0.2.8 # via service-identity
pyasn1==0.4.8 # via pyasn1-modules, service-identity
pycparser==2.20 # via cffi
pygments==2.9.0 # via django-pygments-renderer
pynacl==1.4.0 # via paramiko
pyopenssl==20.0.1 # via twisted
python-dateutil==2.8.1 # via faker
pytz==2021.1 # via django
redis==3.5.3 # via django-redis
requests==2.25.1 # via -r requirements.in
service-identity==21.1.0 # via twisted
six==1.16.0 # via automat, bcrypt, pathlib2, pynacl, pyopenssl, python-dateutil, service-identity
soupsieve==2.2.1 # via beautifulsoup4
sqlparse==0.4.1 # via django
text-unidecode==1.3 # via faker
twisted[tls]==21.2.0 # via daphne
txaio==21.2.1 # via autobahn
urllib3==1.26.4 # via requests
whitenoise==5.2.0 # via -r requirements.in
zope.interface==5.4.0 # via twisted

# The following packages are considered to be unsafe in a requirements file:
# setuptools

0 comments on commit 6885187

Please sign in to comment.