2006-12-26 14:07:01 +01:00
|
|
|
import time
|
2007-06-27 19:46:59 +02:00
|
|
|
|
|
|
|
from quixote import get_publisher
|
|
|
|
|
2006-12-26 14:07:01 +01:00
|
|
|
from quixote.html import htmlescape
|
|
|
|
|
2019-09-29 20:51:22 +02:00
|
|
|
from wcs.qommon import _
|
|
|
|
from wcs.qommon.storage import StorableObject
|
|
|
|
from wcs.qommon import get_cfg, get_logger
|
|
|
|
from wcs.qommon import errors
|
|
|
|
from wcs.qommon import misc
|
2006-10-11 20:18:20 +02:00
|
|
|
|
2019-09-29 20:51:22 +02:00
|
|
|
from wcs.qommon import emails
|
|
|
|
from wcs.qommon.sms import SMS
|
|
|
|
from wcs.qommon.admin.emails import EmailsDirectory
|
2007-06-27 19:46:59 +02:00
|
|
|
|
2006-10-15 16:08:09 +02:00
|
|
|
class AnnounceSubscription(StorableObject):
|
|
|
|
_names = 'announce-subscriptions'
|
2009-06-04 12:25:21 +02:00
|
|
|
_indexes = ['user_id']
|
2006-10-15 16:08:09 +02:00
|
|
|
|
|
|
|
user_id = None
|
|
|
|
email = None
|
|
|
|
sms = None
|
|
|
|
enabled = True
|
2009-04-17 11:18:20 +02:00
|
|
|
enabled_sms = False
|
2009-06-04 12:25:21 +02:00
|
|
|
enabled_themes = None
|
2009-04-17 11:18:20 +02:00
|
|
|
|
|
|
|
def remove(self, type=None):
|
|
|
|
""" type (string) : email or sms """
|
|
|
|
if type == "email":
|
|
|
|
self.email = None
|
|
|
|
elif type == "sms":
|
|
|
|
self.sms = None
|
|
|
|
self.enabled_sms = False
|
|
|
|
if not type or (not self.sms and not self.email):
|
|
|
|
self.remove_self()
|
|
|
|
else:
|
|
|
|
self.store()
|
|
|
|
|
2007-06-27 19:46:59 +02:00
|
|
|
def get_user(self):
|
|
|
|
if self.user_id:
|
2012-01-25 12:36:48 +01:00
|
|
|
try:
|
|
|
|
return get_publisher().user_class.get(self.user_id)
|
|
|
|
except KeyError:
|
|
|
|
return None
|
2007-06-27 19:46:59 +02:00
|
|
|
return None
|
|
|
|
user = property(get_user)
|
|
|
|
|
|
|
|
|
2006-10-11 20:18:20 +02:00
|
|
|
class Announce(StorableObject):
|
|
|
|
_names = 'announces'
|
|
|
|
|
|
|
|
title = None
|
|
|
|
text = None
|
|
|
|
|
2007-06-27 18:35:14 +02:00
|
|
|
hidden = False
|
|
|
|
|
|
|
|
publication_time = None
|
2006-12-26 14:07:01 +01:00
|
|
|
modification_time = None
|
2010-08-12 11:17:33 +02:00
|
|
|
expiration_time = None
|
2007-06-27 19:46:59 +02:00
|
|
|
sent_by_email_time = None
|
2009-04-17 11:18:20 +02:00
|
|
|
sent_by_sms_time = None
|
2009-06-04 12:03:54 +02:00
|
|
|
theme = None
|
2006-12-26 14:07:01 +01:00
|
|
|
|
2007-08-19 14:11:10 +02:00
|
|
|
position = None
|
|
|
|
|
|
|
|
def sort_by_position(cls, links):
|
|
|
|
def cmp_position(x, y):
|
|
|
|
if x.position == y.position:
|
|
|
|
return 0
|
|
|
|
if x.position is None:
|
|
|
|
return 1
|
|
|
|
if y.position is None:
|
|
|
|
return -1
|
|
|
|
return cmp(x.position, y.position)
|
|
|
|
links.sort(cmp_position)
|
|
|
|
sort_by_position = classmethod(sort_by_position)
|
|
|
|
|
2006-12-26 14:07:01 +01:00
|
|
|
def get_atom_entry(self):
|
|
|
|
from pyatom import pyatom
|
|
|
|
entry = pyatom.Entry()
|
|
|
|
entry.id = self.get_url()
|
|
|
|
entry.title = self.title
|
|
|
|
|
|
|
|
entry.content.attrs['type'] = 'html'
|
|
|
|
entry.content.text = str('<p>' + htmlescape(
|
2009-04-21 19:24:40 +02:00
|
|
|
unicode(self.text, get_publisher().site_charset).encode('utf-8')) + '</p>')
|
2006-12-26 14:07:01 +01:00
|
|
|
|
|
|
|
link = pyatom.Link(self.get_url())
|
|
|
|
entry.links.append(link)
|
|
|
|
|
2007-09-12 10:53:36 +02:00
|
|
|
if self.publication_time:
|
|
|
|
entry.published = misc.format_time(self.publication_time,
|
|
|
|
'%(year)s-%(month)02d-%(day)02dT%(hour)02d:%(minute)02d:%(second)02dZ',
|
|
|
|
gmtime = True)
|
|
|
|
|
2006-12-26 14:07:01 +01:00
|
|
|
if self.modification_time:
|
|
|
|
entry.updated = misc.format_time(self.modification_time,
|
|
|
|
'%(year)s-%(month)02d-%(day)02dT%(hour)02d:%(minute)02d:%(second)02dZ',
|
|
|
|
gmtime = True)
|
|
|
|
|
|
|
|
return entry
|
|
|
|
|
|
|
|
def get_url(self):
|
2012-02-28 11:50:19 +01:00
|
|
|
return '%s/announces/%s/' % (get_publisher().get_frontoffice_url(), self.id)
|
2006-12-26 14:07:01 +01:00
|
|
|
|
|
|
|
def store(self):
|
2007-09-12 10:53:36 +02:00
|
|
|
self.modification_time = time.gmtime()
|
2006-12-26 14:07:01 +01:00
|
|
|
StorableObject.store(self)
|
|
|
|
|
2011-01-28 15:30:12 +01:00
|
|
|
def email(self, job=None):
|
2007-09-12 10:53:36 +02:00
|
|
|
self.sent_by_email_time = time.gmtime()
|
2007-06-27 19:46:59 +02:00
|
|
|
StorableObject.store(self)
|
|
|
|
|
|
|
|
data = {
|
|
|
|
'title': self.title,
|
|
|
|
'text': self.text
|
|
|
|
}
|
|
|
|
|
2007-06-27 19:58:03 +02:00
|
|
|
subscribers = AnnounceSubscription.select(lambda x: x.enabled)
|
|
|
|
|
|
|
|
rcpts = []
|
|
|
|
for l in subscribers:
|
2009-06-04 12:30:39 +02:00
|
|
|
if self.theme:
|
|
|
|
if l.enabled_themes is not None:
|
|
|
|
if self.theme not in l.enabled_themes:
|
|
|
|
continue
|
2012-01-25 12:36:48 +01:00
|
|
|
if l.user and l.user.email:
|
2007-06-27 19:58:03 +02:00
|
|
|
rcpts.append(l.user.email)
|
|
|
|
elif l.email:
|
|
|
|
rcpts.append(l.email)
|
|
|
|
|
2017-11-27 16:38:57 +01:00
|
|
|
emails.custom_template_email('aq-announce', data, email_rcpt=rcpts, hide_recipients=True)
|
2007-06-27 19:46:59 +02:00
|
|
|
|
2011-01-28 15:30:12 +01:00
|
|
|
def sms(self, job=None):
|
2009-04-17 11:18:20 +02:00
|
|
|
self.sent_by_sms_time = time.gmtime()
|
|
|
|
StorableObject.store(self)
|
|
|
|
|
|
|
|
subscribers = AnnounceSubscription.select(lambda x: x.enabled_sms)
|
|
|
|
|
|
|
|
rcpts = []
|
|
|
|
for sub in subscribers:
|
2009-06-04 12:30:39 +02:00
|
|
|
if self.theme:
|
2012-01-26 17:36:13 +01:00
|
|
|
if sub.enabled_themes is not None:
|
|
|
|
if self.theme not in sub.enabled_themes:
|
2009-06-04 12:30:39 +02:00
|
|
|
continue
|
2009-04-17 11:18:20 +02:00
|
|
|
if sub.sms:
|
|
|
|
rcpts.append(sub.sms)
|
|
|
|
|
|
|
|
sms_cfg = get_cfg('sms', {})
|
2009-04-20 15:57:08 +02:00
|
|
|
sender = sms_cfg.get('sender', 'AuQuotidien')[:11]
|
2009-04-17 11:18:20 +02:00
|
|
|
message = "%s: %s" % (self.title, self.text)
|
2013-03-05 18:54:03 +01:00
|
|
|
mode = sms_cfg.get('mode', 'none')
|
|
|
|
sms = SMS.get_sms_class(mode)
|
2009-04-17 11:18:20 +02:00
|
|
|
try:
|
2013-03-05 18:54:03 +01:00
|
|
|
sms.send(sender, rcpts, message[:160])
|
2009-04-17 11:18:20 +02:00
|
|
|
except errors.SMSError, e:
|
|
|
|
get_logger().error(e)
|
|
|
|
|
2007-09-12 10:53:36 +02:00
|
|
|
def get_published_announces(cls):
|
|
|
|
announces = cls.select(lambda x: not x.hidden)
|
2007-09-12 11:31:00 +02:00
|
|
|
announces.sort(lambda x,y: cmp(x.publication_time or x.modification_time,
|
|
|
|
y.publication_time or y.modification_time))
|
2010-08-12 11:17:33 +02:00
|
|
|
announces = [x for x in announces if x.publication_time < time.gmtime()
|
|
|
|
and (x.expiration_time is None or x.expiration_time > time.gmtime())]
|
2007-09-12 10:53:36 +02:00
|
|
|
announces.reverse()
|
|
|
|
return announces
|
|
|
|
get_published_announces = classmethod(get_published_announces)
|
|
|
|
|
2007-06-27 19:46:59 +02:00
|
|
|
|
|
|
|
EmailsDirectory.register('aq-announce',
|
|
|
|
N_('Publication of announce to subscriber'),
|
|
|
|
N_('Available variables: title, text'),
|
|
|
|
default_subject = N_('Announce: [title]'),
|
|
|
|
default_body = N_("""\
|
|
|
|
[text]
|
|
|
|
|
|
|
|
--
|
|
|
|
This is an announce sent to you by your city, you can opt to not receive
|
|
|
|
those messages anymore on the city website.
|
|
|
|
"""))
|
|
|
|
|