コード例 #1
0
def create(conn, client, title, parent=None, class_id=None):
    # Initialize folder object with title
    folder = gdata.docs.data.Resource(type="folder", title=title)

    if parent != None:
        parent = client.GetResourceById(parent)

    # Use the Client Object to create the folder in the root of their Drive or the collection specified.
    folder = client.CreateResource(folder, collection=parent)

    if conn:
        if parent != None:
            Database.insert(
                conn,
                Database.structure_insert_string(
                    Utilities.clean_title(title), folder.resource_id.text, parent.resource_id.text, class_id
                ),
            )
        else:
            Database.insert(
                conn,
                Database.structure_insert_string(
                    Utilities.clean_title(title), folder.resource_id.text, parent, class_id
                ),
            )

    return folder
コード例 #2
0
def fix_structure(client, conn):
    tables_exist = False
    folder_list = {}
    exists_list_gd = {}
    exists_list_db = {}
    everything_exists = False

    print "Making sure the database tables exist..."
    # CHECK FOR DATABASE TABLES #
    tables_query = Database.get(Database.execute(conn, "SELECT COUNT(*) FROM pg_tables WHERE schemaname='public' AND tablename LIKE 'vlacs_class_folders%'"))
    if tables_query['count'] > 1:
        print "Database tables exist."
        tables_exist = True

    if not tables_exist:
        print "Database tables do not exist, creating..."
        Database.insert(conn, "CREATE TABLE IF NOT EXISTS vlacs_class_folders_structure(id serial, class_id integer, student_id integer, folder_name text, folder_id text, folder_parent text, isactive int DEFAULT 1 NOT NULL)")
        Database.insert(conn, "CREATE TABLE IF NOT EXISTS vlacs_class_folders_shared(id serial, folder_id text, folder_name text, shared_with text, shared_permission text)")

    print "Making sure the root folders exist in Google Drive and Database..."
    # STORE NAMES OF ROOT LEVEL FOLDERS IN LIST #
    for resource in client.GetAllResources(uri="/feeds/default/private/full/root/contents/-/folder", show_root=True):
        if resource.GetResourceType() == 'folder':
            folder_list[resource.title.text] = resource.resource_id.text

    for folder_name in [config.ROOT_CLASS_FOLDER, config.TEACHER_SHARE_FOLDER, config.STUDENT_SHARE_FOLDER]:
        # CHECK IF FOLDER EXISTS IN DRIVE #
        for title, f_id in folder_list.items():
            if title == folder_name:
                print "--- %s exists in Google Drive." % (folder_name)
                exists_list_gd[folder_name] = True

        #CHECK IF FOLDER EXISTS IN DATATBASE #
            cq = Database.get(Database.execute(conn, "SELECT count(*) FROM vlacs_class_folders_structure WHERE folder_name = '%s'" % folder_name))
            if cq['count'] > 0:
                print "--- %s exists in the Database." % (folder_name)
                exists_list_db[folder_name] = True

    if (config.ROOT_CLASS_FOLDER in exists_list_db and config.ROOT_CLASS_FOLDER in exists_list_gd and 
            config.TEACHER_SHARE_FOLDER in exists_list_db and config.TEACHER_SHARE_FOLDER in exists_list_gd and 
            config.STUDENT_SHARE_FOLDER in exists_list_db and config.STUDENT_SHARE_FOLDER in exists_list_gd):
        everything_exists = True

    if not everything_exists:
        print "Something is missing..."
        # COMPARE AND INSERT / CREATE #
        for folder, config_f in [{'root', config.ROOT_CLASS_FOLDER}, {'teacher', config.TEACHER_SHARE_FOLDER}, {'student', config.STUDENT_SHARE_FOLDER}]:
            if folder in exists_list_db and folder not in exists_list_gd:
                print "--- %s folder is in the database, but not Google Drive. Fixing..." % (config_f)
                f = Folder.create(False, client, config_f)
                Database.insert(conn, "UPDATE vlacs_class_folders_structure SET folder_id = '%s' WHERE folder_name = '%s'" % (f.resource_id.text, config_f))
            elif folder in exists_list_gd and folder not in exists_list_db:
                print "--- %s folder is in Google Drive but not in the database. Fixing..." % (config_f)
                Database.insert(conn, Database.structure_insert_string(config_f, folder_list[config_f]))
            elif folder not in exists_list_db and folder_list not in exists_list_gd:
                print "--- %s folder is not in Google Drive or the database. Fixing..." % (config_f)
                f = Folder.create(conn, client, config_f)
コード例 #3
0
def create_flat(conn, client, title, root_collection, parent=None, class_id=None, student_id=None):
    # Initialize folder object with title
    folder = gdata.docs.data.Resource(type="folder", title=title)

    if root_collection != None:
        root_collection = client.GetResourceById(root_collection)

    # Use the Client Object to create the folder in the root of their Drive or the collection specified.
    folder = client.CreateResource(folder, collection=root_collection)

    # On success insert into database
    Database.insert(
        conn,
        Database.structure_insert_string(
            Utilities.clean_title(title), folder.resource_id.text, parent, class_id, student_id
        ),
    )

    return folder