def tags_index(): """ タグ一覧 """ user = authorized_user() model = DOMAIN['items'] tags = model.get_all_tags() return render_template('tags_index.html', user=user, tags=tags, prefix=app.config["APPLICATION_ROOT"])
def tags_page(tag_name): """ タグ詳細 """ user = authorized_user() model = DOMAIN['items'] items = model.get_index({'tags': tag_name}) follower = DOMAIN['users'].get_index({'following_tags': tag_name}) return render_template('tags.html', user=user, items=items, tag_name=tag_name, follower_count=follower['count'], prefix=app.config["APPLICATION_ROOT"])
def edit_drafts(oid=None): """ 下書き作成 """ user = authorized_user() model = DOMAIN["items"] draft = dict() if oid: draft = model.get_by_id(oid) draft['markdown'] = draft['markdown'].replace('\\', '\\\\').replace('\n', '\\n') return render_template("edit_drafts.html", oid=oid, user=user, draft=draft, prefix=app.config["APPLICATION_ROOT"])
def get_stocked_items(user_name): user = authorized_user() model = DOMAIN['items'] item_ids = [ObjectId(oid) for oid in user['stocks']] query = { 'status': 'published', '_id': {'$in': item_ids} } page = request.args.get('page', default=0, type=int) per_page = request.args.get('per_page', default=0, type=int) ret = model.get_index(query, per_page=per_page, page=page) return prep_response(ret)
def authenticated_users_items(): user = authorized_user() model = DOMAIN['items'] status = request.args.get('status') page = request.args.get('page', default=0, type=int) per_page = request.args.get('per_page', default=0, type=int) query = { 'user_id': user['_id'], 'status': status } items = model.get_index(query, page=page, per_page=per_page) logger.debug(items["page"]) return prep_response(items)
def get_feeds(user_name): user = authorized_user() model = DOMAIN['items'] query = { 'status': 'published', '$or': [ {'user_id': {'$in': user['following_users']}}, {'tags': {'$in': user['following_tags']}}, ], } page = request.args.get('page', default=0, type=int) per_page = request.args.get('per_page', default=0, type=int) ret = model.get_index(query, per_page=per_page, page=page) return prep_response(ret)
def upload_icon(): try: user = authorized_user() uploaded_files = request.files.getlist("files[]") if len(uploaded_files) > 1: uploaded_files = [uploaded_files[-1]] filenames = img_upload(uploaded_files) if len(filenames) > 0: # removed old image if user['user_icon'] != 'default.png': os.remove(os.path.join(app.config['UPLOAD_FOLDER'], user['user_icon'])) DOMAIN['users'].patch(user['_id'], {'user_icon': filenames[-1]}) return prep_response({}) except Exception as e: logger.debug(str(e)) return prep_response('Upload error', 500)
def item_page(user_name, oid): """ 記事閲覧 """ user = authorized_user() author = DOMAIN['users'].get_by_identify(user_name) model = DOMAIN['items'] query = {'status': 'published', 'user_id': author['_id']} item = model.get_by_id(oid, query) comments = DOMAIN["comments"].get_index({'item_id': oid}, sort=('created', 1)) stocks = DOMAIN["users"].get_index({'stocks': oid}) del author['password'] if item: return render_template('item.html', user=user, item=item, author=author, comments=comments, stocks=stocks, prefix=app.config["APPLICATION_ROOT"]) else: abort(404)
def user_page(user_name, target=None): """ ユーザページ """ user = authorized_user() author = DOMAIN['users'].get_by_identify(user_name, password=False) if author: model = DOMAIN['items'] query = {'status': 'published', 'user_id': author['_id']} item = model.get_index(query, sort=("created", -1)) comments = DOMAIN["comments"].get_index({'user_id': author['_id']}) stock_ids = [ObjectId(x) for x in author['stocks']] stocks = model.get_index({'_id': {'$in': stock_ids}}) followers = DOMAIN['users'].get_index({'following_users': author['user_name']}, password=False) return render_template('users.html', user=user, item=item, author=author, stocks=stocks, comments=comments, followers=followers, target=target, prefix=app.config["APPLICATION_ROOT"]) else: abort(404)
def search(): """ 検索 """ user = authorized_user() model = DOMAIN["items"] q = request.args.get("query") terms = q.split() title = map(lambda x: {"title": re.compile(".*"+x+".*")}, terms) tags = map(lambda x: {"tags": re.compile(".*"+x+".*")}, terms) query = { "$or": [{"$and": list(title)}, {"$and": list(tags)}] } result = model.get_index(query) return render_template("search_result.html", user=user, items=result, query=q, prefix=app.config["APPLICATION_ROOT"])
def toggle_follow_tag(oid, operation): user = authorized_user() model = DOMAIN['users'] ret = model.toggle_array(user['_id'], 'following_tags', oid, operation) return prep_response(ret)
def toggle_stock(oid, operation): user = authorized_user() model = DOMAIN['users'] ret = model.toggle_array(user['_id'], "stocks", oid, operation) return prep_response(ret)
def drafts(oid=None): """ 下書き一覧 """ user = authorized_user() return render_template("drafts.html", oid=oid, user=user, prefix=app.config["APPLICATION_ROOT"])
def setting(): """ 設定ページ """ user = authorized_user() return render_template('settings.html', user=user, prefix=app.config["APPLICATION_ROOT"])