1
0
mirror of https://gitlab.com/animath/si/plateforme-corres2math.git synced 2024-12-05 01:26:54 +00:00

Store auth token to don't login everytime

This commit is contained in:
Yohann D'ANELLO 2020-10-29 15:40:30 +01:00
parent db30b481a3
commit 238333a175
2 changed files with 30 additions and 2 deletions

View File

@ -262,6 +262,9 @@ class TeamLeaveView(LoginRequiredMixin, TemplateView):
request.user.registration.team = None
request.user.registration.save()
get_sympa_client().unsubscribe(request.user.email, f"equipe-{team.trigram.lower()}", False)
Matrix.kick(f"#team-{team.trigram.lower()}:correspondances-maths.fr",
f"@{request.user.registration.matrix_username}:correspondances-maths.fr",
"Équipe quittée")
if team.students.count() + team.coachs.count() == 0:
team.delete()
return redirect(reverse_lazy("index"))

View File

@ -1,3 +1,4 @@
import os
from typing import Any, Dict, Optional, Union
from asgiref.sync import async_to_sync
@ -6,11 +7,27 @@ from nio import AsyncClient, RoomCreateError, RoomCreateResponse, RoomKickRespon
class Matrix:
_token: str = None
_device_id: str = None
@classmethod
async def _get_client(cls) -> AsyncClient:
# TODO Store
client = AsyncClient("https://correspondances-maths.fr", "@corres2mathbot:correspondances-maths.fr")
await client.login("toto1234")
if os.path.isfile(".matrix_token"):
with open(".matrix_device", "r") as f:
cls._device_id = f.read().rstrip(" \t\r\n")
client.device_id = cls._device_id
with open(".matrix_token", "r") as f:
cls._token = f.read().rstrip(" \t\r\n")
client.access_token = cls._token
return client
await client.login(password="toto1234", device_name="Plateforme")
cls._token = client.access_token
cls._device_id = client.device_id
with open(".matrix_token", "w") as f:
f.write(cls._token)
return client
@classmethod
@ -49,3 +66,11 @@ class Matrix:
if room_id.startswith("#"):
room_id = await cls.resolve_room_alias(room_id)
return await client.room_invite(room_id, user_id)
@classmethod
@async_to_sync
async def kick(cls, room_id: str, user_id: str, reason: str = None) -> Union[RoomKickResponse, RoomInviteError]:
client = await cls._get_client()
if room_id.startswith("#"):
room_id = await cls.resolve_room_alias(room_id)
return await client.room_kick(room_id, user_id, reason)