MCPcopy Create free account
hub / github.com/codemistic/Web-Development / index

Function index

Url Shortener/core/routes.py:17–49  ·  view source on GitHub ↗

The main view receives POST and GET requests

()

Source from the content-addressed store, hash-verified

15
16@app.route('/', methods=['GET', 'POST'])
17def 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>')
52def redirect_url(short_id: str):

Callers

nothing calls this directly

Calls 2

ShortUrlClass · 0.90
generate_short_idFunction · 0.85

Tested by

no test coverage detected