Example #1
0
    def initContactObjects(self, data):
        # emails_kwargs = OrderedDict(filter(None, [\
        #     ((key, data.get(value)) if data.get(value) else None) for key, value in\
        #     {
        #         'email'         : 'E-mail',
        #         'personal_email': 'Personal E-mail',
        #     }.items()
        # ]))
        #
        # self['E-mails'] = EmailFields(
        #     **emails_kwargs
        # )

        name_kwargs = OrderedDict(
            filter(
                None,
                [
                    ((key, data.get(value)) if data.get(value) else None)
                    for key, value in {
                        "first_name": "First Name",
                        "middle_name": "Middle Name",
                        "family_name": "Surname",
                        "name_prefix": "Name Prefix",
                        "name_suffix": "Name Suffix",
                        "contact": "Contact",
                        "company": "Company",
                        "city": "City",
                        "country": "Country",
                        "state": "State",
                    }.items()
                ],
            )
        )

        self["Name"] = ContactName(**name_kwargs)

        assert self["Name"] is not None, "contact is missing mandatory fields: something went wrong"

        address_kwargs = OrderedDict(
            filter(
                None,
                [
                    ((key, data.get(value)) if data.get(value) else None)
                    for key, value in {
                        "line1": "Address 1",
                        "line2": "Address 2",
                        "city": "City",
                        "postcode": "Postcode",
                        "state": "State",
                        "country": "Country",
                        "company": "Company",
                    }.items()
                ],
            )
        )

        self["Address"] = ContactAddress(**address_kwargs)

        # print "ADDRESS: ", self['Address']

        alt_address_kwargs = OrderedDict(
            filter(
                None,
                [
                    ((key, data.get(value)) if data.get(value) else None)
                    for key, value in {
                        "line1": "Home Address 1",
                        "line2": "Home Address 2",
                        "city": "Home City",
                        "postcode": "Home Postcode",
                        "state": "Home State",
                        "country": "Home Country",
                        "company": "Company",
                    }.items()
                ],
            )
        )

        self["Home Address"] = ContactAddress(**alt_address_kwargs)

        # print "HOME ADDRESS: ", self['Home Address']

        phone_kwargs = OrderedDict(
            filter(
                None,
                [
                    ((key, data.get(value)) if data.get(value) else None)
                    for key, value in {
                        "mob_number": "Mobile Phone",
                        "tel_number": "Phone",
                        "fax_number": "Fax",
                        "mob_pref": "Mobile Phone Preferred",
                        "tel_pref": "Phone Preferred",
                    }.items()
                ],
            )
        )

        self["Phone Numbers"] = ContactPhones(**phone_kwargs)

        social_media_kwargs = OrderedDict(
            filter(
                None,
                [
                    ((key, data.get(value)) if data.get(value) else None)
                    for key, value in {
                        "facebook": "Facebook Username",
                        "twitter": "Twitter Username",
                        "gplus": "GooglePlus Username",
                        "instagram": "Instagram Username",
                        "website": "Web Site",
                    }.items()
                ],
            )
        )

        self["Social Media"] = SocialMediaFields(**social_media_kwargs)

        emails = []
        if data.get("E-mail"):
            emails = listUtils.combineLists(emails, SanitationUtils.findallEmails(data["E-mail"]))
        if data.get("Personal E-mail"):
            emails = listUtils.combineLists(emails, SanitationUtils.findallEmails(data.get("Personal E-mail")))
        self["E-mail"] = emails.pop(0) if emails else None
        self["Personal E-mail"] = ", ".join(emails)

        urls = []
        if data.get("Web Site"):
            urls = listUtils.combineLists(urls, SanitationUtils.findallURLs(data["Web Site"]))
        self["Web Site"] = urls.pop(0) if urls else None

        # if not self['Emails'].valid:
        #     self['emails_reason'] = '\n'.join(filter(None, [
        #         self['Emails'].reason,
        #     ]))

        if not self["Address"].valid or not self["Home Address"].valid:
            self["address_reason"] = "\n".join(
                filter(
                    None,
                    [
                        "ADDRESS: " + self["Address"].reason if not self["Address"].valid else None,
                        "HOME ADDRESS: " + self["Home Address"].reason if not self["Home Address"].valid else None,
                    ],
                )
            )

        if not self["Name"].valid:
            self["name_reason"] = "\n".join(filter(None, [self["Name"].reason]))

        if not self["Phone Numbers"].valid:
            self["phone_reason"] = "\n".join(filter(None, [self["Phone Numbers"].reason]))

        if not self["Social Media"].valid:
            self["social_reason"] = "\n".join(filter(None, [self["Social Media"].reason]))