-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
99 lines (82 loc) · 2.81 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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
from flask import Flask, render_template, request, redirect, session, url_for
from flask_pymongo import PyMongo
from flask_wtf import FlaskForm
from wtforms import TextField, PasswordField, BooleanField, SubmitField
from pymongo import MongoClient
from wtforms.validators import InputRequired, Length
import random
name = ''
rand = ''
app = Flask(__name__)
app.config['SECRET_KEY']= 'TheMainFile'
client = MongoClient("mongodb://****************************")
db = client['url_shortner']
users = db.users
class LoginForm(FlaskForm):
username = TextField('username', validators=[InputRequired()])
password = PasswordField('password', validators=[InputRequired()])
urls = TextField('urls')
@app.route('/', methods=['POST','GET'])
def index():
form = LoginForm()
users = db.users
msg = ''
if request.method == 'POST':
search = users.find_one({'username':form.username.data})
if search:
msg = 'Username already exists'
else:
users.insert({'username':form.username.data,'password':form.password.data})
return redirect(url_for('login'))
return render_template('signup.html', form=form, msg=msg)
@app.route('/login/', methods=['POST','GET'])
def login():
form = LoginForm()
users = db.users
session['username'] = None
msg = 'Invalid Username or Password'
if request.method == 'POST':
search = users.find_one({'username':form.username.data})
if search is None:
return render_template('index.html', msg=msg, form=form)
authu = search['username']
authp = search['password']
if authu == form.username.data and authp == form.password.data:
session['username'] = form.username.data
return redirect(url_for('url', name=authu))
return render_template('index.html', msg=msg, form=form)
return render_template('index.html', form=form)
@app.route('/<name>', methods=['POST','GET'])
def url(name):
form = LoginForm()
urls = db[name]
msg = None
if session['username']:
if request.method == 'POST':
search = urls.find_one({'real':form.urls.data})
if search:
msg = 'URL already shortned'
return redirect(url_for('url', name=name))
else:
rand = random.randint(0, pow(10, 5))
temp = "http://127.0.0.1:5000/"+name+"/"+ str(rand)
urls.insert({'real':form.urls.data,'short':temp})
return redirect(url_for('url', name=name))
else:
hi = urls.find()
return render_template('shortner.html', form=form, name=name, hi=hi)
return redirect(url_for('login'))
@app.route('/<name>/<trunc>', methods=['POST','GET'])
def link(name,trunc):
urls = db[name]
search = urls.find_one({'short':'http://127.0.0.1:5000/'+name+'/'+trunc})
if search:
return redirect(search['real'])
return redirect(url_for('url', name=name))
@app.route('/logout')
def logout():
session['username'] = None
return redirect(url_for('login'))
if __name__ == '__main__':
app.secret_key= 'TheMainFile'
app.run(debug=True)