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
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
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)
def set_to_archived(conn, client):
    enrollments = Database.get(Database.execute(conn, Database.enrollment_query_string()))
    database_contents = Database.get(Database.execute(conn, Database.compare_query_string()))

    items_to_archive = {}

    items_to_archive = [entry for entry in database_contents if Sync.not_exists_in_enrollments(entry, enrollments)]

    for entry in items_to_archive:
        Database.set_entry_to_archived(conn, entry)
def course_needs_renaming(conn, enrollment):
    qs = Database.construct_query_string('vlacs_class_folders_structure', 
                                        {'folder_name' : 
                                            {'value' : Utilities.gen_title(enrollment, "c"),
                                             'type' : 's'}})
    needs_renaming = Database.get(Database.execute(conn, qs))
    if len(needs_renaming) > 0:
        return False
    enrollment.folder_id = needs_renaming['folder_id']
    enrollment.folder_name = Utilites.gen_title(enrollment, "c")
    return True
def ShareFolder(client, conn, folder_entry):
    #Create the share structures and then share and modify permissions
    #for student and teacher.
    enrollment = Database.get(Database.execute(conn, Database.enrollment_query_string(where="class_id = '" + folder_entry['class_id'] + "' AND student_id = '" + folder_entry['student_id'] + "'")))
    structures = retrieve_share_structures()
    created_structures = {}
    
    print "Creating Share Structure..."

    for name, structure in structures.iteritems():
        created_structures[name] = create_share_structure(client, conn, enrollment, structure)
    
    share_roles = retrieve_share_roles(created_structures)

    print "Sharing Folders..."

    for folder in share_roles:
        # In production the teacher and student emails will come from enrollment dict
        if not folder['role']['teacher'] == 'none':
            share(client, folder['folder_id'], '*****@*****.**', folder['role']['teacher'])
        if not folder['role']['student'] == 'none':
            share(client, folder['folder_id'], '*****@*****.**', folder['role']['student'])

    print "Done!"
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