def get_stats(): res = Case.select(Conclusion.article, Conclusion.type, fn.COUNT(Case.itemid)).join(ConclusionCase)\ .join(Conclusion)\ .where(Conclusion.article != None)\ .group_by(Conclusion.base_article, Conclusion.type) return res
def format_structured_json(): cases_id = Case.select(Case.itemid) cases = [] representents = [] for case_id in cases_id: c = c_case.get_case_info(case_id) parties = c['parties'] c['party_1'] = parties[0] c['party_2'] = parties[1] c.update(flatten_simply_dict(c, 'originatingbody')) c['respondent'] = c['respondent'].split(';')[0] c['applicability'] = c['applicability'].split(';')[0] del c['originatingbody'] del c['parties'] del c['appno'] del c['judgment'] for d in c['documents']: c['documents'][d] = c['documents'][d]['uri'] if c['documents'][d][ 'available'] else None c.update(flatten_simply_dict(c, 'documents')) del c['documents'] c['country'] = c['country']['name'] del c['articles'] representents.append(c['representedby']) del c['representedby'] del c['extractedapps'] del c['conclusions'] del c['decisionbody'] del c['scl'] cases.append(c) return cases
def get_cases_per_country(): query = list( Case.select(Case.respondent, fn.COUNT(Case.respondent)).group_by( Case.respondent).tuples()) res = {} for c in query: for e in c[0].split(';'): country = COUNTRIES[e]['name'] res[country] = res.get(country, 0) + c[1] return res
def get_cases_number(): res = Case.select(Case.itemid).count() return res
def get_case(itemid: str): return Case.get_by_id(itemid)
def get_cases(page: int = 1, limit: int = 100): return Case.select().paginate(page, limit)
async def get_cases(request: Request): def parse_request(params): req = {'order': {}, 'search': {}} for k, v in params: if k.startswith('order'): i = list(map(lambda x: x[:-1], k.split('[')[1:])) req['order'][i[1]] = v elif k.startswith('search'): i = list(map(lambda x: x[:-1], k.split('[')[1:])) if i[0] not in req['search']: req['search'][i[0]] = v elif not k.startswith('columns'): req[k] = v return req from echr.data_models.base import db @db.collation() def country_asc(s1, s2): return COUNTRIES[s1.split(';')[0]]['name'] < COUNTRIES[s2.split(';') [0]]['name'] @db.collation() def country_desc(s1, s2): return -country_asc(s1, s2) sorting_map = { '0': Case.itemid, '2': Case.judgementdate, '3': Case.importance, '4': Case.docname, '5': country_asc.collation(Case.respondent), '8': Case.separateopinion } req = parse_request(request.query_params.items()) order_col = sorting_map[req['order']['column']].asc() if req['order']['dir'] == 'desc': order_col = sorting_map[req['order']['column']].desc() if req['order']['column'] == '5': order_col = country_desc.collation(Case.respondent).desc() length = int(req['length']) page = (int(req['start']) // length) + 1 if 'value' in req['search'] and req['search']['value']: t = req['search']['value'] cases = Case.select().where( Case.docname.contains(t)).order_by(order_col) filtered_cases = cases.count() cases = cases.paginate(page, length) else: cases = Case.select().order_by(order_col).paginate(page, length) res = [] for c in cases: articles = [] list_articles = [] for p in c.conclusions: e = model_to_dict(p)['conclusion'] if e['article'] not in list_articles: list_articles.append(e['article']) articles.append(e) parties = [] for p in c.parties: parties.append(p.party.name) res.append(model_to_dict(c)) res[-1]['articles'] = articles res[-1]['parties'] = parties res[-1]['country'] = COUNTRIES[c.respondent.split(';')[0]] res[-1]['originatingbody'] = { 'name': c.originatingbody_name, 'type': c.originatingbody_type } res[-1]['documents'] = c_case.available_documents(c.itemid) for k in res[-1]: if isinstance(res[-1][k], datetime): res[-1][k] = { 'display': str(res[-1][k].strftime('%b %d, %Y')), 'timestamp': (int(datetime.timestamp(res[-1][k]))) } cases_number = Case.select().count() filtered_cases_number = filtered_cases if 'value' in req['search'] and req[ 'search']['value'] else cases_number return JSONResponse({ 'data': res, 'recordsTotal': cases_number, 'recordsFiltered': filtered_cases_number })