The main view receives POST and GET requests
()
| 15 | |
| 16 | @app.route('/', methods=['GET', 'POST']) |
| 17 | def index(): |
| 18 | """ |
| 19 | The main view receives POST and GET requests |
| 20 | """ |
| 21 | if request.method == 'POST': |
| 22 | url = request.form['url'] |
| 23 | short_id = request.form['custom_id'] |
| 24 | |
| 25 | if short_id and ShortUrl.query.filter_by(short_id=short_id).first() is not None: |
| 26 | flash('Please enter different custom id!') |
| 27 | return redirect(url_for('index')) |
| 28 | |
| 29 | if not validators.url(url): |
| 30 | flash('Enter a valid url.') |
| 31 | return redirect(url_for('index')) |
| 32 | |
| 33 | if not url: |
| 34 | flash('The URL is required!') |
| 35 | return redirect(url_for('index')) |
| 36 | |
| 37 | if not short_id: |
| 38 | short_id = generate_short_id(8) |
| 39 | |
| 40 | new_link = ShortUrl(original_url=url, |
| 41 | short_id=short_id, |
| 42 | created_at=datetime.now()) |
| 43 | db.session.add(new_link) |
| 44 | db.session.commit() |
| 45 | short_url = request.host_url + short_id |
| 46 | |
| 47 | return render_template('index.html', short_url=short_url) |
| 48 | |
| 49 | return render_template('index.html') |
| 50 | |
| 51 | @app.route('/<short_id>') |
| 52 | def redirect_url(short_id: str): |
nothing calls this directly
no test coverage detected