candidates.py 文件源码

python
阅读 29 收藏 0 点赞 0 评论 0

项目:django-calaccess-processed-data 作者: california-civic-data-coalition 项目源码 文件源码
def match_form501s_by_name(self):
        """
        Get Form501Filings with names that match the scraped candidate's name.

        Returns a Form501Filing QuerySet.
        """
        from calaccess_processed.models import Form501Filing

        election_data = self.election_proxy.parsed_name
        office_data = self.parse_office_name()

        q = Form501Filing.objects.filter(
            office__iexact=office_data['type'],
            district=office_data['district'],
            election_year__lte=election_data['year'],
        )
        # first, try "<last_name>, <first_name> <middle_name>" format
        last_first_middle_q = q.annotate(
            full_name=Concat(
                'last_name',
                Value(', '),
                'first_name',
                Value(' '),
                'middle_name',
                output_field=CharField(),
            ),
        ).filter(full_name=self.name)

        # limit scope to election_type if it will yield results
        if last_first_middle_q.filter(election_type=election_data['type']).exists():
            q = last_first_middle_q.filter(election_type=election_data['type'])
        # if not, check if dropping election_type filter yields results
        elif last_first_middle_q.exists():
            q = last_first_middle_q
        # if not, change name format
        else:
            last_first_q = q.annotate(
                full_name=Concat(
                    'last_name',
                    Value(', '),
                    'first_name',
                    output_field=CharField(),
                ),
            ).filter(full_name=self.name)

            # again, limit to election_type at first
            if last_first_q.filter(election_type=election_data['type']).exists():
                q = last_first_q.filter(election_type=election_data['type'])
            else:
                q = last_first_q

        return q
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号