# Path: labshare/models.py
# class Device(models.Model):
#     name = models.CharField(max_length=255)
#     user = models.OneToOneField(User, on_delete=models.CASCADE)
# 
#     class Meta:
#         permissions = (
#             ('use_device', 'User/Group is allowed to use that device'),
#         )
# 
#     def __str__(self):
#         return self.name
# 
#     def can_be_used_by(self, user):
#         content_type = ContentType.objects.get_for_model(self)
#         permission_name = "{app}.use_{model}".format(app=content_type.app_label, model=content_type.model)
#         return user.has_perm(permission_name, self) or user.has_perm(permission_name)
# 
#     def serialize(self):
#         return {
#             'name': self.name,
#         }


from django.core.management import BaseCommand
from rest_framework.authtoken.models import Token
from labshare.models import Device



class Command(BaseCommand):
    help = "lists the authentication tokens for all registered devices"

    def handle(self, *args, **options):
        auth_tokens = {}
        for device in Device.objects.all():
            token = Token.objects.get(user=device.user)
            auth_tokens[device.name] = token

        if len(auth_tokens) > 0:
            print("The following devices and tokens are registered:")
            print("\n".join([f"{name}: {token}" for name, token in auth_tokens.items()]))
        else:

# Path: labshare/models.py
# class Device(models.Model):
#     name = models.CharField(max_length=255)
#     user = models.OneToOneField(User, on_delete=models.CASCADE)
# 
#     class Meta:
#         permissions = (
#             ('use_device', 'User/Group is allowed to use that device'),
#         )
# 
#     def __str__(self):
#         return self.name
# 
#     def can_be_used_by(self, user):
#         content_type = ContentType.objects.get_for_model(self)
#         permission_name = "{app}.use_{model}".format(app=content_type.app_label, model=content_type.model)
#         return user.has_perm(permission_name, self) or user.has_perm(permission_name)
# 
#     def serialize(self):
#         return {
#             'name': self.name,
#         }


# Path: labshare/management/commands/tokens.py
from django.core.management import BaseCommand
from rest_framework.authtoken.models import Token
from labshare.models import Device




class Command(BaseCommand):
    help = "lists the authentication tokens for all registered devices"

    def handle(self, *args, **options):
        auth_tokens = {}
        for device in Device.objects.all():
            token = Token.objects.get(user=device.user)
            auth_tokens[device.name] = token

        if len(auth_tokens) > 0:
            print("The following devices and tokens are registered:")
            print("\n".join([f"{name}: {token}" for name, token in auth_tokens.items()]))
        else:

            print("No devices or tokens registered.")