def filter_entries(entry_type): objects_per_page = current_app.config.get("OBJECTS_PER_PAGE", 10) query_dict = json.loads(request.json.get("query", "{}")) query_dict.setdefault("order_by", []).append(dict( field="name", direction="asc", )) page = int(request.json.get("page", 1)) model = get_entry_model(entry_type) if model is not None: query = create_query(db.session, model, query_dict) object_count = query.count() limited_query = query.offset((page - 1) * objects_per_page)\ .limit(objects_per_page) return jsonify( page=page, total_pages=int(math.ceil(\ float(object_count) / float(objects_per_page))), total_object_count=object_count, objects=[entry.data for entry in limited_query.all()], ) else: return jsonify_error( reason="Unkown entry type: {}".format(entry_type), )
def post(self): username = request.json["username"] password = request.json["password"] user = models.User.query.filter_by(username=username).first() if user is not None and user.check_password(password): login_user(user) return jsonify(status="ok", username=current_user.get_id()) else: return jsonify_error( reason="invalid_username_or_password", )
def delete(self, pinboard_id): p = models.Pinboard.query.get(pinboard_id) if p is not None and p.owner == current_user or current_user.admin: db.session.delete(p) db.session.commit() return jsonify( status="ok", ) else: return jsonify_error( reason="invalid_pinboard_id", )
def get(self, pinboard_id): if pinboard_id is None: # list pinboards owned_pinboards = list(current_user.owned_pinboards) other_pinboards = models.Pinboard.query.filter(models.Pinboard.owner != current_user, models.Pinboard.public == True).all() return jsonify( pinboards={p.id: p.as_dict() for p in owned_pinboards + other_pinboards}, ) else: p = models.Pinboard.query.get(pinboard_id) if p is not None and (p.public or p.owner_username == current_user.username): return jsonify(pinboard=p.as_dict(full=True)) else: return jsonify_error( reason="invalid_pinboard_id", )
def put(self): username = request.json["username"] password = request.json["password"] if current_user.is_anonymous(): try: pass except: pass elif current_user.is_authenticated()\ and username == current_user.username: current_user.password = password db.session.commit() return jsonify(status="ok") else: return jsonify_error( reason="invalid_session", )
def put(self, pinboard_id): operation = request.json.get("operation") p = models.Pinboard.query.get(pinboard_id) if p is not None and p.owner == current_user or current_user.admin: if operation == "update": p_data = request.json.get("pinboard") name = p_data.get("name") description = p_data.get("description") public = p_data.get("public", False) p.name = name p.description = description p.public = public db.session.commit() return jsonify( status="ok", pinboard=p.as_dict(full=True), ) elif operation == "add_entry": entry_data = request.json["entry"] entry_type = entry_data["entry_type"] entry_id = entry_data["id"] try: p.add_entry(entry_type, entry_id) db.session.commit() return jsonify( status="ok", pinboard=p.as_dict(full=True), ) except models.Pinboard.EntryAlreadyPresentError: return jsonify_error( reason="entry_already_in_pinboard", ) elif operation == "remove_entry": entry_data = request.json["entry"] entry_type = entry_data["entry_type"] entry_id = entry_data["id"] try: p.remove_entry(entry_type, entry_id) db.session.commit() return jsonify( status="ok", pinboard=p.as_dict(full=True), ) except models.Pinboard.EntryNotPresentError: return jsonify_error( reason="entry_not_present_in_pinboard", ) else: return jsonify_error( reason="invalid_operation", ) else: return jsonify_error( reason="invalid_pinboard_id", )
def get_entry_type_filters(entry_type): model = get_entry_model(entry_type) if model is not None: return jsonify(options=model.get_filter_options()) else: return jsonify_error(reason="Unkown entry type: {}".format(entry_type))