subscription to announces by email
This commit is contained in:
parent
930d99901e
commit
8efa60ca6a
|
@ -1,5 +1,14 @@
|
|||
from wcs.storage import StorableObject
|
||||
|
||||
class AnnounceSubscription(StorableObject):
|
||||
_names = 'announce-subscriptions'
|
||||
|
||||
user_id = None
|
||||
email = None
|
||||
sms = None
|
||||
enabled = True
|
||||
|
||||
|
||||
class Announce(StorableObject):
|
||||
_names = 'announces'
|
||||
|
||||
|
|
|
@ -11,21 +11,32 @@ from wcs import errors
|
|||
from wcs.form import *
|
||||
from wcs.roles import logged_users_role
|
||||
|
||||
from wcs import emails
|
||||
from wcs.categories import Category
|
||||
from wcs.formdef import FormDef
|
||||
from wcs.tokens import Token
|
||||
from wcs.admin.settings import EmailsDirectory
|
||||
|
||||
from links import Link
|
||||
from announces import Announce
|
||||
from announces import Announce, AnnounceSubscription
|
||||
|
||||
OldRootDirectory = wcs.root.RootDirectory
|
||||
|
||||
class AnnouncesDirectory(Directory):
|
||||
_q_exports = ['', 'subscribe', 'email', 'atom']
|
||||
_q_exports = ['', 'subscribe', 'email', 'atom', 'sms', 'emailconfirm']
|
||||
|
||||
def _q_traverse(self, path):
|
||||
get_response().breadcrumb.append(('announces/', _('Announces')))
|
||||
return Directory._q_traverse(self, path)
|
||||
|
||||
def _q_index (self):
|
||||
raise NotImplementedError
|
||||
|
||||
def sms(self):
|
||||
raise NotImplementedError
|
||||
|
||||
def subscribe [html] (self):
|
||||
get_response().breadcrumb.append(('subscribe', _('Subscription')))
|
||||
template.html_top(_('Receiving Announces'))
|
||||
|
||||
'<p>'
|
||||
|
@ -38,11 +49,12 @@ class AnnouncesDirectory(Directory):
|
|||
'</ul>'
|
||||
|
||||
'<p>'
|
||||
_('There is also a feed available:')
|
||||
_('There is also a feed available: ')
|
||||
'<a class="feed-link" href="atom">%s</a>' % _('Announces Feed')
|
||||
'</p>'
|
||||
|
||||
def email [html] (self):
|
||||
get_response().breadcrumb.append(('email', _('Email Subscription')))
|
||||
template.html_top(_('Receiving Announces by email'))
|
||||
|
||||
form = Form(enctype='multipart/form-data')
|
||||
|
@ -52,7 +64,7 @@ class AnnouncesDirectory(Directory):
|
|||
get_request().user.email
|
||||
form.add_submit('submit', _('Subscribe'))
|
||||
else:
|
||||
_("You are logged in but you didn't enter any email address.")
|
||||
_("You are logged in but there is no email address in your profile.")
|
||||
form.add(EmailWidget, 'email', title = _('Email'), required = True)
|
||||
form.add_submit('submit', _('Subscribe'))
|
||||
form.add_submit('submit-remember', _('Subscribe and add this email to my profile'))
|
||||
|
@ -69,10 +81,121 @@ class AnnouncesDirectory(Directory):
|
|||
return redirect('subscribe')
|
||||
|
||||
if form.is_submitted() and not form.has_errors():
|
||||
raise NotImplementedError
|
||||
s = self.email_submit(form)
|
||||
if s is not False:
|
||||
return s
|
||||
else:
|
||||
form.render()
|
||||
|
||||
def email_submit(self, form):
|
||||
sub = AnnounceSubscription()
|
||||
|
||||
if get_request().user:
|
||||
sub.user_id = get_request().user.id
|
||||
|
||||
if form.get_widget('email'):
|
||||
sub.email = form.get_widget('email').parse()
|
||||
|
||||
if not get_request().user:
|
||||
sub.enabled = False
|
||||
|
||||
sub.store()
|
||||
|
||||
if get_request().user:
|
||||
def email_submit_ok [html] ():
|
||||
'<p>'
|
||||
_('You have been subscribed to the announces.')
|
||||
'</p>'
|
||||
'<a href="/">%s</a>' % _('Back Home')
|
||||
|
||||
return email_submit_ok()
|
||||
|
||||
# asking email confirmation before subscribing someone
|
||||
token = Token(3 * 86400)
|
||||
token.type = 'announces-subscription-confirmation'
|
||||
token.subscription_id = sub.id
|
||||
token.store()
|
||||
mail_body = _("""\
|
||||
You have (or someone impersonating you has) requested to subscribe to
|
||||
announces from [website]. To confirm this request, visit the
|
||||
following link:
|
||||
|
||||
[confirm_url]
|
||||
|
||||
If you are not the person who made this request, or you wish to cancel
|
||||
this request, visit the following link:
|
||||
|
||||
[cancel_url]
|
||||
|
||||
If you do nothing, the request will lapse after 3 days (precisely on
|
||||
[time]).
|
||||
""")
|
||||
misc_cfg = misc.cfg.get('misc', {})
|
||||
data = {
|
||||
'confirm_url': get_request().get_url() + 'confirm?t=%s&a=cfm' % token.id,
|
||||
'cancel_url': get_request().get_url() + 'confirm?t=%s&a=cxl' % token.id,
|
||||
'time': misc.localstrftime(time.localtime(token.expiration)),
|
||||
'website': misc_cfg.get('sitename'),
|
||||
}
|
||||
|
||||
emails.custom_ezt_email('announces-subscription-confirmation',
|
||||
_('Announce Subscription Request'),
|
||||
mail_body, data,
|
||||
sub.email,
|
||||
exclude_current_user = False)
|
||||
|
||||
def email_submit_ok [html] ():
|
||||
'<p>'
|
||||
_('You have been sent an email for confirmation')
|
||||
'</p>'
|
||||
'<a href="/">%s</a>' % _('Back Home')
|
||||
|
||||
return email_submit_ok()
|
||||
|
||||
def emailconfirm(self):
|
||||
tokenv = get_request().form.get('t')
|
||||
action = get_request().form.get('a')
|
||||
|
||||
try:
|
||||
token = Token.get(tokenv)
|
||||
except KeyError:
|
||||
return template.error_page(
|
||||
_('The token you submitted does not exist, has expired, or has been cancelled.'),
|
||||
continue_to = ('/', _('home page')))
|
||||
|
||||
if token.type != 'announces-subscription-confirmation':
|
||||
return template.error_page(
|
||||
_('The token you submitted is not appropriate for the requested task.'),
|
||||
continue_to = ('/', _('home page')))
|
||||
|
||||
sub = AnnounceSubscription.get(token.subscription_id)
|
||||
|
||||
if action == 'cxl':
|
||||
def cancel [html]():
|
||||
template.html_top('Email Subscription')
|
||||
'<h1>%s</h1>' % _('Request Cancelled')
|
||||
'<p>%s</p>' % _('The request for subscription has been cancelled.')
|
||||
'<p>'
|
||||
htmltext(_('Continue to <a href="/">home page</a>'))
|
||||
'</p>'
|
||||
token.remove_self()
|
||||
sub.remove_self()
|
||||
return cancel()
|
||||
|
||||
if action == 'cfm':
|
||||
#token.remove_self()
|
||||
sub.enabled = True
|
||||
sub.store()
|
||||
def sub [html] ():
|
||||
template.html_top('Email Subscription')
|
||||
'<h1>%s</h1>' % _('Subscription Confirmation')
|
||||
'<p>%s</p>' % _('Your subscription to announces is now effective.')
|
||||
'<p>'
|
||||
htmltext(_('Continue to <a href="/">home page</a>'))
|
||||
'</p>'
|
||||
return sub()
|
||||
|
||||
|
||||
def atom(self):
|
||||
raise NotImplementedError
|
||||
|
||||
|
@ -235,3 +358,9 @@ class AlternateRootDirectory(OldRootDirectory):
|
|||
raise NotImplementedError
|
||||
|
||||
wcs.root.RootDirectory = AlternateRootDirectory
|
||||
|
||||
|
||||
EmailsDirectory.register('announces-subscription-confirmation',
|
||||
N_('Confirmation of Announces Subscription'),
|
||||
N_('Available variables: change_url, cancel_url, time, website'))
|
||||
|
||||
|
|
Reference in New Issue