Esempio n. 1
0
    def create(self,
               evidence=None,
               extensions=None,
               notify=False,
               check_completions=True,
               allow_uppercase=False,
               badgr_app=None,
               **kwargs):
        """
        Convenience method to award a badge to a recipient_id
        :param allow_uppercase: bool
        :type badgeclass: BadgeClass
        :type issuer: Issuer
        :type notify: bool
        :type check_completions: bool
        :type evidence: list of dicts(url=string, narrative=string)
        """
        recipient_identifier = kwargs.pop('recipient_identifier')
        recipient_identifier = recipient_identifier if allow_uppercase else recipient_identifier.lower(
        )

        badgeclass = kwargs.pop('badgeclass', None)
        issuer = kwargs.pop('issuer', badgeclass.issuer)

        # self.model would be a BadgeInstance
        new_instance = self.model(recipient_identifier=recipient_identifier,
                                  badgeclass=badgeclass,
                                  issuer=issuer,
                                  **kwargs)

        with transaction.atomic():
            new_instance.save()

            if evidence is not None:
                from issuer.models import BadgeInstanceEvidence
                for evidence_obj in evidence:
                    evidence_url = evidence_obj.get('evidence_url')
                    narrative = evidence_obj.get('narrative')
                    new_evidence = BadgeInstanceEvidence(
                        badgeinstance=new_instance, evidence_url=evidence_url)
                    if narrative:
                        new_evidence.narrative = narrative
                    new_evidence.save()

            if extensions is not None:
                for name, ext in extensions.items():
                    new_instance.badgeinstanceextension_set.create(
                        name=name, original_json=json.dumps(ext))

        if check_completions:
            award_badges_for_pathway_completion.delay(
                badgeinstance_pk=new_instance.pk)

        if not notify and getattr(settings,
                                  'GDPR_COMPLIANCE_NOTIFY_ON_FIRST_AWARD'):
            # always notify if this is the first time issuing to a recipient if configured for GDPR compliance
            if self.filter(
                    recipient_identifier=recipient_identifier).count() == 1:
                notify = True

        if notify:
            new_instance.notify_earner(badgr_app=badgr_app)

        if badgeclass.recipient_count() == 1 and (not getattr(
                settings,
                'BADGERANK_NOTIFY_ON_BADGECLASS_CREATE', True) and getattr(
                    settings, 'BADGERANK_NOTIFY_ON_FIRST_ASSERTION', True)):
            from issuer.tasks import notify_badgerank_of_badgeclass
            notify_badgerank_of_badgeclass.delay(badgeclass_pk=badgeclass.pk)

        return new_instance
Esempio n. 2
0
    def create(self,
        evidence=None,
        extensions=None,
        notify=False,
        check_completions=True,
        allow_uppercase=False,
        badgr_app=None,
        **kwargs
    ):

        """
        Convenience method to award a badge to a recipient_id
        :param allow_uppercase: bool
        :type badgeclass: BadgeClass
        :type issuer: Issuer
        :type notify: bool
        :type check_completions: bool
        :type evidence: list of dicts(url=string, narrative=string)
        """
        recipient_identifier = kwargs.pop('recipient_identifier')
        recipient_identifier = recipient_identifier if allow_uppercase else recipient_identifier.lower()

        badgeclass = kwargs.pop('badgeclass', None)
        issuer = kwargs.pop('issuer', badgeclass.issuer)

        # self.model would be a BadgeInstance
        new_instance = self.model(
            recipient_identifier=recipient_identifier,
            badgeclass=badgeclass,
            issuer=issuer,
            **kwargs
        )

        with transaction.atomic():
            new_instance.save()

            if evidence is not None:
                from issuer.models import BadgeInstanceEvidence
                for evidence_obj in evidence:
                    evidence_url = evidence_obj.get('evidence_url')
                    narrative = evidence_obj.get('narrative')
                    new_evidence = BadgeInstanceEvidence(badgeinstance=new_instance, evidence_url=evidence_url)
                    if narrative:
                        new_evidence.narrative = narrative
                    new_evidence.save()

            if extensions is not None:
                for name, ext in extensions.items():
                    new_instance.badgeinstanceextension_set.create(
                        name=name,
                        original_json=json.dumps(ext)
                    )

        if check_completions:
            award_badges_for_pathway_completion.delay(badgeinstance_pk=new_instance.pk)

        if not notify:
            # always notify if this is the first time issuing to a recipient
            if self.filter(recipient_identifier=recipient_identifier).count() == 1:
                notify = True

        if notify:
            new_instance.notify_earner(badgr_app=badgr_app)

        if badgeclass.recipient_count() == 1 and (
                not getattr(settings, 'BADGERANK_NOTIFY_ON_BADGECLASS_CREATE', True) and
                getattr(settings, 'BADGERANK_NOTIFY_ON_FIRST_ASSERTION', True)):
            from issuer.tasks import notify_badgerank_of_badgeclass
            notify_badgerank_of_badgeclass.delay(badgeclass_pk=badgeclass.pk)

        return new_instance