def add_bookmark(): form = BookmarkForm(link='http://') if form.validate_on_submit() and request.method == 'POST': # Get form data link = form.link.data follow_redirects = form.follow_redirects.data # T/F for following link redirects # Test that link works, or return error to user try: r = requests.get(link, headers={'user-agent': USER_AGENT}, allow_redirects=follow_redirects, timeout=TIMEOUT) r.raise_for_status() # Exceptions for 400 or 500 errors except requests.exceptions.HTTPError as e: flash('Please check your link. It leads to this error: {}.'.format( e), category='danger') # Missing schema except requests.exceptions.MissingSchema as e: flash('No schema. Did you mean http://{}?'.format(link), category='danger') # Timeout errors except requests.exceptions.Timeout: flash('There was a timeout error. Please check URL.', category='danger') # Connection errors except requests.exceptions.ConnectionError: flash('Could not connect to your link. Please check URL.', category='danger') # Too many redirects except requests.exceptions.TooManyRedirects: flash('Exceeded max number of redirects. Please check URL.', category='danger') # All other requests-related exceptions except requests.exceptions.RequestException as e: flash('Please check your link. It leads to this error: {}.'.format( e), category='danger') # No errors when requesting link, so add to database else: # Generate a possible id b_id = hex_gen() # If it exists, keep regenerating while (Bookmark.query.filter(Bookmark.id == b_id).one_or_none() is not None): b_id = hex_gen() url = r.url # Get final url (from redirects) url_root = request.url_root b = Bookmark(b_id, link=url, user_id=flask_login.current_user.id) db_session.add(b) db_session.commit() msg_uf = ('Successfully added {0}. Your short link is ' + '<a target="_blank" href="{1}">{1}</a>.') msg = msg_uf.format(url, url_root + b_id) flash(Markup(msg), category='success') return redirect(url_for('add_bookmark'), 303) return render_template('add_bookmark.html', form=form)
def bookmark_edit(id): bookmark = Bookmark.query.filter_by(id=id).first() if not bookmark: return abort(404) form = BookmarkForm(request.form, obj=bookmark) if form.validate_on_submit(): form.populate_obj(bookmark) db.session.add(bookmark) db.session.commit() flash(u'Done!') return redirect('%d' % bookmark.id) flash_errors(form) return render_template('bookmark-form.html', form=form)
def edit_bookmark(bookmark_id): bookmark = Bookmark.query.get_or_404(bookmark_id) if current_user != bookmark.user: return render_template('403.html') form = BookmarkForm(obj=bookmark) if form.validate_on_submit(): form.populate_obj(bookmark) db.session.commit() flash("Stored '{}'".format(bookmark.description)) return redirect(url_for('user', username=current_user.username)) return render_template('bookmark_form.html', form=form, title="Edit bookmark")
def add(): form = BookmarkForm() if form.validate_on_submit(): url = form.url.data description = form.description.data tags = form.tags.data bm = Bookmark(user=current_user, url=url, description=description, tags=tags) db.session.add(bm) db.session.commit() flash("Stored '{}'".format(description)) return redirect(url_for('index')) return render_template('bookmark_form.html', form=form, title="Add a bookmark")
def bookmark_new(): form = BookmarkForm(request.form) if form.validate_on_submit(): try: bookmark = Bookmark(form.url.data, form.title.data, description=form.description.data, tags=form.tags.data, public=form.public.data) db.session.add(bookmark) db.session.commit() except IntegrityError: flash(u'Boomark for "%s" exists already.' % form.url.data, 'error') return render_template('bookmark-form.html', form=form) flash(u'Done!') return redirect('%d' % bookmark.id) flash_errors(form) return render_template('bookmark-form.html', form=form)