Compare commits
4 Commits
refactoring
...
poc-sip
| Author | SHA1 | Date | |
|---|---|---|---|
| d0fef03555 | |||
| 2a32330c0d | |||
| 9117bf3bef | |||
| 668c692127 |
@@ -141,6 +141,13 @@ class RoomSerializer(serializers.ModelSerializer):
|
||||
),
|
||||
}
|
||||
|
||||
# Todo - discuss this part, retrieve phone number from a setting? Dynamically?
|
||||
# Todo - is it the right place?
|
||||
# Todo - discuss the method `to_representation` which is quite dirty IMO
|
||||
pin_code = self.instance.pin_code
|
||||
if pin_code is not None:
|
||||
output["livekit"]["sip"] = {"pin_code": pin_code, "phone_number": "wip"}
|
||||
|
||||
output["is_administrable"] = is_admin
|
||||
|
||||
return output
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
# Generated by Django 5.1.4 on 2025-01-12 22:30
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('core', '0009_alter_recording_status'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AlterModelOptions(
|
||||
name='resourceaccess',
|
||||
options={'ordering': ('-created_at',), 'verbose_name': 'Resource access', 'verbose_name_plural': 'Resource accesses'},
|
||||
),
|
||||
migrations.AlterModelOptions(
|
||||
name='user',
|
||||
options={'ordering': ('-created_at',), 'verbose_name': 'user', 'verbose_name_plural': 'users'},
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,18 @@
|
||||
# Generated by Django 5.1.4 on 2025-01-12 22:34
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('core', '0010_alter_resourceaccess_options_alter_user_options'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='room',
|
||||
name='pin_code',
|
||||
field=models.CharField(blank=True, help_text="Unique n-digit code that identifies this room. Automatically generated on creation. Displayed with '#' suffix.", max_length=100, null=True, unique=True, verbose_name='Room PIN code'),
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,34 @@
|
||||
from django.db import migrations
|
||||
import random
|
||||
|
||||
|
||||
def generate_pin_for_rooms(apps, schema_editor):
|
||||
"""Generate unique 9-digit PIN codes for existing rooms.
|
||||
|
||||
The PIN code is required for upcoming SIP telephony features.
|
||||
"""
|
||||
Room = apps.get_model('core', 'Room')
|
||||
rooms_without_pin = Room.objects.filter(pin_code__isnull=True)
|
||||
|
||||
def generate_pin():
|
||||
while True:
|
||||
pin = str(random.randint(0, 999999999)).zfill(9)
|
||||
if not Room.objects.filter(pin_code=pin).exists():
|
||||
return pin
|
||||
|
||||
for room in rooms_without_pin:
|
||||
room.pin_code = generate_pin()
|
||||
room.save()
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
dependencies = [
|
||||
('core', '0011_room_pin_code'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(
|
||||
generate_pin_for_rooms,
|
||||
reverse_code=migrations.RunPython.noop
|
||||
),
|
||||
]
|
||||
@@ -11,13 +11,15 @@ from django.contrib.auth import models as auth_models
|
||||
from django.contrib.auth.base_user import AbstractBaseUser
|
||||
from django.core import mail, validators
|
||||
from django.core.exceptions import PermissionDenied, ValidationError
|
||||
from django.db import models
|
||||
from django.db import IntegrityError, models
|
||||
from django.utils.functional import lazy
|
||||
from django.utils.text import capfirst, slugify
|
||||
from django.utils.translation import gettext_lazy as _
|
||||
|
||||
from timezone_field import TimeZoneField
|
||||
|
||||
from core import utils
|
||||
|
||||
logger = getLogger(__name__)
|
||||
|
||||
|
||||
@@ -361,13 +363,23 @@ class Room(Resource):
|
||||
primary_key=True,
|
||||
)
|
||||
slug = models.SlugField(max_length=100, blank=True, null=True, unique=True)
|
||||
|
||||
configuration = models.JSONField(
|
||||
blank=True,
|
||||
default=dict,
|
||||
verbose_name=_("Visio room configuration"),
|
||||
help_text=_("Values for Visio parameters to configure the room."),
|
||||
)
|
||||
pin_code = models.CharField(
|
||||
max_length=100,
|
||||
unique=True,
|
||||
blank=True,
|
||||
null=True,
|
||||
verbose_name=_("Room PIN code"),
|
||||
help_text=_(
|
||||
"Unique n-digit code that identifies this room. Automatically generated on creation."
|
||||
" Displayed with '#' suffix."
|
||||
),
|
||||
)
|
||||
|
||||
class Meta:
|
||||
db_table = "meet_room"
|
||||
@@ -378,6 +390,23 @@ class Room(Resource):
|
||||
def __str__(self):
|
||||
return capfirst(self.name)
|
||||
|
||||
def save(self, *args, **kwargs):
|
||||
"""Generate a unique n-digit pin code for new rooms.
|
||||
|
||||
This uses the DB to ensure uniqueness of the code. Better than checking separately
|
||||
with the DB and then saving, which introduces a race condition.
|
||||
"""
|
||||
|
||||
if self.pk or self.pin_code:
|
||||
return super().save(*args, **kwargs)
|
||||
|
||||
while True:
|
||||
try:
|
||||
self.pin_code = utils.generate_pin_code(n=settings.ROOM_PIN_CODE_LENGTH)
|
||||
return super().save(*args, **kwargs)
|
||||
except IntegrityError:
|
||||
continue
|
||||
|
||||
def clean_fields(self, exclude=None):
|
||||
"""
|
||||
Automatically generate the slug from the name and make sure it does not look like a UUID.
|
||||
|
||||
@@ -15,6 +15,11 @@ from django.conf import settings
|
||||
from livekit.api import AccessToken, VideoGrants
|
||||
|
||||
|
||||
def generate_pin_code(n: int) -> str:
|
||||
"""Generate a n-digit pin code"""
|
||||
return f"{''.join([str(random.randint(0, 9)) for _ in range(n)])}"
|
||||
|
||||
|
||||
def generate_color(identity: str) -> str:
|
||||
"""Generates a consistent HSL color based on a given identity string.
|
||||
|
||||
|
||||
@@ -483,6 +483,18 @@ class Base(Configuration):
|
||||
)
|
||||
BREVO_API_CONTACT_ATTRIBUTES = values.DictValue({"VISIO_USER": True})
|
||||
|
||||
# SIP Telephony
|
||||
ROOM_PIN_CODE_LENGTH = values.PositiveIntegerValue(
|
||||
9, # this value cannot exceed 100 digits due to database constraints
|
||||
environ_name="ROOM_PIN_CODE_LENGTH",
|
||||
environ_prefix=None,
|
||||
)
|
||||
ROOM_PIN_CODE_GENERATION_MAX_RETRY = values.PositiveIntegerValue(
|
||||
3,
|
||||
environ_name="ROOM_PIN_CODE_GENERATION_MAX_RETRY",
|
||||
environ_prefix=None,
|
||||
)
|
||||
|
||||
# pylint: disable=invalid-name
|
||||
@property
|
||||
def ENVIRONMENT(self):
|
||||
|
||||
Reference in New Issue
Block a user