#!/usr/bin/env python

"""Delete one or more posts by ID."""

import  argparse
import  pprint

import  atc_tools

# Parse command line.

parser = argparse.ArgumentParser( description = __doc__, formatter_class = argparse.RawTextHelpFormatter )
parser.epilog = """
removing a post

    atc-delete-post a0b1c2d3e4f5abcdefabcdefabcdef

removing multiple posts

    atc-delete-post a0b1c2d3e4f5abcdefabcdefabcdef bcdefgabcdefab01234123151baba11

"""
parser.add_argument( "_ids"     ,       help = "List of post IDs to act upon."                , nargs = "+"           )
parser.add_argument( "--test"   , "-t", help = "Test mode: Server only validates, no inserts.", action = "store_true" )
args = parser.parse_args()

# 

client  = atc_tools.client()
posts   = client.posts
targets = client.targets

#

for _id in args._ids :

    print

    # Attempt to retrieve post.

    print "Retrieving post   ", _id
    doc = posts.get( _id )

    # Identify referenced target.

    target_id = doc[ "target_id" ]
    print "Referenced target ", target_id

    # Stop processing in test mode.

    if args.test :
        print "TEST skipping delete..."
        continue

    # Attempt to delete the post.

    print "Deleting post     ", _id
    doc = posts.delete( _id )
    pprint.pprint( doc )

    # Trigger target-level map-reduce.

    print "Retrieving target ", target_id
    doc = targets.get( target_id )

    # Attempt to retrieve deleted post.

    print "Testing post      ", _id
    try :
        doc = posts.get( _id )
    except atc_tools.exceptions.NotFound :
        pass
