Exemplo n.º 1
0
    def test_archive_all_for_person(self) -> None:
        """
        Archive all should archive the given consents
        then bulk create new unset consents.
        """
        terms = Term.objects.active()
        self.assertNotEqual(len(terms), 0)
        person1 = Person.objects.create(personal="Harry",
                                        family="Potter",
                                        email="*****@*****.**")
        person2 = Person.objects.create(
            personal="Ron",
            family="Weasley",
            email="*****@*****.**",
            username="******",
        )
        # both people consent to all terms
        self.person_consent_active_terms(person1)
        self.person_consent_active_terms(person2)
        active_consents = Consent.objects.active()
        self.assertEqual(
            len(
                active_consents.filter(person=person1,
                                       term_option__isnull=False)),
            len(terms),
        )
        self.assertEqual(
            len(
                active_consents.filter(person=person2,
                                       term_option__isnull=False)),
            len(terms),
        )

        Consent.archive_all_for_person(person1)
        active_consents = Consent.objects.active()
        # All consents for person1 is archived. And a new unset consent is active
        self.assertEqual(
            len(
                active_consents.filter(person=person1,
                                       term_option__isnull=False)), 0)
        self.assertEqual(
            len(
                active_consents.filter(person=person1,
                                       term_option__isnull=True)),
            len(terms),
        )
        # person2 consents remain unchanged
        self.assertEqual(
            len(
                active_consents.filter(person=person2,
                                       term_option__isnull=False)),
            len(terms),
        )
Exemplo n.º 2
0
def consent_to_all_required_consents(person: Person) -> None:
    """
    Helper function shared by SuperuserMixin and TestBase
    """
    terms = (Term.objects.filter(required_type=Term.PROFILE_REQUIRE_TYPE).
             active().prefetch_active_options())
    old_consents = Consent.objects.filter(person=person,
                                          term__in=terms).active()
    old_consents_by_term_id = {
        consent.term_id: consent
        for consent in old_consents
    }
    for term in terms:
        old_consent = old_consents_by_term_id[term.id]
        Consent.reconsent(old_consent, term.options[0])
Exemplo n.º 3
0
def create_unset_consents_on_user_create(sender, instance: Person,
                                         created: bool, **kwargs):
    if created:
        Consent.objects.bulk_create(
            Consent(
                person=instance,
                term=term,
                term_option=None,
                archived_at=term.archived_at,
            ) for term in Term.objects.all())
Exemplo n.º 4
0
def create_unset_consents_on_term_create(sender, instance: Term, created: bool,
                                         **kwargs):
    if created:
        Consent.objects.bulk_create(
            Consent(
                person=person,
                term=instance,
                term_option=None,
                archived_at=instance.archived_at,
            ) for person in Person.objects.all())
Exemplo n.º 5
0
    def fake_consents(self):
        terms = Term.objects.active().prefetch_active_options()
        count = Person.objects.all().count() * len(
            terms)  # all persons * number of consents generated
        self.stdout.write("Generating {} fake consents...".format(count))

        consents: List[Consent] = []
        people = Person.objects.all()
        for person in people:
            for term in terms:
                consents.append(
                    Consent(person=person,
                            term_option=choice(term.options),
                            term=term))
        # Archive unset old consents before adding new ones
        Consent.objects.filter(
            person__in=people,
            term__in=terms,
        ).active().update(archived_at=timezone.now())
        Consent.objects.bulk_create(consents)
Exemplo n.º 6
0
    def setUp(self):
        self.admin = Person.objects.create_superuser(
            username="******",
            personal="Super",
            family="User",
            email="*****@*****.**",
            password="******",
        )
        consent_to_all_required_consents(self.admin)
        self.admin.airport = Airport.objects.first()
        self.admin.save()

        self.event = Event.objects.create(
            slug="test-event",
            host=Organization.objects.first(),
            administrator=Organization.objects.first(),
            assigned_to=self.admin,
        )

        self.award = Award.objects.create(
            person=self.admin,
            badge=Badge.objects.first(),
            event=self.event,
        )
        self.term = Term.objects.active().prefetch_active_options()[0]
        old_consent = Consent.objects.filter(
            person=self.admin,
            term=self.term,
        ).active()[0]
        self.consent = Consent.reconsent(consent=old_consent,
                                         term_option=self.term.options[0])

        self.instructor_role = Role.objects.create(name="instructor")

        self.task = Task.objects.create(event=self.event,
                                        person=self.admin,
                                        role=self.instructor_role)

        self.client.login(username="******", password="******")
Exemplo n.º 7
0
def unset_consents_on_person_archive(sender, **kwargs) -> None:
    person = kwargs["person"]
    Consent.archive_all_for_person(person=person)
Exemplo n.º 8
0
 def reconsent(person: Person, term: Term,
               term_option: Optional[TermOption]) -> Consent:
     consent = Consent.objects.get(person=person,
                                   term=term,
                                   archived_at__isnull=True)
     return Consent.reconsent(consent, term_option)
Exemplo n.º 9
0
 def email_users_to_reconsent(self, request, queryset):
     if not self.check_terms_for_consent_email(request, queryset):
         return
     Consent.archive_all_for_term(queryset)
     for term in queryset:
         send_consent_email(request, term)
Exemplo n.º 10
0
    def setUp(self):
        def get_option(term_slug: str, option_type: str) -> TermOption:
            return [
                option for option in terms_dict[term_slug].options
                if option.option_type == option_type
            ][0]

        def get_consent(term_slug: str, person: Person) -> Consent:
            return [
                consent for consent in old_consents
                if consent.term.slug == term_slug and consent.person == person
            ][0]

        self.admin_1 = Person.objects.create_superuser(
            username="******",
            personal="Super",
            family="User",
            email="*****@*****.**",
            password="******",
        )
        consent_to_all_required_consents(self.admin_1)
        self.admin_1.airport = Airport.objects.first()
        self.admin_1.save()

        self.admin_2 = Person.objects.create_superuser(
            username="******",
            personal="Super",
            family="User",
            email="*****@*****.**",
            password="******",
        )
        consent_to_all_required_consents(self.admin_2)
        self.admin_2.airport = Airport.objects.first()
        self.admin_2.save()

        terms = (Term.objects.filter(
            slug__in=["may-contact", "public-profile"
                      ]).active().prefetch_active_options())
        terms_dict = {term.slug: term for term in terms}
        may_contact_agree = get_option("may-contact", TermOption.AGREE)
        may_contact_decline = get_option("may-contact", TermOption.DECLINE)
        publish_profile_agree = get_option("public-profile", TermOption.AGREE)
        publish_profile_decline = get_option("public-profile",
                                             TermOption.DECLINE)

        old_consents = (Consent.objects.filter(
            person__in=[self.admin_1, self.admin_2],
            term__slug__in=["may-contact", "public-profile"],
        ).active().select_related("term", "person"))

        Consent.reconsent(
            consent=get_consent("may-contact", self.admin_1),
            term_option=may_contact_agree,
        )
        Consent.reconsent(
            consent=get_consent("may-contact", self.admin_2),
            term_option=may_contact_decline,
        )
        Consent.reconsent(
            consent=get_consent("public-profile", self.admin_1),
            term_option=publish_profile_agree,
        )
        Consent.reconsent(
            consent=get_consent("public-profile", self.admin_2),
            term_option=publish_profile_decline,
        )

        self.client.login(username="******", password="******")