# Path: apps/core/models.py
# class EmailDomain(models.Model):
#     """
#     denotes an email domain
#     - domain:    the email domain
#     - is_banned: banned status
#     """
#     domain = models.CharField(max_length=100, unique=True)
#     is_banned = models.BooleanField(default=True)


import requests
from django.core.management.base import BaseCommand, CommandError
from apps.core.models import EmailDomain

class Command(BaseCommand):
    help = 'Update list of banned email domains'

    def add_arguments(self, parser):
        parser.add_argument(
            '--overwrite',
            action='store_true',
            dest='overwrite',
            help='Overwrite configured data',
        )
        parser.add_argument(
            '--clean',
            action='store_true',
            dest='clean',
            help='Empty the table and start from the beginning',
        )

    def handle(self, *args, **options):
        try:
            domains_raw = requests.get(DATA_URL).text.split('\n')
            domains = [x for x in [y.strip() for y in domains_raw] if x]
        except Exception:
            raise CommandError(f'cannot fetch data from {DATA_URL}')

        if options['clean']:
            EmailDomain.objects.all().delete()

        count_created, count_overwrited = 0, 0
        for domain in domains:

# Path: apps/core/models.py
# class EmailDomain(models.Model):
#     """
#     denotes an email domain
#     - domain:    the email domain
#     - is_banned: banned status
#     """
#     domain = models.CharField(max_length=100, unique=True)
#     is_banned = models.BooleanField(default=True)


# Path: apps/core/management/commands/update-banned-email.py
import requests
from django.core.management.base import BaseCommand, CommandError
from apps.core.models import EmailDomain


class Command(BaseCommand):
    help = 'Update list of banned email domains'

    def add_arguments(self, parser):
        parser.add_argument(
            '--overwrite',
            action='store_true',
            dest='overwrite',
            help='Overwrite configured data',
        )
        parser.add_argument(
            '--clean',
            action='store_true',
            dest='clean',
            help='Empty the table and start from the beginning',
        )

    def handle(self, *args, **options):
        try:
            domains_raw = requests.get(DATA_URL).text.split('\n')
            domains = [x for x in [y.strip() for y in domains_raw] if x]
        except Exception:
            raise CommandError(f'cannot fetch data from {DATA_URL}')

        if options['clean']:
            EmailDomain.objects.all().delete()

        count_created, count_overwrited = 0, 0
        for domain in domains:

            obj, created = EmailDomain.objects.get_or_create(domain=domain)