1
0
mirror of https://gitlab.com/animath/si/plateforme-corres2math.git synced 2025-02-16 01:41:17 +00:00

Compare commits

...

2 Commits

Author SHA1 Message Date
Yohann D'ANELLO
238333a175 Store auth token to don't login everytime 2020-10-29 15:40:30 +01:00
Yohann D'ANELLO
db30b481a3 Reorder imports 2020-10-29 15:09:24 +01:00
3 changed files with 37 additions and 15 deletions

View File

@ -1,22 +1,19 @@
import asyncio
import os
import re
from django.template.loader import render_to_string
from nio import RoomVisibility, RoomPreset
from corres2math.lists import get_sympa_client
from corres2math.matrix import Matrix
from django.core.exceptions import ObjectDoesNotExist
from django.core.validators import RegexValidator
from django.db import models
from django.db.models import Index
from django.template.loader import render_to_string
from django.urls import reverse_lazy
from django.utils import timezone
from django.utils.crypto import get_random_string
from django.utils.text import format_lazy
from django.utils.translation import gettext_lazy as _
from corres2math.matrix import Matrix
from nio import RoomPreset, RoomVisibility
class Team(models.Model):

View File

@ -1,10 +1,6 @@
import asyncio
from io import BytesIO
from zipfile import ZipFile
from django.shortcuts import redirect
from django.views.generic.base import TemplateView
from corres2math.lists import get_sympa_client
from corres2math.matrix import Matrix
from corres2math.views import AdminMixin
@ -13,10 +9,12 @@ from django.core.exceptions import PermissionDenied
from django.core.mail import send_mail
from django.db import transaction
from django.http import HttpResponse
from django.shortcuts import redirect
from django.template.loader import render_to_string
from django.urls import reverse_lazy
from django.utils.translation import gettext_lazy as _
from django.views.generic import CreateView, DetailView, FormView, RedirectView, UpdateView
from django.views.generic.base import TemplateView
from django.views.generic.edit import FormMixin, ProcessFormView
from django_tables2 import SingleTableView
from magic import Magic
@ -264,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,17 +1,33 @@
import asyncio
import os
from typing import Any, Dict, Optional, Union
from asgiref.sync import async_to_sync
from nio import AsyncClient, RoomCreateError, RoomCreateResponse, RoomInviteError, RoomInviteResponse, RoomPreset, \
RoomVisibility, RoomResolveAliasResponse
from nio import AsyncClient, RoomCreateError, RoomCreateResponse, RoomKickResponse, RoomInviteError,\
RoomInviteResponse, RoomPreset, RoomResolveAliasResponse, RoomVisibility
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
@ -50,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)