def load_grants():
    s = grants.Load(terms=[int(clerk_utils.current_term())])
    s.scrape()

    messages = utils.load_cases(scotusbot.MONGODB_DATABASE.grants, s.cases, "GRANTED", "question_url", alert=True)

    for message in messages:
        outputs.append([scotusbot.CHANNEL, message])
def load_slipopinions():
    s = slipopinions.Load(terms=[int(clerk_utils.current_term())])
    s.scrape()

    messages = utils.load_cases(scotusbot.MONGODB_DATABASE.slipopinions, s.cases, "DECIDED", "opinion_pdf_url", alert=True)

    for message in messages:
        outputs.append([scotusbot.CHANNEL, message])
def load_orders():
    o = orders.Load(terms=[int(clerk_utils.current_term())])
    o.scrape()

    collection = scotusbot.MONGODB_DATABASE.orders

    for order in o.orders:
        if not collection.find_one({'term': order.term, 'orders_pdf_url': order.orders_pdf_url}):
            collection.insert(order.__dict__)
            message = "*ORDERS*\n%s" % order.orders_pdf_url
            outputs.append([scotusbot.CHANNEL,message])
예제 #4
0
"""
Preloads grants and slip opinions.
Don't want to alert a bunch of grants and opinions that we
already know about.
"""
from clerk import utils as clerk_utils
from docket import grants
from docket import slipopinions

import scotusbot
from scotusbot import utils

# Preload grants.
s = grants.Load(terms=[int(clerk_utils.current_term())])
s.scrape()
utils.load_cases(scotusbot.MONGODB_DATABASE.grants, s.cases, "GRANTED", "question_url")

# Preload slip opinions.
o = slipopinions.Load(terms=[int(clerk_utils.current_term())])
o.scrape()
utils.load_cases(scotusbot.MONGODB_DATABASE.slipopinions, o.cases, "DECIDED", "opinion_pdf_url")
예제 #5
0
def cases_by_term(request):
    """
    /api/v1/case/by-term/
    Returns cases and their ideology grouped by Term.
    For a graphic by Alicia Parlapiano.
    term,share 9,share 8,share 7,share 6,share 5,share 0,share -5,share -6,share -7,share -8,share -9b,barwidth
    barwidth: number of terms
    term: term year
    """
    payload = []

    cases = models.Case.valid.filter(decisiondirection__in=['2', '1'])
    terms = range(1946, int(clerk_utils.current_term()) + 1)

    SHARE_KEYS = (
        "share -9",
        "share -8",
        "share -7",
        "share -6",
        "share -5",
        "share 5",
        "share 6",
        "share 7",
        "share 8",
        "share 9"
    )

    def init_court_row():
        payload = {}
        for key in SHARE_KEYS:
            payload[key] = 0
        payload['term'] = None
        payload['kennedy share -5'] = 0
        payload['kennedy share 5'] = 0
        payload['powell share -5'] = 0
        payload['powell share 5'] = 0
        return dict(payload)

    def compute_shares(row, case_count):
        if case_count > 0:
            for key in SHARE_KEYS:
                row[key] = float(row[key]) / float(case_count)
        return row

    def produce_row(row, header):
        """
        You can put the fields wherever you want.
        If the fields are not returned here, they will not be in the output.
        """
        output = (row['term'],)
        for key in SHARE_KEYS:
            output = output + (row[key],)
        output = output + (row["kennedy share -5"], row["kennedy share 5"], row["powell share -5"], row["powell share 5"])
        return output

    k = models.Justice.objects.get(justicename="AMKennedy")
    p = models.Justice.objects.get(justicename="LFPowell")

    for term in terms:
        court_cases = models.Case.valid\
            .filter(decisiondirection__in=['2', '1'])\
            .filter(term=term)\
            .values('casename', 'weighted_majvotes', 'term', 'decisiondirection')
        court_row = dict(init_court_row())
        court_row['term'] = term
        for c in court_cases:
            if c['weighted_majvotes']:
                court_row['share %s' % c['weighted_majvotes']] += 1
        court_row = compute_shares(court_row, court_cases.count())

        """
        Grab votes by kennedy and powell where they were on the winning side of a 5-4.
        """
        try:
            court_row['powell share -5'] = float(models.Vote.valid.filter(
                justice=p.justice,
                term=term,
                weighted_majvotes=-5,
                majority="2",
                decisiondirection__in=['2','1']).count()) / court_cases.count()
            court_row['powell share 5'] = float(models.Vote.valid.filter(
                justice=p.justice,
                term=term,
                weighted_majvotes=5,
                majority="2",
                decisiondirection__in=['2','1']).count()) / court_cases.count()
        except ZeroDivisionError:
            pass

        try:
            court_row['kennedy share -5'] = float(models.Vote.valid.filter(justice=k.justice,
            term=term,
            weighted_majvotes=-5,
            majority="2",
            decisiondirection__in=['2','1']).count()) / court_cases.count()
            court_row['kennedy share 5'] = float(models.Vote.valid.filter(justice=k.justice,
            term=term,
            weighted_majvotes=5,
            majority="2",
            decisiondirection__in=['2','1']).count()) / court_cases.count()
        except ZeroDivisionError:
            pass

        payload.append(court_row)

    payload = sorted(payload, key=lambda x:(x['term']))

    response = HttpResponse(content_type='text/csv')
    writer = csv.writer(response)
    header = (
        "term",
        "share -9",
        "share -8",
        "share -7",
        "share -6",
        "share -5",
        "share 5",
        "share 6",
        "share 7",
        "share 8",
        "share 9",
        "kennedy share -5",
        "kennedy share 5",
        "powell share -5",
        "powell share 5"
    )
    writer.writerow(header)
    for row in payload:
        writer.writerow(produce_row(row,header))
    return response
예제 #6
0
파일: views.py 프로젝트: newsdev/nyt-scotus
def cases_by_term(request):
    """
    /api/v1/case/by-term/
    Returns cases and their ideology grouped by Term.
    For a graphic by Alicia Parlapiano.
    term,share 9,share 8,share 7,share 6,share 5,share 0,share -5,share -6,share -7,share -8,share -9b,barwidth
    barwidth: number of terms
    term: term year
    """
    payload = []

    cases = models.MeritsCase.valid.filter(decisiondirection__in=[u'2', u'1'])
    terms = range(1946, int(clerk_utils.current_term()) + 1)

    SHARE_KEYS = ("share -9","share -8","share -7","share -6","share -5","share 5","share 6","share 7","share 8","share 9")

    def init_court_row():
        payload = {}
        for key in SHARE_KEYS:
            payload[key] = 0
        payload['term'] = None
        payload['kennedy share -5'] = 0
        payload['kennedy share 5'] = 0
        payload['powell share -5'] = 0
        payload['powell share 5'] = 0
        return dict(payload)

    def compute_shares(row, case_count):
        if case_count > 0:
            for key in SHARE_KEYS:
                row[key] = float(row[key]) / float(case_count)
        return row

    def produce_row(row, header):
        """
        You can put the fields wherever you want.
        If the fields are not returned here, they will not be in the output.
        """
        output = (row['term'],)
        for key in SHARE_KEYS:
            output = output + (row[key],)
        output = output + (row["kennedy share -5"], row["kennedy share 5"], row["powell share -5"], row["powell share 5"])
        return output

    k = models.Justice.objects.get(justicename="AMKennedy")
    p = models.Justice.objects.get(justicename="LFPowell")

    for term in terms:
        court_cases = models.MeritsCase.valid.filter(decisiondirection__in=[u'2', u'1']).filter(term=term).values('casename', 'weighted_majvotes', 'term', 'decisiondirection')
        court_row = dict(init_court_row())
        court_row['term'] = term
        for c in court_cases:
            if c['weighted_majvotes']:
                court_row['share %s' % c['weighted_majvotes']] += 1
        court_row = compute_shares(court_row, court_cases.count())

        """
        Grab votes by kennedy and powell where they were on the winning side of a 5-4.
        """
        try:
            court_row['powell share -5'] = float(models.Vote.valid.filter(justice=p.justice, term=term, weighted_majvotes=-5, majority="2", decisiondirection__in=[u'2', u'1']).count()) / court_cases.count()
            court_row['powell share 5'] = float(models.Vote.valid.filter(justice=p.justice, term=term, weighted_majvotes=5, majority="2", decisiondirection__in=[u'2', u'1']).count()) / court_cases.count()
        except ZeroDivisionError:
            pass

        try:
            court_row['kennedy share -5'] = float(models.Vote.valid.filter(justice=k.justice, term=term, weighted_majvotes=-5, majority="2", decisiondirection__in=[u'2', u'1']).count()) / court_cases.count()
            court_row['kennedy share 5'] = float(models.Vote.valid.filter(justice=k.justice, term=term, weighted_majvotes=5, majority="2", decisiondirection__in=[u'2', u'1']).count()) / court_cases.count()
        except ZeroDivisionError:
            pass

        payload.append(court_row)

    payload = sorted(payload, key=lambda x:(x['term']))

    response = HttpResponse(content_type='text/csv')
    writer = csv.writer(response)
    header = ("term", "share -9","share -8","share -7","share -6","share -5","share 5","share 6","share 7","share 8","share 9", "kennedy share -5", "kennedy share 5", "powell share -5", "powell share 5")
    writer.writerow(header)
    for row in payload:
        writer.writerow(produce_row(row, header))
    return response