def build_pathway(self, creator): pathway = self.create_pathway(creator=creator) element_infos = [{ 'name': 'First Element', 'description': 'Element numero uno', 'parent': pathway.slug }, { 'name': 'Second Element', 'description': 'Element numero dos', 'parent': pathway.slug }, { 'name': 'Third Element', 'description': 'Element numero tres', 'parent': pathway.slug }] children = [] for element_info in element_infos: new_element = self.create_element(pathway, element_info, creator=creator) requirements = { "junctionConfig": { "requiredNumber": 1, "@type": "Disjunction" }, "@type": "BadgeJunction", "badges": [self.test_badgeclass.public_url] } new_element.completion_requirements = \ CompletionRequirementSpecFactory.parse_obj(requirements).serialize() new_element.save() children.append(new_element.jsonld_id) root_requirements = { "junctionConfig": { "requiredNumber": 1, "@type": "Disjunction" }, "@type": "ElementJunction", "elements": [children[0], children[1]] } pathway.root_element.completion_requirements = \ CompletionRequirementSpecFactory.parse_obj(root_requirements).serialize() pathway.root_element.save() return pathway
def cached_completions(self, pathway): # get recipients instances that are aligned to this pathway badgeclasses = pathway.cached_badgeclasses() instances = filter( lambda i: not i.revoked and i.cached_badgeclass in badgeclasses, self.cached_badge_instances()) # recurse the tree to build completions tree = pathway.element_tree completion_spec = CompletionRequirementSpecFactory.parse_element( tree['element']) if not completion_spec: # if there is no completionspec, infer one of elementjunction of all children elements completion_spec = ElementJunctionCompletionRequirementSpec( junction_type=CompletionRequirementSpecFactory. JUNCTION_TYPE_CONJUNCTION, required_number=len(tree['children']), elements=(c['element'].jsonld_id for c in tree['children'].itervalues())) tree[ 'element'].completion_requirements = completion_spec.serialize( ) if completion_spec.completion_type == CompletionRequirementSpecFactory.BADGE_JUNCTION: return [completion_spec.check_completion(tree, instances)] elif completion_spec.completion_type == CompletionRequirementSpecFactory.ELEMENT_JUNCTION: return completion_spec.check_completions(tree, instances) else: # unsupported completion_type return []
def update(self, element, validated_data): parent_element = None parent_slug = validated_data.get('parent') if parent_slug: try: parent_element = PathwayElement.cached.get_by_slug_or_id(parent_slug) except PathwayElement.DoesNotExist: raise ValidationError("Invalid parent") completion_requirements = None requirements = validated_data.get('requirements') if requirements: try: completion_requirements = CompletionRequirementSpecFactory.parse(requirements).serialize() except ValueError as e: raise ValidationError("Invalid requirements: {}".format(e)) child_ids = validated_data.get('children') order = 1 if child_ids: for element_id in child_ids: try: r = resolve(element_id.replace(OriginSetting.HTTP, '')) except Resolver404: raise ValidationError("Invalid child id: {}".format(element_id)) element_slug = r.kwargs.get('element_slug') try: child = PathwayElement.cached.get(slug=element_slug) except PathwayElement.DoesNotExist: raise ValidationError("Invalid child id: {}".format(element_id)) else: old_child_parent = child.parent_element child.parent_element = element child.ordering = order order += 1 child.save() old_child_parent.publish() old_parent = None if parent_element: old_parent = element.parent_element element.parent_element = parent_element element.completion_badgeclass = validated_data.get('completion_badgeclass') element.name = validated_data.get('name') element.description = validated_data.get('description') element.alignment_url = validated_data.get('alignmentUrl') if validated_data.get('alignmentUrl') else None element.ordering = validated_data.get('ordering', 99) element.completion_requirements = completion_requirements element.save() if old_parent: old_parent.publish() return element
def create(self, validated_data): pathway_slug = self.context.get('pathway_slug', None) if not pathway_slug: raise ValidationError("Could not determine pathway") try: pathway = Pathway.cached.get_by_slug_or_id(pathway_slug) except Pathway.DoesNotExist: raise ValidationError("Could not determine pathway") parent_slug = validated_data.get('parent') try: parent_element = PathwayElement.cached.get_by_slug_or_id( parent_slug) except PathwayElement.DoesNotExist: raise ValidationError("Invalid parent") else: if parent_element.pathway != pathway: raise ValidationError("Invalid parent") try: ordering = int(validated_data.get('ordering', 99)) except ValueError: ordering = 99 completion_requirements = None requirement_string = validated_data.get('requirements', None) if requirement_string: try: completion_requirements = CompletionRequirementSpecFactory.parse( requirement_string).serialize() except ValueError as e: raise ValidationError("Invalid completion spec: {}".format( str(e))) element = PathwayElement( pathway=pathway, parent_element=parent_element, ordering=ordering, name=validated_data.get('name'), description=validated_data.get('description', None), alignment_url=validated_data.get('alignmentUrl', None), completion_badgeclass=validated_data.get('completion_badgeclass'), completion_requirements=completion_requirements) element.save() return element
def build_single_element_pathway(self, creator): pathway = self.create_pathway(creator=creator) root_requirements = { "junctionConfig": { "requiredNumber": 1, "@type": "Disjunction" }, "@type": "BadgeJunction", "badges": [ self.test_badgeclass.public_url, ] } pathway.root_element.completion_requirements = \ CompletionRequirementSpecFactory.parse_obj(root_requirements).serialize() pathway.root_element.save() return pathway
def create(self, validated_data): pathway_slug = self.context.get('pathway_slug', None) if not pathway_slug: raise ValidationError("Could not determine pathway") try: pathway = Pathway.cached.get_by_slug_or_id(pathway_slug) except Pathway.DoesNotExist: raise ValidationError("Could not determine pathway") parent_slug = validated_data.get('parent') try: parent_element = PathwayElement.cached.get_by_slug_or_id(parent_slug) except PathwayElement.DoesNotExist: raise ValidationError("Invalid parent") else: if parent_element.pathway != pathway: raise ValidationError("Invalid parent") try: ordering = int(validated_data.get('ordering', 99)) except ValueError: ordering = 99 completion_requirements = None requirement_string = validated_data.get('requirements', None) if requirement_string: try: completion_requirements = CompletionRequirementSpecFactory.parse(requirement_string).serialize() except ValueError as e: raise ValidationError("Invalid completion spec: {}".format(e.message)) element = PathwayElement(pathway=pathway, parent_element=parent_element, ordering=ordering, name=validated_data.get('name'), description=validated_data.get('description', None), alignment_url=validated_data.get('alignmentUrl', None), completion_badgeclass=validated_data.get('completion_badgeclass'), completion_requirements=completion_requirements) element.save() return element