# Path: apps/cryptonews/models/db.py
# class News(models.Model):
#     slug = fields.RandomCharField(
#         length=12,
#         unique=True,
#         include_alpha=False,
#         db_index=True,
#         editable=False,
#     )
#     title = models.CharField(max_length=1024)
#     content = models.TextField(blank=True, default="")
#     published_at = models.DateTimeField(default=timezone.now, db_index=True)
#     origin_link = models.URLField(
#         max_length=1024, null=True, blank=True, unique=True
#     )
# 
#     class Meta:
#         ordering = ["-published_at"]
# 
#     def __str__(self):
#         return f"{self.title} - {self.published_at} - {self.origin_link}"
# 
#     @cached_property
#     def domain(self):
#         o = parse.urlparse(self.origin_link)
#         return o.netloc
# 
# Path: apps/cryptonews/serializers.py
# class CryptoNewsSerializer(serializers.ModelSerializer):
#     domain = serializers.CharField(read_only=True)
# 
#     class Meta:
#         model = News
#         # exclude = ["id"]
#         fields = "__all__"
#         read_only_fields = ["id", "slug"]


from rest_framework import viewsets
from rest_framework import permissions
from ..models import News
from ..serializers import CryptoNewsSerializer


class CryptoNewsViewSet(viewsets.ModelViewSet):
    queryset = News.objects.all()
    serializer_class = CryptoNewsSerializer

# Path: apps/cryptonews/models/db.py
# class News(models.Model):
#     slug = fields.RandomCharField(
#         length=12,
#         unique=True,
#         include_alpha=False,
#         db_index=True,
#         editable=False,
#     )
#     title = models.CharField(max_length=1024)
#     content = models.TextField(blank=True, default="")
#     published_at = models.DateTimeField(default=timezone.now, db_index=True)
#     origin_link = models.URLField(
#         max_length=1024, null=True, blank=True, unique=True
#     )
# 
#     class Meta:
#         ordering = ["-published_at"]
# 
#     def __str__(self):
#         return f"{self.title} - {self.published_at} - {self.origin_link}"
# 
#     @cached_property
#     def domain(self):
#         o = parse.urlparse(self.origin_link)
#         return o.netloc
# 
# Path: apps/cryptonews/serializers.py
# class CryptoNewsSerializer(serializers.ModelSerializer):
#     domain = serializers.CharField(read_only=True)
# 
#     class Meta:
#         model = News
#         # exclude = ["id"]
#         fields = "__all__"
#         read_only_fields = ["id", "slug"]


# Path: apps/cryptonews/views/api.py
from rest_framework import viewsets
from rest_framework import permissions
from ..models import News
from ..serializers import CryptoNewsSerializer



class CryptoNewsViewSet(viewsets.ModelViewSet):
    queryset = News.objects.all()
    serializer_class = CryptoNewsSerializer

    permission_classes = [permissions.IsAuthenticatedOrReadOnly]