Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Added possibility to list scheduled posts and draft posts.
By default, printing name and path of next scheduled post if there is one.
  • Loading branch information
felixfontein committed May 28, 2015
1 parent 4911008 commit 5f5e29b
Showing 1 changed file with 42 additions and 18 deletions.
60 changes: 42 additions & 18 deletions nikola/plugins/command/status.py
Expand Up @@ -40,8 +40,26 @@ class CommandDeploy(Command):
doc_purpose = "display site status"
doc_description = "Show information about the posts and site deployment."
logger = None

def _execute(self, command, args):
cmd_options = [
{
'name': 'list_drafts',
'short': 'd',
'long': 'list-drafts',
'type': bool,
'default': False,
'help': 'List all drafts',
},
{
'name': 'list_scheduled',
'short': 's',
'long': 'list-scheduled',
'type': bool,
'default': False,
'help': 'List all scheduled posts',
},
]

def _execute(self, options, args):

self.site.scan_posts()

Expand Down Expand Up @@ -73,23 +91,29 @@ def _execute(self, command, args):
else:
print("Last deployment {0} ago.".format(self.human_time(last_deploy_offset)))

now = datetime.utcnow().replace(tzinfo=gettz("UTC"))

posts_count = len(self.site.all_posts)
posts_drafts = 0
posts_scheduled = 0
nearest_scheduled_offset = None

for post in self.site.all_posts:
if post.is_draft:
posts_drafts = posts_drafts + 1
if post.publish_later:
posts_scheduled = posts_scheduled + 1
post_due_offset = post.date - datetime.utcnow().replace(tzinfo=gettz("UTC"))
if (nearest_scheduled_offset is None) or (post_due_offset.seconds < nearest_scheduled_offset.seconds):
nearest_scheduled_offset = post_due_offset

if posts_scheduled > 0 and nearest_scheduled_offset is not None:
print("{0} to next scheduled post.".format(self.human_time(nearest_scheduled_offset)))
print("{0} posts in total, {1} scheduled, and {2} drafts.".format(posts_count, posts_scheduled, posts_drafts))

# find all drafts
posts_drafts = [post for post in self.site.all_posts if post.is_draft]
posts_drafts = sorted(posts_drafts, key=lambda post: post.source_path)

# find all scheduled posts with offset from now until publishing time
posts_scheduled = [(post.date - now, post) for post in self.site.all_posts if post.publish_later]
posts_scheduled = sorted(posts_scheduled, key=lambda offset_post: (offset_post[0],offset_post[1].source_path))

if len(posts_scheduled) > 0:
if options['list_scheduled']:
for offset, post in posts_scheduled:
print("Scheduled: '{1}' ({2}; source: {3}) in {0}".format(self.human_time(offset), post.meta('title'), post.permalink(), post.source_path))
else:
offset, post = posts_scheduled[0]
print("{0} to next scheduled post ('{1}'; {2}; source: {3}).".format(self.human_time(offset), post.meta('title'), post.permalink(), post.source_path))
if options['list_drafts']:
for post in posts_drafts:
print("Draft: '{0}' ({1}; source: {2})".format(post.meta('title'), post.permalink(), post.source_path))
print("{0} posts in total, {1} scheduled, and {2} drafts.".format(posts_count, len(posts_scheduled), len(posts_drafts)))

def human_time(self, dt):
days = dt.days
Expand Down

0 comments on commit 5f5e29b

Please sign in to comment.