Exemplo n.º 1
0
    def update(self, instance, validated_data):
        """
        Override the `update` function to add extra functinality.
        """
        # Get our inputs.
        request = self.context.get('request')
        watch_slug = validated_data.get('watch')
        watch = Watch.objects.get(slug=watch_slug)

        # Update our model.
        instance.watch = watch
        instance.last_modified_by = request.user
        instance.last_modified_from = request.client_ip
        instance.last_modified_from_is_public = request.client_ip_is_routable
        instance.save()

        # Run the following which will save our searchable content.
        instance.indexed_text = Member.get_searchable_content(instance)
        instance.last_modified_by = request.user
        instance.last_modified_from = request.client_ip
        instance.last_modified_from_is_public = request.client_ip_is_routable
        instance.save()

        # raise serializers.ValidationError({ # Uncomment when not using this code but do not delete!
        #     "error": "Terminating for debugging purposes only."
        # })

        # Return our modified instances.
        return instance
    def update(self, instance, validated_data):
        """
        Override the `update` function to add extra functinality.
        """
        # ------ MEMBER ADDRESS ------ #
        request = self.context.get('request')
        instance.country = validated_data.get('country', None)
        instance.province = validated_data.get('province', None)
        instance.city = validated_data.get('city', None)
        instance.street_number = validated_data.get('street_number', None)
        instance.street_name = validated_data.get('street_name', None)
        instance.apartment_unit = validated_data.get('apartment_unit', None)
        instance.street_type = validated_data.get('street_type', None)
        instance.street_type_other = validated_data.get('street_type_other', None)
        instance.street_direction = validated_data.get('street_direction', None)
        instance.postal_code = validated_data.get('postal_code', None)
        instance.save()
        logger.info("Updated area_coordinator address.")

        # Run the following which will save our searchable content.
        instance.member.indexed_text = Member.get_searchable_content(instance.member)
        instance.member.save()

        # raise serializers.ValidationError({ # Uncomment when not using this code but do not delete!
        #     "error": "Terminating for debugging purposes only."
        # })

        return instance
Exemplo n.º 3
0
    def handle(self, *args, **options):
        # Connection needs first to be at the public schema, as this is where
        # the database needs to be set before creating a new tenant. If this is
        # not done then django-tenants will raise a "Can't create tenant outside
        # the public schema." error.
        connection.set_schema_to_public()  # Switch to Public.

        # Get the user inputs.
        schema_name = options['schema_name'][0]
        length = options['length'][0]

        try:
            organization = SharedOrganization.objects.get(
                schema_name=schema_name)
        except SharedOrganization.DoesNotExist:
            raise CommandError(_('Organization does not exist!'))

        # Connection will set it back to our tenant.
        connection.set_schema(organization.schema_name,
                              True)  # Switch to Tenant.

        members = Member.seed(organization, length)

        # Iterate through all the randomly generated members
        for member in members:
            freezer = freeze_time(member.last_modified_at)
            freezer.start()

            # Run the following which will save our searchable content.
            indexed_text = Member.get_searchable_content(member)
            member.indexed_text = Truncator(indexed_text).chars(1023)
            member.save()

            freezer.stop()

        # For debugging purposes.
        self.stdout.write(
            self.style.SUCCESS(
                _('Successfully seed the following member(s):')))

        for member in members:
            self.stdout.write(
                self.style.SUCCESS(
                    _('Slug %(slug)s.') % {
                        'slug': member.user.slug,
                    }))
    def update(self, instance, validated_data):
        """
        Override the `update` function to add extra functinality.
        """
        # DEVELOPERS NOTE:
        # (1) The body of the metrics data.
        instance.how_did_you_hear = validated_data.get('how_did_you_hear')
        instance.how_did_you_hear_other = validated_data.get('how_did_you_hear_other', None)
        instance.expectation = validated_data.get('expectation')
        instance.expectation_other = validated_data.get('expectation_other', None)
        instance.meaning = validated_data.get('meaning')
        instance.meaning_other = validated_data.get('meaning_other', None)
        instance.gender = validated_data.get('gender')
        instance.willing_to_volunteer = validated_data.get('willing_to_volunteer')
        instance.year_of_birth = validated_data.get('year_of_birth')

        # DEVELOPERS NOTE:
        # (1) Modified household statistics dependent on whether the household
        #     staff was registered or not.
        another_household_member_registered = validated_data.get('another_household_member_registered')
        instance.another_household_member_registered = another_household_member_registered
        if another_household_member_registered:
            instance.total_household_count = None
            instance.over_18_years_household_count = None
        else:
            instance.total_household_count = validated_data.get('total_household_count')
            instance.over_18_years_household_count = validated_data.get('over_18_years_household_count')

        # DEVELOPERS NOTE:
        # (1) Non-business staffs cannot have the following fields set,
        #     therefore we need to remove the data if the user submits them.
        if instance.member.type_of != Member.MEMBER_TYPE_OF.BUSINESS:
            organization_employee_count = None
            organization_founding_year = None
        else:
            instance.organization_employee_count = validated_data.get('organization_employee_count')
            instance.organization_founding_year = validated_data.get('organization_founding_year')

        # DEVELOPERS NOTE:
        # (1) Update the `last_modified` fields from the context.
        request = self.context.get('request')
        instance.last_modified_by = request.user
        instance.last_modified_from = request.client_ip
        instance.last_modified_from_is_public = request.client_ip_is_routable
        instance.save()
        logger.info("Updated staff metrics.")

        # Run the following which will save our searchable content.
        instance.member.indexed_text = Member.get_searchable_content(instance.member)
        instance.member.save()

        # raise serializers.ValidationError({ # Uncomment when not using this code but do not delete!
        #     "error": "Terminating for debugging purposes only."
        # })

        return instance
    def update(self, instance, validated_data):
        """
        Override the `update` function to add extra functinality.
        """
        request = self.context.get('request')
        instance.is_ok_to_email = validated_data.get('is_ok_to_email',
                                                     instance.is_ok_to_email)
        instance.is_ok_to_text = validated_data.get('is_ok_to_text',
                                                    instance.is_ok_to_text)
        instance.organization_name = validated_data.get(
            'organization_name', instance.organization_name)
        instance.organization_type_of = validated_data.get(
            'organization_type_of', instance.organization_type_of)
        instance.first_name = validated_data.get('first_name',
                                                 instance.first_name)
        instance.last_name = validated_data.get('last_name',
                                                instance.last_name)
        instance.email = validated_data.get('email', instance.email)
        instance.primary_phone = validated_data.get('primary_phone',
                                                    instance.primary_phone)
        instance.secondary_phone = validated_data.get('secondary_phone',
                                                      instance.secondary_phone)
        instance.last_modified_by = request.user
        instance.last_modified_from = request.client_ip
        instance.last_modified_from_is_public = request.client_ip_is_routable

        # # DEVELOPERS NOTE:
        # (1) Non-business members cannot have the following fields set,
        #     therefore we need to remove the data if the user submits them.
        if instance.member.type_of != Member.MEMBER_TYPE_OF.BUSINESS:
            instance.organization_name = None
            instance.organization_type_of = MemberContact.MEMBER_ORGANIZATION_TYPE_OF.UNSPECIFIED

        instance.save()
        logger.info("Updated member contact.")

        # Run the following which will save our searchable content.
        instance.member.indexed_text = Member.get_searchable_content(
            instance.member)
        instance.member.last_modified_by = request.user
        instance.member.last_modified_from = request.client_ip
        instance.member.last_modified_from_is_public = request.client_ip_is_routable
        instance.member.save()

        # raise serializers.ValidationError({ # Uncomment when not using this code but do not delete!
        #     "error": "Terminating for debugging purposes only."
        # })

        return instance
    def handle(self, *args, **options):
        # Connection needs first to be at the public schema, as this is where
        # the database needs to be set before creating a new tenant. If this is
        # not done then django-tenants will raise a "Can't create tenant outside
        # the public schema." error.
        connection.set_schema_to_public()  # Switch to Public.

        # Get the user inputs.
        schema_name = options['schema_name'][0]
        length = options['length'][0]

        try:
            organization = SharedOrganization.objects.get(
                schema_name=schema_name)
        except SharedOrganization.DoesNotExist:
            raise CommandError(_('Organization does not exist!'))

        # Connection will set it back to our tenant.
        connection.set_schema(organization.schema_name,
                              True)  # Switch to Tenant.

        members = Member.seed(organization, length)

        # Iterate through all the randomly generated members
        for member in members:
            freezer = freeze_time(member.last_modified_at)
            freezer.start()

            # Run the following which will save our searchable content.
            indexed_text = Member.get_searchable_content(member)
            member.indexed_text = Truncator(indexed_text).chars(1023)
            member.save()

            # Promote the `member` to be an `area coordinator`.
            associate = member.promote_to_associate(
                defaults={
                    'has_signed_associate_agreement': True,
                    'has_signed_conflict_of_interest_agreement': True,
                    'has_signed_code_of_conduct_agreement': True,
                    'has_signed_confidentiality_agreement': True,
                    'police_check_date': timezone.now(),
                    'created_by': None,
                    'created_from': None,
                    'created_from_is_public': False,
                    'last_modified_by': None,
                    'last_modified_from': None,
                    'last_modified_from_is_public': False,
                })

            # For debugging purposes.
            self.stdout.write(
                self.style.SUCCESS(
                    _('Promoted member slug %(slug)s to area coordinator.') % {
                        'slug': member.user.slug,
                    }))

            freezer.stop()

        # For debugging purposes.
        self.stdout.write(
            self.style.SUCCESS(
                _('Successfully seed the following member(s):')))

        for member in members:
            self.stdout.write(
                self.style.SUCCESS(
                    _('Slug %(slug)s.') % {
                        'slug': member.user.slug,
                    }))
Exemplo n.º 7
0
    def create(self, validated_data):
        """
        Override the `create` function to add extra functinality.
        """
        request = self.context.get('request')

        # ------ MEMBER ------ #

        type_of = validated_data.get('type_of')
        first_name = validated_data.get('first_name')
        last_name = validated_data.get('last_name')
        email = validated_data.get('email')
        watch_slug = validated_data.get('watch')
        watch = Watch.objects.filter(slug=watch_slug).first()

        user = SharedUser.objects.create(
            tenant=request.tenant,
            first_name=first_name,
            last_name=last_name,
            email=email,
            was_email_activated=True,
            is_active=True,
        )
        logger.info("Created shared user.")

        # Attach the user to the `Member` group.
        user.groups.add(SharedGroup.GROUP_MEMBERSHIP.MEMBER)
        logger.info("Shared user assigned as member.")

        member = Member.objects.create(
            user=user,
            type_of=type_of,
            watch=watch,
            created_by=request.user,
            created_from=request.client_ip,
            created_from_is_public=request.client_ip_is_routable,
            last_modified_by=request.user,
            last_modified_from=request.client_ip,
            last_modified_from_is_public=request.client_ip_is_routable,
        )
        logger.info("Created member.")

        # ------ MEMBER CONTACT ------ #

        is_ok_to_email = validated_data.get('is_ok_to_email')
        is_ok_to_text = validated_data.get('is_ok_to_text')
        organization_name = validated_data.get('organization_name')
        organization_type_of = validated_data.get('organization_type_of')
        primary_phone = validated_data.get('primary_phone', None)
        secondary_phone = validated_data.get('secondary_phone', None)

        # DEVELOPERS NOTE:
        # (1) Non-business members cannot have the following fields set,
        #     therefore we need to remove the data if the user submits them.
        if type_of != Member.MEMBER_TYPE_OF.BUSINESS:
            organization_name = None
            organization_type_of = MemberContact.MEMBER_ORGANIZATION_TYPE_OF.UNSPECIFIED

        member_contact = MemberContact.objects.create(
            member=member,
            is_ok_to_email=is_ok_to_email,
            is_ok_to_text=is_ok_to_text,
            organization_name=organization_name,
            organization_type_of=organization_type_of,
            first_name=first_name,
            last_name=last_name,
            email=email,
            primary_phone=primary_phone,
            secondary_phone=secondary_phone,
            created_by=request.user,
            created_from=request.client_ip,
            created_from_is_public=request.client_ip_is_routable,
            last_modified_by=request.user,
            last_modified_from=request.client_ip,
            last_modified_from_is_public=request.client_ip_is_routable,
        )
        logger.info("Created member contact.")

        # ------ MEMBER ADDRESS ------ #

        country = validated_data.get('country', None)
        province = validated_data.get('province', None)
        city = validated_data.get('city', None)
        street_number = validated_data.get('street_number', None)
        street_name = validated_data.get('street_name', None)
        apartment_unit = validated_data.get('apartment_unit', None)
        street_type = validated_data.get('street_type', None)
        street_type_other = validated_data.get('street_type_other', None)
        street_direction = validated_data.get(
            'street_direction', MemberAddress.STREET_DIRECTION.NONE)
        postal_code = validated_data.get('postal_code', None)
        member_address = MemberAddress.objects.create(
            member=member,
            country=country,
            province=province,
            city=city,
            street_number=street_number,
            street_name=street_name,
            apartment_unit=apartment_unit,
            street_type=street_type,
            street_type_other=street_type_other,
            street_direction=street_direction,
            postal_code=postal_code,
            created_by=request.user,
            created_from=request.client_ip,
            created_from_is_public=request.client_ip_is_routable,
            last_modified_by=request.user,
            last_modified_from=request.client_ip,
            last_modified_from_is_public=request.client_ip_is_routable,
        )
        logger.info("Created member address.")

        # ------ MEMBER METRICS ------ #

        how_did_you_hear = validated_data.get('how_did_you_hear')
        how_did_you_hear_other = validated_data.get('how_did_you_hear_other',
                                                    None)
        expectation = validated_data.get('expectation')
        expectation_other = validated_data.get('expectation_other', None)
        meaning = validated_data.get('meaning')
        meaning_other = validated_data.get('meaning_other', None)
        gender = validated_data.get('gender')
        willing_to_volunteer = validated_data.get('willing_to_volunteer')
        another_household_member_registered = validated_data.get(
            'another_household_member_registered')
        year_of_birth = validated_data.get('year_of_birth')
        total_household_count = validated_data.get('total_household_count')
        over_18_years_household_count = validated_data.get(
            'over_18_years_household_count')
        organization_employee_count = validated_data.get(
            'organization_employee_count')
        organization_founding_year = validated_data.get(
            'organization_founding_year')
        is_aboriginal = validated_data.get('is_aboriginal', False)
        is_transgender = validated_data.get('is_transgender', False)
        is_visible_minority = validated_data.get('is_visible_minority', False)
        is_disabled_or_has_barriers = validated_data.get(
            'is_disabled_or_has_barriers', False)
        is_over_fifty_five = validated_data.get('is_over_fifty_five', False)

        # DEVELOPERS NOTE:
        # (1) Non-business members cannot have the following fields set,
        #     therefore we need to remove the data if the user submits them.
        if type_of != Member.MEMBER_TYPE_OF.BUSINESS:
            organization_employee_count = None
            organization_founding_year = None

        member_metric = MemberMetric.objects.create(
            member=member,
            how_did_you_hear=how_did_you_hear,
            how_did_you_hear_other=how_did_you_hear_other,
            expectation=expectation,
            expectation_other=expectation_other,
            meaning=meaning,
            meaning_other=meaning_other,
            gender=gender,
            willing_to_volunteer=willing_to_volunteer,
            another_household_member_registered=
            another_household_member_registered,
            year_of_birth=year_of_birth,
            total_household_count=total_household_count,
            over_18_years_household_count=over_18_years_household_count,
            organization_employee_count=organization_employee_count,
            organization_founding_year=organization_founding_year,
            is_aboriginal=is_aboriginal,
            is_transgender=is_transgender,
            is_visible_minority=is_visible_minority,
            is_disabled_or_has_barriers=is_disabled_or_has_barriers,
            is_over_fifty_five=is_over_fifty_five,
        )
        logger.info("Created member metric.")

        # Attached our tags.
        tags = validated_data.get('tags', None)
        if tags is not None:
            if len(tags) > 0:
                member_metric.tags.set(tags)
                logger.info("Attached tag to member metric.")

        # Run the following which will save our searchable content.
        indexed_text = Member.get_searchable_content(member)
        member.indexed_text = Truncator(indexed_text).chars(1023)
        member.save()

        # raise serializers.ValidationError({ # Uncomment when not using this code but do not delete!
        #     "error": "Terminating for debugging purposes only."
        # })

        return member