def get(self): '''no arguments''' today = datetime.today() query = Picture.all().filter('uploaded >', datetime(today.year, today.month, today.day, 0, 0) ) if query.count(): logging.debug('New pictures for newsletter: %s' % query.count()) pictures_today = list(query) query = Subscription.all().filter('active =', True) if query.count(): logging.info('Sending newsletter to %s recipients' % query.count()) recipients = list(query) today = datetime.today() message = mail.EmailMessage() message.sender = '%s (Photoblog) <%s>' % ( conf.mail_from_name, conf.mail_from_address ) message.to = conf.mail_from_address message.bcc = [r.email for r in recipients] message.subject = conf.mail_newsletter_subject message.body = render('newsletter.txt', {'pics':pictures_today}) message.send()
def generate_feed(uid): subscriptions = Subscription.query(Subscription.uid == uid).fetch(200) subscription_urls = [sub.url for sub in subscriptions if sub.url] if len(subscription_urls) > 0: sources = Source.query(Source.url.IN(subscription_urls)).order(-Source.most_recent_article_added_date).fetch(len(subscription_urls)) source_jsons = {} for source_json in memcache.get_multi([source.feed_cache_key() for source in sources]).itervalues(): source_jsons[source_json['id']] = source_json to_fetch = [source for source in sources if source.key.id() not in source_jsons] print 'HITS {0} TO_FETCH {1}'.format(len(source_jsons), len(to_fetch)) if len(to_fetch): source_promises = [src.json(include_articles=True, article_limit=FEED_ARTICLE_LIMIT, return_promise=True) for src in to_fetch] for promise in source_promises: data = promise() source_jsons[data['id']] = data # put the cache keys: if len(to_fetch): memcache.set_multi({source.feed_cache_key(): source_jsons[source.key.id()] for source in to_fetch if (source.key.id() in source_jsons)}) source_json = [source_jsons[source.key.id()] for source in sources if source.key.id() in source_jsons] else: source_json = [] return { "sources": source_json }
def create_subscription(user_name, plan_id): subscription = Subscription(user_name=user_name, plan_id=plan_id) db.session.add(subscription) db.session.commit() return subscription
def sources_subscribed_by_id(uid, just_inserted=None): subs = Subscription.query(Subscription.uid == uid).fetch(limit=100) if just_inserted and just_inserted.key.id() not in [sub.key.id() for sub in subs]: subs = [just_inserted] + subs json_futures = [sub.json(return_promise=True) for sub in subs] jsons = [j() for j in json_futures] jsons = [j for j in jsons if j['source']] return jsons
def post(self): '''email address has been entered validate and send the confirmation mail''' to_address_1 = self.request.get('to_1') to_address_2 = self.request.get('to_2') if not to_address_1 == to_address_2: self.redirect('/abo/subscribe?error=mails') else: key = str(uuid().int) logging.error('%s' % key) confirmation_link = '%s/abo/confirm/%s' % (conf.host, key) mail_content = render('subscribe_mail.txt', {'link':confirmation_link, 'host':conf.host, 'sender_mail':conf.mail_from_address, 'sender_name':conf.mail_from_name } ) query = Subscription.all().filter('email =', to_address_1) if query.count(): # email has been submitted before prev_mail = query.get() if prev_mail.active: logging.debug('Repeat subscription: %s' % prev_mail.email) self.redirect('/abo/success') else: logging.debug('New subscription key for %s' % prev_mail.email) prev_mail.delete() subscription = Subscription(email=to_address_1, validation_key=key, active=False) subscription.put() logging.info('New subscription: %s' % subscription.email) logging.debug('Sending confirmation mail to %s' % to_address_1) mail.send_mail( sender='%s (Photoblog) <%s>' % (conf.mail_from_name, conf.mail_from_address ), to=to_address_1, subject=conf.mail_confirmation_subject, body = mail_content ) self.redirect('/abo/success')
def add_new(self, current_revision, end_date, font_id, start_date, user_id): new_subscription = Subscription( current_revision=current_revision, end_date=end_date, font_id=font_id, start_date=start_date, user_id=user_id ) db_session.add(new_subscription) db_session.commit()
def get(self): email = self.request.get('email') token = self.request.get('token') is_verified = reveal.verify('unsubscribe:%s' % email, token) if not is_verified: return self.error(200, _('This link is invalid.')) subscription = Subscription.get(self.subdomain, self.params.id, email) if subscription: db.delete(subscription) return self.info(200, _('You have successfully unsubscribed.')) else: return self.error(200, _('You are already unsubscribed.'))
def get(self): email = self.request.get('email') token = self.request.get('token') is_verified = reveal.verify('unsubscribe:%s' % email, token) if not is_verified: return self.error(200, _('This link is invalid.')) subscription = Subscription.get(self.repo, self.params.id, email) if subscription: db.delete(subscription) return self.info(200, _('You have successfully unsubscribed.')) else: return self.error(200, _('You are already unsubscribed.'))
def get(self, key): '''check if the given key is valid''' query = Subscription.all().filter('validation_key =', key) if not query.count(): logging.debug('Subscription failed with key %s' % key) resp = render('abo_subscribe_wrong_key.html') else: subscription = query.get() subscription.active = True subscription.put() logging.info('New subscription: %s' % subscription.email) resp = render('abo_subscribe_complete.html') self.response.out.write(resp)
def _subscribe_multi(uid, urls): sources = {url: ensure_source(url) for url in urls} # TODO: make 4 a shared value source_json_futures = {url: source.json(include_articles=True, return_promise=True, article_limit=4) for url, source in sources.iteritems()} source_json = {url: f() for url, f in source_json_futures.iteritems()} source_json = {url: source_json for url, source_json in source_json.iteritems() if len(source_json['articles'])} subscription_futures = {url: Subscription.get_or_insert_async(Subscription.id_for_subscription(source.url, uid)) for url, source in sources.iteritems()} subscriptions = {url: f.get_result() for url, f in subscription_futures.iteritems()} for url, sub in subscriptions.iteritems(): canonical_url = sources[url].url sub.url = canonical_url sub.uid = uid ndb.put_multi(subscriptions.values()) Feed.get_for_user(uid).update_in_place(just_added_sources_json=source_json.values()) sub_json_promises = {url: sub.json(return_promise=True) for url, sub in subscriptions.iteritems()} sub_jsons = {url: f() for url, f in sub_json_promises.iteritems()} return {"source_jsons": source_json, "subscription_jsons": sub_jsons}
def get(self, address): '''unsubscribe the given address @param address: urllib.quote_plus() has been used on it''' address = unquote_plus(address) query = Subscription.all().filter('email =', address) if not query.count() == 1: logging.debug('Failed unsubscription: %s' % address) resp = render('abo_unsubscribe_failed.html') else: subscription = query.get() subscription.active = False subscription.put() logging.info('Unsubscribed %s' % address) resp = render('abo_unsubscribe_successful.html') self.response.out.write(resp)
def unsubscribe(uid, url): ndb.Key(Subscription, Subscription.id_for_subscription(url, uid)).delete() Feed.get_for_user(uid).update_in_place(just_removed_source_urls=[url]) return True