-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
executable file
·280 lines (218 loc) · 7.74 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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#!/usr/bin/python
import os
import json
from flask import Flask, Response, render_template, url_for, jsonify, request, session, redirect, flash
from werkzeug import secure_filename
from flask.ext.sqlalchemy import SQLAlchemy, BaseQuery
from flask.ext.bcrypt import Bcrypt
from flask.ext.socketio import SocketIO, emit, send, join_room, leave_room
from flask.ext.login import LoginManager, login_user, login_required, logout_user, current_user
from sqlalchemy_searchable import search, make_searchable
import gevent
import gevent.monkey
from gevent.pywsgi import WSGIServer
gevent.monkey.patch_all()
from fuzzywuzzy import process
from forms import *
#Creating the flask app and pointing to the config
app = Flask(__name__)
app.config.from_object('config.DevelopmentConfig')
#create database object
db = SQLAlchemy(app)
make_searchable()
#create brypt object
bcrypt = Bcrypt(app)
#Create socketio server instance
socketio = SocketIO(app)
#create instance of Login Manager
login_manager = LoginManager()
login_manager.init_app(app)
from models import *
onlineUsers = [];
@app.route('/', methods=['GET', 'POST'])
@app.route('/index', methods=['GET', 'POST'])
@app.route('/index/<int:page>', methods=['GET', 'POST'])
def home(page=1):
if current_user.is_authenticated():
files = filestable.query.filter_by(ownerhostel = current_user.hostel).paginate(page,app.config["FILES_PER_PAGE"], False)
for i in xrange(0, len(files.items)):
files.items[i].name = files.items[i].name.replace("_"," ")
return render_template('index.html', files=files)
else:
return redirect(url_for('add'))
def event_stream():
count = 0
while True:
gevent.sleep(2)
yield 'data: %s\n\n' % count
count += 1
@app.route('/sse_event_source')
def sse_request():
return Response(
event_stream(),
mimetype='text/event-stream')
@app.route('/sse')
def sse_page():
return render_template('sse.html')
@app.route('/add', methods=['GET', 'POST'])
@login_required
def add():
if request.method == 'POST':
receivedNames = request.json["names"]
receivedSizes = request.json["sizes"]
receivedExtns = request.json["extensions"]
for i in xrange(0, len(receivedNames)):
securename = secure_filename(receivedNames[i])
qry = filestable(unicode(securename), receivedExtns[i], receivedSizes[i], 'misc', current_user.id, current_user.hostel, 0)
db.session.add(qry)
db.session.commit()
result = filestable.query.all()
print result
return render_template('add.html')
return render_template('add.html')
login_manager.login_view = "login"
login_manager.login_message = "Please login to view this page"
@login_manager.user_loader
def load_user(userid):
return User.query.filter(User.id == int(userid)).first()
@app.route('/login', methods=['GET', 'POST'])
def login():
error = ''
form = LoginForm(request.form)
if request.method == 'POST':
if form.validate_on_submit():
user = User.query.filter_by(username=request.form['username']).first()
if user is not None and bcrypt.check_password_hash(user.password, request.form['password']):
#session['logged_in'] = True
result = login_user(user)
print result
flash('You are now logged in.')
return redirect(url_for('home'))
else:
error = "Invalid Credentials, try again"
else:
render_template('login.html', form=form, error=error)
return render_template('login.html', form=form)
@app.route('/newuser', methods=['GET', 'POST'])
def newuser():
form = RegisterForm()
if form.validate_on_submit():
user = User(
username=form.username.data,
password=form.password.data,
firstname=form.firstname.data,
lastname=form.lastname.data,
hostel=form.hostel.data,
year=form.year.data,
room=form.room.data
)
db.session.add(user)
db.session.commit()
login_user(user)
return redirect(url_for('home'))
return render_template('newuser.html', form=form)
@app.route('/logout')
@login_required
def logout():
logout_user()
flash('You have been logged out')
return redirect(url_for('login'))
@app.route('/search', methods=['GET', 'POST'])
@login_required
def search():
form = SearchForm()
return render_template('search.html', form=form)
@app.route('/results', methods=['GET', 'POST'])
def results():
if request.method == 'POST':
qry = request.json["query"]
print qry
dbresults = []
dbresults = filestable.query.filter_by(ownerhostel = current_user.hostel).search(unicode(qry)).all()
i = 0
print 'HELLO ' + str(dbresults)
if len(dbresults) < 15:
print "Inside"
much = 15 - len(dbresults)
dbresults += filestable.query.search(unicode(qry)).limit(much).all()
print len(dbresults)
if len(dbresults) > 0:
print 'dbresults is : ' + str(type(dbresults))
print 'Type of dbresults[0] is : ' + str(type(dbresults[0]))
dbresultsname = []
print 'AND NOW ' + str(dbresults[0].ownerhostel)
for i in xrange(0, len(dbresults)):
print str(type(dbresults[i]))
dbresultsname.append(str(dbresults[i].name.replace("_"," ")))
i = i + 1
fuzzyResults = process.extract(unicode(qry),dbresultsname,limit=5)
print 'AND HI ' + str(fuzzyResults)
else:
fuzzyResults = ""
print "Sorry No results"
return jsonify(result = fuzzyResults)
else:
return redirect(url_for('search'))
@app.route('/filetransfer/', defaults={'room_name': 'default'})
@app.route('/filetransfer/<room_name>')
@login_required
def filetransfer(room_name):
return render_template('filetransfer.html', room_name=room_name)
@app.route('/sse')
def sse():
return redirect(url_for('home'))
clients = {}
def logger(text):
array = ['Message from server: ']
array.append(text)
emit('logger', array)
@socketio.on('got connected')
def handle_got_connected():
print('Received id: ' + str(request.namespace.socket.sessid))
@socketio.on('create or join')
def create_or_join(room):
print 'Received request from clientid' + request.namespace.socket.sessid + ' to create or join room ' + str(room)
if str(room) in clients:
clients[str(room)].append(request.namespace.socket.sessid)
print 'Dictionary being updated'
else:
clients.update({ str(room): [request.namespace.socket.sessid]})
print 'Dictionary entry being created'
numClients = len(clients[str(room)])
print numClients
if numClients <= 1:
join_room(str(room))
logger('Client ID ' + request.namespace.socket.sessid + ' created room ' + str(room))
print 'Client ID ' + request.namespace.socket.sessid + ' created room ' + str(room)
emit('created', room, request.namespace.socket.sessid)
elif numClients <= 2:
logger('Client ID ' + request.namespace.socket.sessid + ' joined room ' + str(room))
print 'Client ID ' + request.namespace.socket.sessid + ' joined room ' + str(room)
join_room(str(room))
emit('joined', room, request.namespace.socket.sessid)
emit('nowready', room=room) #This sends it to all the clients FROM the server since the socketio
else:#Max 2 clients
print "Room is full"
emit('full', room)
@socketio.on('message')
def message(message):
logger('Client said: ' + str(message))
emit('message', message, broadcast=True)
@socketio.on('on_disconnect')
def on_disconnect(room):
print 'Client id '+ request.namespace.socket.sessid +' disconnected'
if str(room) in clients:
if request.namespace.socket.sessid in clients[str(room)] is not None:
print 'Removed'
clients[room].remove(request.namespace.socket.sessid)
@socketio.on('leave')
def on_leave(room):
if str(room) in clients:
if request.namespace.socket.sessid in clients[str(room)]:
logger('Client id ' + request.namespace.socket.sessid + ' has left the room ' + str(room))
print 'Client id ' + request.namespace.socket.sessid + ' has left the room ' + str(room)
leave_room(str(room))
clients[str(room)].remove(request.namespace.socket.sessid)
#start the server with the run method
if __name__ == '__main__':
socketio.run(app, host='127.0.0.1', port=4000,policy_server=False)