Exemplo n.º 1
0
def add_tags(tags, objects, creator, section, action_type, action_context=None, comment=None):
    tag_set = TagSet(
        creator = creator,
        section = section,
        action_type = action_type,
        comment = comment,
    )
    if action_context:
        tag_set.action_context = action_context
    tag_set.save()

    for object in objects:
        for tag in tags:
            tagged = TaggedItem(
                tag_set = tag_set,
                tag = tag,
                active = True,
            )
            if object.__class__.__name__ == 'Voter':
                tagged.voter = object
            elif object.__class__.__name__ == 'Election':
                tagged.election = object
            elif object.__class__.__name__ == 'District':
                tagged.district = object
            elif object.__class__.__name__ == 'Supporter':
                tagged.supporter = object
            elif object.__class__.__name__ == 'Organization':
                tagged.organization = object
            elif object.__class__.__name__ == 'Donation':
                tagged.donation = object
            elif object.__class__.__name__ == 'Activity':
                tagged.activity = object
            elif object.__class__.__name__ == 'Team':
                tagged.team = object
            tagged.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