def follow(username): try: user = User.get(User.username**username) except DoesNotExist: abort(406) else: try: Relationship.get(Relationship.from_user == g.user, Relationship.to_user == user) except DoesNotExist: Relationship.create(from_user=g.user, to_user=user) flash('Followed {}!'.format(user.username)) return redirect(url_for('user_view', username=user.username)) else: flash('You already followed {}.'.format(user.username)) return redirect(url_for('user_view', username=user.username))
def bookmark_add(): error = {} bookmark = {} user = auth.get_logged_in_user() if request.method == 'POST': if not request.form['url']: error['url'] = u'书签的网址不能为空' if not request.form['url'].startswith('http://') and not request.form['url'].startswith('https://'): request.form['url'] = ''.join(['http://', request.form['url']]) if not error: try: bookmark = Bookmark.select().where(Bookmark.user == user, Bookmark.url == request.form['url'] ).get() except Bookmark.DoesNotExist: try: db.database.set_autocommit(False) bookmark = Bookmark.create( user=user, url=request.form['url'], title=request.form['title'] ) bookmark.fetch_image() bookmark.save() tagnames = re.split('\s+', request.form['tags'].strip()) # marksure request.form['tags'] not a empty string if tagnames[0]: for tagname in tagnames: if not Tag.select().where(Tag.user == user, Tag.name == tagname ).exists(): tag = Tag.create(user=user, name=tagname) tag.save() relationship = Relationship.create( user=user, tag=tag, bookmark=bookmark) relationship.save() except Exception as e: db.database.rollback() flash(u'对不起,服务器太累了,刚罢工了一会儿', 'error') else: try: db.database.commit() except Exception as e: db.database.rollback() flash(u'对不起,服务器太累了,刚罢工了一会儿', 'error') finally: db.database.set_autocommit(True) if not get_flashed_messages(): flash(u'你已经成功添加一个书签', 'success') return redirect(url_for('bookmark')) else: flash(Markup(u'书签已经存在,也许你想要<a href="' + url_for('bookmark_edit', id=bookmark.id) + u'">编辑</a>此书签'), 'info') return render_template('bookmark_add.html', error=error, form=request.form, user=user, bookmark=bookmark)
def follow(username): """Follow user function. Login required. :param username: :return: redirect() """ try: to_user = User.get(User.username**username) except User.DoesNotExist: abort(404) else: try: Relationship.create(from_user=g.user._get_current_object(), to_user=to_user) except Relationship.IntegrityError: to_user = User.get(User.username**username) else: flash("Following {}".format(to_user.username), "success") return redirect(url_for('feed', username=to_user.username))
def bookmark_edit(id): user = auth.get_logged_in_user() bookmark = {} try: bookmark = Bookmark.get(Bookmark.id == id) bookmark.tags = ' '.join([Tag.get(Tag.id == tagID).name for tagID in [tag.tag for tag in bookmark.Tags]]) except Bookmark.DoesNotExist: flash(u'你要编辑的书签不存在', 'error') return redirect(url_for('page_404')) error = {} if request.method == 'POST': try: bookmark = Bookmark.get(Bookmark.id == request.form['id']) except Bookmark.DoesNotExist: flash(u'你要编辑的书签不存在', 'error') return redirect(url_for('page_404')) if not request.form['url']: error['url'] = u'书签的网址不能为空' if not error: try: db.database.set_autocommit(False) # before update image, should remove old version if bookmark.url != request.form['url']: bookmark.destory_image() bookmark.url = request.form['url'] bookmark.fetch_image() bookmark.title = request.form['title'] bookmark.save() tagnames = re.split('\s+', request.form['tags'].strip()) # marksure request.form['tags'] not a empty string if tagnames[0]: for tagname in tagnames: if not Tag.select().where(Tag.user == user, Tag.name == tagname ).exists(): tag = Tag.create(user=user, name=tagname) tag.save() relationship = Relationship.create( user=user, tag=tag, bookmark=bookmark) relationship.save() except Exception as e: db.database.rollback() flash(u'对不起,服务器太累了,刚罢工了一会儿', 'error') else: try: db.database.commit() except Exception as e: db.database.rollback() flash(u'对不起,服务器太累了,刚罢工了一会儿', 'error') finally: db.database.set_autocommit(True) if not get_flashed_messages(): flash(u'你刚刚完成一个书签的编辑', 'success') return redirect(url_for('bookmark')) return render_template('bookmark_edit.html', error=error, form=request.form, bookmark=bookmark, user=user)