def get_or_create_address(address_info): """ Receives a dictionary of address object attributes and either returns an existing address matching the attributes or creates a new one. This method should be used everywhere an address might be created so it is a hook for future functionality such as address standardization """ filter = {} for key in ['address1','address2','address3','city','zip']: if address_info.get(key, None): filter[key] = address_info[key] else: filter[key + '__isnull'] = True try: address = ContactAddress.objects.get(**filter) except ContactAddress.DoesNotExist: address_type = address_info.get('address_type',None) if not address_type: address_type = get_default_address_type() address = ContactAddress( address1 = address_info.get('address1',None), address2 = address_info.get('address2',None), address3 = address_info.get('address3',None), city = address_info.get('city',None), state = address_info.get('state',None), zip = address_info.get('zip',None), address_type = address_type, ) address.save() return address
def batch_make_supporters(self): queryset = self.current_queryset # FIXME: This is a hack address_type = ContactAddressType.objects.get(id=1) for voter in self.selected: contact_profile = voter.contact_profile if not contact_profile: contact_profile = ContactProfile( ) contact_profile.save() voter.contact_profile = contact_profile try: address = ContactAddress.objects.get( address1 = voter.household.get_address(), city = voter.household.city, state = voter.household.state, zip = voter.household.zip, address_type = address_type, ) except ContactAddress.DoesNotExist: address = ContactAddress( address1 = voter.household.get_address(), city = voter.household.city, state = voter.household.state, zip = voter.household.zip, address_type = address_type, ) address.save() contact_profile.primary_address = address contact_profile.save() supporter = Supporter( first_name = voter.first_name, middle_name = voter.middle_name, last_name = voter.last_name, birthdate = voter.date_birth, creator = self.request.user, #contact_profile = contact_profile, ) supporter.save() return HttpResponseRedirect('/supporters/')
def save(self, request): supporter = Supporter( first_name=self.cleaned_data.get("first_name"), middle_name=self.cleaned_data.get("middle_name"), last_name=self.cleaned_data.get("last_name"), birthdate=self.cleaned_data.get("birthdate"), age=self.cleaned_data.get("age"), gender=self.cleaned_data.get("gender"), creator=request.user, ) supporter.save() tags = self.cleaned_data.get("tags") if tags: tag_set = TagSet( creator=request.user, section=request.session["site"].active_section._path, action_type="add", action_context=supporter, ) tag_set.save() for tag in tags: tagged_item = TaggedItem(tag_set=tag_set, tag=tag, supporter=supporter, active=True) tagged_item.save() address_type = self.cleaned_data.get("address_type") state = self.cleaned_data.get("state") contact_profile = None voter = self.cleaned_data.get("voter") if voter: if not contact_profile: contact_profile = ContactProfile() contact_profile.save() voter.contact_profile = contact_profile voter.save() if address_type and state: if not contact_profile: contact_profile = ContactProfile() contact_profile.save() try: contact_address = ContactAddress.objects.get( "", address_type=address_type, address1=self.cleaned_data.get("address_line_1"), address2=self.cleaned_data.get("address_line_2"), address3=self.cleaned_data.get("address_line_3"), city=self.cleaned_data.get("city"), state=state, zip=self.cleaned_data.get("zip"), ) except: contact_address = ContactAddress( address_type=address_type, address1=self.cleaned_data.get("address_line_1"), address2=self.cleaned_data.get("address_line_2"), address3=self.cleaned_data.get("address_line_3"), city=self.cleaned_data.get("city"), state=state, zip=self.cleaned_data.get("zip"), ) contact_address.save() contact_profile.primary_address = contact_address organization = self.cleaned_data.get("organization") if organization: if not contact_profile: contact_profile = ContactProfile() contact_profile.save() contact_profile.organizations primary_phone = self.cleaned_data.get("primary_phone") primary_phone_type = self.cleaned_data.get("primary_phone_type") if primary_phone: if not contact_profile: contact_profile = ContactProfile() contact_profile.save() primary_phone_type = self.cleaned_data.get("primary_phone_type") try: primary_phone = PhoneNumber.objects.get( number=primary_phone, type=self.cleaned_data.get("primary_phone_type") ) except: primary_phone = PhoneNumber(number=primary_phone, type=self.cleaned_data.get("primary_phone_type")) primary_phone.save() if contact_profile.primary_phone != primary_phone: if contact_profile.primary_phone: contact_profile.other_phones.add(contact_profile.primary_phone) contact_profile.primary_phone = primary_phone primary_email = self.cleaned_data.get("primary_email") primary_email_type = self.cleaned_data.get("primary_email_type") if primary_email: if not contact_profile: contact_profile = ContactProfile() contact_profile.save() primary_email_type = self.cleaned_data.get("primary_email_type") try: primary_email = EmailAddress.objects.get( email=primary_email, type=self.cleaned_data.get("primary_email_type") ) except: primary_email = EmailAddress(email=primary_email, type=self.cleaned_data.get("primary_email_type")) primary_email.save() if contact_profile.primary_email != primary_email: if contact_profile.primary_email: contact_profile.other_emails.add(contact_profile.primary_email) contact_profile.primary_email = primary_email if contact_profile: contact_profile.save() supporter.contact_profile = contact_profile supporter.save() return supporter