def atom_feed_latest(request, mailbox_slug): """ renders the mailbox as an atom feed """ ctx = request.context mb = ctx.get_mailbox(mailbox_slug) if mb is None: return HttpResponse(status=404) if not ctx.user.has_perm(PERM_READ, mb): return HttpResponse(status=401) # number of entries try: limit = min(int(request.GET.get('limit', DEFAULT_ATOM_ENTRIES)), MAX_ATOM_ENTRIES) except: return HttpResponse(status=400) params = {'limit': limit, 'include_docs': True, 'reduce': False, 'descending': True} # starting point in time if 'startkey' in request.GET: params['startkey'] = request.GET['startkey'] latest_messages = Message.view(mb, Message.by_timestamp, **params) return _render_atom_feed(request, mb, latest_messages)
def view_mailbox_latest(request, mailbox_slug): ctx = request.context mb = ctx.get_mailbox(mailbox_slug) if mb is None: return HttpResponse(status=404) if not ctx.user.has_perm(PERM_READ, mb): return handle_unauth(request) start = request.GET.get('start', None) limit = 10 params = {'limit': limit + 1, 'include_docs': True, 'reduce': False, 'descending': True} if start is not None: params['startkey'] = start messages = [] next_key = None for i, row in enumerate(mb.view(Message.by_timestamp, **params)): if i == limit: next_key = row.key else: messages.append(Message.wrap(row.doc)) if next_key: next_params = {'start': next_key} else: next_params = None return _render_messages(request, mb, messages, next_params=next_params)
def test_message_delete(self): """ test deleting a message by DELETE """ from radarpost.mailbox import Message message1 = Message(title='Message 1') message2 = Message(title='Message 2') mb = self.create_test_mailbox() slug = get_mailbox_slug(self.config, mb.name) c = self.get_test_app() self.login_as_admin(c) message1.store(mb) message2.store(mb) assert message1.id in mb assert message2.id in mb c.delete(self.url_for('message_rest', mailbox_slug=slug, message_slug=message1.id)) assert message1.id not in mb assert message2.id in mb
def test_mailbox_trim(): """ create a mailbox add some dated messages trim out old messages check correct messages remain check old messages are not present """ from datetime import datetime, timedelta from radarpost.mailbox import Message, trim_mailbox # create a mailbox mb = create_test_mailbox() # insert 10 messages, each one day older # than the next, starting with today. now = datetime.utcnow() delta = timedelta(days=-1) cur = now messages = [] for i in range(10): m = Message() m.timestamp = cur m.store(mb) messages.append(m) cur += delta # all messages should be in the mailbox now for m in messages: assert m.id in mb # get rid of messages older than 5 days trim_mailbox(mb, timedelta(days=5)) for m in messages[:5]: assert m.id in mb for m in messages[5:]: assert not m.id in mb
def _delete_message(request, mailbox_slug, message_slug): ctx = request.context mb = ctx.get_mailbox(mailbox_slug) if mb is None: return HttpResponse(status=404) if not ctx.user.has_perm(PERM_UPDATE, mb): return HttpResponse(status=401) message = Message.load(mb, message_slug) if message is None or message.type != MESSAGE_TYPE: return HttpResponse(status=404) try: del mb[message.id] except ResourceNotFound: return HttpResponse(status=404) return HttpResponse()
def view_mailbox(request, mailbox_slug): ctx = request.context mb = ctx.get_mailbox(mailbox_slug) if mb is None: return HttpResponse(status=404) params = {'limit': 10, 'include_docs': True, 'reduce': False, 'descending': True} info = MailboxInfo.get(mb) items = Message.view(mb, Message.by_timestamp, **params) ctx = {} ctx['mailbox_slug'] = mailbox_slug ctx['mailbox_title'] = info.title or mailbox_slug ctx['items'] = items return render_to_response('radar/view_mailbox.html', TemplateContext(request, ctx))
def test_mailbox_trim_sub(): """ test trimming down a subscription to a maximum number of items. create a mailbox add some dated messages associated with some subscriptions. trim down one of the subscriptions check that old messages are gone and everything else remains """ from datetime import datetime, timedelta from radarpost.mailbox import Message, Subscription, trim_subscription # create a mailbox mb = create_test_mailbox() sub0 = 'sub0' sub1 = 'sub1' sub2 = 'sub2' # insert 10 messages, each one day older # than the next, starting with today. now = datetime.utcnow() delta = timedelta(days=-1) cur = now messages = [] other_messages = [] for i in range(125): m = Message() m.timestamp = cur m.source.subscription_id = sub1 m.store(mb) messages.append(m) # create some similar messages belonging to # different subscriptions bookending the one # we are going to delete from. m2 = Message() m2.timestamp = cur m2.source.subscription_id = sub0 m2.store(mb) other_messages.append(m2) m2 = Message() m2.timestamp = cur m2.source.subscription_id = sub2 m2.store(mb) other_messages.append(m2) cur += delta # all messages should be in the mailbox now for m in messages: assert m.id in mb for m in other_messages: assert m.id in mb subscription = Subscription(id=sub1) trim_subscription(mb, subscription, max_entries=51, batch_size=25) for i, m in enumerate(messages[:51]): assert m.id in mb for i, m in enumerate(messages[51:]): assert not m.id in mb # irrelevant messages should not have been touched. for m in other_messages: assert m.id in mb