Last active
January 26, 2016 11:13
-
-
Save raphigaziano/7ff83c0f27bbd2beaa8f to your computer and use it in GitHub Desktop.
fix argument handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys | |
from datetime import datetime | |
from freezegun import freeze_time | |
from django.core.management import call_command | |
from django.core.management.commands.runserver import Command as RunServerCmd | |
class Command(RunServerCmd): | |
""" | |
Handy wrapper to run the dev server while simulating an arbitrary date. | |
Requires freezegun installed (https://github.com/spulec/freezegun). | |
""" | |
def add_arguments(self, parser): | |
# our own arg should be added before the call to super, so that it is | |
# not confused with the following port number. | |
parser.add_argument('date', type=str) | |
super().add_arguments(parser) | |
def handle(self, *args, **kwargs): | |
try: | |
date_string = kwargs['date'] | |
_ = datetime.strptime(date_string, '%Y-%m-%d') | |
except ValueError: | |
print('Invalid date format: %s' % date_string) | |
sys.exit(1) | |
with freeze_time(date_string): | |
call_command('runserver', *args, **kwargs) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment