-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
55 lines (41 loc) · 1.61 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
from flask import Flask, redirect, url_for, render_template, request,session, flash
from pymongo import MongoClient
app= Flask(__name__)
app.secret_key='wreyuiyhsfkdl972348v7897v9080kljvkjhgv8709HKJHG0jf'
clinet =MongoClient()
client = MongoClient("mongodb+srv://flyingphoneix:[email protected]/?retryWrites=true&w=majority")
# client = MongoClient('mongodb://localhost:27017/')
mydatabase = client['url_shortner']
mycollection=mydatabase['urls']
@app.route('/')
def home():
return render_template('home.html')
@app.route('/shorten', methods=['GET','POST'])
def short():
if request.method=='GET':
return render_template('method_error.html')
else:
code=request.form['code']
long_url=request.form['long_url']
query=mydatabase['urls'].find_one({"code":code})
if query is None:
rec={
"long_url":long_url,
"code":code
}
record=mydatabase['urls'].insert_one(rec)
new_url="https://url-shortner-x6kn.onrender.com/"+code
# new_url="127.0.0.1:5000/"+code
return render_template('success.html',new_url=new_url)
else:
flash('Code is already in use Please Try Again with Different Code')
return redirect(url_for('home'))
@app.route('/<string:code>')
def redirection(code):
query=mydatabase['urls'].find_one({'code':code})
if query is None:
flash("NO SUCH CODE")
return redirect(url_for('home'))
else:
rurl=query['long_url']
return redirect(rurl)