Exemplo n.º 1
0
    def save(self):
        voter = self.cleaned_data.get("voter")
        supporter = self.cleaned_data.get("supporter")

        if voter:
            contact_profile = voter.contact_profile
            if not contact_profile:
                contact_profile = ContactProfile()
                contact_profile.save()
                voter.contact_profile = contact_profile
                voter.save()
        else:
            contact_profile = supporter.contact_profile
            if not contact_profile:
                contact_profile = ContactProfile()
                contact_profile.save()
                supporter.contact_profile = contact_profile
                supporter.save()

        number = self.cleaned_data.get("number")
        extension = self.cleaned_data.get("extension")
        phone_type = self.cleaned_data.get("phone_type")
        active = self.cleaned_data.get("active")
        sms_ok = self.cleaned_data.get("sms_ok")
        do_not_call = self.cleaned_data.get("do_not_call")

        try:
            phone = PhoneNumber.objects.get(number=number, extension=extension)
            changed = False
            if phone.type != phone_type:
                phone.type = phone_type
                changed = True
            if phone.sms_ok != sms_ok or changed:
                # FIXME: Add a field to types to flag whether sms should be available for type rather than hard coding here
                if phone.type.title != "Cell" and phone.sms_ok == True:
                    phone.sms_ok = False
                    changed = True
                elif phone.type.title == "Cell" and phone.sms_ok != sms_ok:
                    phone.sms_ok = sms_ok
                    changed = True
            if phone.active != active:
                phone.active = active
                changed = True
            # Only allow flagging do_not_call to True via import to make this flag sticky and hard to remove
            if phone.do_not_call == False and do_not_call == True:
                phone.do_not_call = do_not_call
                changed = True
        except PhoneNumber.DoesNotExist:
            sms_ok_by_type = False
            if phone_type.title == "Cell" and sms_ok:
                # FIXME: Add a field to types to flag whether sms should be available for type rather than hard coding here
                sms_ok_by_type = True

            phone = PhoneNumber(
                number=number,
                extension=extension,
                type=phone_type,
                active=active,
                sms_ok=sms_ok_by_type,
                do_not_call=do_not_call,
            )

            phone.save()

        primary = self.cleaned_data.get("primary")
        if primary:
            if contact_profile.primary_phone and contact_profile.primary_phone != phone:
                contact_profile.other_phones.add(contact_profile.primary_phone)
            contact_profile.primary_phone = phone
        else:
            if contact_profile.primary_phone != phone and not contact_profile.other_phones.filter(id=phone.id).count():
                contact_profile.other_phones.add(phone)

        contact_profile.save()
Exemplo n.º 2
0
    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