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 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
def student_needs_renaming(enrollment, database_contents):
    for entry in database_contents:
        if (enrollment.student.id == entry['student_id'] and
            Utilities.gen_title(enrollment, "s") != entry['folder_name']):
            enrollment.folder_id = entry['folder_id']
            enrollment.folder_name = entry['folder_name']
            return True
    return False
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 create(self, db_result):
		self.master_id         = db_result['master_id']
		
		self.course.id         = db_result['course_id']
		self.course.name       = db_result['course_name']
		self.course.version    = Utilities.course_version(db_result['course_full_name'])

		self.student.id        = db_result['student_id']
		self.student.firstname = db_result['student_firstname']
		self.student.lastname  = db_result['student_lastname']
		self.student.email     = db_result['student_email']

		self.teacher.id        = db_result['teacher_id']
		self.teacher.firstname = db_result['teacher_firstname']
		self.teacher.lastname  = db_result['teacher_lastname']
		self.teacher.email     = db_result['teacher_email']
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
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