-
Notifications
You must be signed in to change notification settings - Fork 2
/
views.py
executable file
·189 lines (154 loc) · 6.37 KB
/
views.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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
'''
Backend on heroku. For localhost run views.2py
'''
from gevent import monkey
monkey.patch_all()
import time
from threading import Thread
from flask import Flask, render_template, session, request, redirect, url_for, session
from flask.ext.socketio import SocketIO, emit
import random
import string
import mongosaveapi
app = Flask(__name__)
app.debug = True
app.config['SECRET_KEY'] = 'xxxxxxxx'
socketio = SocketIO(app)
thread = None
masterdict = {}
codepadlst = [None] * 2 # for maintaining the rooms in codepad
count = 0
uniqid = 'random'
@app.route('/about')
def about():
return render_template('about.html')
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':
if request.form.get('login', None) == 'Sign Up/Login':
user_email = request.form.get('email', None)
user_pass = request.form.get('password', None)
if len(user_email) > 5 and len(user_pass) > 5:
dbclass = mongosaveapi.mongosave()
session_id = dbclass.userlogin(user_email, user_pass)
print 'SESSION_ID', session_id, '\n'
if (session_id == -1):
return 'ERROR: RELOAD'
if (session_id == -2):
return 'User already exists and wrong password for this email'
session['id'] = session_id
#this condition for directly going to publicpad using link
if request.args.get('idofpad'):
return redirect(url_for('publicpad', padid=request.args.get('idofpad')))
return redirect(url_for('index'))
else:
return render_template('login.html')
return render_template('login.html')
@app.route('/logout', methods=['GET', 'POST'])
def logout():
session.pop('id', None)
return redirect(url_for('index'))
@app.route('/publicpad/<padid>') #unique url
def publicpad(padid):
session_id = session.get('id', 0)
if not session_id:
return redirect(url_for('login', idofpad=padid))
dbclass = mongosaveapi.mongosave()
valid_check = dbclass.login_check(session_id)
print '%%%%%%%%%%%%%%%', session_id, '\n'
if valid_check == -1:
return "Please login again"
else:
return render_template('newpad.html', sessionid=session_id)
@app.route('/docpad/<padid>')
def textpad(padid):
return render_template('docpad.html')
@app.route('/pastepad/<padid>', methods=['GET','POST'])
def pastepad(padid):
global uniqid
uniqid = padid
checkpad = mongosaveapi.mongosave()
pasteres = checkpad.check(padid)#x random data
if pasteres:
if request.form.get('pasteedit', None) == 'Edit':
return render_template("pastepad.html", result=pasteres)
return render_template("pasteresult.html", result=pasteres)
else:
return render_template('pastepad.html')
@app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST'])
def index():
if request.method == 'POST':
if request.form.get('public', None) == 'New Public Codepad':
codepadurl = ''.join(random.choice(string.ascii_letters+string.digits) for i in range(9))
return redirect(url_for('publicpad', padid=codepadurl))
if request.form.get('docpad', None) == 'New Document Pad':
docpadurl = ''.join(random.choice(string.ascii_letters+string.digits) for i in range(8))
return redirect(url_for('textpad', padid=docpadurl))
if request.form.get('paste', None) == 'Paste Pad':
uniquelink = ''.join(random.choice(string.ascii_letters+string.digits) for i in range(10))
return redirect(url_for('pastepad', padid=uniquelink))
return render_template("index.html", title="CodeBuddy")
@socketio.on('connect', namespace='/test')
def local_client_connect():
pass
@socketio.on('dbpaste', namespace='/test')
def test_message(message):
saveobj = mongosaveapi.mongosave()
saveobj.save(uniqid, message['data'])
emit('my response', {'data': message['data']})
@socketio.on('removeid', namespace='/test')
def remove_id(message):
print 'disconnecting\n'
if message['id'] in codepadlst:
codepadlst[codepadlst.index(message['id'])] = None
print codepadlst
@socketio.on('sync', namespace='/test') #code sync
def sync(message):
#doing all the checks for only two connection allowable
#########This is not executed for 3rd pad. look into it#########
data = masterdict.get(message['id'])
session_id = session['id']
print 'PRINT ', data, session_id
if not data:
masterdict[message['id']] = []
masterdict[message['id']].append(session_id)
emit('sync response', {'data': message['data'], 'id': session_id, 'url': message['id']}, broadcast=True)
else:
if len(data) < 2 and session_id not in data:
masterdict[message['id']].append(session_id)
emit('sync response', {'data': message['data'], 'id': session_id, 'url': message['id']}, broadcast=True)
print 'MasterDict right now ', masterdict
if session_id in data:
emit('sync response', {'data': message['data'], 'id': session_id, 'url': message['id']}, broadcast=True)
elif len(data)>=2 and session_id not in data:
emit('error response', {'error': 'CodePad already in use. Ask creater to allow you. Thank you.', 'block': 'yes'})
def fool():
if codepadlst[0] == None and message['id'] not in codepadlst:
codepadlst[0] = message['id']
elif codepadlst[1] == None and message['id'] not in codepadlst:
codepadlst[1] = message['id']
elif message['id'] in codepadlst:
pass
else:
emit('error response', {'error': 'CodePad already in use. Ask creater to allow you. Thank you.'})
return
emit('sync response', {'data': message['data'], 'id': message['id']}, broadcast=True)
@socketio.on('pingback', namespace='/test') #chek connection
def ping(message):
print codepadlst
if message['id'] in codepadlst:
return
else:
try:
del(codepadlst[codepadlst.index(message['id'])])
except:
print('ERROR')
@socketio.on('connect', namespace='/test')
def test_connect():
pass
@socketio.on('disconnect', namespace='/test')
def test_disconnect():
print('Client disconnected')
if __name__ == '__main__':
socketio.run(app, host='0.0.0.0')