@pjax()<EOL>def legislators(request, abbr):
try:<EOL><INDENT>meta = Metadata.get_object(abbr)<EOL><DEDENT>except DoesNotExist:<EOL><INDENT>raise Http404<EOL><DEDENT>spec = {'<STR_LIT>': True, '<STR_LIT>': {'<STR_LIT>': True}}<EOL>chambers = dict((k, v['<STR_LIT:name>']) for k, v in meta['<STR_LIT>'].items())<EOL>chamber = request.GET.get('<STR_LIT>', '<STR_LIT>')<EOL>if chamber in chambers:<EOL><INDENT>spec['<STR_LIT>'] = chamber<EOL>chamber_title = meta['<STR_LIT>'][chamber]['<STR_LIT:title>'] + '<STR_LIT:s>'<EOL><DEDENT>else:<EOL><INDENT>chamber = '<STR_LIT>'<EOL>chamber_title = '<STR_LIT>'<EOL><DEDENT>fields = mongo_fields('<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>'<STR_LIT>', '<STR_LIT>', '<STR_LIT>', '<STR_LIT>',<EOL>billy_settings.LEVEL_FIELD, '<STR_LIT>')<EOL>sort_key = '<STR_LIT>'<EOL>sort_order = <NUM_LIT:1><EOL>if request.GET:<EOL><INDENT>sort_key = request.GET.get('<STR_LIT:key>', sort_key)<EOL>sort_order = int(request.GET.get('<STR_LIT>', sort_order))<EOL><DEDENT>legislators = meta.legislators(extra_spec=spec, fields=fields)<EOL>def sort_by_district(obj):<EOL><INDENT>matchobj = re.search(r'<STR_LIT>', obj.get('<STR_LIT>', '<STR_LIT>') or '<STR_LIT>')<EOL>if matchobj:<EOL><INDENT>return int(matchobj.group())<EOL><DEDENT>else:<EOL><INDENT>return obj.get('<STR_LIT>', '<STR_LIT>')<EOL><DEDENT><DEDENT>legislators = sorted(legislators, key=sort_by_district)<EOL>if sort_key != '<STR_LIT>':<EOL><INDENT>legislators = sorted(legislators, key=operator.itemgetter(sort_key),<EOL>reverse=(sort_order == -<NUM_LIT:1>))<EOL><DEDENT>else:<EOL><INDENT>legislators = sorted(legislators, key=sort_by_district,<EOL>reverse=bool(<NUM_LIT:0> > sort_order))<EOL><DEDENT>sort_order = {<NUM_LIT:1>: -<NUM_LIT:1>, -<NUM_LIT:1>: <NUM_LIT:1>}[sort_order]<EOL>legislators = list(legislators)<EOL>return TemplateResponse(<EOL>request, templatename('<STR_LIT>'),<EOL>dict(metadata=meta, chamber=chamber,<EOL>chamber_title=chamber_title,<EOL>chamber_select_template=templatename('<STR_LIT>'),<EOL>chamber_select_collection='<STR_LIT>',<EOL>chamber_select_chambers=chambers, show_chamber_column=True,<EOL>abbr=abbr, legislators=legislators, sort_order=sort_order,<EOL>sort_key=sort_key,<EOL>legislator_table=templatename('<STR_LIT>'),<EOL>nav_active='<STR_LIT>'))<EOL>
Context:
    - metadata
    - chamber
    - chamber_title
    - chamber_select_template
    - chamber_select_collection
    - chamber_select_chambers
    - show_chamber_column
    - abbr
    - legislators
    - sort_order
    - sort_key
    - legislator_table
    - nav_active

Templates:
    - billy/web/public/legislators.html
    - billy/web/public/chamber_select_form.html
    - billy/web/public/legislator_table.html