mirror of
https://gitlab.ub.uni-bielefeld.de/sfb1288inf/nopaque.git
synced 2025-06-14 10:00:40 +00:00
Change the user session SocketIO Logic
This commit is contained in:
5
app/users/__init__.py
Normal file
5
app/users/__init__.py
Normal file
@ -0,0 +1,5 @@
|
||||
from flask import Blueprint
|
||||
|
||||
|
||||
bp = Blueprint('users', __name__)
|
||||
from . import events, routes # noqa
|
32
app/users/events.py
Normal file
32
app/users/events.py
Normal file
@ -0,0 +1,32 @@
|
||||
from app import hashids, socketio
|
||||
from app.decorators import socketio_login_required
|
||||
from app.models import User
|
||||
from flask_login import current_user
|
||||
from flask_socketio import join_room, leave_room
|
||||
|
||||
|
||||
@socketio.on('SUBSCRIBE /users/<user_id>')
|
||||
@socketio_login_required
|
||||
def subscribe_user(user_hashid):
|
||||
user_id = hashids.decode(user_hashid)
|
||||
user = User.query.get(user_id)
|
||||
if user is None:
|
||||
return {'code': 404, 'msg': 'Not found'}
|
||||
if not (user == current_user or current_user.is_administrator):
|
||||
return {'code': 403, 'msg': 'Forbidden'}
|
||||
dict_user = user.to_dict(backrefs=True, relationships=True)
|
||||
join_room(f'/users/{user.hashid}')
|
||||
return {'code': 200, 'msg': 'OK', 'payload': dict_user}
|
||||
|
||||
|
||||
@socketio.on('UNSUBSCRIBE /users/<user_id>')
|
||||
@socketio_login_required
|
||||
def subscribe_user(user_hashid):
|
||||
user_id = hashids.decode(user_hashid)
|
||||
user = User.query.get(user_id)
|
||||
if user is None:
|
||||
return {'code': 404, 'msg': 'Not found'}
|
||||
if not (user == current_user or current_user.is_administrator):
|
||||
return {'code': 403, 'msg': 'Forbidden'}
|
||||
leave_room(f'/users/{user.hashid}')
|
||||
return {'code': 200, 'msg': 'OK'}
|
50
app/users/routes.py
Normal file
50
app/users/routes.py
Normal file
@ -0,0 +1,50 @@
|
||||
from app.models import User
|
||||
from flask import render_template, request, url_for
|
||||
from . import bp
|
||||
|
||||
|
||||
@bp.route('/')
|
||||
def users():
|
||||
return render_template(
|
||||
'users/users.html.j2',
|
||||
title='Users'
|
||||
)
|
||||
|
||||
|
||||
@bp.route('/api_users')
|
||||
def api_users():
|
||||
query = User.query
|
||||
|
||||
# search filter
|
||||
search = request.args.get('search')
|
||||
if search:
|
||||
query = query.filter(User.username.like(f'%{search}%') | User.email.like(f'%{search}%'))
|
||||
total = query.count()
|
||||
|
||||
# sorting
|
||||
sort = request.args.get('sort')
|
||||
if sort:
|
||||
order = []
|
||||
for s in sort.split(','):
|
||||
direction = s[0]
|
||||
name = s[1:]
|
||||
if name not in ['username', 'email']:
|
||||
name = 'username'
|
||||
col = getattr(User, name)
|
||||
if direction == '-':
|
||||
col = col.desc()
|
||||
order.append(col)
|
||||
if order:
|
||||
query = query.order_by(*order)
|
||||
|
||||
# pagination
|
||||
offset = request.args.get('offset', type=int, default=-1)
|
||||
limit = request.args.get('limit', type=int, default=-1)
|
||||
if offset != -1 and limit != -1:
|
||||
query = query.offset(offset).limit(limit)
|
||||
|
||||
# response
|
||||
return {
|
||||
'data': [user.to_dict() for user in query],
|
||||
'total': total
|
||||
}
|
Reference in New Issue
Block a user