Exemplo n.º 1
0
    def get_or_create_content_group(store, course):
        """
        Returns the first user partition from the course which uses the
        CohortPartitionScheme, or generates one if no such partition is
        found.  The created partition is not saved to the course until
        the client explicitly creates a group within the partition and
        POSTs back.
        """
        content_group_configuration = get_cohorted_user_partition(course)
        if content_group_configuration is None:
            content_group_configuration = UserPartition(
                id=generate_int_id(MINIMUM_GROUP_ID, MYSQL_MAX_INT, GroupConfiguration.get_used_ids(course)),
                name=CONTENT_GROUP_CONFIGURATION_NAME,
                description=CONTENT_GROUP_CONFIGURATION_DESCRIPTION,
                groups=[],
                scheme_id=COHORT_SCHEME
            )
            return content_group_configuration.to_json()

        content_group_configuration = GroupConfiguration.update_content_group_usage_info(
            store,
            course,
            content_group_configuration
        )
        return content_group_configuration
Exemplo n.º 2
0
def _unique_partition_id(course):
    """Return a unique user partition ID for the course. """
    # Exclude all previously used IDs, even for partitions that have been disabled
    # (e.g. if the course author deleted an in-course reverifification block but
    # there are courseware components that reference the disabled partition).
    used_ids = set(p.id for p in course.user_partitions)
    return generate_int_id(used_ids=used_ids)
Exemplo n.º 3
0
    def get_or_create_content_group(store, course):
        """
        Returns the first user partition from the course which uses the
        CohortPartitionScheme, or generates one if no such partition is
        found.  The created partition is not saved to the course until
        the client explicitly creates a group within the partition and
        POSTs back.
        """
        content_group_configuration = get_cohorted_user_partition(course)
        if content_group_configuration is None:
            content_group_configuration = UserPartition(
                id=generate_int_id(MINIMUM_GROUP_ID, MYSQL_MAX_INT, GroupConfiguration.get_used_ids(course)),
                name=CONTENT_GROUP_CONFIGURATION_NAME,
                description=CONTENT_GROUP_CONFIGURATION_DESCRIPTION,
                groups=[],
                scheme_id=COHORT_SCHEME
            )
            return content_group_configuration.to_json()

        content_group_configuration = GroupConfiguration.update_partition_usage_info(
            store,
            course,
            content_group_configuration
        )
        return content_group_configuration
Exemplo n.º 4
0
def _unique_partition_id(course):
    """Return a unique user partition ID for the course. """
    # Exclude all previously used IDs, even for partitions that have been disabled
    # (e.g. if the course author deleted an in-course reverifification block but
    # there are courseware components that reference the disabled partition).
    used_ids = set(p.id for p in course.user_partitions)
    return generate_int_id(used_ids=used_ids)
Exemplo n.º 5
0
 def test_no_used_ids(self, times):
     """
     Verify that we get a random integer within the specified range
     when there are no used ids.
     """
     minimum = 1
     maximum = times
     for __ in range(times):
         self.assertIn(generate_int_id(minimum, maximum), range(minimum, maximum + 1))
Exemplo n.º 6
0
 def test_no_used_ids(self, times):
     """
     Verify that we get a random integer within the specified range
     when there are no used ids.
     """
     minimum = 1
     maximum = times
     for __ in range(times):
         self.assertIn(generate_int_id(minimum, maximum), list(range(minimum, maximum + 1)))
Exemplo n.º 7
0
 def assign_id(self, configuration_id=None):
     """
     Assign id for the json representation of group configuration.
     """
     if configuration_id:
         self.configuration['id'] = int(configuration_id)
     else:
         self.configuration['id'] = generate_int_id(
             MINIMUM_GROUP_ID, MYSQL_MAX_INT,
             GroupConfiguration.get_used_ids(self.course))
Exemplo n.º 8
0
    def assign_id(course, certificate_data, certificate_id=None):
        """
        Assign an identifier to the provided certificate data.
        If the caller did not provide an identifier, we autogenerate a unique one for them
        In addition, we check the certificate's signatories and ensure they also have unique ids
        """
        used_ids = CertificateManager.get_used_ids(course)
        if certificate_id:
            certificate_data['id'] = int(certificate_id)
        else:
            certificate_data['id'] = generate_int_id(CERTIFICATE_MINIMUM_ID,
                                                     MYSQL_MAX_INT, used_ids)

        for index, signatory in enumerate(certificate_data['signatories']):  # pylint: disable=unused-variable
            if signatory and not signatory.get('id', False):
                signatory['id'] = generate_int_id(used_ids=used_ids)
            used_ids.append(signatory['id'])

        return certificate_data
Exemplo n.º 9
0
 def assign_group_ids(self):
     """
     Assign ids for the group_configuration's groups.
     """
     used_ids = [g.id for p in self.course.user_partitions for g in p.groups]
     # Assign ids to every group in configuration.
     for group in self.configuration.get('groups', []):
         if group.get('id') is None:
             group["id"] = generate_int_id(MINIMUM_GROUP_ID, MYSQL_MAX_INT, used_ids)
             used_ids.append(group["id"])
Exemplo n.º 10
0
 def assign_id(self, configuration_id=None):
     """
     Assign id for the json representation of group configuration.
     """
     if configuration_id:
         self.configuration['id'] = int(configuration_id)
     else:
         self.configuration['id'] = generate_int_id(
             MINIMUM_GROUP_ID, MYSQL_MAX_INT, GroupConfiguration.get_used_ids(self.course)
         )
Exemplo n.º 11
0
 def assign_group_ids(self):
     """
     Assign ids for the group_configuration's groups.
     """
     used_ids = [g.id for p in get_all_partitions_for_course(self.course) for g in p.groups]
     # Assign ids to every group in configuration.
     for group in self.configuration.get('groups', []):
         if group.get('id') is None:
             group["id"] = generate_int_id(MINIMUM_GROUP_ID, MYSQL_MAX_INT, used_ids)
             used_ids.append(group["id"])
Exemplo n.º 12
0
 def test_used_ids(self, times):
     """
     Verify that we get a random integer within the specified range
     but not in a list of used ids.
     """
     minimum = 1
     maximum = times
     used_ids = {2, 4, 6, 8}
     for __ in range(times):
         int_id = generate_int_id(minimum, maximum, used_ids)
         self.assertIn(int_id, list(set(range(minimum, maximum + 1)) - used_ids))
Exemplo n.º 13
0
 def test_used_ids(self, times):
     """
     Verify that we get a random integer within the specified range
     but not in a list of used ids.
     """
     minimum = 1
     maximum = times
     used_ids = {2, 4, 6, 8}
     for __ in range(times):
         int_id = generate_int_id(minimum, maximum, used_ids)
         self.assertIn(int_id, list(set(range(minimum, maximum + 1)) - used_ids))
Exemplo n.º 14
0
    def assign_id(course, certificate_data, certificate_id=None):
        """
        Assign an identifier to the provided certificate data.
        If the caller did not provide an identifier, we autogenerate a unique one for them
        In addition, we check the certificate's signatories and ensure they also have unique ids
        """
        used_ids = CertificateManager.get_used_ids(course)
        if certificate_id:
            certificate_data['id'] = int(certificate_id)
        else:
            certificate_data['id'] = generate_int_id(
                CERTIFICATE_MINIMUM_ID,
                MYSQL_MAX_INT,
                used_ids
            )

        for index, signatory in enumerate(certificate_data['signatories']):  # pylint: disable=unused-variable
            if signatory and not signatory.get('id', False):
                signatory['id'] = generate_int_id(used_ids=used_ids)
            used_ids.append(signatory['id'])

        return certificate_data