remarkz / remarkz.py

#!/usr/local/bin/python
# -*- coding:utf-8 -*-

import time

from itertools import ifilter, imap

from flask import Flask, request, render_template, url_for, redirect

import redis

try:
    import local_config as config
except ImportError:
    import config

from utils import split_tags, format_output


app = Flask(config.NAME, config.STATIC_PATH)
app.config.from_object(config)

storage = redis.Redis(host=config.REDIS_HOST, port=config.REDIS_PORT,
                      db=config.REDIS_DB)

@app.route('/', methods=['GET'])
@app.route('/<path:tags>', methods=['GET'])
@split_tags
def main(*tags):
    if tags:
        page = int(request.args.get('page', 1))
        count = config.ITEMS_COUNT

        tempkey = str(time.time())
        storage.zinterstore(tempkey, tags, 'MAX')
        start = (page - 1) * count
        result = storage.zrevrange(tempkey, start,
                                   (page * count) - 1,
                                   True
                                   )
        storage.delete(tempkey)

        items = imap(format_output, result)

        last_page = True if len(result) < count else False

        return render_template('list.html', tags=tags, page=page,
                               start=start, last_page=last_page,
                               items=items)
    else:
        return render_template('main.html')

@app.route('/', methods=['GET'])
@app.route('/<path:tags>', methods=['GET'])
def get(tags):
    redirect(url_for('main'), tags)
        

@app.route('/add/', methods=['POST'])
@app.route('/add/<path:tags>', methods=['POST'])
@split_tags
def add(*tags):
    if tags:
        try:
            _add(tags)
            return redirect(url_for('get', tags=u'/'.join(tags)))
        except ValueError as e:
            return render_template('main.html', error=unicode(e))
    else:
        return render_template('main.html',
                               error='Provide at least one tag!')


@app.route('/bookmarklet/<path:tags>', methods=['POST'])
@split_tags
def bookmarklet(*tags):
    try:
        _add(tags)
        msg = u'''Remark was succesfully added with tags:{tags}.
        <a href="{url}">Show</a>
        '''.format(tags=' '.join(tags),
                   url=url_for('get', tags='/'.join(tags)))
    except ValueError as e:
        msg = unicode(e)
    return msg


def _add(tags):
    item = request.form['item'].strip()
    if len(item) > 200:
        raise ValueError(u'Error! Remark can contain at most 200 characters.')
    for tag in tags:
        storage.zadd(tag, item, int(time.time()))


@app.route('/about', methods=['GET'])
@app.route('/about/', methods=['GET', 'POST'])
@app.route('/about/<path:tags>', methods=['GET', 'POST'])
@split_tags
def about(*tags):
    if request.method == 'GET':
        return render_template('about.html')
    else:
        return render_template('about.html', tags=tags)


if __name__ == '__main__':
    app.run(config.HOST, config.PORT, debug=config.DEBUG)
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.