-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathforms.py
More file actions
136 lines (104 loc) · 3.67 KB
/
forms.py
File metadata and controls
136 lines (104 loc) · 3.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""
=================
learnpython.forms
=================
Contacts and subscribe form for Learn Python site.
All forms are built on top of ``Flask-WTF`` extension, all emails will send
using ``Flask-Mail`` extension.
Default recipient
=================
By default, all emails will sent to ``BaseContactsForm.default_recipient``
email. To customize things, setup ``MAIL_RECIPIENTS`` setting with tuple or
list of recipients.
"""
import operator
from flask import render_template
from flask.ext import wtf
from flask.ext.babel import lazy_gettext as _
from flask.ext.mail import Message
from learnpython.app import app, mail, pages
__all__ = ('ContactsForm', 'SubscribeForm')
FLOW_CHOICES = \
map(lambda page: (page.path.replace('flows/', ''), page['title']),
sorted(filter(lambda page: page.path.startswith('flows/'), pages),
key=operator.itemgetter('order')))
class Email(wtf.Email, object):
"""
Localize message for email validator.
"""
def __init__(self, message=None):
message = message or _('Invalid email address.')
super(Email, self).__init__(message)
class Required(wtf.Required, object):
"""
Localize message for required validator.
"""
def __init__(self, message=None):
message = message or _('This field is required.')
super(Required, self).__init__(message)
class BaseContactsForm(wtf.Form, object):
"""
Base contacts form.
Provide common fields, config settings and method to send email after
succeed form validation.
"""
name = wtf.TextField(_('Name'), validators=[Required()])
email = wtf.TextField(_('Email'), validators=[Required(), Email()])
template = None
title = None
def get_title(self):
"""
Prepend flow name for message title if possible or return default
message title from class attribute.
"""
if 'flow' in self.data:
return u'{0}: {1}'.format(self.title, self.data['flow'])
return self.title
@property
def recipients(self):
"""
Read list of recipients from application config.
"""
default = [self.default_recipient]
return app.config.get('MAIL_RECIPIENTS', default)
def send(self):
"""
Send email to all form recipients.
"""
assert self.title, 'Please, supply "title" attribute first.'
assert self.template, 'Please, supply "template" attribute first.'
title = self.get_title()
message = Message(u'[Learn Python] {0}'.format(title),
sender=(self.data['name'], self.data['email']),
recipients=self.recipients)
message.body = render_template(self.template, **self.data)
mail.send(message)
class ContactsForm(BaseContactsForm):
"""
Feedback form.
"""
subject = wtf.TextField(_('Subject'))
message = wtf.TextField(
_('Message'), validators=[Required()], widget=wtf.TextArea()
)
template = 'mails/contacts.txt'
title = _('Feedback')
def send(self):
"""
Use custom subject for email message if user filled in "Subject" field.
"""
self.title = self.data['subject'] or self.title
super(ContactsForm, self).send()
class SubscribeForm(BaseContactsForm):
"""
Subscribe form.
"""
phone = wtf.TextField(_('Phone'))
skype = wtf.TextField(_('Skype'))
flow = wtf.SelectField(
_('Flow'), choices=FLOW_CHOICES, validators=[Required()]
)
comments = wtf.TextField(_('Additional comments'), widget=wtf.TextArea())
template = 'mails/subscribe.txt'
title = _('Flow subscription')