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})'

[((68, 108), 'django.db.models.CharField', 'models.CharField', (['"""Code"""'], {'max_length': '(128)'}), "('Code', max_length=128)\n", (84, 108), False, 'from django.db import models\n'), ((123, 190), 'django.db.models.CharField', 'models.CharField', (['"""Tab url"""'], {'max_length': '(512)', 'default': '""""""', 'blank': '(True)'}), "('Tab url', max_length=512, default='', blank=True)\n", (139, 190), False, 'from django.db import models\n'), ((441, 512), 'django.db.models.ForeignKey', 'models.ForeignKey', (['Room'], {'related_name': '"""users"""', 'on_delete': 'models.CASCADE'}), "(Room, related_name='users', on_delete=models.CASCADE)\n", (458, 512), False, 'from django.db import models\n'), ((529, 589), 'django.db.models.CharField', 'models.CharField', (['"""Username"""'], {'max_length': '(128)', 'default': '"""user"""'}), "('Username', max_length=128, default='user')\n", (545, 589), False, 'from django.db import models\n'), ((601, 631), 'django.db.models.BooleanField', 'models.BooleanField', (['"""Is host"""'], {}), "('Is host')\n", (620, 631), False, 'from django.db import models\n')]