def presences(): depute = request.args.get('depute',None) commission = request.args.get('commission',None) page = int(request.args.get('page','1'))-1 nb = int(request.args.get('itemsperpage','25')) groupe = request.args.get('groupe',request.args.get('group',None)) skip = nb*page filters = [] if depute: filters.append({'depute_shortid': depute}) if groupe: filters.append({'groupe_abrev':groupe}) if commission: filters.append({'commission_sid':commission}) if len(filters)==0: com_filter = {} elif len(filters)==1: com_filter = filters[0] else: com_filter = {'$and':filters} prescom = list(mdb.presences.find(com_filter).sort([('presence_date',-1)]).skip(skip).limit(nb)) def countItems(): rcount = mdb.presences.find(com_filter).count() return {'totalitems':rcount} cachekey= u"comm%s_%s_%s" % (depute,groupe,commission) counts = use_cache(cachekey,lambda:countItems(),expires=3600) import math nbpages = int(math.ceil(float(counts['totalitems'])/nb)) result = dict(nbitems=len(prescom),nbpages=nbpages, currentpage=1+page,itemsperpage=nb, items=prescom,**counts) return json_response(result)
def interventions(): nb = int(request.args.get('itemsperpage', '25')) page = int(request.args.get('page', '1')) - 1 groupe = request.args.get('groupe', request.args.get('group', None)) search = request.args.get('requete', request.args.get('query', '')) depute = request.args.get('depute', None) session = request.args.get('session', None) date = request.args.get('date', None) skip = nb * page filters = [] if depute: filters.append({'depute_shortid': depute}) if groupe: filters.append({'groupe_abrev': groupe}) if session: filters.append({'session_id': session}) if date: filters.append({'itv_date': date}) if search: filters.append({'$text': {'$search': '"' + search + '"'}}) if len(filters) == 0: itv_filter = {} elif len(filters) == 1: itv_filter = filters[0] else: itv_filter = {'$and': filters} itvs = list( mdb.interventions.find(itv_filter).sort([ ('itv_date', -1), ('session_id', 1), ('itv_n', 1) ]).skip(skip).limit(nb)) def countItems(): rcount = mdb.interventions.find(itv_filter).count() return {'totalitems': rcount} cachekey = u"itv%s_%s_%s_%s_%s" % (depute, groupe, search, session, date) counts = use_cache(cachekey, lambda: countItems(), expires=3600) regx = re.compile(search, re.IGNORECASE) if search: for itv in itvs: repl = regx.subn('<strong>' + search + '</strong>', itv['itv_contenu']) if repl[1]: itv['itv_contenu'] = repl[0] import math nbpages = int(math.ceil(float(counts['totalitems']) / nb)) result = dict(nbitems=len(itvs), nbpages=nbpages, currentpage=1 + page, itemsperpage=nb, items=itvs, **counts) return json_response(result)
def scrutinscles(): nb = int(request.args.get('nb', '0')) scrutins_cles = use_cache('scrutins_cles', lambda: getScrutinsCles(), expires=3600) scrutins_positions = use_cache('scrutins_positions', lambda: getScrutinsPositions(), expires=36000) scrutins = mdb.scrutins.find({ 'scrutin_num': { '$in': scrutins_cles.keys() } }).sort([('scrutin_num', -1)]) if nb > 0: scrutins = scrutins.limit(nb) scles = [] for s in scrutins: if s['scrutin_dossierLibelle'] == 'N/A' and scrutins_cles[ s['scrutin_num']]['dossier']: dossier = scrutins_cles[s['scrutin_num']]['dossier'] else: dossier = s['scrutin_dossierLibelle'] positions = dict(scrutins_positions[s['scrutin_num']]) if scrutins_cles[s['scrutin_num']]['inversion'] == 'oui': positions['position'] = { 'pour': 'contre', 'contre': 'pour' }.get(positions['position'], positions['position']) positions['pour'] = scrutins_positions[s['scrutin_num']]['contre'] positions['contre'] = scrutins_positions[s['scrutin_num']]['pour'] scles.append( dict(desc=s['scrutin_desc'], date=s['scrutin_date'], sort=s['scrutin_sort'], dossierLibelle=dossier.replace(u'\u0092', "'"), detail=scrutins_cles[s['scrutin_num']], positions=positions)) return json_response(scles)
def deputehasard(): from obsapis.controllers.scrutins import getScrutinsCles scrutins_cles = use_cache('scrutins_cles',lambda:getScrutinsCles(),expires=3600) nbdeputes = use_cache('nbdeputes',lambda:mdb.deputes.find({'depute_actif':True}).count(),expires=3600) mfields = dict((f,1) for f in deputesfields) mfields.update({'_id':None}) depute = mdb.deputes.find({'depute_actif':True},mfields).skip(int(random.random()*nbdeputes)).limit(1)[0] photo_an='http://www2.assemblee-nationale.fr/static/tribun/15/photos/'+depute['depute_uid'][2:]+'.jpg' depnumdep = depute['depute_departement_id'][1:] if depute['depute_departement_id'][0]=='0' else depute['depute_departement_id'] if depute['depute_region']==depute['depute_departement']: depute_circo_complet = "%s (%s) / %se circ" % (depute['depute_departement'],depnumdep,depute['depute_circo']) else: depute_circo_complet = "%s / %s (%s) / %se circ" % (depute['depute_region'],depute['depute_departement'],depnumdep,depute['depute_circo']) resp = dict(depute_circo_complet = depute_circo_complet, depute_photo_an = photo_an, id = depute['depute_shortid'], **depute) return json_response(resp)
def hemicycle(): depute = request.args.get('depute', '') place = mdb.deputes.find_one({'depute_shortid': depute}, {'depute_place': 1}) place = place.get('depute_place', '') if place else '' place = request.args.get('place', place) groupe = request.args.get('groupe') base_url = request.args.get( 'baseurl', 'http://dev.observatoire-democratie.fr/assemblee/deputes') def genhemicycle(): hrefs = {} titles = {} classes = {} for d in mdb.deputes.find({}, { 'depute_shortid': 1, '_id': None, 'depute_place': 1, 'depute_nom': 1, 'groupe_abrev': 1 }): plc = d['depute_place'] hrefs[plc] = base_url + '/' + d[ 'depute_shortid'] if 'base_url' else '#' titles[plc] = "place %s : %s (%s)" % (plc, d['depute_nom'], d['groupe_abrev']) classes[plc] = d['groupe_abrev'] return render_template('svg/hemicycle-paths.svg', hrefs=hrefs, titles=titles, classes=classes) cachekey = u"hemicycle-paths" + base_url paths = use_cache(cachekey, lambda: genhemicycle(), expires=24 * 3600) return render_template('svg/hemicycle.svg', paths=paths, groupe=groupe, place='p' + (place or 'nope'))
def scrutinsdetail(num): scrutin = mdb.scrutins.find_one({'scrutin_num': num}) scrutins_cles = use_cache('scrutins_cles', lambda: getScrutinsCles(), expires=3600) scrutin.update(scrutins_cles.get(num, {})) positions = {} for dpos in mdb.votes.find({'scrutin_num': num}, { 'depute_shortid': 1, 'groupe_abrev': 1, 'vote_position': 1 }): depid = dpos['depute_shortid'] gp = dpos['groupe_abrev'] pos = dpos['vote_position'] if not pos in positions.keys(): positions[pos] = {} if not gp in positions[pos].keys(): positions[pos][gp] = [] positions[pos][gp].append(depid) scrutin['positions'] = positions return json_response(scrutin)
def deputeget(shortid): from obsapis.controllers.scrutins import getScrutinsCles scrutins_cles = use_cache('scrutins_cles',lambda:getScrutinsCles(),expires=36000) mfields = dict((f,1) for f in deputefields) mfields.update({'_id':None}) depute = mdb.deputes.find_one({'depute_shortid':shortid},mfields) if not depute: depute = mdb.deputes.find_one({'depute_shortid':deputesFI[int(random.random()*len(deputesFI))]},mfields) else: pass #obsass_log('fiche',shortid) photo_an='http://www2.assemblee-nationale.fr/static/tribun/15/photos/'+depute['depute_uid'][2:]+'.jpg' depnumdep = depute['depute_departement_id'][1:] if depute['depute_departement_id'][0]=='0' else depute['depute_departement_id'] if depute['depute_region']==depute['depute_departement']: depute_circo_complet = "%s (%s) / %se circ" % (depute['depute_departement'],depnumdep,depute['depute_circo']) else: depute_circo_complet = "%s / %s (%s) / %se circ" % (depute['depute_region'],depute['depute_departement'],depnumdep,depute['depute_circo']) votes = list(mdb.votes.find({'depute_uid':depute['depute_uid']}).sort('scrutin_num',-1)) votes_cles = list(mdb.votes.find({'depute_uid':depute['depute_uid'],'scrutin_num':{'$in':scrutins_cles.keys()}},{'scrutin_num':1,'vote_position':1,'scrutin_date':1,'scrutin_dossierLibelle':1}).sort('scrutin_num',-1)) from collections import OrderedDict s_cles = OrderedDict() for v in votes_cles: v.update(scrutins_cles[v['scrutin_num']]) v['vote_position'] = {'pour':'contre','contre':'pour'}.get(v['vote_position'],v['vote_position']) if v['inversion']=='oui' else v['vote_position'] if v['scrutin_dossierLibelle']=='N/A' and scrutins_cles[v['scrutin_num']]['dossier']: dossier = scrutins_cles[v['scrutin_num']]['dossier'] else: dossier = v['scrutin_dossierLibelle'] v['scrutin_dossierLibelle'] = dossier.replace(u'\u0092',"'") if not v['theme'] in s_cles: s_cles[v['theme']] = [] s_cles[v['theme']].append(v) dates = {} weeks = {} for v in votes: pdat = datetime.datetime.strptime(v['scrutin_date'],'%d/%m/%Y') wdat = pdat.strftime('%Y-S%W') sdat = pdat.strftime('%Y-%m-%d') if not wdat in weeks.keys(): weeks[wdat] = {'e':0,'n':0} if not sdat in dates.keys(): dates[sdat] = {'e':0,'n':0} weeks[wdat]['n']+= 1 dates[sdat]['n']+= 1 weeks[wdat]['e']+= 1 if v['vote_position']!='absent' else 0 dates[sdat]['e']+= 1 if v['vote_position']!='absent' else 0 if 'depute_collaborateurs_hatvp' in depute.keys(): depute['depute_collaborateurs'] = depute['depute_collaborateurs_hatvp'] resp = dict(dates=sorted([{"date": dat,"pct":round(float(v['e'])/v['n'],3)} for dat,v in dates.iteritems()],key=lambda x:x['date']), weeks=sorted([{"week": w,"pct":100*round(float(v['e'])/v['n'],2)} for w,v in weeks.iteritems()],key=lambda x:x['week']), votes_cles=s_cles, depute_circo_complet = depute_circo_complet, depute_photo_an = photo_an, id = depute['depute_shortid'], **depute) return resp
'depute_csp','groupe_qualite','depute_nom_sa','depute_vote_groupe_scrutin', 'depute_circo','depute_nom','depute_contacts','groupe_abrev','groupe_libelle', 'depute_election','depute_profession','depute_naissance','depute_suppleant', 'depute_actif','depute_mandat_debut','depute_mandat_fin','depute_mandat_fin_cause', 'depute_bureau','depute_mandats','depute_autresmandats','depute_collaborateurs', 'depute_travaux', 'depute_hatvp','depute_nuages','depute_place','stats'] deputesfields = ['depute_uid','depute_id','depute_shortid','depute_region','depute_departement','depute_departement_id','depute_nom_sa', 'depute_csp','depute_contacts','depute_suppleant','depute_bureau','depute_mandat_debut','depute_mandat_fin','depute_mandat_fin_cause', 'depute_circo','depute_nom','groupe_abrev','groupe_libelle', 'depute_profession','depute_naissance','depute_actif','depute_place','stats'] #deputesfields = deputefields #scrutins_by_id = cache.disk('scrutins_by_id',lambda: dict((s['scrutin_id'],s) for s in mdb.scrutins.find()), time_expire=3600) deputesFI = use_cache('deputesfi',lambda:mdb.deputes.find({'groupe_abrev':'FI'}).distinct('depute_shortid'),expires=3600) @app.route('/deputes/hasard') def deputehasard(): from obsapis.controllers.scrutins import getScrutinsCles scrutins_cles = use_cache('scrutins_cles',lambda:getScrutinsCles(),expires=3600) nbdeputes = use_cache('nbdeputes',lambda:mdb.deputes.find({'depute_actif':True}).count(),expires=3600) mfields = dict((f,1) for f in deputesfields) mfields.update({'_id':None}) depute = mdb.deputes.find({'depute_actif':True},mfields).skip(int(random.random()*nbdeputes)).limit(1)[0] photo_an='http://www2.assemblee-nationale.fr/static/tribun/15/photos/'+depute['depute_uid'][2:]+'.jpg' depnumdep = depute['depute_departement_id'][1:] if depute['depute_departement_id'][0]=='0' else depute['depute_departement_id'] if depute['depute_region']==depute['depute_departement']: depute_circo_complet = "%s (%s) / %se circ" % (depute['depute_departement'],depnumdep,depute['depute_circo']) else:
def votes(): scrutins_data = use_cache('scrutins_data_new', lambda: getScrutinsData(), expires=300) nb = int(request.args.get('itemsperpage', '25')) page = int(request.args.get('page', '1')) - 1 groupe = request.args.get('groupe', request.args.get('group', None)) search = request.args.get('requete', request.args.get('query', '')) scrutingroupe = request.args.get('scrutingroupe', None) scrutin = request.args.get('scrutin', None) csp = request.args.get('csp', None) age = request.args.get('age', None) region = request.args.get('region', None) depute = request.args.get('depute', None) position = request.args.get('position', None) dissidence = int(request.args.get('dissidence', '0')) skip = nb * page filters = [] if scrutingroupe: filters.append({'scrutin_groupe': scrutingroupe}) if dissidence: filters.append({'vote_dissident': True}) if position: filters.append({'vote_position': position}) if depute: filters.append({'depute_shortid': depute}) if csp: filters.append({'depute_csp': csp}) if age: filters.append({'depute_classeage': age}) if groupe: filters.append({'groupe_abrev': groupe}) if region: filters.append({'depute_region': region}) if scrutin: try: scrutin = int(scrutin) except: pass filters.append({'scrutin_num': scrutin}) if search: filters.append({'$text': {'$search': '"' + search + '"'}}) if len(filters) == 0: vote_filter = {} elif len(filters) == 1: vote_filter = filters[0] else: vote_filter = {'$and': filters} votes = [] import re for v in mdb.votes.find(vote_filter).sort('scrutin_num', -1).skip(skip).limit(nb): v['scrutin_sort'] = scrutins_data[v['scrutin_num']]['sort'] if scrutins_data[v['scrutin_num']]['urlAmendement']: pass #v['scrutin_desc'] = re.sub(r'([0-9]+)',r'<a target="_blank" href="'+scrutins_data[v['scrutin_num']]['urlAmendement']+r'">\1</a>',v['scrutin_desc'],1) for i, lien in enumerate( scrutins_data[v['scrutin_num']]['scrutin_lientexte']): v['scrutin_desc'] = v['scrutin_desc'].replace( lien[0], 'LIEN%d' % i) for i, lien in enumerate( scrutins_data[v['scrutin_num']]['scrutin_lientexte']): v['scrutin_desc'] = v['scrutin_desc'].replace( 'LIEN%d' % i, '<a target="_blank" href="' + lien[1] + r'">' + lien[0] + '</a>') v['scrutin_dossierLibelle'] = v['scrutin_dossierLibelle'].replace( u'\u0092', "'") # pb apostrophe votes.append(v) def countItems(): rcount = mdb.votes.find(vote_filter).count() return {'totalitems': rcount} cachekey = u"vot%s_%s_%s_%s_%s_%s_%s_%s_%s_%s" % ( depute, position, scrutingroupe, dissidence, scrutin, age, csp if csp else csp, groupe, search, region if region else region) counts = use_cache(cachekey, lambda: countItems(), expires=3600) regx = re.compile(search, re.IGNORECASE) if search: for v in votes: repl = regx.subn('<strong>' + search + '</strong>', v['scrutin_desc']) if repl[1]: v['scrutin_desc'] = repl[0] import math nbpages = int(math.ceil(float(counts['totalitems']) / nb)) result = dict(nbitems=len(votes), nbpages=nbpages, currentpage=1 + page, itemsperpage=nb, items=votes, **counts) return json_response(result)
def wrapped_f(*args, **kwargs): return use_cache(request.url, lambda: f(*args, **kwargs), expires=expires)
def view_travaux(): amd_sorts = { 'retire': u'Retiré', 'tombe': u'Tombé', 'adopte': u'Adopté', 'rejete': u'Rejeté', 'nonsoutenu': u'Non soutenu', 'nonrenseigne': u'Non renseigné' } nb = int(request.args.get('itemsperpage', '25')) page = int(request.args.get('page', '1')) - 1 search = request.args.get('requete', request.args.get('query', '')) depute = request.args.get('depute', None) groupe = request.args.get('groupe', None) cosig = request.args.get('cosignataire', None) sort = request.args.get('sort', None) ttype = request.args.get('type', None) asupp = request.args.get('suppression', None) skip = nb * page filters = [] if 'suppression' in request.args.keys() and ttype == 'amendement': filters.append({'suppression': (asupp not in ("", "0"))}) if sort and ttype == 'amendement' and sort in amd_sorts.keys(): filters.append({'sort': amd_sorts[sort]}) if depute: filters.append({'depute': depute}) filters.append({'auteur': False if cosig else {'$in': [None, True]}}) elif groupe: filters.append({'groupe': groupe}) else: filters.append({'depute': None}) if ttype == 'question': filters.append({'type': {'$in': ['QG', 'QE', 'QOSD']}}) elif ttype == 'amendement': filters.append({'type': 'amendement'}) elif ttype in [ 'QG', 'QE', 'QOSD', 'propositiondeloi', 'propositionderesolution', 'rapportdinformation', 'rapport', 'avis', 'projetdeloi' ]: filters.append({'type': ttype}) elif ttype == 'document': filters.append({ 'type': { '$nin': ['propositiondeloi', 'QG', 'QE', 'QOSD', 'amendement'] } }) if search: search = '"' + search + '"' def searchText(): txt_amd = [ a['id'] for a in mdb.amendements.find({'$text': { '$search': search }}) ] txt_que = [ q['id'] for q in mdb.questions.find({'$text': { '$search': search }}) ] txt_doc = [ d['id'] for d in mdb.documentsan.find({'$text': { '$search': search }}) ] return txt_amd + txt_que + txt_doc cachekey = u"trvtxt%s" % (search) ids = use_cache(cachekey, lambda: searchText(), expires=3600) filters.append({'idori': {'$in': ids}}) def makefilter(f): if len(f) == 0: mf = {} elif len(f) == 1: mf = f[0] else: mf = {'$and': f} return mf tfilter = makefilter(filters) print filters, tfilter travaux = list( mdb.travaux.find(tfilter).sort('date', -1).skip(skip).limit(nb)) print travaux def countItems(): rcount = mdb.travaux.find(tfilter).count() return {'totalitems': rcount} cachekey = u"trv%s_%s_%s_%s_%s_%s" % (depute, groupe, search, ttype, sort, cosig) counts = use_cache(cachekey, lambda: countItems(), expires=3600) import math for t in travaux: t['description'] = t['description'].replace(u'\u0092', "'").replace( u'\u2019', "'") t['dossier'] = t['dossier'].replace(u'\u0092', "'").replace(u'\u2019', "'") nbpages = int(math.ceil(float(counts['totalitems']) / nb)) result = dict(nbitems=len(travaux), nbpages=nbpages, currentpage=1 + page, itemsperpage=nb, items=travaux, **counts) return json_response(result)