-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #310 from glogiotatidis/auto-disable-snippets
[Fix Bug 1411921] Auto disable snippets that have passed their publish end date.
- Loading branch information
Showing
7 changed files
with
73 additions
and
20 deletions.
There are no files selected for viewing
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
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
Empty file.
Empty file.
23 changes: 23 additions & 0 deletions
23
snippets/base/management/commands/disable_snippets_past_publish_date.py
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
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)) |
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
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) |
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