Spaces:
Running
Running
deploy from github actions
Browse files- app/routers/alabanza.py +6 -4
- app/routers/iglesia.py +0 -28
- app/routers/lyrics.py +60 -43
- app/routers/songs.py +24 -13
- limpiar_compartidos_huerfanos.py +80 -0
app/routers/alabanza.py
CHANGED
|
@@ -689,6 +689,9 @@ def eliminar_grupo(grupo_id: int, db: Session = Depends(get_db), current_user: U
|
|
| 689 |
# Eliminar invitaciones del grupo
|
| 690 |
db.query(Invitation).filter(Invitation.group_id == grupo_id).delete()
|
| 691 |
|
|
|
|
|
|
|
|
|
|
| 692 |
# Eliminar miembros del grupo
|
| 693 |
db.query(UserGroup).filter(UserGroup.group_id == grupo_id).delete()
|
| 694 |
|
|
@@ -699,10 +702,9 @@ def eliminar_grupo(grupo_id: int, db: Session = Depends(get_db), current_user: U
|
|
| 699 |
|
| 700 |
# Notificar a los miembros por WebSocket que el grupo fue eliminado
|
| 701 |
try:
|
| 702 |
-
|
| 703 |
-
|
| 704 |
-
"grupo_id": grupo_id
|
| 705 |
-
}, db)
|
| 706 |
except Exception:
|
| 707 |
pass
|
| 708 |
|
|
|
|
| 689 |
# Eliminar invitaciones del grupo
|
| 690 |
db.query(Invitation).filter(Invitation.group_id == grupo_id).delete()
|
| 691 |
|
| 692 |
+
# Guardar a quién avisar antes de borrar las membresías (después ya no queda nadie en el grupo)
|
| 693 |
+
member_ids = [m.user_id for m in db.query(UserGroup.user_id).filter(UserGroup.group_id == grupo_id, UserGroup.estado == "activo").all()]
|
| 694 |
+
|
| 695 |
# Eliminar miembros del grupo
|
| 696 |
db.query(UserGroup).filter(UserGroup.group_id == grupo_id).delete()
|
| 697 |
|
|
|
|
| 702 |
|
| 703 |
# Notificar a los miembros por WebSocket que el grupo fue eliminado
|
| 704 |
try:
|
| 705 |
+
from app.websocket_manager import send_sync_message
|
| 706 |
+
for uid in member_ids:
|
| 707 |
+
send_sync_message({"type": "group_deleted", "grupo_id": grupo_id}, uid)
|
|
|
|
| 708 |
except Exception:
|
| 709 |
pass
|
| 710 |
|
app/routers/iglesia.py
CHANGED
|
@@ -736,20 +736,6 @@ def expulsar_usuario(iglesia_id: int, user_id: int,
|
|
| 736 |
if not member:
|
| 737 |
raise HTTPException(status_code=404, detail="Usuario no encontrado")
|
| 738 |
member.estado = "expulsado"
|
| 739 |
-
|
| 740 |
-
# Expulsar de todos los grupos de alabanza de esta iglesia y revocar propiedad intelectual
|
| 741 |
-
try:
|
| 742 |
-
from app.routers.alabanza import revoke_user_group_intellectual_property, broadcast_to_group
|
| 743 |
-
church_groups = db.query(Group).filter(Group.iglesia_id == iglesia_id).all()
|
| 744 |
-
for cg in church_groups:
|
| 745 |
-
ug = db.query(UserGroup).filter(UserGroup.user_id == user_id, UserGroup.group_id == cg.id).first()
|
| 746 |
-
if ug:
|
| 747 |
-
ug.estado = "expulsado"
|
| 748 |
-
revoke_user_group_intellectual_property(user_id, cg.id, db)
|
| 749 |
-
broadcast_to_group(cg.id, {"type": "group_updated", "grupo_id": cg.id}, db)
|
| 750 |
-
except Exception as e_grp:
|
| 751 |
-
print(f"[Iglesia] Error revocando grupos de alabanza al expulsar usuario: {e_grp}")
|
| 752 |
-
|
| 753 |
db.commit()
|
| 754 |
from app.websocket_manager import send_sync_message
|
| 755 |
send_sync_message({"type": "user_expelled", "iglesia_id": iglesia_id}, user_id)
|
|
@@ -782,20 +768,6 @@ def salir_iglesia(iglesia_id: int, db: Session = Depends(get_db), current_user:
|
|
| 782 |
|
| 783 |
# Borrar la membresia (ponerlo como salido)
|
| 784 |
member.estado = "salido"
|
| 785 |
-
|
| 786 |
-
# Salir de todos los grupos de alabanza de esta iglesia y revocar propiedad intelectual
|
| 787 |
-
try:
|
| 788 |
-
from app.routers.alabanza import revoke_user_group_intellectual_property, broadcast_to_group
|
| 789 |
-
church_groups = db.query(Group).filter(Group.iglesia_id == iglesia_id).all()
|
| 790 |
-
for cg in church_groups:
|
| 791 |
-
ug = db.query(UserGroup).filter(UserGroup.user_id == current_user.id, UserGroup.group_id == cg.id).first()
|
| 792 |
-
if ug:
|
| 793 |
-
ug.estado = "salido"
|
| 794 |
-
revoke_user_group_intellectual_property(current_user.id, cg.id, db, current_user.is_premium)
|
| 795 |
-
broadcast_to_group(cg.id, {"type": "group_updated", "grupo_id": cg.id}, db)
|
| 796 |
-
except Exception as e_grp:
|
| 797 |
-
print(f"[Iglesia] Error revocando grupos de alabanza al salir de iglesia: {e_grp}")
|
| 798 |
-
|
| 799 |
db.commit()
|
| 800 |
broadcast_to_church(iglesia_id, {"type": "church_updated", "iglesia_id": iglesia_id}, db)
|
| 801 |
return {"message": "Has salido de la iglesia exitosamente"}
|
|
|
|
| 736 |
if not member:
|
| 737 |
raise HTTPException(status_code=404, detail="Usuario no encontrado")
|
| 738 |
member.estado = "expulsado"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 739 |
db.commit()
|
| 740 |
from app.websocket_manager import send_sync_message
|
| 741 |
send_sync_message({"type": "user_expelled", "iglesia_id": iglesia_id}, user_id)
|
|
|
|
| 768 |
|
| 769 |
# Borrar la membresia (ponerlo como salido)
|
| 770 |
member.estado = "salido"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 771 |
db.commit()
|
| 772 |
broadcast_to_church(iglesia_id, {"type": "church_updated", "iglesia_id": iglesia_id}, db)
|
| 773 |
return {"message": "Has salido de la iglesia exitosamente"}
|
app/routers/lyrics.py
CHANGED
|
@@ -24,6 +24,12 @@ def _store_lyric_cover(cover: Optional[str]) -> Optional[str]:
|
|
| 24 |
except ValueError as e:
|
| 25 |
raise HTTPException(status_code=400, detail=f"Error subiendo portada: {e}")
|
| 26 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
# --- LYRICS ENDPOINTS ---
|
| 28 |
|
| 29 |
@router.post("")
|
|
@@ -178,11 +184,16 @@ def update_lyric(
|
|
| 178 |
repertoire = db.query(Repertorio).filter(Repertorio.id == edit_context_repertoire_id).first()
|
| 179 |
if not repertoire:
|
| 180 |
raise HTTPException(status_code=404, detail="Repertorio no encontrado")
|
| 181 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 182 |
if db_lyric.is_repertoire_only:
|
| 183 |
# Actualizar directamente si ya es exclusivo de repertorios
|
| 184 |
-
|
| 185 |
-
setattr(db_lyric, key, value)
|
| 186 |
db.commit()
|
| 187 |
db.refresh(db_lyric)
|
| 188 |
return db_lyric
|
|
@@ -242,11 +253,22 @@ def update_lyric(
|
|
| 242 |
group = db.query(Group).filter(Group.id == edit_context_group_id).first()
|
| 243 |
if not group:
|
| 244 |
raise HTTPException(status_code=404, detail="Grupo no encontrado")
|
| 245 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 246 |
is_exclusive_to_group = db_lyric.group_id == edit_context_group_id and len(db_lyric.group_ids) <= 1 and not db_lyric.is_repertoire_only
|
| 247 |
if is_exclusive_to_group:
|
| 248 |
-
|
| 249 |
-
setattr(db_lyric, key, value)
|
| 250 |
db.commit()
|
| 251 |
db.refresh(db_lyric)
|
| 252 |
try:
|
|
@@ -310,6 +332,8 @@ def update_lyric(
|
|
| 310 |
|
| 311 |
# --- CASO 3: Edición desde el Catálogo Personal (Creador original) ---
|
| 312 |
else:
|
|
|
|
|
|
|
| 313 |
has_shared_references = len(db_lyric.groups_shared) > 0
|
| 314 |
if has_shared_references and update_references is False:
|
| 315 |
# Clonar la letra original con el contenido actual (viejo) para los grupos y repertorios
|
|
@@ -358,9 +382,7 @@ def update_lyric(
|
|
| 358 |
db.add(new_rl)
|
| 359 |
|
| 360 |
# Guardar los nuevos cambios en la original (personal)
|
| 361 |
-
|
| 362 |
-
if key != "group_id":
|
| 363 |
-
setattr(db_lyric, key, value)
|
| 364 |
|
| 365 |
db.commit()
|
| 366 |
db.refresh(db_lyric)
|
|
@@ -374,8 +396,7 @@ def update_lyric(
|
|
| 374 |
return db_lyric
|
| 375 |
else:
|
| 376 |
# Edición directa (in-place)
|
| 377 |
-
|
| 378 |
-
setattr(db_lyric, key, value)
|
| 379 |
|
| 380 |
if db_lyric.group_id is not None:
|
| 381 |
group = db.query(Group).filter(Group.id == db_lyric.group_id).first()
|
|
@@ -398,30 +419,13 @@ def delete_lyric(lyric_id: int, db: Session = Depends(get_db), current_user: Use
|
|
| 398 |
if not db_lyric:
|
| 399 |
raise HTTPException(status_code=404, detail="Letra no encontrada")
|
| 400 |
|
| 401 |
-
|
| 402 |
-
if db_lyric.created_by =
|
| 403 |
-
is_authorized = True
|
| 404 |
-
else:
|
| 405 |
-
from app.routers.alabanza import check_group_permission
|
| 406 |
-
allowed_group_ids = list(db_lyric.group_ids)
|
| 407 |
-
if db_lyric.group_id is not None:
|
| 408 |
-
allowed_group_ids.append(db_lyric.group_id)
|
| 409 |
-
for gid in allowed_group_ids:
|
| 410 |
-
ug = db.query(UserGroup).filter(
|
| 411 |
-
UserGroup.user_id == current_user.id,
|
| 412 |
-
UserGroup.group_id == gid,
|
| 413 |
-
UserGroup.estado == "activo"
|
| 414 |
-
).first()
|
| 415 |
-
if ug and (ug.role == "admin" or check_group_permission(ug, "letras", "delete", db)):
|
| 416 |
-
is_authorized = True
|
| 417 |
-
break
|
| 418 |
-
|
| 419 |
-
if not is_authorized:
|
| 420 |
raise HTTPException(
|
| 421 |
-
status_code=403,
|
| 422 |
-
detail="
|
| 423 |
)
|
| 424 |
-
|
| 425 |
shared_group_ids = list(db_lyric.group_ids)
|
| 426 |
if db_lyric.group_id is not None and db_lyric.group_id not in shared_group_ids:
|
| 427 |
shared_group_ids.append(db_lyric.group_id)
|
|
@@ -688,12 +692,18 @@ def expulsar_miembro_grupo(group_id: int, user_id: int, db: Session = Depends(ge
|
|
| 688 |
if not ug_admin:
|
| 689 |
raise HTTPException(status_code=403, detail="No autorizado")
|
| 690 |
|
|
|
|
|
|
|
| 691 |
ug_target = db.query(UserGroup).filter_by(user_id=user_id, group_id=group_id).first()
|
| 692 |
if not ug_target:
|
| 693 |
raise HTTPException(status_code=404, detail="Usuario no encontrado en el grupo")
|
| 694 |
-
|
| 695 |
ug_target.estado = "expulsado"
|
|
|
|
|
|
|
|
|
|
| 696 |
db.commit()
|
|
|
|
| 697 |
return {"message": "Usuario expulsado del grupo"}
|
| 698 |
|
| 699 |
@router.get("/groups/{group_id}/solicitudes")
|
|
@@ -758,34 +768,41 @@ def share_lyric(
|
|
| 758 |
is_owner = lyric.created_by == current_user.id
|
| 759 |
is_group_admin = ug.role == "admin"
|
| 760 |
|
| 761 |
-
# Para "share":
|
| 762 |
-
# Para "unshare":
|
| 763 |
if data.action == "share":
|
| 764 |
-
if not is_owner
|
|
|
|
|
|
|
| 765 |
raise HTTPException(status_code=403, detail="No tienes permisos para compartir letras con este grupo")
|
| 766 |
elif data.action == "unshare":
|
| 767 |
if not is_owner and not is_group_admin:
|
| 768 |
raise HTTPException(status_code=403, detail="Solo el creador o un administrador puede desvincular esta letra del grupo")
|
| 769 |
-
|
|
|
|
|
|
|
| 770 |
group = db.query(Group).filter(Group.id == data.group_id).first()
|
| 771 |
if not group:
|
| 772 |
raise HTTPException(status_code=404, detail="Grupo no encontrado")
|
| 773 |
|
| 774 |
if data.action == "share":
|
| 775 |
-
if ug.role != "admin" and not ug.perm_compartir_letras:
|
| 776 |
-
raise HTTPException(status_code=403, detail="No tienes permisos para compartir letras con este grupo")
|
| 777 |
if group not in lyric.groups_shared:
|
| 778 |
lyric.groups_shared.append(group)
|
| 779 |
if lyric.group_id is None:
|
| 780 |
lyric.group_id = data.group_id
|
| 781 |
lyric.created_at = datetime.utcnow()
|
| 782 |
-
|
| 783 |
if group in lyric.groups_shared:
|
| 784 |
lyric.groups_shared.remove(group)
|
| 785 |
if lyric.group_id == data.group_id:
|
| 786 |
lyric.group_id = lyric.groups_shared[0].id if lyric.groups_shared else None
|
| 787 |
-
|
| 788 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 789 |
|
| 790 |
db.commit()
|
| 791 |
try:
|
|
|
|
| 24 |
except ValueError as e:
|
| 25 |
raise HTTPException(status_code=400, detail=f"Error subiendo portada: {e}")
|
| 26 |
|
| 27 |
+
def _apply_lyric_update(db_lyric: Lyric, lyric_update: LyricCreate):
|
| 28 |
+
"""Copia los campos editables. Grupo, clon y repertorio no cambian al editar: el grupo solo se cambia al compartir."""
|
| 29 |
+
for key, value in lyric_update.dict().items():
|
| 30 |
+
if key not in ("group_id", "is_repertoire_only", "parent_lyric_id"):
|
| 31 |
+
setattr(db_lyric, key, value)
|
| 32 |
+
|
| 33 |
# --- LYRICS ENDPOINTS ---
|
| 34 |
|
| 35 |
@router.post("")
|
|
|
|
| 184 |
repertoire = db.query(Repertorio).filter(Repertorio.id == edit_context_repertoire_id).first()
|
| 185 |
if not repertoire:
|
| 186 |
raise HTTPException(status_code=404, detail="Repertorio no encontrado")
|
| 187 |
+
in_repertoire = db.query(RepertorioLyric).filter(
|
| 188 |
+
RepertorioLyric.repertorio_id == edit_context_repertoire_id,
|
| 189 |
+
RepertorioLyric.lyric_id == db_lyric.id
|
| 190 |
+
).first()
|
| 191 |
+
if not in_repertoire:
|
| 192 |
+
raise HTTPException(status_code=400, detail="La letra no pertenece a este repertorio")
|
| 193 |
+
|
| 194 |
if db_lyric.is_repertoire_only:
|
| 195 |
# Actualizar directamente si ya es exclusivo de repertorios
|
| 196 |
+
_apply_lyric_update(db_lyric, lyric_update)
|
|
|
|
| 197 |
db.commit()
|
| 198 |
db.refresh(db_lyric)
|
| 199 |
return db_lyric
|
|
|
|
| 253 |
group = db.query(Group).filter(Group.id == edit_context_group_id).first()
|
| 254 |
if not group:
|
| 255 |
raise HTTPException(status_code=404, detail="Grupo no encontrado")
|
| 256 |
+
if edit_context_group_id not in db_lyric.group_ids and db_lyric.group_id != edit_context_group_id:
|
| 257 |
+
raise HTTPException(status_code=400, detail="La letra no está compartida con este grupo")
|
| 258 |
+
is_active_member = db.query(UserGroup).filter(
|
| 259 |
+
UserGroup.user_id == current_user.id,
|
| 260 |
+
UserGroup.group_id == edit_context_group_id,
|
| 261 |
+
UserGroup.estado == "activo"
|
| 262 |
+
).first()
|
| 263 |
+
if not is_active_member:
|
| 264 |
+
raise HTTPException(status_code=403, detail="No perteneces de forma activa a este grupo")
|
| 265 |
+
from app.routers.alabanza import check_group_permission
|
| 266 |
+
if db_lyric.created_by != current_user.id and not check_group_permission(is_active_member, "letras", "edit", db):
|
| 267 |
+
raise HTTPException(status_code=403, detail="No tienes permisos para editar letras en este grupo")
|
| 268 |
+
|
| 269 |
is_exclusive_to_group = db_lyric.group_id == edit_context_group_id and len(db_lyric.group_ids) <= 1 and not db_lyric.is_repertoire_only
|
| 270 |
if is_exclusive_to_group:
|
| 271 |
+
_apply_lyric_update(db_lyric, lyric_update)
|
|
|
|
| 272 |
db.commit()
|
| 273 |
db.refresh(db_lyric)
|
| 274 |
try:
|
|
|
|
| 332 |
|
| 333 |
# --- CASO 3: Edición desde el Catálogo Personal (Creador original) ---
|
| 334 |
else:
|
| 335 |
+
if db_lyric.created_by != current_user.id:
|
| 336 |
+
raise HTTPException(status_code=403, detail="Solo el creador puede editar la letra original")
|
| 337 |
has_shared_references = len(db_lyric.groups_shared) > 0
|
| 338 |
if has_shared_references and update_references is False:
|
| 339 |
# Clonar la letra original con el contenido actual (viejo) para los grupos y repertorios
|
|
|
|
| 382 |
db.add(new_rl)
|
| 383 |
|
| 384 |
# Guardar los nuevos cambios en la original (personal)
|
| 385 |
+
_apply_lyric_update(db_lyric, lyric_update)
|
|
|
|
|
|
|
| 386 |
|
| 387 |
db.commit()
|
| 388 |
db.refresh(db_lyric)
|
|
|
|
| 396 |
return db_lyric
|
| 397 |
else:
|
| 398 |
# Edición directa (in-place)
|
| 399 |
+
_apply_lyric_update(db_lyric, lyric_update)
|
|
|
|
| 400 |
|
| 401 |
if db_lyric.group_id is not None:
|
| 402 |
group = db.query(Group).filter(Group.id == db_lyric.group_id).first()
|
|
|
|
| 419 |
if not db_lyric:
|
| 420 |
raise HTTPException(status_code=404, detail="Letra no encontrada")
|
| 421 |
|
| 422 |
+
# La letra es del creador: solo él la elimina. Un admin del grupo solo puede quitarla de su grupo (unshare).
|
| 423 |
+
if db_lyric.created_by != current_user.id:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 424 |
raise HTTPException(
|
| 425 |
+
status_code=403,
|
| 426 |
+
detail="Solo el creador puede eliminar esta letra. Si eres administrador del grupo, puedes quitarla del grupo."
|
| 427 |
)
|
| 428 |
+
|
| 429 |
shared_group_ids = list(db_lyric.group_ids)
|
| 430 |
if db_lyric.group_id is not None and db_lyric.group_id not in shared_group_ids:
|
| 431 |
shared_group_ids.append(db_lyric.group_id)
|
|
|
|
| 692 |
if not ug_admin:
|
| 693 |
raise HTTPException(status_code=403, detail="No autorizado")
|
| 694 |
|
| 695 |
+
if user_id == current_user.id:
|
| 696 |
+
raise HTTPException(status_code=400, detail="No puedes expulsarte a ti mismo")
|
| 697 |
ug_target = db.query(UserGroup).filter_by(user_id=user_id, group_id=group_id).first()
|
| 698 |
if not ug_target:
|
| 699 |
raise HTTPException(status_code=404, detail="Usuario no encontrado en el grupo")
|
| 700 |
+
|
| 701 |
ug_target.estado = "expulsado"
|
| 702 |
+
from app.routers.alabanza import revoke_user_group_intellectual_property, broadcast_to_group
|
| 703 |
+
target_user = db.query(User).filter(User.id == user_id).first()
|
| 704 |
+
revoke_user_group_intellectual_property(user_id, group_id, db, bool(target_user and target_user.is_premium))
|
| 705 |
db.commit()
|
| 706 |
+
broadcast_to_group(group_id, {"type": "group_updated", "grupo_id": group_id}, db)
|
| 707 |
return {"message": "Usuario expulsado del grupo"}
|
| 708 |
|
| 709 |
@router.get("/groups/{group_id}/solicitudes")
|
|
|
|
| 768 |
is_owner = lyric.created_by == current_user.id
|
| 769 |
is_group_admin = ug.role == "admin"
|
| 770 |
|
| 771 |
+
# Para "share": sólo el creador (y con permiso de compartir en ese grupo)
|
| 772 |
+
# Para "unshare": creador o admin del grupo (el admin puede sacarla de su grupo)
|
| 773 |
if data.action == "share":
|
| 774 |
+
if not is_owner:
|
| 775 |
+
raise HTTPException(status_code=403, detail="Solo el creador puede compartir esta letra")
|
| 776 |
+
if not is_group_admin and not ug.perm_compartir_letras:
|
| 777 |
raise HTTPException(status_code=403, detail="No tienes permisos para compartir letras con este grupo")
|
| 778 |
elif data.action == "unshare":
|
| 779 |
if not is_owner and not is_group_admin:
|
| 780 |
raise HTTPException(status_code=403, detail="Solo el creador o un administrador puede desvincular esta letra del grupo")
|
| 781 |
+
else:
|
| 782 |
+
raise HTTPException(status_code=400, detail="Acción inválida")
|
| 783 |
+
|
| 784 |
group = db.query(Group).filter(Group.id == data.group_id).first()
|
| 785 |
if not group:
|
| 786 |
raise HTTPException(status_code=404, detail="Grupo no encontrado")
|
| 787 |
|
| 788 |
if data.action == "share":
|
|
|
|
|
|
|
| 789 |
if group not in lyric.groups_shared:
|
| 790 |
lyric.groups_shared.append(group)
|
| 791 |
if lyric.group_id is None:
|
| 792 |
lyric.group_id = data.group_id
|
| 793 |
lyric.created_at = datetime.utcnow()
|
| 794 |
+
else:
|
| 795 |
if group in lyric.groups_shared:
|
| 796 |
lyric.groups_shared.remove(group)
|
| 797 |
if lyric.group_id == data.group_id:
|
| 798 |
lyric.group_id = lyric.groups_shared[0].id if lyric.groups_shared else None
|
| 799 |
+
# Quitar también sus referencias en los repertorios de ese grupo
|
| 800 |
+
group_rep_ids = [r.id for r in db.query(Repertorio.id).filter(Repertorio.group_id == data.group_id).all()]
|
| 801 |
+
if group_rep_ids:
|
| 802 |
+
db.query(RepertorioLyric).filter(
|
| 803 |
+
RepertorioLyric.repertorio_id.in_(group_rep_ids),
|
| 804 |
+
RepertorioLyric.lyric_id == lyric_id
|
| 805 |
+
).delete(synchronize_session=False)
|
| 806 |
|
| 807 |
db.commit()
|
| 808 |
try:
|
app/routers/songs.py
CHANGED
|
@@ -1164,33 +1164,41 @@ def share_song(
|
|
| 1164 |
is_owner = task.user_id == current_user.id
|
| 1165 |
is_group_admin = ug.role == "admin"
|
| 1166 |
|
| 1167 |
-
# Para "share":
|
| 1168 |
-
# Para "unshare":
|
| 1169 |
if data.action == "share":
|
| 1170 |
-
if not is_owner
|
|
|
|
|
|
|
| 1171 |
raise HTTPException(status_code=403, detail="No tienes permisos para compartir pistas con este grupo")
|
| 1172 |
elif data.action == "unshare":
|
| 1173 |
if not is_owner and not is_group_admin:
|
| 1174 |
raise HTTPException(status_code=403, detail="Solo el propietario o un administrador puede desvincular esta pista del grupo")
|
| 1175 |
-
|
|
|
|
|
|
|
| 1176 |
group = db.query(Group).filter(Group.id == data.group_id).first()
|
| 1177 |
if not group:
|
| 1178 |
raise HTTPException(status_code=404, detail="Grupo no encontrado")
|
| 1179 |
|
| 1180 |
if data.action == "share":
|
| 1181 |
-
if ug.role != "admin" and not ug.perm_compartir_pistas:
|
| 1182 |
-
raise HTTPException(status_code=403, detail="No tienes permisos para compartir pistas con este grupo")
|
| 1183 |
if group not in task.groups_shared:
|
| 1184 |
task.groups_shared.append(group)
|
| 1185 |
if task.group_id is None:
|
| 1186 |
task.group_id = data.group_id
|
| 1187 |
-
|
|
|
|
|
|
|
| 1188 |
if group in task.groups_shared:
|
| 1189 |
task.groups_shared.remove(group)
|
| 1190 |
if task.group_id == data.group_id:
|
| 1191 |
task.group_id = task.groups_shared[0].id if task.groups_shared else None
|
| 1192 |
-
|
| 1193 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1194 |
|
| 1195 |
db.commit()
|
| 1196 |
try:
|
|
@@ -1219,11 +1227,14 @@ def update_song(
|
|
| 1219 |
if not task:
|
| 1220 |
raise HTTPException(status_code=404, detail="Canción no encontrada")
|
| 1221 |
|
| 1222 |
-
# Verificar permisos: propietario o miembro
|
| 1223 |
if task.user_id != current_user.id:
|
| 1224 |
-
|
| 1225 |
-
|
| 1226 |
-
|
|
|
|
|
|
|
|
|
|
| 1227 |
raise HTTPException(status_code=403, detail="No tienes permisos para modificar esta canción")
|
| 1228 |
|
| 1229 |
if request.song_name is not None:
|
|
|
|
| 1164 |
is_owner = task.user_id == current_user.id
|
| 1165 |
is_group_admin = ug.role == "admin"
|
| 1166 |
|
| 1167 |
+
# Para "share": sólo el propietario (y con permiso de compartir en ese grupo)
|
| 1168 |
+
# Para "unshare": propietario o admin del grupo (el admin puede sacarla de su grupo)
|
| 1169 |
if data.action == "share":
|
| 1170 |
+
if not is_owner:
|
| 1171 |
+
raise HTTPException(status_code=403, detail="Solo el propietario puede compartir esta pista")
|
| 1172 |
+
if not is_group_admin and not ug.perm_compartir_pistas:
|
| 1173 |
raise HTTPException(status_code=403, detail="No tienes permisos para compartir pistas con este grupo")
|
| 1174 |
elif data.action == "unshare":
|
| 1175 |
if not is_owner and not is_group_admin:
|
| 1176 |
raise HTTPException(status_code=403, detail="Solo el propietario o un administrador puede desvincular esta pista del grupo")
|
| 1177 |
+
else:
|
| 1178 |
+
raise HTTPException(status_code=400, detail="Acción inválida")
|
| 1179 |
+
|
| 1180 |
group = db.query(Group).filter(Group.id == data.group_id).first()
|
| 1181 |
if not group:
|
| 1182 |
raise HTTPException(status_code=404, detail="Grupo no encontrado")
|
| 1183 |
|
| 1184 |
if data.action == "share":
|
|
|
|
|
|
|
| 1185 |
if group not in task.groups_shared:
|
| 1186 |
task.groups_shared.append(group)
|
| 1187 |
if task.group_id is None:
|
| 1188 |
task.group_id = data.group_id
|
| 1189 |
+
# Las pistas de grupo no expiran (misma política que al terminar el procesamiento)
|
| 1190 |
+
task.cloud_expires_at = None
|
| 1191 |
+
else:
|
| 1192 |
if group in task.groups_shared:
|
| 1193 |
task.groups_shared.remove(group)
|
| 1194 |
if task.group_id == data.group_id:
|
| 1195 |
task.group_id = task.groups_shared[0].id if task.groups_shared else None
|
| 1196 |
+
# Si ya no está en ningún grupo y el dueño es normal, vuelve a expirar
|
| 1197 |
+
if not task.groups_shared and task.cloud_expires_at is None and task.telegram_file_ids:
|
| 1198 |
+
from datetime import timedelta
|
| 1199 |
+
owner = db.query(User).filter(User.id == task.user_id).first()
|
| 1200 |
+
if not (owner and owner.is_premium):
|
| 1201 |
+
task.cloud_expires_at = datetime.utcnow() + timedelta(days=30)
|
| 1202 |
|
| 1203 |
db.commit()
|
| 1204 |
try:
|
|
|
|
| 1227 |
if not task:
|
| 1228 |
raise HTTPException(status_code=404, detail="Canción no encontrada")
|
| 1229 |
|
| 1230 |
+
# Verificar permisos: propietario o miembro de un grupo de la canción con permiso de edición
|
| 1231 |
if task.user_id != current_user.id:
|
| 1232 |
+
from app.routers.alabanza import check_group_permission
|
| 1233 |
+
can_edit = any(
|
| 1234 |
+
ug.estado == 'activo' and ug.group_id in task.group_ids and check_group_permission(ug, "pistas", "edit", db)
|
| 1235 |
+
for ug in current_user.groups
|
| 1236 |
+
)
|
| 1237 |
+
if not can_edit:
|
| 1238 |
raise HTTPException(status_code=403, detail="No tienes permisos para modificar esta canción")
|
| 1239 |
|
| 1240 |
if request.song_name is not None:
|
limpiar_compartidos_huerfanos.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
Quita de cada grupo las letras y pistas cuyo dueño ya no es miembro activo de ese grupo.
|
| 3 |
+
|
| 4 |
+
Antes otros miembros podían re-compartir archivos ajenos, y al salir el dueño esos archivos se quedaban
|
| 5 |
+
en grupos donde él no está. Usa la misma revocación que al salir o ser expulsado de un grupo.
|
| 6 |
+
|
| 7 |
+
Uso:
|
| 8 |
+
python limpiar_compartidos_huerfanos.py # solo muestra lo que quitaría
|
| 9 |
+
python limpiar_compartidos_huerfanos.py --aplicar # lo quita
|
| 10 |
+
"""
|
| 11 |
+
import os
|
| 12 |
+
import sys
|
| 13 |
+
|
| 14 |
+
sys.path.append(os.path.dirname(os.path.abspath(__file__)))
|
| 15 |
+
|
| 16 |
+
from app.database import SessionLocal
|
| 17 |
+
from app.models.lyrics import Group, UserGroup, Lyric, lyric_groups, task_groups
|
| 18 |
+
from app.models.process import Task
|
| 19 |
+
from app.models.user import User
|
| 20 |
+
from app.routers.alabanza import revoke_user_group_intellectual_property
|
| 21 |
+
|
| 22 |
+
|
| 23 |
+
def buscar_huerfanos(db):
|
| 24 |
+
"""Devuelve {(dueño, grupo): {"letras": [...], "pistas": [...]}} de lo compartido sin que el dueño esté activo."""
|
| 25 |
+
activos = {(ug.user_id, ug.group_id) for ug in db.query(UserGroup).filter(UserGroup.estado == "activo").all()}
|
| 26 |
+
huerfanos = {}
|
| 27 |
+
|
| 28 |
+
for lyric_id, group_id, owner_id, title in (
|
| 29 |
+
db.query(lyric_groups.c.lyric_id, lyric_groups.c.group_id, Lyric.created_by, Lyric.title)
|
| 30 |
+
.join(Lyric, Lyric.id == lyric_groups.c.lyric_id).all()
|
| 31 |
+
):
|
| 32 |
+
if (owner_id, group_id) not in activos:
|
| 33 |
+
huerfanos.setdefault((owner_id, group_id), {"letras": [], "pistas": []})["letras"].append(f"{lyric_id} {title}")
|
| 34 |
+
|
| 35 |
+
for task_id, group_id, owner_id, name in (
|
| 36 |
+
db.query(Task.task_id, task_groups.c.group_id, Task.user_id, Task.song_name)
|
| 37 |
+
.join(Task, Task.id == task_groups.c.task_id).all()
|
| 38 |
+
):
|
| 39 |
+
if (owner_id, group_id) not in activos:
|
| 40 |
+
huerfanos.setdefault((owner_id, group_id), {"letras": [], "pistas": []})["pistas"].append(f"{task_id} {name}")
|
| 41 |
+
|
| 42 |
+
return huerfanos
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
def main():
|
| 46 |
+
aplicar = "--aplicar" in sys.argv
|
| 47 |
+
db = SessionLocal()
|
| 48 |
+
try:
|
| 49 |
+
huerfanos = buscar_huerfanos(db)
|
| 50 |
+
if not huerfanos:
|
| 51 |
+
print("No hay archivos compartidos en grupos donde su dueño no esté activo.")
|
| 52 |
+
return
|
| 53 |
+
|
| 54 |
+
total_letras = total_pistas = 0
|
| 55 |
+
for (owner_id, group_id), items in sorted(huerfanos.items(), key=lambda kv: (kv[0][1] or 0, kv[0][0] or 0)):
|
| 56 |
+
owner = db.query(User).filter(User.id == owner_id).first()
|
| 57 |
+
group = db.query(Group).filter(Group.id == group_id).first()
|
| 58 |
+
print(f"\nGrupo {group_id} ({group.name if group else '?'}) - dueño {owner_id} ({owner.username if owner else '?'})")
|
| 59 |
+
for l in items["letras"]:
|
| 60 |
+
print(f" letra {l}")
|
| 61 |
+
for p in items["pistas"]:
|
| 62 |
+
print(f" pista {p}")
|
| 63 |
+
total_letras += len(items["letras"])
|
| 64 |
+
total_pistas += len(items["pistas"])
|
| 65 |
+
|
| 66 |
+
if aplicar:
|
| 67 |
+
revoke_user_group_intellectual_property(owner_id, group_id, db, bool(owner and owner.is_premium))
|
| 68 |
+
|
| 69 |
+
print(f"\nTotal: {total_letras} letras y {total_pistas} pistas en {len(huerfanos)} pares dueño/grupo.")
|
| 70 |
+
if aplicar:
|
| 71 |
+
db.commit()
|
| 72 |
+
print("Quitados de sus grupos.")
|
| 73 |
+
else:
|
| 74 |
+
print("Nada cambió. Ejecuta con --aplicar para quitarlos.")
|
| 75 |
+
finally:
|
| 76 |
+
db.close()
|
| 77 |
+
|
| 78 |
+
|
| 79 |
+
if __name__ == "__main__":
|
| 80 |
+
main()
|