78 lines
2.2 KiB
Python
78 lines
2.2 KiB
Python
from flask_login import current_user
|
|
from models import RoomMemberPermission
|
|
from datetime import datetime, timezone
|
|
|
|
def user_has_permission(room, perm_name):
|
|
"""
|
|
Check if the current user has a specific permission in a room.
|
|
|
|
Args:
|
|
room: Room object
|
|
perm_name: Name of the permission to check (e.g., 'can_view', 'can_upload')
|
|
|
|
Returns:
|
|
bool: True if user has permission, False otherwise
|
|
"""
|
|
# Admin users have all permissions
|
|
if current_user.is_admin:
|
|
return True
|
|
|
|
# Check if user is a member of the room
|
|
if current_user not in room.members:
|
|
return False
|
|
|
|
# Get user's permissions for this room
|
|
permission = RoomMemberPermission.query.filter_by(
|
|
room_id=room.id,
|
|
user_id=current_user.id
|
|
).first()
|
|
|
|
# If no specific permissions are set, user only has view access
|
|
if not permission:
|
|
return perm_name == 'can_view'
|
|
|
|
# Check the specific permission
|
|
return getattr(permission, perm_name, False)
|
|
|
|
def clean_path(path):
|
|
"""
|
|
Clean a file path by removing leading/trailing slashes and normalizing separators.
|
|
|
|
Args:
|
|
path: Path string to clean
|
|
|
|
Returns:
|
|
str: Cleaned path
|
|
"""
|
|
if not path:
|
|
return ''
|
|
return path.strip('/\\')
|
|
|
|
def timeago(date):
|
|
"""Convert a datetime to a human-readable time ago string."""
|
|
if not date:
|
|
return ''
|
|
|
|
# Convert naive datetime to UTC if it's naive
|
|
if date.tzinfo is None:
|
|
date = date.replace(tzinfo=timezone.utc)
|
|
|
|
now = datetime.now(timezone.utc)
|
|
diff = now - date
|
|
|
|
if diff.days > 365:
|
|
years = diff.days // 365
|
|
return f'{years} year{"s" if years != 1 else ""} ago'
|
|
elif diff.days > 30:
|
|
months = diff.days // 30
|
|
return f'{months} month{"s" if months != 1 else ""} ago'
|
|
elif diff.days > 0:
|
|
return f'{diff.days} day{"s" if diff.days != 1 else ""} ago'
|
|
elif diff.seconds > 3600:
|
|
hours = diff.seconds // 3600
|
|
return f'{hours} hour{"s" if hours != 1 else ""} ago'
|
|
elif diff.seconds > 60:
|
|
minutes = diff.seconds // 60
|
|
return f'{minutes} minute{"s" if minutes != 1 else ""} ago'
|
|
else:
|
|
return 'just now' |