Example #1
0
 def commit_new_badge():
     with transaction.atomic():
         issuer, issuer_created = Issuer.objects.get_or_create_from_ob2(
             issuer_obo,
             original_json=original_json.get(issuer_obo.get('id')),
             image=issuer_image)
         badgeclass, badgeclass_created = BadgeClass.objects.get_or_create_from_ob2(
             issuer,
             badgeclass_obo,
             original_json=original_json.get(badgeclass_obo.get('id')),
             image=badgeclass_image)
         if badgeclass_created and (getattr(
                 settings, 'BADGERANK_NOTIFY_ON_BADGECLASS_CREATE',
                 True) or 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 BadgeInstance.objects.get_or_create_from_ob2(
             badgeclass,
             assertion_obo,
             recipient_identifier=recipient_identifier,
             recipient_type=recipient_type,
             original_json=original_json.get(assertion_obo.get('id')),
             image=badgeinstance_image)
Example #2
0
    def create(self, **kwargs):
        obj = self.model(**kwargs)
        obj.save()

        if getattr(settings, 'BADGERANK_NOTIFY_ON_BADGECLASS_CREATE', True):
            from issuer.tasks import notify_badgerank_of_badgeclass
            notify_badgerank_of_badgeclass.delay(badgeclass_pk=obj.pk)

        return obj
Example #3
0
    def create(self, **kwargs):
        obj = self.model(**kwargs)
        obj.save()

        if getattr(settings, 'BADGERANK_NOTIFY_ON_BADGECLASS_CREATE', True):
            from issuer.tasks import notify_badgerank_of_badgeclass
            notify_badgerank_of_badgeclass.delay(badgeclass_pk=obj.pk)

        return obj
Example #4
0
 def put(self, request, **kwargs):
     response = super(BadgeClassDetail, self).put(request, **kwargs)
     if response.status_code == 200 and getattr(
             settings, 'BADGERANK_NOTIFY_ON_FIRST_ASSERTION', True):
         badgeclass = self.get_object(request, **kwargs)
         if badgeclass.recipient_count() > 0:
             from issuer.tasks import notify_badgerank_of_badgeclass
             notify_badgerank_of_badgeclass.delay(
                 badgeclass_pk=badgeclass.pk)
     return response
Example #5
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
Example #6
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