def delete(bot, msg): """Delete a shorturl.""" slug = msg.match.group(1) with shorturl_db(user='******', password=bot.mysql_password) as ctx: delete_shorturl(ctx, slug) msg.respond(f'shorturl `{slug}` has been deleted.')
def replace(bot, msg): """Replace the target of a shorturl slug.""" slug = msg.match.group(1) new_target = msg.match.group(2) with shorturl_db(user='******', password=bot.mysql_password) as ctx: replace_shorturl(ctx, slug, new_target) msg.respond(f'shorturl `{slug}` updated')
def rename(bot, msg): """Rename a shorturl.""" old_slug = msg.match.group(1) new_slug = msg.match.group(2) with shorturl_db(user='******', password=bot.mysql_password) as ctx: rename_shorturl(ctx, old_slug, new_slug) msg.respond(f'shorturl `{old_slug}` has been renamed to `{new_slug}`')
def show(bot, msg): """Return a shorturl by slug.""" slug = msg.match.group(1) with shorturl_db() as ctx: target = get_shorturl(ctx, slug) if not target: msg.respond(f'shorturl `{slug}` does not exist.') else: msg.respond(target, ping=False)
def delete_url(query=None): password = pass_extract() with shorturl_db(user='******', password=password) as ctx: try: delete_shorturl(ctx, query) except: return render_template('page.html', success='Error') else: return render_template('page.html', success='Deleted', search_results=[]) return render_template('page.html', success='Error')
def search_url(): query = request.form['searchInput'] password = pass_extract() with shorturl_db(user='******', password=password) as ctx: try: target = get_shorturl(ctx, query) except: return render_template('page.html', success='Error') else: target = target if target else "No defined shortlink" search_results = [{'name' : query, 'url' : target}] return render_template('page.html', success=None, search_results=search_results) return render_template('page.html', success='Error')
def add(bot, msg): """Add a new shorturl.""" slug = msg.match.group(1) target = msg.match.group(2) if len(slug) > 100: msg.respond('shorturl slugs must be <= 100 characters') return with shorturl_db(user='******', password=bot.mysql_password) as ctx: # validation occurs in ocflib, and uniqueness is guaranteed # by the database constraint. The error will propagate up if # someone tries to add or rename an entry that results in a dupe add_shorturl(ctx, slug, target) msg.respond('shorturl added as `{}`'.format(slug))
def add(bot, msg): """Add a new shorturl.""" slug = msg.match.group(1) target = msg.match.group(2) if len(slug) > 100: msg.respond('shorturl slugs must be <= 100 characters') return with shorturl_db(user='******', password=bot.mysql_password) as ctx: try: add_shorturl(ctx, slug, target) except pymysql.err.IntegrityError: msg.respond( 'shorturl already exists, did you mean `!shorturl replace`?', ) else: msg.respond(f'shorturl added as `{slug}`')
def add_url(): original_url = request.form['urlInput'] new_name = request.form['nameInput'] password = pass_extract() if len(new_name) > 100: return render_template('page.html', success='New name too long') with shorturl_db(user='******', password=password) as ctx: try: add_shorturl(ctx, new_name, original_url) except: return render_template('page.html', success='Error') else: return render_template('page.html', success='Added') return render_template('page.html', success='Error', search_results=[])