# Path: domainchecks/models.py
# class DomainCheck(models.Model):
#     """Configured website check."""
# 
#     PROTOCOL_HTTP = 'http'
#     PROTOCOL_HTTPS = 'https'
# 
#     PROTOCOL_CHOICES = (
#         (PROTOCOL_HTTP, 'HTTP'),
#         (PROTOCOL_HTTPS, 'HTTPS'),
#     )
# 
#     METHOD_GET = 'get'
#     METHOD_POST = 'post'
#     METHOD_PUT = 'put'
#     METHOD_DELETE = 'delete'
#     METHOD_HEAD = 'head'
# 
#     METHOD_CHOICES = (
#         (METHOD_GET, 'GET'),
#         (METHOD_POST, 'POST'),
#         (METHOD_PUT, 'PUT'),
#         (METHOD_DELETE, 'DELETE'),
#         (METHOD_HEAD, 'HEAD'),
#     )
# 
#     domain = models.ForeignKey(Domain)
#     path = models.CharField(max_length=1024)
#     protocol = models.CharField(
#         max_length=5, choices=PROTOCOL_CHOICES, default=PROTOCOL_HTTP)
#     method = models.CharField(
#         max_length=6, choices=METHOD_CHOICES, default=METHOD_GET)
#     is_active = models.BooleanField(default=True)
# 
#     objects = DomainCheckQuerySet.as_manager()
# 
#     def __str__(self):
#         return '{method} {url}'.format(
#             method=self.get_method_display(), url=self.url)
# 
#     @property
#     def url(self):
#         return '{protocol}://{domain}{path}'.format(
#             protocol=self.protocol, domain=self.domain, path=self.path)
# 
#     def run_check(self, timeout=10):
#         start = time.time()
#         result = CheckResult(domain_check=self, checked_on=now())
#         try:
#             response = requests.request(
#                 self.method, self.url, allow_redirects=False, timeout=timeout)
#             result.status_code = response.status_code
#             response.raise_for_status()
#         except requests.exceptions.ConnectionError:
#             # Host could not be resolved or the connection was refused
#             pass
#         except requests.exceptions.Timeout:
#             # Request timed out
#             pass
#         except requests.exceptions.RequestException:
#             # Server responded with 4XX or 5XX status code
#             result.response_body = response.text
#         else:
#             result.response_body = response.text
#         finally:
#             result.response_time = time.time() - start
#             result.save()


import datetime
from django.core.management import BaseCommand
from ...models import DomainCheck




class Command(BaseCommand):
    help = 'Pings configured domain checks for their current status.'

    def add_arguments(self, parser):
        parser.add_argument(
            '--minutes', type=int, dest='minutes', default=5,
            help='Time cutoff from the last check (in minutes).')
        parser.add_argument(
            '--timeout', type=int, dest='timeout', default=10,
            help='Timeout for server response (in seconds).')

    def handle(self, *args, **options):

# Path: domainchecks/models.py
# class DomainCheck(models.Model):
#     """Configured website check."""
# 
#     PROTOCOL_HTTP = 'http'
#     PROTOCOL_HTTPS = 'https'
# 
#     PROTOCOL_CHOICES = (
#         (PROTOCOL_HTTP, 'HTTP'),
#         (PROTOCOL_HTTPS, 'HTTPS'),
#     )
# 
#     METHOD_GET = 'get'
#     METHOD_POST = 'post'
#     METHOD_PUT = 'put'
#     METHOD_DELETE = 'delete'
#     METHOD_HEAD = 'head'
# 
#     METHOD_CHOICES = (
#         (METHOD_GET, 'GET'),
#         (METHOD_POST, 'POST'),
#         (METHOD_PUT, 'PUT'),
#         (METHOD_DELETE, 'DELETE'),
#         (METHOD_HEAD, 'HEAD'),
#     )
# 
#     domain = models.ForeignKey(Domain)
#     path = models.CharField(max_length=1024)
#     protocol = models.CharField(
#         max_length=5, choices=PROTOCOL_CHOICES, default=PROTOCOL_HTTP)
#     method = models.CharField(
#         max_length=6, choices=METHOD_CHOICES, default=METHOD_GET)
#     is_active = models.BooleanField(default=True)
# 
#     objects = DomainCheckQuerySet.as_manager()
# 
#     def __str__(self):
#         return '{method} {url}'.format(
#             method=self.get_method_display(), url=self.url)
# 
#     @property
#     def url(self):
#         return '{protocol}://{domain}{path}'.format(
#             protocol=self.protocol, domain=self.domain, path=self.path)
# 
#     def run_check(self, timeout=10):
#         start = time.time()
#         result = CheckResult(domain_check=self, checked_on=now())
#         try:
#             response = requests.request(
#                 self.method, self.url, allow_redirects=False, timeout=timeout)
#             result.status_code = response.status_code
#             response.raise_for_status()
#         except requests.exceptions.ConnectionError:
#             # Host could not be resolved or the connection was refused
#             pass
#         except requests.exceptions.Timeout:
#             # Request timed out
#             pass
#         except requests.exceptions.RequestException:
#             # Server responded with 4XX or 5XX status code
#             result.response_body = response.text
#         else:
#             result.response_body = response.text
#         finally:
#             result.response_time = time.time() - start
#             result.save()


# Path: domainchecks/management/commands/checkdomains.py
import datetime
from django.core.management import BaseCommand
from ...models import DomainCheck





class Command(BaseCommand):
    help = 'Pings configured domain checks for their current status.'

    def add_arguments(self, parser):
        parser.add_argument(
            '--minutes', type=int, dest='minutes', default=5,
            help='Time cutoff from the last check (in minutes).')
        parser.add_argument(
            '--timeout', type=int, dest='timeout', default=10,
            help='Timeout for server response (in seconds).')

    def handle(self, *args, **options):

        verbosity = options['verbosity']