1
0
mirror of https://gitlab.com/animath/si/plateforme-corres2math.git synced 2025-02-21 23:41:18 +00:00

Compare commits

..

No commits in common. "238333a1755ce28f221fb20e7a98a3dfe07d2f12" and "6647a284f4456ea35913e2b2310c0d6d219bfb61" have entirely different histories.

3 changed files with 15 additions and 37 deletions

View File

@ -1,19 +1,22 @@
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 nio import RoomPreset, RoomVisibility
from corres2math.matrix import Matrix
class Team(models.Model):

View File

@ -1,6 +1,10 @@
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
@ -9,12 +13,10 @@ 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
@ -262,9 +264,6 @@ 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,33 +1,17 @@
import os
import asyncio
from typing import Any, Dict, Optional, Union
from asgiref.sync import async_to_sync
from nio import AsyncClient, RoomCreateError, RoomCreateResponse, RoomKickResponse, RoomInviteError,\
RoomInviteResponse, RoomPreset, RoomResolveAliasResponse, RoomVisibility
from nio import AsyncClient, RoomCreateError, RoomCreateResponse, RoomInviteError, RoomInviteResponse, RoomPreset, \
RoomVisibility, RoomResolveAliasResponse
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")
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)
await client.login("toto1234")
return client
@classmethod
@ -66,11 +50,3 @@ 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)