Пример #1
0
def compare_db_with_drive(client, conn, limit, offset):
    enrollments = Database.get(Database.execute(conn, Database.enrollment_query_string(limit=limit, offset=offset)))
    database_contents = Database.get(Database.execute(conn, Database.compare_query_string()))

    gd_root_folders = Folder.list_sub_folders(client, "root")
    gd_contents = Folder.list_sub_folders(client, gd_root_folders[config.ROOT_CLASS_FOLDER])

    # REMOVE SYNCED ENROLLMENTS FROM DICT #
    enrollments = [enrollment for enrollment in enrollments if Sync.not_synced(enrollment, database_contents)]
    
    # MOVE ENROLLMENTS THAT NEED TO BE ARCHIVED TO archive_in_drive #
    archive_in_drive = [enrollment for enrollment in enrollments if Sync.should_archive(enrollment, database_contents)]
    enrollments = Utilities.remove_from_list(archive_in_drive, enrollments)

    # MOVE ENROLLMENTS THAT NEED RENAMING TO rename_in_drive #
    rename_course_in_drive = [enrollment for enrollment in enrollments if Sync.course_needs_renaming(conn, enrollment)]
    rename_in_drive = [enrollment for enrollment in enrollments if Sync.student_needs_renaming(enrollment, database_contents)]
    enrollments = Utilities.remove_from_list(rename_in_drive, enrollments)
    
    # MOVE ENROLLMENTS THAT ARE LEFT NEED TO BE CREATED IN DRIVE #
    create_in_drive = enrollments
    rename_course_in_drive = Utilities.remove_from_list(create_in_drive, rename_course_in_drive)

    archive_in_drive = ObjectUtilites.enrollment_list_from_dict(archive_in_drive)
    rename_course_in_drive = ObjectUtilites.enrollment_list_from_dict(rename_course_in_drive)
    rename_in_drive = ObjectUtilites.enrollment_list_from_dict(rename_in_drive)
    create_in_drive = ObjectUtilites.enrollment_list_from_dict(create_in_drive)

    return create_in_drive, rename_course_in_drive, rename_in_drive, archive_in_drive
Пример #2
0
def create_in_drive(conn, client, enrollments, count, offset):
    if offset != None:
        offset = int(offset)
        count = offset

    last_disp = len(enrollments)
    if offset != None:
        last_disp = len(enrollments) + offset

    for enrollment in enrollments:
        try:
            print("Processing enrollment %s/%s..." % (count, last_disp))

            fe_q = Database.get(Database.execute(conn, Database.folder_exists_query_string(enrollment.course.id)))
            rcf_q = Database.get(Database.execute(conn, query="SELECT folder_id FROM vlacs_class_folders_structure WHERE folder_name = '%s'" % (config.ROOT_CLASS_FOLDER)))
            fe_id = fe_q['folder_id']
            rcf_id = rcf_q['folder_id']

            if folder_exists:
                if isinstance(folder_exists, list):
                    folder_exists = folder_exists[0]
                print "Creating Student Folder: %s" % Utilities.gen_title(enrollment, "s")
                studentfolder = Folder.create_flat(conn, client, Utilities.gen_title(enrollment, "s"), rcf_id, fe_id, enrollment.course.id, enrollment.student.id)
            else:
                title = Utilities.gen_title(enrollment, "c")
                print "Creating Class Folder: %s" % title
                classfolder = Folder.create_flat(conn, client, title, rcf_id, rcf_id, enrollment.course.id)
                print "Creating Student Folder: %s" % Utilities.gen_title(enrollment, "s")
                studentfolder = Folder.create_flat(conn, client, Utilities.gen_title(enrollment, "s"), rcf_id, classfolder.resource_id.text, enrollment.course.id, enrollment.student.id)
            count += 1

        except GDRequestError as e:
            print "ERROR:", e.status
            count += 1
Пример #3
0
def create_share_structure(client, conn, enrollment, structure):
    parents = {}
    currentdir_folders = {}
    parsed_templates = {}
    max_level = 0
    created_structure = []

    for template, level in structure.iteritems():
        if(level > max_level):
            max_level = level
    for template, level in structure.iteritems():
        if template not in parsed_templates:
            parsed_templates[template] = ShareTemplate.get(client, conn, template, enrollment)

        folder = parsed_templates[template]
        #Create the folder in drive and set the folder id!
        if level == 0:
            #Level 0 will always be the root folders
            currentdir_folders = Folder.list_sub_folders(client, folder['folder_id'])
            cr_folder = folder_not_exists_create(client, conn, folder, template, folder['folder_id'], currentdir_folders)
            parents[level+1] = cr_folder['folder_id']
        else:
            currentdir_folders = Folder.list_sub_folders(client, parents[level])
            cr_folder = folder_not_exists_create(client, conn, folder, template, parents[level], currentdir_folders)
            parents[level+1] = cr_folder['folder_id']

        created_structure.append(cr_folder)
        dbg_arrow = '--' * level + '>'
        fmt = '{0:25}'
        print fmt.format(template), dbg_arrow, cr_folder['folder_id']

    return created_structure
Пример #4
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)
Пример #5
0
def folder_not_exists_create(client, conn, folder, template, parent, currentdir_folders):
    if folder['folder_name'] in currentdir_folders:
        #folder exists, update the folder_id and return it
        folder['folder_id'] = currentdir_folders[folder['folder_name']]
        return folder
    elif folder['copy']:
        #this is a folder that needs to be copied from somewhere else
        if template == "{{STUDENT_ASSIGNMENTS}}":
            Folder.copy(client, folder['folder_id'], parent)
            return folder
        elif template == "{{CLASS_FILES}}":
            table = "vlacs_class_folders_shared"
            cols = {'folder_name':{'value':folder['folder_name'],'type':'s'}}
            class_files = Database.insert_if_not_exists(conn, table, cols)

            if class_files:
                cr_folder = Folder.create(False, client, folder['folder_name'], parent)
                cols = {'folder_id':{'value':cr_folder.resource_id.text,'type':'s'}}
                wheres = {'folder_name':{'value':folder['folder_name'],'type':'s'}}
                Database.update(conn, table, cols, wheres)
                folder['folder_id'] = cr_folder.resource_id.text
            else:
                folder['folder_id'] = class_files['folder_id']
                Folder.copy(client, folder['folder_id'], parent)
            return folder
    else:
        #folder does not exist, create it and update the folder_id and return it
        cr_folder = Folder.create(False, client, folder['folder_name'], parent)
        folder['folder_id'] = cr_folder.resource_id.text
        return folder
def get(client, conn, template, enrollment=None):
	parsed_template = {'folder_name': "", 'role': {'teacher': "", 'student': ""}}

	template_variables = {
			"{{CLASS_FILES}}" : {
					 'folder_id'   : None,
					 'folder_name' : "Class Files (%s)" % (enrollment['class_id']),
					        'role' : {
										'teacher' : 'writer',
										'student' : 'reader',
									},
					'copy' : True
			},
			"{{CLASSROOM}}" : {
					 'folder_id'   : None,
					 'folder_name' : "%s (%s - %s)" % (enrollment['course_name'], enrollment['class_id'], Utilities.course_version(enrollment['course_full_name'])),
					        'role' : {
										'teacher' : 'reader',
										'student' : 'reader',
									},
					'copy' : False
			},
			"{{COURSE_NAME}}" : {
					 'folder_id'   : None,
					 'folder_name' : enrollment['course_name'],
					        'role' : {
										'teacher' : 'reader',
										'student' : 'reader',
									},
					'copy' : False
			},
			"{{COURSES}}" : {
					 'folder_id'   : None,
					 'folder_name' : "Courses",
					        'role' : {
										'teacher' : 'reader',
										'student' : 'reader',
									},
					'copy' : False
			},
			"{{STUDENT_ASSIGNMENTS}}" : {
					 'folder_id'   : None,
					 'folder_name' : None,
					        'role' : {
										'teacher' : 'writer',
										'student' : 'writer',
									},
					'copy' : True
			},
			"{{STUDENT_NAME}}" : {
					 'folder_name' : "%s, %s" % (enrollment['student_lastname'], enrollment['student_firstname']),
					        'role' : {
										'teacher' : 'reader',
										'student' : 'none',
									},
					'copy' : False
			},
			"{{STUDENT_ROOT}}" : {
					 'folder_name' : None,
					 'folder_id'   : None,
					        'role' : {
										'teacher' : 'none',
										'student' : 'reader',
									},
					'copy' : False
			},
			"{{STUDENTS}}" : {
					 'folder_id'   : None,
					 'folder_name' : "Students",
					        'role' : {
										'teacher' : 'reader',
										'student' : 'none',
									},
					'copy' : False
			},
			"{{TEACHER_ROOT}}" : {
					 'folder_id'   : None,
					 'folder_name' : None,
					        'role' : {
										'teacher' : 'reader',
										'student' : 'none',
									},
					'copy' : False
			},
		}

	# These template variables rely on external systems, we shouldn't generate them
	# each time the function is called.
	if template == "{{TEACHER_ROOT}}":
		root_folders = Folder.list_sub_folders(client, "root")
		template_variables[template]['folder_id'] = root_folders[config.TEACHER_SHARE_FOLDER]
		template_variables[template]['folder_name'] = config.TEACHER_SHARE_FOLDER + " (%s)" % (enrollment['teacher_id'])
	elif template == "{{STUDENT_ROOT}}":
		root_folders = Folder.list_sub_folders(client, "root")
		template_variables[template]['folder_id'] = root_folders[config.STUDENT_SHARE_FOLDER]
		template_variables[template]['folder_name'] = config.STUDENT_SHARE_FOLDER + " (%s)" % (enrollment['student_id'])
	elif template == "{{STUDENT_ASSIGNMENTS}}":
		folder_id = Database.get(Database.execute(conn, Database.structure_get_folder_id_string(Utilities.gen_title(enrollment, "s"), enrollment['class_id'], enrollment['student_id'])))
		folder_id = folder_id['folder_id']
		template_variables[template]['folder_id'] = folder_id
		template_variables[template]['folder_name'] = "%s, %s - Assignments" % (enrollment['student_lastname'], enrollment['student_firstname'])
		
	parsed_template = template_variables[template]

	return parsed_template