Skip to content

Commit

Permalink
Merge pull request #310 from glogiotatidis/auto-disable-snippets
Browse files Browse the repository at this point in the history
[Fix Bug 1411921] Auto disable snippets that have passed their publish end date.
  • Loading branch information
glogiotatidis committed Oct 27, 2017
2 parents 8ac743b + 2ffe098 commit d3fce97
Show file tree
Hide file tree
Showing 7 changed files with 73 additions and 20 deletions.
3 changes: 3 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -271,3 +271,6 @@ icalendar==3.11.7 \
python-dateutil==2.6.1 \
--hash=sha256:891c38b2a02f5bb1be3e4793866c8df49c7d19baabf9c1bad62547e0b4866aca \
--hash=sha256:95511bae634d69bc7329ba55e646499a842bc4ec342ad54a8cdb65645a0aad3c
babis==0.2.1 \
--hash=sha256:610416de748d0708a153dd7e2a42c95b4938689e033eb96b92e92b0a0049dc24 \
--hash=sha256:d900322567fe7acd5898f91e1e9a21dff47b4d29a38c28c98f2e6e6745b5108b
33 changes: 14 additions & 19 deletions scripts/cron.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,24 @@
import datetime
import os
import sys
from subprocess import check_call

from django.core.management import call_command
from django.conf import settings
from django.db import connections

import requests
import babis
from apscheduler.schedulers.blocking import BlockingScheduler

from snippets.base.util import create_countries, create_locales


MANAGE = os.path.join(settings.ROOT, 'manage.py')
schedule = BlockingScheduler()


def call_command(command):
check_call('python {0} {1}'.format(MANAGE, command), shell=True)


class scheduled_job(object):
"""Decorator for scheduled jobs. Takes same args as apscheduler.schedule_job."""
def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -47,27 +51,18 @@ def log(self, message):
print(msg, file=sys.stderr)


def ping_dms(function):
"""Pings Dead Man's Snitch after job completion if URL is set."""
def _ping():
function()
if settings.DEAD_MANS_SNITCH_URL:
utcnow = datetime.datetime.utcnow()
payload = {'m': 'Run {} on {}'.format(function.__name__, utcnow.isoformat())}
requests.get(settings.DEAD_MANS_SNITCH_URL, params=payload)
_ping.__name__ = function.__name__
return _ping


@scheduled_job('cron', month='*', day='*', hour='*/12', minute='10', max_instances=1, coalesce=True)
@ping_dms
@babis.decorator(ping_after=settings.DEAD_MANS_SNITCH_PRODUCT_DETAILS)
def job_update_product_details():
call_command('update_product_details')
create_countries()
create_locales()
# Django won't close db connections after call_command. Close them manually
# to prevent errors in case the DB goes away, e.g. during a failover event.
connections.close_all()


@scheduled_job('cron', month='*', day='*', hour='*/12', minute='20', max_instances=1, coalesce=True)
@babis.decorator(ping_after=settings.DEAD_MANS_SNITCH_DISABLE_SNIPPETS)
def job_disable_snippets_past_publish_date():
call_command('disable_snippets_past_publish_date')


def run():
Expand Down
Empty file.
Empty file.
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from datetime import datetime

from django.core.management.base import BaseCommand
from django.db import transaction

from snippets.base.models import Snippet


class Command(BaseCommand):
args = '(no args)'
help = 'Disable snippets past publish date'

@transaction.atomic
def handle(self, *args, **options):
now = datetime.utcnow()
snippets = Snippet.objects.filter(disabled=False, publish_end__lte=now)
disabled = snippets.update(disabled=True)
running = Snippet.objects.filter(disabled=False).count()

self.stdout.write(
'Snippets Disabled: {disabled}\n'
'Snippets Running: {running}\n'.format(disabled=disabled,
running=running))
31 changes: 31 additions & 0 deletions snippets/base/tests/test_commands.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
from datetime import datetime, timedelta

from mock import Mock

from django.core.management import call_command

from snippets.base.tests import SnippetFactory, TestCase


class DisableSnippetsPastPublishDateTests(TestCase):
def test_base(self):
snippet_without_end_date = SnippetFactory(disabled=False)
snippet_without_end_date.publish_end = None

snippet_that_has_ended = SnippetFactory(disabled=False)
snippet_that_has_ended.publish_end = datetime.utcnow()
snippet_that_has_ended.save()

snippet_ending_in_the_future = SnippetFactory(disabled=False)
snippet_ending_in_the_future.publish_end = datetime.utcnow() + timedelta(days=1)
snippet_ending_in_the_future.save()

call_command('disable_snippets_past_publish_date', stdout=Mock())

snippet_without_end_date.refresh_from_db()
snippet_that_has_ended.refresh_from_db()
snippet_ending_in_the_future.refresh_from_db()

self.assertTrue(snippet_that_has_ended.disabled)
self.assertFalse(snippet_without_end_date.disabled)
self.assertFalse(snippet_ending_in_the_future.disabled)
3 changes: 2 additions & 1 deletion snippets/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,8 @@ def mysql_version(self):
MEDIA_BUNDLES_ROOT: 'max-age=2592000', # 1 Month
}

DEAD_MANS_SNITCH_URL = config('DEAD_MANS_SNITCH_URL', default=None)
DEAD_MANS_SNITCH_PRODUCT_DETAILS = config('DEAD_MANS_SNITCH_PRODUCT_DETAILS', default=None)
DEAD_MANS_SNITCH_DISABLE_SNIPPETS = config('DEAD_MANS_SNITCH_DISABLE_SNIPPETS', default=None)

SNIPPETS_PER_PAGE = config('SNIPPETS_PER_PAGE', default=50)

Expand Down

0 comments on commit d3fce97

Please sign in to comment.