Skip to content

Instantly share code, notes, and snippets.

@bencleary
Last active December 8, 2017 15:28
Show Gist options
  • Save bencleary/4471156beed90fdc6ef5e31214535c2b to your computer and use it in GitHub Desktop.
Save bencleary/4471156beed90fdc6ef5e31214535c2b to your computer and use it in GitHub Desktop.
from django.core.management.base import BaseCommand, CommandError
from crunchy_waffles_app.models import *
from django.utils import timezone
class Command(BaseCommand):
help = 'Closes or opens auctions based their individual DateTime values'
def add_arguments(self, parser):
parser.add_argument('--id')
def handle(self, *args, **options):
if options['id']:
id = options['id']
try:
auction = Auction.objects.get(pk=id)
except:
raise CommandError("Auction with that ID doesn't exists")
self.override_and_expire(auction)
else:
auctions = Auction.AuctionManager.active()
for auction in auctions:
self.check_if_expired_and_expire(auction)
def check_if_expired_and_expire(self, auction):
now = timezone.now()
if auction.expiry_date <= now:
auction.active = False
auction.save()
self.stdout.write(self.style.SUCCESS("{0} - Expired and Deactivated".format(auction.title)))
def override_and_expire(self, auction):
auction.active = False
auction.save()
self.stdout.write(self.style.SUCCESS("{0} - Manually Expired and Deactivated".format(auction.title)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment