Commits

Roma Sokolov committed 60c78cb

error handling in main

Comments (0)

Files changed (1)

 @app.route('/get/<path:tags>', methods=['GET'])
 @split_tags
 def get(*tags):
-    page = request.args.get('page', 1)
-    count = config.ITEMS_COUNT
+    if tags:
+        page = request.args.get('page', 1)
+        count = config.ITEMS_COUNT
 
-    tempkey = str(time.time())
-    storage.zinterstore(tempkey, tags, 'MAX')
-    result = storage.zrange(tempkey, (page - 1) * count, page * count)
-    storage.delete(tempkey)
+        tempkey = str(time.time())
+        storage.zinterstore(tempkey, tags, 'MAX')
+        result = storage.zrange(tempkey, (page - 1) * count,
+                                page * count)
+        storage.delete(tempkey)
 
-    items = imap(uni, result)
+        items = imap(uni, result)
 
-    last_page = True if len(result) < count else False
+        last_page = True if len(result) < count else False
 
-    return render_template('list.html', tags=tags, page=page,
-                           items=items, last_page=last_page)
+        return render_template('list.html', tags=tags, page=page,
+                               items=items, last_page=last_page)
+    else:
+        return redirect(url_for('main'))
 
 
 @app.route('/add/', methods=['POST'])
 @split_tags
 def add(*tags):
     if tags:
-        _add(tags)
-        return redirect(url_for('get', tags=' '.join(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):
-    _add(tags)
-    tmp = '''Note:`{item}` was succesfully added with tags:{tags}.
-    <a href="{url}">Find similiar</a>
-    '''
-    return tmp.format(item=item, tags=' '.join(tags),
-                      url=url_for('get', tags='/'.join(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']
+    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, 1)
+        storage.zadd(tag, item, 1) # TODO: fix it!
 
 
 @app.route('/about', methods=['GET'])