예제 #1
0
파일: utils.py 프로젝트: ptudor/lgr-django
def var_to_slug(variant):
    """
    Convert a variant to a slug that can be used in URL.

    :param variant: Variant to convert.
    :return: Slug to be used in URL.
    """
    return '{},{},{}'.format(
        cp_to_slug(variant.cp),
        quote_plus(variant.when or ''),
        quote_plus(variant.not_when or '')
    )
예제 #2
0
    def dict_for_csv(self, base_url=None):
        elected_for_csv = ""
        image_copyright = ""
        image_uploading_user = ""
        image_uploading_user_notes = ""
        proxy_image_url_template = ""

        primary_image = None
        for image in self.images.all():
            if image.is_primary:
                primary_image = image
        primary_image_url = None
        if primary_image:
            primary_image_url = urljoin(base_url, primary_image.image.url)
            if settings.IMAGE_PROXY_URL and base_url:
                encoded_url = quote_plus(primary_image_url)
                proxy_image_url_template = (settings.IMAGE_PROXY_URL +
                                            encoded_url +
                                            "/{height}/{width}.{extension}")

            try:
                image_copyright = primary_image.copyright
                user = primary_image.uploading_user
                if user is not None:
                    image_uploading_user = primary_image.uploading_user.username
                image_uploading_user_notes = primary_image.user_notes
            except ObjectDoesNotExist:
                pass

        twitter_id = self.get_single_identifier_of_type("twitter_username")
        if twitter_id:
            twitter_user_id = twitter_id.internal_identifier
            twitter_user_name = twitter_id.value
        else:
            twitter_user_name = twitter_user_id = ""

        theyworkforyou_url = ""
        parlparse_id = ""
        twfy_id = self.get_single_identifier_of_type("theyworkforyou")
        if twfy_id:
            parlparse_id = "uk.org.publicwhip/person/{}".format(
                twfy_id.internal_identifier)
            theyworkforyou_url = "http://www.theyworkforyou.com/mp/{}".format(
                twfy_id.internal_identifier)

        row = {
            "id":
            self.id,
            "name":
            self.name,
            "honorific_prefix":
            self.honorific_prefix,
            "honorific_suffix":
            self.honorific_suffix,
            "gender":
            self.gender,
            "birth_date":
            self.birth_date,
            "email":
            self.get_email,
            "twitter_username":
            twitter_user_name,
            "twitter_user_id":
            twitter_user_id,
            "facebook_page_url":
            self.get_single_identifier_value("facebook_page_url"),
            "favourite_biscuits":
            self.favourite_biscuit or "",
            "linkedin_url":
            self.get_single_identifier_value("linkedin_url"),
            "party_ppc_page_url":
            self.get_single_identifier_value("party_ppc_page_url"),
            "facebook_personal_url":
            self.get_single_identifier_value("facebook_personal_url"),
            "homepage_url":
            self.get_single_identifier_value("homepage_url"),
            "wikipedia_url":
            self.get_single_identifier_value("wikipedia_url"),
            "wikidata_url":
            self.get_single_identifier_value("wikidata_url"),
            "theyworkforyou_url":
            theyworkforyou_url,
            "parlparse_id":
            parlparse_id,
            "image_url":
            primary_image_url,
            "proxy_image_url_template":
            proxy_image_url_template,
            "image_copyright":
            image_copyright,
            "image_uploading_user":
            image_uploading_user,
            "image_uploading_user_notes":
            image_uploading_user_notes,
        }
        return row
예제 #3
0
    def as_list_of_dicts(self, election, base_url=None, redirects=None):
        result = []
        if not base_url:
            base_url = ''
        if not redirects:
            redirects = {}
        # Find the list of relevant candidacies. So as not to cause
        # extra queries, we don't use filter but instead iterate over
        # all objects:
        candidacies = []
        for m in self.base.memberships.all():
            try:
                m_extra = m.extra
            except ObjectDoesNotExist:
                continue
            if not m_extra.election:
                continue
            expected_role = m.extra.election.candidate_membership_role
            if election is None:
                if expected_role == m.role:
                    candidacies.append(m)
            else:
                if m_extra.election == election and expected_role == m.role:
                    candidacies.append(m)
        for candidacy in candidacies:
            candidacy_extra = candidacy.extra
            party = candidacy.on_behalf_of
            post = candidacy.post
            elected = candidacy_extra.elected
            elected_for_csv = ''
            image_copyright = ''
            image_uploading_user = ''
            image_uploading_user_notes = ''
            proxy_image_url_template = ''
            if elected is not None:
                elected_for_csv = str(elected)
            mapit_url = ''
            primary_image = None
            for image in self.images.all():
                if image.is_primary:
                    primary_image = image
            primary_image_url = None
            if primary_image:
                primary_image_url = urljoin(base_url, primary_image.image.url)
                if settings.IMAGE_PROXY_URL and base_url:
                    encoded_url = quote_plus(primary_image_url)
                    proxy_image_url_template = settings.IMAGE_PROXY_URL + \
                        encoded_url + '/{height}/{width}.{extension}'

                try:
                    image_copyright = primary_image.extra.copyright
                    user = primary_image.extra.uploading_user
                    if user is not None:
                        image_uploading_user = primary_image.extra.uploading_user.username
                    image_uploading_user_notes = primary_image.extra.user_notes
                except ObjectDoesNotExist:
                    pass
            twitter_user_id = ''
            for identifier in self.base.identifiers.all():
                if identifier.scheme == 'twitter':
                    twitter_user_id = identifier.identifier
            old_person_ids = ';'.join(
                text_type(i) for i in redirects.get(self.base.id, []))

            row = {
                'id': self.base.id,
                'name': self.base.name,
                'honorific_prefix': self.base.honorific_prefix,
                'honorific_suffix': self.base.honorific_suffix,
                'gender': self.base.gender,
                'birth_date': self.base.birth_date,
                'election': candidacy_extra.election.slug,
                'election_date': candidacy_extra.election.election_date,
                'election_current': candidacy_extra.election.current,
                'party_id': party.extra.slug,
                'party_lists_in_use':
                candidacy_extra.election.party_lists_in_use,
                'party_list_position': candidacy_extra.party_list_position,
                'party_name': party.name,
                'post_id': post.extra.slug,
                'post_label': post.extra.short_label,
                'mapit_url': mapit_url,
                'elected': elected_for_csv,
                'email': self.base.email,
                'twitter_username': self.twitter_username,
                'twitter_user_id': twitter_user_id,
                'facebook_page_url': self.facebook_page_url,
                'linkedin_url': self.linkedin_url,
                'party_ppc_page_url': self.party_ppc_page_url,
                'facebook_personal_url': self.facebook_personal_url,
                'homepage_url': self.homepage_url,
                'wikipedia_url': self.wikipedia_url,
                'image_url': primary_image_url,
                'proxy_image_url_template': proxy_image_url_template,
                'image_copyright': image_copyright,
                'image_uploading_user': image_uploading_user,
                'image_uploading_user_notes': image_uploading_user_notes,
                'old_person_ids': old_person_ids,
            }
            from ..election_specific import get_extra_csv_values
            extra_csv_data = get_extra_csv_values(self.base, election, post)
            row.update(extra_csv_data)
            result.append(row)

        return result
    def as_dict(self, election, base_url=None):
        if not base_url:
            base_url = ""
        candidacy_extra = (
            MembershipExtra.objects.select_related("base", "base__post__area")
            .prefetch_related("base__post__extra", "base__on_behalf_of__extra", "base__post__area__other_identifiers")
            .get(election=election, base__person=self.base, base__role=election.candidate_membership_role)
        )
        party = candidacy_extra.base.on_behalf_of
        post = candidacy_extra.base.post
        elected = self.get_elected(election)
        elected_for_csv = ""
        image_copyright = ""
        image_uploading_user = ""
        image_uploading_user_notes = ""
        proxy_image_url_template = ""
        if elected is not None:
            elected_for_csv = str(elected)
        mapit_identifier = post.area.other_identifiers.filter(scheme="mapit-area-url").first()
        if mapit_identifier:
            mapit_url = mapit_identifier.identifier
        else:
            mapit_url = ""
        primary_image = self.images.select_related("extra").filter(is_primary=True).first()
        if primary_image:
            primary_image_url = urljoin(base_url, primary_image.image.url)
            if settings.IMAGE_PROXY_URL and base_url:
                encoded_url = quote_plus(primary_image_url)
                proxy_image_url_template = settings.IMAGE_PROXY_URL + encoded_url + "/{height}/{width}.{extension}"

            try:
                image_copyright = primary_image.extra.copyright
                user = primary_image.extra.uploading_user
                if user is not None:
                    image_uploading_user = primary_image.extra.uploading_user.username
                image_uploading_user_notes = primary_image.extra.user_notes
            except ObjectDoesNotExist:
                pass
        else:
            primary_image_url = ""

        row = {
            "id": self.base.id,
            "name": self.base.name,
            "honorific_prefix": self.base.honorific_prefix,
            "honorific_suffix": self.base.honorific_suffix,
            "gender": self.base.gender,
            "birth_date": self.base.birth_date,
            "election": election.slug,
            "party_id": party.extra.slug,
            "party_name": party.name,
            "post_id": post.extra.slug,
            "post_label": post.extra.short_label,
            "mapit_url": mapit_url,
            "elected": elected_for_csv,
            "email": self.base.email,
            "twitter_username": self.twitter_username,
            "facebook_page_url": self.facebook_page_url,
            "linkedin_url": self.linkedin_url,
            "party_ppc_page_url": self.party_ppc_page_url,
            "facebook_personal_url": self.facebook_personal_url,
            "homepage_url": self.homepage_url,
            "wikipedia_url": self.wikipedia_url,
            "image_url": primary_image_url,
            "proxy_image_url_template": proxy_image_url_template,
            "image_copyright": image_copyright,
            "image_uploading_user": image_uploading_user,
            "image_uploading_user_notes": image_uploading_user_notes,
        }
        from ..election_specific import get_extra_csv_values

        extra_csv_data = get_extra_csv_values(self.base, election)
        row.update(extra_csv_data)

        return row
예제 #5
0
    def as_dict(self, election, base_url=None):
        if not base_url:
            base_url = ''
        candidacy_extra = MembershipExtra.objects \
            .select_related('base', 'base__post__area') \
            .prefetch_related(
                'base__post__extra',
                'base__on_behalf_of__extra',
                'base__post__area__other_identifiers',
            ) \
            .get(
                election=election,
                base__person=self.base,
                base__role=election.candidate_membership_role,
            )
        party = candidacy_extra.base.on_behalf_of
        post = candidacy_extra.base.post
        elected = self.get_elected(election)
        elected_for_csv = ''
        image_copyright = ''
        image_uploading_user = ''
        image_uploading_user_notes = ''
        proxy_image_url_template = ''
        if elected is not None:
            elected_for_csv = str(elected)
        mapit_identifier = post.area.other_identifiers \
            .filter(scheme='mapit-area-url').first()
        if mapit_identifier:
            mapit_url = mapit_identifier.identifier
        else:
            mapit_url = ''
        primary_image = self.images \
            .select_related('extra') \
            .filter(
                is_primary=True
            ).first()
        if primary_image:
            primary_image_url = urljoin(base_url, primary_image.image.url)
            if settings.IMAGE_PROXY_URL and base_url:
                encoded_url = quote_plus(primary_image_url)
                proxy_image_url_template = settings.IMAGE_PROXY_URL + \
                    encoded_url + '/{height}/{width}.{extension}'

            try:
                image_copyright = primary_image.extra.copyright
                user = primary_image.extra.uploading_user
                if user is not None:
                    image_uploading_user = primary_image.extra.uploading_user.username
                image_uploading_user_notes = primary_image.extra.user_notes
            except ObjectDoesNotExist:
                pass
        else:
            primary_image_url = ''

        row = {
            'id': self.base.id,
            'name': self.base.name,
            'honorific_prefix': self.base.honorific_prefix,
            'honorific_suffix': self.base.honorific_suffix,
            'gender': self.base.gender,
            'birth_date': self.base.birth_date,
            'election': election.slug,
            'party_id': party.extra.slug,
            'party_name': party.name,
            'post_id': post.extra.slug,
            'post_label': post.extra.short_label,
            'mapit_url': mapit_url,
            'elected': elected_for_csv,
            'email': self.base.email,
            'twitter_username': self.twitter_username,
            'facebook_page_url': self.facebook_page_url,
            'linkedin_url': self.linkedin_url,
            'party_ppc_page_url': self.party_ppc_page_url,
            'facebook_personal_url': self.facebook_personal_url,
            'homepage_url': self.homepage_url,
            'wikipedia_url': self.wikipedia_url,
            'image_url': primary_image_url,
            'proxy_image_url_template': proxy_image_url_template,
            'image_copyright': image_copyright,
            'image_uploading_user': image_uploading_user,
            'image_uploading_user_notes': image_uploading_user_notes,
        }
        from ..election_specific import get_extra_csv_values
        extra_csv_data = get_extra_csv_values(self.base, election)
        row.update(extra_csv_data)

        return row
예제 #6
0
 def to_slug(self):
     return '{},{},{}'.format(self['cp'].value(),
                              quote_plus(self['when'].value() or ''),
                              quote_plus(self['not_when'].value() or ''))
예제 #7
0
    def as_dict(self, election, base_url=None):
        if not base_url:
            base_url = ''
        candidacy_extra = MembershipExtra.objects \
            .select_related('base', 'base__post__area') \
            .prefetch_related(
                'base__post__extra',
                'base__on_behalf_of__extra',
                'base__post__area__other_identifiers',
            ) \
            .get(
                election=election,
                base__person=self.base,
                base__role=election.candidate_membership_role,
            )
        party = candidacy_extra.base.on_behalf_of
        post = candidacy_extra.base.post
        elected = self.get_elected(election)
        elected_for_csv = ''
        image_copyright = ''
        image_uploading_user = ''
        image_uploading_user_notes = ''
        proxy_image_url_template = ''
        if elected is not None:
            elected_for_csv = str(elected)
        mapit_identifier = post.area.other_identifiers \
            .filter(scheme='mapit-area-url').first()
        if mapit_identifier:
            mapit_url = mapit_identifier.identifier
        else:
            mapit_url = ''
        primary_image = self.images \
            .select_related('extra') \
            .filter(
                is_primary=True
            ).first()
        if primary_image:
            primary_image_url = urljoin(base_url, primary_image.image.url)
            if settings.IMAGE_PROXY_URL and base_url:
                encoded_url = quote_plus(primary_image_url)
                proxy_image_url_template = settings.IMAGE_PROXY_URL + \
                    encoded_url + '/{height}/{width}.{extension}'

            try:
                image_copyright = primary_image.extra.copyright
                user = primary_image.extra.uploading_user
                if user is not None:
                    image_uploading_user = primary_image.extra.uploading_user.username
                image_uploading_user_notes = primary_image.extra.user_notes
            except ObjectDoesNotExist:
                pass
        else:
            primary_image_url = ''

        row = {
            'id': self.base.id,
            'name': self.base.name,
            'honorific_prefix': self.base.honorific_prefix,
            'honorific_suffix': self.base.honorific_suffix,
            'gender': self.base.gender,
            'birth_date': self.base.birth_date,
            'election': election.slug,
            'party_id': party.extra.slug,
            'party_name': party.name,
            'post_id': post.extra.slug,
            'post_label': post.extra.short_label,
            'mapit_url': mapit_url,
            'elected': elected_for_csv,
            'email': self.base.email,
            'twitter_username': self.twitter_username,
            'facebook_page_url': self.facebook_page_url,
            'linkedin_url': self.linkedin_url,
            'party_ppc_page_url': self.party_ppc_page_url,
            'facebook_personal_url': self.facebook_personal_url,
            'homepage_url': self.homepage_url,
            'wikipedia_url': self.wikipedia_url,
            'image_url': primary_image_url,
            'proxy_image_url_template': proxy_image_url_template,
            'image_copyright': image_copyright,
            'image_uploading_user': image_uploading_user,
            'image_uploading_user_notes': image_uploading_user_notes,
        }
        from ..election_specific import get_extra_csv_values
        extra_csv_data = get_extra_csv_values(self.base, election)
        row.update(extra_csv_data)

        return row
예제 #8
0
    def as_list_of_dicts(self, election, base_url=None, redirects=None):
        result = []
        if not base_url:
            base_url = ""
        if not redirects:
            redirects = {}
        # Find the list of relevant candidacies. So as not to cause
        # extra queries, we don't use filter but instead iterate over
        # all objects:
        candidacies = []
        for m in self.memberships.all():
            if not m.post_election.election:
                continue
            expected_role = m.post_election.election.candidate_membership_role
            if election is None:
                if expected_role == m.role:
                    candidacies.append(m)
            else:
                if (m.post_election.election == election
                        and expected_role == m.role):
                    candidacies.append(m)
        for candidacy in candidacies:
            party = candidacy.party
            post = candidacy.post
            elected = candidacy.elected
            elected_for_csv = ""
            image_copyright = ""
            image_uploading_user = ""
            image_uploading_user_notes = ""
            proxy_image_url_template = ""
            if elected is not None:
                elected_for_csv = str(elected)
            mapit_url = ""
            primary_image = None
            for image in self.images.all():
                if image.is_primary:
                    primary_image = image
            primary_image_url = None
            if primary_image:
                primary_image_url = urljoin(base_url, primary_image.image.url)
                if settings.IMAGE_PROXY_URL and base_url:
                    encoded_url = quote_plus(primary_image_url)
                    proxy_image_url_template = (
                        settings.IMAGE_PROXY_URL + encoded_url +
                        "/{height}/{width}.{extension}")

                try:
                    image_copyright = primary_image.copyright
                    user = primary_image.uploading_user
                    if user is not None:
                        image_uploading_user = (
                            primary_image.uploading_user.username)
                    image_uploading_user_notes = primary_image.user_notes
                except ObjectDoesNotExist:
                    pass
            twitter_user_id = ""
            for identifier in self.identifiers.all():
                if identifier.scheme == "twitter":
                    twitter_user_id = identifier.identifier
            old_person_ids = ";".join(
                text_type(i) for i in redirects.get(self.id, []))

            row = {
                "id": self.id,
                "name": self.name,
                "honorific_prefix": self.honorific_prefix,
                "honorific_suffix": self.honorific_suffix,
                "gender": self.gender,
                "birth_date": self.birth_date,
                "election": candidacy.post_election.election.slug,
                "election_date":
                candidacy.post_election.election.election_date,
                "election_current": candidacy.post_election.election.current,
                "party_id": party.legacy_slug,
                "party_lists_in_use":
                candidacy.post_election.election.party_lists_in_use,
                "party_list_position": candidacy.party_list_position,
                "party_name": party.name,
                "post_id": post.slug,
                "post_label": post.short_label,
                "mapit_url": mapit_url,
                "elected": elected_for_csv,
                "email": self.email,
                "twitter_username": self.twitter_username,
                "twitter_user_id": twitter_user_id,
                "facebook_page_url": self.facebook_page_url,
                "linkedin_url": self.linkedin_url,
                "party_ppc_page_url": self.party_ppc_page_url,
                "facebook_personal_url": self.facebook_personal_url,
                "homepage_url": self.homepage_url,
                "wikipedia_url": self.wikipedia_url,
                "image_url": primary_image_url,
                "proxy_image_url_template": proxy_image_url_template,
                "image_copyright": image_copyright,
                "image_uploading_user": image_uploading_user,
                "image_uploading_user_notes": image_uploading_user_notes,
                "old_person_ids": old_person_ids,
            }
            from candidates.election_specific import get_extra_csv_values

            extra_csv_data = get_extra_csv_values(self, election, post)
            row.update(extra_csv_data)
            result.append(row)

        return result
    def as_list_of_dicts(self, election, base_url=None):
        result = []
        user_settings = get_current_usersettings()
        if not base_url:
            base_url = ''
        # Find the list of relevant candidacies. So as not to cause
        # extra queries, we don't use filter but instead iterate over
        # all objects:
        candidacies = []
        for m in self.base.memberships.all():
            try:
                m_extra = m.extra
            except ObjectDoesNotExist:
                continue
            if not m_extra.election:
                continue
            expected_role = m.extra.election.candidate_membership_role
            if election is None:
                if expected_role == m.role:
                    candidacies.append(m)
            else:
                if m_extra.election == election and expected_role == m.role:
                    candidacies.append(m)
        for candidacy in candidacies:
            candidacy_extra = candidacy.extra
            party = candidacy.on_behalf_of
            post = candidacy.post
            elected = candidacy_extra.elected
            elected_for_csv = ''
            image_copyright = ''
            image_uploading_user = ''
            image_uploading_user_notes = ''
            proxy_image_url_template = ''
            if elected is not None:
                elected_for_csv = str(elected)
            mapit_identifier = None
            for identifier in post.area.other_identifiers.all():
                if identifier.scheme == 'mapit-area-url':
                    mapit_identifier = identifier
            if mapit_identifier:
                mapit_url = mapit_identifier.identifier
            else:
                mapit_url = ''
            primary_image = None
            for image in self.images.all():
                if image.is_primary:
                    primary_image = image
            primary_image_url = None
            if primary_image:
                primary_image_url = urljoin(base_url, primary_image.image.url)
                if user_settings.IMAGE_PROXY_URL and base_url:
                    encoded_url = quote_plus(primary_image_url)
                    proxy_image_url_template = user_settings.IMAGE_PROXY_URL + \
                        encoded_url + '/{height}/{width}.{extension}'

                try:
                    image_copyright = primary_image.extra.copyright
                    user = primary_image.extra.uploading_user
                    if user is not None:
                        image_uploading_user = primary_image.extra.uploading_user.username
                    image_uploading_user_notes = primary_image.extra.user_notes
                except ObjectDoesNotExist:
                    pass
            twitter_user_id = ''
            for identifier in self.base.identifiers.all():
                if identifier.scheme == 'twitter':
                    twitter_user_id = identifier.identifier

            row = {
                'id': self.base.id,
                'name': self.base.name,
                'honorific_prefix': self.base.honorific_prefix,
                'honorific_suffix': self.base.honorific_suffix,
                'gender': self.base.gender,
                'birth_date': self.base.birth_date,
                'election': candidacy_extra.election.slug,
                'election_date': candidacy_extra.election.election_date,
                'election_current': candidacy_extra.election.current,
                'party_id': party.extra.slug,
                'party_lists_in_use': candidacy_extra.election.party_lists_in_use,
                'party_list_position': candidacy_extra.party_list_position,
                'party_name': party.name,
                'post_id': post.extra.slug,
                'post_label': post.extra.short_label,
                'mapit_url': mapit_url,
                'elected': elected_for_csv,
                'email': self.base.email,
                'twitter_username': self.twitter_username,
                'twitter_user_id': twitter_user_id,
                'facebook_page_url': self.facebook_page_url,
                'linkedin_url': self.linkedin_url,
                'party_ppc_page_url': self.party_ppc_page_url,
                'facebook_personal_url': self.facebook_personal_url,
                'homepage_url': self.homepage_url,
                'wikipedia_url': self.wikipedia_url,
                'image_url': primary_image_url,
                'proxy_image_url_template': proxy_image_url_template,
                'image_copyright': image_copyright,
                'image_uploading_user': image_uploading_user,
                'image_uploading_user_notes': image_uploading_user_notes,
            }
            from ..election_specific import get_extra_csv_values
            extra_csv_data = get_extra_csv_values(self.base, election, post)
            row.update(extra_csv_data)
            result.append(row)

        return result