def test_set_wn_card_empty(self, client): res = post_set_wn_cards(client, []) self.assertEqual(400, res.status_code) third_party = current_app.test_client() get_db().whatsnew.update({}, {"$set": {"show": [], "cards": []}}) index = third_party.get('/') assert_index_page(self, index) self.assertTrue("no news" in index.data)
def get_segmented_page_list(): total = get_db().usercontent.count() def get(x, y): return list(get_db().usercontent.find({}).sort("_id", -1).skip( x * 10).limit(y)) def clear_not_allowed(l): out = [] for x in l: y = get_page_to_view(x["_id"]) if y is not None: out.append(x) return out skip = int(request.args.get("skip", 0)) number = int(request.args.get("number", 10)) pgs = clear_not_allowed(get(skip, number)) count = len(pgs) while len(pgs) != number and count < total: skip += 1 tmp = clear_not_allowed(get(skip, number - len(pgs))) pgs += tmp count += number for x in pgs: x["_id"] = str(x["_id"]) return jsonify(pgs)
def list_wn_cards(cls): ids = get_db().whatsnew.find_one({}) if ids is None or ("cards" not in ids): # Not in the db, most likely a local instance return [] return map(WhatsNewCard.load, ids["cards"])
def get_page_with_id(id): oid = get_obj_id(id) if oid is None: return None return get_db().usercontent.find_one({"_id": oid})
def get_allowed_pages(): all_pages = list(get_db().usercontent.find({}).sort("_id", -1)) allowed = [] for page in all_pages: valid_page = check_blog_permissions(page) if valid_page: allowed.append(valid_page) return allowed
def test_add_wn_card(self, client): with open( os.path.join(os.path.dirname(__file__), "res", "python-logo.png"), "r") as f: res = post_add_wn_card(client, f, "Caption", "Sub-caption", "http://test.example.com") self.assertEqual(200, res.status_code) l = get_db().cards.find_one({"caption": "Caption"})
def get_frontpage_cards(cls): # Only one doc in this collection # It has a list of card ids under # its "show" field. ids = get_db().whatsnew.find_one({}) if ids is None or ("show" not in ids): # Not in the db, most likely a local instance return [] # Map the card loading over the ids return map(WhatsNewCard.load, ids["show"])
def set_wn_cards(): # form = SetWnCardsForm(request.form) new_wn_list = [] data = request.get_json() if data is None or "ids" not in data: return abort(400) ids = data['ids'] if request.method == "POST": for id_field in ids: if len(str(id_field)) == 0: continue try: oid = get_obj_id()(str(id_field)) except Exception, e: return "", 400 if not card_exists(oid): return "", 400 new_wn_list.append(oid) get_db().whatsnew.update({}, {"$set": { "show" : new_wn_list }}) return "", 200
def get_deletable_pages(): if has_admin(): return get_db().usercontent.find({}) else: return get_my_pages()
def update_document(id, doc): get_db().usercontent.update_one({"_id": id}, {"$set": doc})
def remove_document(doc_id): get_db().usercontent.delete_one({"_id": doc_id})
def blog_page_count(): return range(get_db().usercontent.count())
def add_new_document(doc): return get_db().usercontent.insert_one(doc).inserted_id
def get_show_list(): return reduce(_card_from_id, get_db().whatsnew.find_one({})["show"], [])
def get_my_pages(): if current_user.is_authenticated: return get_db().usercontent.find({"owner": current_user.get_id()}) else: return []
def list_all_pages(): return list(get_db().usercontent.find({}))
def get(x, y): return list(get_db().usercontent.find({}).sort("_id", -1).skip( x * 10).limit(y))
def _card_from_id(others, id): return others + [get_db().cards.find_one({"_id": id})]
def store_card_id(cls, obj_id): get_db().whatsnew.update_one({}, {"$addToSet" : {"cards": obj_id}})