# 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 PushExchangeAnnSerializer(serializers.ModelSerializer):
#     channel = serializers.CharField(
#         default="ExchangeAnnChannel", read_only=True
#     )
# 
#     class Meta:
#         model = News
#         exclude = [
#             "id",
#             "content",
#             "slug",
#         ]


import re
import requests
from django.core.management import BaseCommand
from django.conf import settings
from apps.cryptonews.models import News
from apps.cryptonews.serializers import PushExchangeAnnSerializer
# import json

# from datetime import datetime, timedelta

translate_url = getattr(settings, "TRANSLATE_BOT_URL")


p = re.compile(r"^\[.*?\]")


class Command(BaseCommand):
    def handle(self, *args, **options):
        # _expire = datetime.utcnow() - timedelta(days=1)  # noqa
        news = News.objects.filter(origin_link__icontains="upbit").first()
        # print(_expire.astimezone())
        # if _expire.timestamp() < news.published_at.timestamp():
        ser = PushExchangeAnnSerializer(instance=news)
        _data = ser.data

        _title = _data["title"]
        _title = p.sub("", _title).strip()
        _data.update({"title": f"[Upbit] {_title}"})

        _data.update({"channel": "testAnnChannel"})

# 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 PushExchangeAnnSerializer(serializers.ModelSerializer):
#     channel = serializers.CharField(
#         default="ExchangeAnnChannel", read_only=True
#     )
# 
#     class Meta:
#         model = News
#         exclude = [
#             "id",
#             "content",
#             "slug",
#         ]


# Path: apps/cryptonews/management/commands/test_push_channel.py
import re
import requests
from django.core.management import BaseCommand
from django.conf import settings
from apps.cryptonews.models import News
from apps.cryptonews.serializers import PushExchangeAnnSerializer

# import json

# from datetime import datetime, timedelta

translate_url = getattr(settings, "TRANSLATE_BOT_URL")


p = re.compile(r"^\[.*?\]")


class Command(BaseCommand):
    def handle(self, *args, **options):
        # _expire = datetime.utcnow() - timedelta(days=1)  # noqa
        news = News.objects.filter(origin_link__icontains="upbit").first()
        # print(_expire.astimezone())
        # if _expire.timestamp() < news.published_at.timestamp():
        ser = PushExchangeAnnSerializer(instance=news)
        _data = ser.data

        _title = _data["title"]
        _title = p.sub("", _title).strip()
        _data.update({"title": f"[Upbit] {_title}"})

        _data.update({"channel": "testAnnChannel"})

        requests.post(url="http://tg-bot:5000/push", json=_data)