/django-subscribe-form

Primary LanguagePythonMIT LicenseMIT

Build Status Coverage Status codecov Requirements Status PyPI

Project for merging different file types, as example easy thumbnail image and unpacking archive in one field

Installation

pip install django-subscribe-form

or from git

pip install -e git+https://github.com/Apkawa/django-subscribe-form.git#egg=django-subscribe-form

Django and python version

  • python-2.7 - django>=1.8,<=1.11
  • python-3.4 - django>=1.8,<=1.11
  • python-3.5 - django>=1.8,<=1.11
  • python-3.6 - django>=1.11

Configuration

  1. Add to INSTALLED_APPS post_office, corsheaders and subscribe_form
INSTALLED_APPS = [
    #...
    'post_office',
    'subscribe_form',
    #...
]
  1. Configure corsheaders
MIDDLEWARE_CLASSES = (
    #...
    'corsheaders.middleware.CorsMiddleware',
    #...
    )
CORS_ORIGIN_ALLOW_ALL = True
CORS_ALLOW_CREDENTIALS = True
from corsheaders.defaults import default_headers

CORS_ALLOW_HEADERS = default_headers + (
    'api-key',
)
  1. Add to urls.py
from django.conf.urls import include, url

urlpatterns = [
    url(r'^subscribe/', include('subscribe_form.urls')),
]
  1. Run migrations
./manage migrate

Usage

  1. Create post_office.EmailTemplate instance in admin http://127.0.0.1:8000/admin/post_office/emailtemplate/add/

Example content fields

  • subject
From site {{ host }} was submited form from {{ fields.full_name.value|default:email }}

  • content
{{ fields.full_name.value }}
{% for key,field in fields.items %}
{{ field.display_name|default:key }}: {{ field.value }}
{% endfor %}

Host: {{ host }}
Referer: {{ referer }}
  1. Create subscribe_form.EmailTemplate instance http://127.0.0.1:8000/admin/subscribe_form/emailtemplate/add/

  2. Create subscrube_form.Form instance and get emdedding code

  3. Add embedding code to page:

    <script 
     data-endpoint="//example.com/api/v1/subscribe/subscribe/" 
     data-key="4a96f68d-7acb-4f7f-8e43-63e8902bf08f" 
     src="//example.com/static/subscribe_form/js/subscribe.js?r=dev">
     </script>

API

Simple usage

curl -X POST //example.com/api/v1/subscribe/subscribe/ \
    -H "Content-Type: multipart/form-data" \
    -H 'API-Key: 4a96f68d-7acb-4f7f-8e43-63e8902bf08f' \
    -H 'Accept: application/json' \
    -F form_data='[{"value": "example@email.com", "display_name": "User email*", "name": "email", "is_file": false}]' 

Send file

curl -X POST //example.com/api/v1/subscribe/subscribe/ \
    -H "Content-Type: multipart/form-data" \
    -H 'API-Key: 4a96f68d-7acb-4f7f-8e43-63e8902bf08f' \
    -H 'Accept: application/json' \
    -F form_data='[{"display_name": "User avatar*", "name": "avatar", "is_file": true}]'  \
    -F "avatar=@/home/user/avatar.png"  \

Contributing

run tests

pip install -r requirements.txt
./test/manage.py migrate
pytest
tox

publish pypi

python setup.py sdist upload -r pypi