def update_or_create_document(yaml_obj):
    """
    Submit an object read from our YAML files and it will update it in the
    database, creating it if it doesn't already exist. 
    
    Returns the database object, and a boolean that is true if a new object 
    was created.
    """
    # Check if the table already exists in the datastore
    obj = Document.get_by_key_name(yaml_obj.get('slug'))
    # Update the obj if it exists
    if obj:
        # Loop through the keys and update the object one by one.
        for key in yaml_obj.keys():
            # With some special casing for projects...
            if key == 'project_slug':
                proj = Project.get_by_key_name(yaml_obj.get('project_slug'))
                obj.project = proj
            # ...and for tags.
            elif key == 'tags':
                obj.tags = get_tag_keys(yaml_obj.get("tags"))
            else:
                setattr(obj, key, yaml_obj.get(key))
        # Save it out
        obj.put()
        created = False
    # Create it if it doesn't
    else:
        # If it has tags....
        if yaml_obj.has_key('tags'):
            # Convert to database keys
            tags = get_tag_keys(yaml_obj.pop("tags"))
            # Load the data
            obj = Document(key_name=yaml_obj.get('slug'), **yaml_obj)
            # Set the tags
            obj.tags = tags
        # Otherwise....
        else:
            # Update the basic values
            obj = Document(key_name=yaml_obj.get('slug'), **yaml_obj)
            # And clear out the tag data
            obj.tags = []
            obj.similar_documents = []
        # Connected it to a project, if it exists
        if yaml_obj.has_key('project_slug'):
            proj = Project.get_by_key_name(yaml_obj.get('project_slug'))
            obj.project = proj
        # Save it out
        obj.put()
        created = True
    
    # Update the similarity lists of documents with the same tags
    taskqueue.add(
        url='/_/document/update-similar/',
        params=dict(key=obj.key()),
        method='GET'
    )
    
    # Pass it out
    return obj, created
示例#2
0
 def handle(self, *args, **options):
     # Make sure they provided a table name
     try:
         key_name = args[0]
     except:
         raise CommandError("You must provide the key_name of a document as the first argument.")
     
     # Login
     self.authorize(options)
     
     # Get it
     obj = Document.get_by_key_name(key_name)
     # Drop it
     if obj:
         print "Deleting %s" % obj
         obj.delete()
     else:
         print "'%s' does not exist" % key_name
     # Update the related list of all the related documents
     taskqueue.add(
         url='/_/document/update-similar/',
         params=dict(key=obj.key()),
         method='GET'
     )