class AreaCoordinatorListSerializer(serializers.Serializer): type_of = serializers.IntegerField( source="user.member.type_of", read_only=True, ) organization_name = serializers.CharField( allow_blank=False, validators=[], source="user.member.contact.organization_name", read_only=True, ) organization_type_of = serializers.IntegerField( read_only=True, allow_null=True, source="user.member.contact.organization_type_of", ) first_name = serializers.CharField( read_only=True, allow_blank=False, validators=[], source="user.member.contact.first_name", ) last_name = serializers.CharField( read_only=True, allow_blank=False, validators=[], source="user.member.contact.last_name", ) primary_phone_e164 = E164PhoneNumberField( allow_null=False, read_only=True, source="user.member.contact.primary_phone", ) primary_phone_national = NationalPhoneNumberField( allow_null=False, read_only=True, source="user.member.contact.primary_phone", ) email = serializers.EmailField( read_only=True, allow_blank=False, validators=[], source="user.member.contact.email", ) slug = serializers.SlugField( read_only=True, allow_blank=False, validators=[], source="user.slug", ) street_address = serializers.CharField( read_only=True, allow_blank=False, validators=[], source="user.member.address.street_address", ) country = serializers.CharField( read_only=True, allow_blank=False, validators=[], source="user.member.address.country", ) province = serializers.CharField( read_only=True, allow_blank=False, validators=[], source="user.member.address.province", ) city = serializers.CharField( read_only=True, allow_blank=False, validators=[], source="user.member.address.city", ) postal_code = serializers.CharField( read_only=True, allow_blank=False, validators=[], source="user.member.address.postal_code", ) state = serializers.CharField( read_only=True, source="user.member.state", ) position = serializers.SerializerMethodField() def setup_eager_loading(cls, queryset): """ Perform necessary eager loading of data. """ queryset = queryset.prefetch_related('user', 'user__groups', 'user__member__contact', 'user__member__address', 'user__member__metric', 'created_by', 'last_modified_by') return queryset def get_position(self, obj): return get_arr_from_point(obj.user.member.address.position)
class AssociateCreateSerializer(serializers.Serializer): # ------ MEMBER ------ # type_of = serializers.IntegerField() # ------ MEMBER CONTACT ------ # is_ok_to_email = serializers.BooleanField() is_ok_to_text = serializers.BooleanField() organization_name = serializers.CharField( required=False, allow_blank=True, allow_null=True, validators=[ UniqueValidator(queryset=AssociateContact.objects.all()), ], ) organization_type_of = serializers.ChoiceField( required=False, allow_null=True, choices=AssociateContact.MEMBER_ORGANIZATION_TYPE_OF_CHOICES, ) first_name = serializers.CharField() last_name = serializers.CharField() email = serializers.EmailField(validators=[ UniqueValidator(queryset=SharedUser.objects.all()), ], ) primary_phone = E164PhoneNumberField() secondary_phone = E164PhoneNumberField(allow_null=True, required=False) # ------ MEMBER ADDRESS ------ # country = serializers.CharField() province = serializers.CharField() city = serializers.CharField() street_number = serializers.CharField() street_name = serializers.CharField() apartment_unit = serializers.CharField( required=False, allow_null=True, allow_blank=True, ) street_type = serializers.ChoiceField( choices=AssociateAddress.STREET_TYPE_CHOICES, ) street_type_other = serializers.CharField( required=False, allow_null=True, allow_blank=True, ) street_direction = serializers.ChoiceField( choices=AssociateAddress.STREET_DIRECTION_CHOICES, ) postal_code = serializers.CharField() # ------ MEMBER WATCH ------ # #TODO: IMPLEMENT FIELDS. # ------ MEMBER METRICS ------ # tags = serializers.PrimaryKeyRelatedField( many=True, queryset=Tag.objects.all(), allow_null=True, required=False, ) how_did_you_hear = serializers.PrimaryKeyRelatedField( many=False, required=True, allow_null=False, queryset=HowHearAboutUsItem.objects.all()) how_did_you_hear_other = serializers.CharField( required=False, allow_null=True, allow_blank=True, ) expectation = serializers.PrimaryKeyRelatedField( many=False, required=True, allow_null=False, queryset=ExpectationItem.objects.all()) expectation_other = serializers.CharField( required=False, allow_null=True, allow_blank=True, ) meaning = serializers.PrimaryKeyRelatedField( many=False, required=True, allow_null=False, queryset=MeaningItem.objects.all()) meaning_other = serializers.CharField( required=False, allow_null=True, allow_blank=True, ) gender = serializers.CharField( required=True, allow_blank=False, allow_null=False, ) willing_to_volunteer = serializers.IntegerField() another_household_associate_registered = serializers.BooleanField() year_of_birth = serializers.IntegerField() total_household_count = serializers.IntegerField(required=False, ) over_18_years_household_count = serializers.IntegerField(required=False, ) organization_employee_count = serializers.IntegerField(required=False, ) organization_founding_year = serializers.IntegerField(required=False, ) organization_type_of = serializers.IntegerField(required=False, ) def validate_organization_name(self, value): """ Custom validation to handle case of user selecting an organization type of associate but then forgetting to fill in the `organization_name` field. """ request = self.context.get('request') type_of = request.data.get('type_of') if type_of == Associate.MEMBER_TYPE_OF.BUSINESS: if value == None or value == "": raise serializers.ValidationError( _('Please fill this field in.')) return value 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') 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 `Associate` group. user.groups.add(SharedGroup.GROUP_MEMBERSHIP.MEMBER) logger.info("Shared user assigned as associate.") associate = Associate.objects.create( user=user, type_of=type_of, 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 associate.") # ------ 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 associates cannot have the following fields set, # therefore we need to remove the data if the user submits them. if type_of != Associate.MEMBER_TYPE_OF.BUSINESS: organization_name = None organization_type_of = AssociateContact.MEMBER_ORGANIZATION_TYPE_OF.UNSPECIFIED associate_contact = AssociateContact.objects.create( associate=associate, 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 associate 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', AssociateAddress.STREET_DIRECTION.NONE) postal_code = validated_data.get('postal_code', None) associate_address = AssociateAddress.objects.create( associate=associate, 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 associate 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_associate_registered = validated_data.get( 'another_household_associate_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') # DEVELOPERS NOTE: # (1) Non-business associates cannot have the following fields set, # therefore we need to remove the data if the user submits them. if type_of != Associate.MEMBER_TYPE_OF.BUSINESS: organization_employee_count = None organization_founding_year = None associate_metric = AssociateMetric.objects.create( associate=associate, 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_associate_registered= another_household_associate_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, ) logger.info("Created associate metric.") # Attached our tags. tags = validated_data.get('tags', None) if tags is not None: if len(tags) > 0: associate_metric.tags.set(tags) logger.info("Attached tag to associate metric.") ''' Run in the background the code which will `process` the newly created associate object. ''' django_rq.enqueue(process_associate_with_slug_func, request.tenant.schema_name, associate.user.slug) # raise serializers.ValidationError({ # Uncomment when not using this code but do not delete! # "error": "Terminating for debugging purposes only." # }) return associate
class StaffUpdateSerializer(serializers.Serializer): # ------ MEMBER ------ # type_of = serializers.IntegerField(write_only=True, ) # ------ MEMBER CONTACT ------ # is_ok_to_email = serializers.IntegerField(write_only=True, ) is_ok_to_text = serializers.IntegerField(write_only=True, ) organization_name = serializers.CharField( required=False, allow_blank=True, allow_null=True, validators=[ UniqueValidator(queryset=StaffContact.objects.all()), ], write_only=True, ) organization_type_of = serializers.IntegerField( required=False, allow_null=True, write_only=True, ) first_name = serializers.CharField(write_only=True, ) last_name = serializers.CharField(write_only=True, ) email = serializers.EmailField( validators=[ UniqueValidator(queryset=SharedUser.objects.all()), ], write_only=True, ) primary_phone = E164PhoneNumberField(write_only=True, ) secondary_phone = E164PhoneNumberField( allow_null=True, required=False, write_only=True, ) # ------ MEMBER ADDRESS ------ # country = serializers.CharField(write_only=True, ) province = serializers.CharField(write_only=True, ) city = serializers.CharField(write_only=True, ) street_number = serializers.CharField(write_only=True, ) street_name = serializers.CharField(write_only=True, ) apartment_unit = serializers.CharField(write_only=True, ) street_type = serializers.CharField(write_only=True, ) street_type_other = serializers.CharField( required=False, allow_null=True, allow_blank=True, write_only=True, ) street_direction = serializers.CharField( required=False, allow_null=True, allow_blank=True, write_only=True, ) postal_code = serializers.CharField(write_only=True, ) # ------ MEMBER WATCH ------ # #TODO: IMPLEMENT FIELDS. # ------ MEMBER METRICS ------ # tags = serializers.PrimaryKeyRelatedField( many=True, queryset=Tag.objects.all(), allow_null=True, required=False, write_only=True, ) how_did_you_hear = serializers.PrimaryKeyRelatedField( many=False, required=True, allow_null=False, queryset=HowHearAboutUsItem.objects.all(), write_only=True, ) how_did_you_hear_other = serializers.CharField( required=False, allow_null=True, allow_blank=True, write_only=True, ) expectation = serializers.PrimaryKeyRelatedField( many=False, required=True, allow_null=False, queryset=ExpectationItem.objects.all(), write_only=True, ) expectation_other = serializers.CharField( required=False, allow_null=True, allow_blank=True, write_only=True, ) meaning = serializers.PrimaryKeyRelatedField( many=False, required=True, allow_null=False, queryset=MeaningItem.objects.all(), write_only=True, ) meaning_other = serializers.CharField( required=False, allow_null=True, allow_blank=True, write_only=True, ) gender = serializers.CharField( required=True, allow_blank=False, allow_null=False, write_only=True, ) willing_to_volunteer = serializers.IntegerField(write_only=True, ) another_household_staff_registered = serializers.BooleanField( write_only=True, ) year_of_birth = serializers.IntegerField(write_only=True, ) total_household_count = serializers.IntegerField(write_only=True, ) over_18_years_household_count = serializers.IntegerField(write_only=True, ) organization_employee_count = serializers.IntegerField(write_only=True, ) organization_founding_year = serializers.IntegerField(write_only=True, ) organization_type_of = serializers.IntegerField(write_only=True, ) def update(self, instance, validated_data): return validated_data """ Override the `create` function to add extra functinality. """ request = self.context.get('request') print("---->>>>", request) #TODO: IMPLEMENT CODE BELOW. # # ------ 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') # # 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("Updated shared user.") # # # Attach the user to the `Staff` group. # user.groups.add(MEMBER_GROUP_ID) # logger.info("Shared user assigned as staff.") # # staff = Staff.objects.create( # user=user, # type_of=type_of, # 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("Updated staff.") # # # ------ 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) # primary_phone = phonenumbers.parse(primary_phone, "CA") # secondary_phone = validated_data.get('secondary_phone', None) # if secondary_phone is not None: # secondary_phone = phonenumbers.parse(secondary_phone, "CA") # # staff_contact = StaffContact.objects.create( # staff=staff, # 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("Updated staff 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', None) # postal_code = validated_data.get('postal_code', None) # staff_address = StaffAddress.objects.create( # staff=staff, # 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("Updated staff 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', 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_staff_registered = validated_data.get('another_household_staff_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') # # staff_metric = StaffMetric.objects.create( # staff=staff, # 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_staff_registered=another_household_staff_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, # ) # logger.info("Updated staff metric.") # # # Attached our tags. # tags = validated_data.get('tags', None) # if tags is not None: # if len(tags) > 0: # staff_metric.tags.set(tags) # logger.info("Attached tag to staff metric.") # # return staff raise serializers.ValidationError( { # Uncomment when not using this code but do not delete! "error": "Terminating for debugging purposes only." }) logger.info("Updated area coordinator.") return staff
class MemberUpdateSerializer(serializers.Serializer): # ------ MEMBER ------ # type_of = serializers.IntegerField() # ------ MEMBER CONTACT ------ # is_ok_to_email = serializers.IntegerField() is_ok_to_text = serializers.IntegerField() organization_name = serializers.CharField( required=False, allow_blank=True, allow_null=True, validators=[ UniqueValidator(queryset=MemberContact.objects.all()), ], ) organization_type_of = serializers.IntegerField( required=False, allow_null=True, ) first_name = serializers.CharField() last_name = serializers.CharField() email = serializers.EmailField(validators=[ UniqueValidator(queryset=SharedUser.objects.all()), ], ) primary_phone = E164PhoneNumberField() secondary_phone = E164PhoneNumberField(allow_null=True, required=False) # ------ MEMBER ADDRESS ------ # country = serializers.CharField() province = serializers.CharField() city = serializers.CharField() street_number = serializers.CharField() street_name = serializers.CharField() apartment_unit = serializers.CharField() street_type = serializers.CharField() street_type_other = serializers.CharField( required=False, allow_null=True, allow_blank=True, ) street_direction = serializers.CharField( required=False, allow_null=True, allow_blank=True, ) postal_code = serializers.CharField() # ------ MEMBER WATCH ------ # #TODO: IMPLEMENT FIELDS. # ------ MEMBER METRICS ------ # tags = serializers.PrimaryKeyRelatedField( many=True, queryset=Tag.objects.all(), allow_null=True, required=False, ) how_did_you_hear = serializers.PrimaryKeyRelatedField( many=False, required=True, allow_null=False, queryset=HowHearAboutUsItem.objects.all()) how_did_you_hear_other = serializers.CharField( required=False, allow_null=True, allow_blank=True, ) expectation = serializers.PrimaryKeyRelatedField( many=False, required=True, allow_null=False, queryset=ExpectationItem.objects.all()) expectation_other = serializers.CharField( required=False, allow_null=True, allow_blank=True, ) meaning = serializers.PrimaryKeyRelatedField( many=False, required=True, allow_null=False, queryset=MeaningItem.objects.all()) meaning_other = serializers.CharField( required=False, allow_null=True, allow_blank=True, ) gender = serializers.CharField( required=True, allow_blank=False, allow_null=False, ) willing_to_volunteer = serializers.IntegerField() another_household_member_registered = serializers.BooleanField() year_of_birth = serializers.IntegerField() total_household_count = serializers.IntegerField() over_18_years_household_count = serializers.IntegerField() organization_employee_count = serializers.IntegerField() organization_founding_year = serializers.IntegerField() organization_type_of = serializers.IntegerField() def update(self, instance, validated_data): return validated_data """ Override the `create` function to add extra functinality. """ request = self.context.get('request') print("---->>>>", request)
class MemberListSerializer(serializers.Serializer): type_of = serializers.IntegerField() organization_name = serializers.CharField( required=True, allow_blank=False, validators=[], source="contact.organization_name", ) organization_type_of = serializers.IntegerField( required=False, allow_null=True, source="contact.organization_type_of", ) first_name = serializers.CharField( required=True, allow_blank=False, validators=[], source="contact.first_name", ) last_name = serializers.CharField( required=True, allow_blank=False, validators=[], source="contact.last_name", ) primary_phone_e164 = E164PhoneNumberField( allow_null=False, required=True, source="contact.primary_phone", ) primary_phone_national = NationalPhoneNumberField( allow_null=False, required=True, source="contact.primary_phone", ) email = serializers.EmailField( required=True, allow_blank=False, validators=[], source="contact.email", ) slug = serializers.SlugField( required=True, allow_blank=False, validators=[], source="user.slug", ) street_address = serializers.CharField( required=True, allow_blank=False, validators=[], source="address.street_address", ) country = serializers.CharField( required=True, allow_blank=False, validators=[], source="address.country", ) province = serializers.CharField( required=True, allow_blank=False, validators=[], source="address.province", ) city = serializers.CharField( required=True, allow_blank=False, validators=[], source="address.city", ) postal_code = serializers.CharField( required=True, allow_blank=False, validators=[], source="address.postal_code", ) state = serializers.CharField(read_only=True, ) role_id = serializers.IntegerField(read_only=True, source="user.role_id") def setup_eager_loading(cls, queryset): """ Perform necessary eager loading of data. """ queryset = queryset.prefetch_related('user', 'user__groups', 'contact', 'address', 'metric', 'created_by', 'last_modified_by') return queryset
class AssociateContactUpdateSerializer(serializers.Serializer): # ------ MEMBER CONTACT ------ # is_ok_to_email = serializers.BooleanField() is_ok_to_text = serializers.BooleanField() organization_name = serializers.CharField( required=False, allow_blank=True, allow_null=True, validators=[ UniqueValidator(queryset=AssociateContact.objects.all()), ], ) organization_type_of = serializers.ChoiceField( required=False, allow_null=True, choices=AssociateContact.MEMBER_ORGANIZATION_TYPE_OF_CHOICES, ) first_name = serializers.CharField() last_name = serializers.CharField() email = serializers.EmailField( validators=[ UniqueValidator(queryset=SharedUser.objects.all()), ], ) primary_phone = E164PhoneNumberField() secondary_phone = E164PhoneNumberField(allow_null=True, required=False) def validate_organization_name(self, value): """ Custom validation to handle case of user selecting an organization type of associate but then forgetting to fill in the `organization_name` field. """ request = self.context.get('request') type_of = request.data.get('type_of') if type_of == Member.MEMBER_TYPE_OF.BUSINESS: if value == None or value == "": raise serializers.ValidationError(_('Please fill this field in.')) return value def update(self, instance, validated_data): """ Override the `update` function to add extra functinality. """ request = self.context.get('request') 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.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.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 associates 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 associate contact.") # 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
class MemberCreateSerializer(serializers.Serializer): # ------ MEMBER ------ # type_of = serializers.IntegerField() # ------ MEMBER CONTACT ------ # is_ok_to_email = serializers.BooleanField() is_ok_to_text = serializers.BooleanField() organization_name = serializers.CharField( required=False, allow_blank=True, allow_null=True, validators=[ UniqueValidator(queryset=MemberContact.objects.all()), ], ) organization_type_of = serializers.ChoiceField( required=False, allow_null=True, choices=MemberContact.MEMBER_ORGANIZATION_TYPE_OF_CHOICES, ) first_name = serializers.CharField() last_name = serializers.CharField() email = serializers.EmailField(validators=[ UniqueValidator(queryset=SharedUser.objects.all()), ], ) primary_phone = E164PhoneNumberField() secondary_phone = E164PhoneNumberField(allow_null=True, required=False) # ------ MEMBER ADDRESS ------ # country = serializers.CharField() province = serializers.CharField() city = serializers.CharField() street_number = serializers.CharField() street_name = serializers.CharField() apartment_unit = serializers.CharField( required=False, allow_null=True, allow_blank=True, ) street_type = serializers.ChoiceField( choices=MemberAddress.STREET_TYPE_CHOICES, ) street_type_other = serializers.CharField( required=False, allow_null=True, allow_blank=True, ) street_direction = serializers.ChoiceField( choices=MemberAddress.STREET_DIRECTION_CHOICES, ) postal_code = serializers.CharField() # ------ MEMBER WATCH ------ # watch = serializers.SlugField( allow_null=False, allow_blank=False, ) # ------ MEMBER METRICS ------ # tags = serializers.PrimaryKeyRelatedField( many=True, queryset=Tag.objects.all(), allow_null=True, required=False, ) how_did_you_hear = serializers.PrimaryKeyRelatedField( many=False, required=True, allow_null=False, queryset=HowHearAboutUsItem.objects.all()) how_did_you_hear_other = serializers.CharField( required=False, allow_null=True, allow_blank=True, ) expectation = serializers.PrimaryKeyRelatedField( many=False, required=True, allow_null=False, queryset=ExpectationItem.objects.all()) expectation_other = serializers.CharField( required=False, allow_null=True, allow_blank=True, ) meaning = serializers.PrimaryKeyRelatedField( many=False, required=True, allow_null=False, queryset=MeaningItem.objects.all()) meaning_other = serializers.CharField( required=False, allow_null=True, allow_blank=True, ) gender = serializers.CharField( required=True, allow_blank=False, allow_null=False, ) willing_to_volunteer = serializers.IntegerField() another_household_member_registered = serializers.BooleanField() year_of_birth = serializers.IntegerField( required=False, allow_null=True, ) total_household_count = serializers.IntegerField(required=False, ) over_18_years_household_count = serializers.IntegerField(required=False, ) organization_employee_count = serializers.IntegerField(required=False, ) organization_founding_year = serializers.IntegerField(required=False, ) organization_type_of = serializers.IntegerField(required=False, ) is_aboriginal = serializers.BooleanField(required=False, ) is_transgender = serializers.BooleanField(required=False, ) is_visible_minority = serializers.BooleanField(required=False, ) is_disabled_or_has_barriers = serializers.BooleanField(required=False, ) is_over_fifty_five = serializers.BooleanField(required=False, ) def validate_organization_name(self, value): """ Custom validation to handle case of user selecting an organization type of member but then forgetting to fill in the `organization_name` field. """ request = self.context.get('request') type_of = request.data.get('type_of') if type_of == Member.MEMBER_TYPE_OF.BUSINESS: if value == None or value == "": raise serializers.ValidationError( _('Please fill this field in.')) return value def validate_watch(self, value): #TODO: ADD SECURITY SO NON-EXECUTIVES CANNOT ATTACH TO OTHER USERS. if not Watch.objects.filter(slug=value).exists(): raise serializers.ValidationError("Watch does not exist") return value 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
class AreaCoordinatorRetrieveSerializer(serializers.Serializer): # ------ MEMBER ------ # slug = serializers.SlugField( source="user.slug", read_only=True, ) type_of = serializers.IntegerField( source="user.member.type_of", read_only=True, ) type_of_label = serializers.CharField( source="user.member.get_pretty_type_of", read_only=True, allow_null=True, ) avatar_url = serializers.SerializerMethodField() state = serializers.CharField( source="user.member.state", read_only=True, ) # ------ MEMBER CONTACT ------ # is_ok_to_email = serializers.IntegerField( source="user.member.contact.is_ok_to_email", read_only=True, ) is_ok_to_text = serializers.IntegerField( source="user.member.contact.is_ok_to_text", read_only=True, ) organization_name = serializers.CharField( source="user.member.contact.organization_name", read_only=True, allow_null=True, ) organization_type_of = serializers.IntegerField( source="user.member.contact.organization_type_of", read_only=True, ) first_name = serializers.CharField( source="user.member.contact.first_name", read_only=True, allow_null=True, ) last_name = serializers.CharField( source="user.member.contact.last_name", read_only=True, allow_null=True, ) full_name = serializers.SerializerMethodField() email = serializers.EmailField( source="user.member.contact.email", read_only=True, ) primary_phone_e164 = E164PhoneNumberField( source="user.member.contact.primary_phone", read_only=True, ) primary_phone_national = NationalPhoneNumberField( source="user.member.contact.primary_phone", read_only=True, ) secondary_phone_e164 = E164PhoneNumberField( source="user.member.contact.secondary_phone", read_only=True, ) secondary_phone_national = NationalPhoneNumberField( source="user.member.contact.secondary_phone", read_only=True, ) # ------ MEMBER ADDRESS ------ # country = serializers.CharField( source="user.member.address.country", read_only=True, ) province = serializers.CharField( source="user.member.address.province", read_only=True, ) city = serializers.CharField( source="user.member.address.city", read_only=True, ) street_number = serializers.CharField( source="user.member.address.street_number", read_only=True, ) street_name = serializers.CharField( source="user.member.address.street_name", read_only=True, ) apartment_unit = serializers.CharField( source="user.member.address.apartment_unit", read_only=True, ) street_type = serializers.IntegerField( source="user.member.address.street_type", read_only=True, ) street_type_other = serializers.CharField( source="user.member.address.street_type_other", read_only=True, ) street_direction = serializers.IntegerField( source="user.member.address.street_direction", read_only=True, ) postal_code = serializers.CharField( source="user.member.address.postal_code", read_only=True, ) address = serializers.CharField( source="user.member.address.street_address", read_only=True, ) google_maps_url = serializers.URLField( source="user.member.address.google_maps_url", read_only=True, ) position = serializers.SerializerMethodField() # ------ MEMBER WATCH ------ # #TODO: IMPLEMENT FIELDS. # ------ MEMBER METRICS ------ # tags = TagListCreateSerializer( source="user.member.metric.tags", many=True, read_only=True, ) how_did_you_hear = serializers.PrimaryKeyRelatedField( source="user.member.metric.how_did_you_hear", many=False, read_only=True, allow_null=False, ) how_did_you_hear_other = serializers.CharField( source="user.member.metric.how_did_you_hear_other", allow_null=True, allow_blank=True, read_only=True, ) how_did_you_hear_label = serializers.CharField( source="user.member.metric.how_did_you_hear.text", read_only=True, allow_null=True, ) expectation = serializers.PrimaryKeyRelatedField( source="user.member.metric.expectation", many=False, read_only=True, allow_null=False, ) expectation_other = serializers.CharField( source="user.member.metric.expectation_other", allow_null=True, allow_blank=True, read_only=True, ) expectation_label = serializers.CharField( source="user.member.metric.expectation.text", read_only=True, allow_null=True, ) meaning = serializers.PrimaryKeyRelatedField( source="user.member.metric.meaning", many=False, read_only=True, allow_null=False, ) meaning_other = serializers.CharField( source="user.member.metric.meaning_other", allow_null=True, allow_blank=True, read_only=True, ) meaning_label = serializers.CharField( source="user.member.metric.meaning.text", read_only=True, allow_null=True, ) gender = serializers.IntegerField( source="user.member.metric.gender", read_only=True, ) gender_label = serializers.CharField( source="user.member.metric.get_pretty_gender", read_only=True, allow_null=True, ) willing_to_volunteer = serializers.IntegerField( source="user.member.metric.willing_to_volunteer", read_only=True, ) willing_to_volunteer_label = serializers.CharField( source="user.member.metric.get_pretty_willing_to_volunteer", read_only=True, allow_null=True, ) another_household_member_registered = serializers.BooleanField( source="user.member.metric.another_household_member_registered", read_only=True, ) year_of_birth = serializers.IntegerField( source="user.member.metric.year_of_birth", read_only=True, ) total_household_count = serializers.IntegerField( source="user.member.metric.total_household_count", read_only=True, ) over_18_years_household_count = serializers.IntegerField( source="user.member.metric.over_18_years_household_count", read_only=True, ) organization_employee_count = serializers.IntegerField( source="user.member.metric.organization_employee_count", read_only=True, ) organization_founding_year = serializers.IntegerField( source="user.member.metric.organization_founding_year", read_only=True, ) # ------ AUDITING ------ # created_by = serializers.CharField( source="created_by.get_full_name", allow_null=True, read_only=True, ) last_modified_by = serializers.CharField( source="last_modified_by.get_full_name", allow_null=True, read_only=True, ) # ------ FUNCTIONS ------ # def get_full_name(self, obj): try: return obj.user.member.contact.first_name + " " + obj.user.member.contact.last_name except Exception as e: print(e) return None def get_avatar_url(self, obj): try: return obj.user.member.avatar_image.image_file.url except Exception as e: return None def get_position(self, obj): return get_arr_from_point(obj.user.member.address.position)
class MemberRetrieveSerializer(serializers.Serializer): # ------ MEMBER ------ # slug = serializers.SlugField(source="user.slug", read_only=True,) type_of = serializers.IntegerField(read_only=True,) type_of_label = serializers.CharField(source="get_pretty_type_of", read_only=True,) avatar_url = serializers.SerializerMethodField() state = serializers.CharField(read_only=True,) role_id = serializers.IntegerField(source="user.role_id", read_only=True,) role_label = serializers.CharField(source="get_role_label", read_only=True,) # ------ MEMBER CONTACT ------ # is_ok_to_email = serializers.IntegerField(source="contact.is_ok_to_email", read_only=True,) is_ok_to_text = serializers.IntegerField(source="contact.is_ok_to_text", read_only=True,) organization_name = serializers.CharField(source="contact.organization_name", read_only=True,) organization_type_of = serializers.IntegerField(source="contact.organization_type_of", read_only=True,) first_name = serializers.CharField(source="contact.first_name", read_only=True,) last_name = serializers.CharField(source="contact.last_name", read_only=True,) full_name = serializers.SerializerMethodField() email = serializers.EmailField(source="contact.email", read_only=True,) primary_phone_e164 = E164PhoneNumberField(source="contact.primary_phone", read_only=True,) primary_phone_national = NationalPhoneNumberField(source="contact.primary_phone", read_only=True,) secondary_phone_e164 = E164PhoneNumberField(source="contact.secondary_phone", read_only=True,) secondary_phone_national = NationalPhoneNumberField(source="contact.secondary_phone", read_only=True,) # ------ MEMBER ADDRESS ------ # country = serializers.CharField(source="address.country", read_only=True,) province = serializers.CharField(source="address.province", read_only=True,) city = serializers.CharField(source="address.city", read_only=True,) street_number = serializers.CharField(source="address.street_number", read_only=True,) street_name =serializers.CharField(source="address.street_name", read_only=True,) apartment_unit = serializers.CharField(source="address.apartment_unit", read_only=True,) street_type = serializers.IntegerField(source="address.street_type", read_only=True,) street_type_other = serializers.CharField(source="address.street_type_other", read_only=True,) street_direction = serializers.IntegerField(source="address.street_direction", read_only=True,) postal_code = serializers.CharField(source="address.postal_code", read_only=True,) address = serializers.CharField(source="address.street_address", read_only=True,) google_maps_url = serializers.URLField(source="address.google_maps_url", read_only=True,) position = serializers.SerializerMethodField() # ------ MEMBER WATCH ------ # watch_name = serializers.CharField(source="watch.name", read_only=True,) watch_slug = serializers.SlugField(source="watch.slug", read_only=True,) watch_type_of = serializers.IntegerField(source="watch.type_of", read_only=True,) watch_is_virtual = serializers.BooleanField(source="watch.is_virtual", read_only=True,) # ------ MEMBER METRICS ------ # tags = TagListCreateSerializer(source="metric.tags", many=True, read_only=True,) how_did_you_hear = serializers.PrimaryKeyRelatedField( source="metric.how_did_you_hear", many=False, required=True, allow_null=False, queryset=HowHearAboutUsItem.objects.all() ) how_did_you_hear_other = serializers.CharField( source="metric.how_did_you_hear_other", required=False, allow_null=True, allow_blank=True,) how_did_you_hear_label = serializers.CharField( source="metric.how_did_you_hear.text", allow_null=True, read_only=True,) expectation = serializers.PrimaryKeyRelatedField( source="metric.expectation", many=False, required=True, allow_null=False, queryset=ExpectationItem.objects.all() ) expectation_other = serializers.CharField( source="metric.expectation_other", required=False, allow_null=True, allow_blank=True,) expectation_label = serializers.CharField( source="metric.expectation.text", allow_null=True, read_only=True,) meaning = serializers.PrimaryKeyRelatedField( source="metric.meaning", many=False, required=True, allow_null=False, queryset=MeaningItem.objects.all() ) meaning_other = serializers.CharField(source="metric.meaning_other", allow_null=True, allow_blank=True, read_only=True,) meaning_label = serializers.CharField(source="metric.meaning.text", allow_null=True, read_only=True,) gender = serializers.IntegerField(source="metric.gender", read_only=True,) gender_label = serializers.CharField(source="metric.get_pretty_gender", read_only=True,) willing_to_volunteer = serializers.IntegerField(source="metric.willing_to_volunteer", read_only=True,) willing_to_volunteer_label = serializers.CharField(source="metric.get_pretty_willing_to_volunteer", read_only=True,) another_household_member_registered = serializers.BooleanField(source="metric.another_household_member_registered", read_only=True,) year_of_birth = serializers.IntegerField(source="metric.year_of_birth", read_only=True,) total_household_count = serializers.IntegerField(source="metric.total_household_count", read_only=True,) over_18_years_household_count = serializers.IntegerField(source="metric.over_18_years_household_count", read_only=True,) organization_employee_count = serializers.IntegerField(source="metric.organization_employee_count", read_only=True,) organization_founding_year = serializers.IntegerField(source="metric.organization_founding_year", read_only=True,) is_aboriginal = serializers.BooleanField(allow_null=True, read_only=True, source="metric.is_aboriginal",) is_transgender = serializers.BooleanField(allow_null=True, read_only=True, source="metric.is_transgender",) is_visible_minority = serializers.BooleanField(allow_null=True, read_only=True, source="metric.is_visible_minority",) is_disabled_or_has_barriers = serializers.BooleanField(allow_null=True, read_only=True, source="metric.is_disabled_or_has_barriers",) is_over_fifty_five = serializers.BooleanField(allow_null=True, read_only=True, source="metric.is_over_fifty_five",) # ------ AUDITING ------ # created_by = serializers.CharField(source="created_by.get_full_name", allow_null=True, read_only=True,) last_modified_by = serializers.CharField(source="last_modified_by.get_full_name", allow_null=True, read_only=True,) # ------ OPERATIONS ------ # badges = BadgeRetrieveSerializer(source="user.badges", read_only=True, many=True,) # ------ FUNCTIONS ------ # def get_full_name(self, obj): try: return obj.contact.first_name + " " + obj.contact.last_name except Exception as e: print(e) return None def get_avatar_url(self, obj): try: return obj.avatar_image.image_file.url except Exception as e: return None def get_position(self, obj): return get_arr_from_point(obj.address.position)