def test_default(self): cache = Cache(4) cache.add(1) cache.add('q') cache.add(0.99) cache.add('q') cache.add(0.988) self.assertEqual(cache.length, 4) cache.remove('q') self.assertEqual(cache.length, 2) cache = Cache(3) cache.add(1) cache.add('q') cache.add('a') cache.add('b') cache.add('c') self.assertEqual(cache.length, 3) values = cache.cache self.assertEqual(values[0], 'a') self.assertEqual(values[1], 'b') self.assertEqual(values[2], 'c') self.assertFalse(cache.exists(2)) self.assertTrue(cache.exists('c'))
def delete_post(board, thread_num, post_num, rape_msg): last_deletion = False th = Thread.load(thread_num, board) [post] = [p for p in th.posts if p.get('post') == post_num] logging.info("found: %r" % post) key = post.get("key") if key: post.pop("key", None) post.pop("image", None) info = blobstore.BlobInfo.get( blobstore.BlobKey(key)) if info: info.delete() try: th.images.remove(post.get("key")) except: pass logging.info("removed image %r" % post) else: last_deletion = True post['text'] = 'Fuuuuuu' post['text_html'] = 'Fuuuuuu' post['rainbow_html'] = u'<b>' + rape_msg + '</b>' th.put() Cache.remove("board", board) r = Render(thread=th) #kind of shit: r.create(th.posts[0]) for a_post in th.posts[1:]: r.append(a_post) r.save() return last_deletion
def save_post(request, data, board, thread): board_db = Board.get_by_key_name(board) if not board_db: board_db = Board(key_name = board, thread = []) board_db.counter += 1 # create new thread new = False if thread == 'new': new = True if data.get("sage"): raise NotFound() # FIXME: move to form thread = board_db.counter posts = [] thread_db = Thread.create(thread, board) thread_db.posts = [] thread_db.subject = data.get("subject")[:SUBJECT_MAX] else: thread = int(thread) if thread in board_db.thread and not data.get("sage"): board_db.thread.remove(thread) thread_db = Thread.load(thread, board) if not thread_db: raise NotFound() if not data.get("sage"): board_db.thread.insert(0, thread) per_page = get_config('aib.ib', 'thread_per_page') pages = get_config('aib.ib', 'board_pages') board_db.thread = board_db.thread[:per_page*pages] rb = rainbow.make_rainbow(request.remote_addr, board, thread) data['rainbow'] = rb data['overlay'] = board in OVER data['text_html'] = markup( board=board, postid=board_db.counter, data=escape(data.get('text', '')), ) # save thread and post number data['post'] = board_db.counter data['thread'] = thread now = datetime.now() data['time'] = now.strftime("%Y-%m-%d, %H:%M") data['timestamp'] = int(now.strftime("%s")) img_key = data.get("key") if img_key: blob_key = blobstore.BlobKey(img_key) blob_info = blobstore.BlobInfo.get(blob_key) data['image'] = { "size" : blob_info.size, "content_type" : blob_info.content_type, "full" : images.get_serving_url(img_key), "thumb" : images.get_serving_url(img_key, 200), } for fname in OPTIONS.get(board, []): func = globals().get('option_'+fname) if func: func(request, data) thread_db.posts.append(data) db.put( (thread_db, board_db)) Cache.remove("board", board) r = Render(board, thread) r.add(data, new) r.save() deferred.defer(rss.add, board, thread, board_db.counter, data.get("text_html") ) if not new: deferred.defer( watchers_post_notify, board, thread, r.post_html, len(thread_db.posts), board_db.counter ) return board_db.counter, thread