from django.db import models


class Room(models.Model):
    code = models.CharField('Code', max_length=128)
    tab_url = models.CharField('Tab url', max_length=512, default='', blank=True)

    def to_dict(self):
        return {
            'users': [u.to_dict() for u in self.users.all()],
            'tabUrl': self.tab_url
        }

    def __str__(self):
        return f'Room {self.code}'


class RoomUser(models.Model):
    room = models.ForeignKey(Room, related_name='users', on_delete=models.CASCADE)

    username = models.CharField('Username', max_length=128, default="user")
    host = models.BooleanField('Is host')

    def to_dict(self):
        return {
            'id': self.id,
            'username': self.username,
            'isHost': self.host,
        }

    def __str__(self):
        return f'{self.username} ({self.id})'

[(5, 'django.db.models.CharField', 'models.CharField', (['"""Code"""'], {'max_length': '(128)'}), False, 'from django.db import models\n'), (6, 'django.db.models.CharField', 'models.CharField', (['"""Tab url"""'], {'max_length': '(512)', 'default': '""""""', 'blank': '(True)'}), False, 'from django.db import models\n'), (19, 'django.db.models.ForeignKey', 'models.ForeignKey', (['Room'], {'related_name': '"""users"""', 'on_delete': 'models.CASCADE'}), False, 'from django.db import models\n'), (21, 'django.db.models.CharField', 'models.CharField', (['"""Username"""'], {'max_length': '(128)', 'default': '"""user"""'}), False, 'from django.db import models\n'), (22, 'django.db.models.BooleanField', 'models.BooleanField', (['"""Is host"""'], {}), False, 'from django.db import models\n')]