def top_pour_lois(request): results = [] for dossier in Dossier.objects.filter(date_promulgation__isnull=False): c = 0 c = Vote.objects.filter(scrutin_dossier=None, scrutin__etape__dossier=dossier, position='pour').count() c2 = Vote.objects.filter(scrutin_dossier=None, scrutin__etape__dossier=dossier).count() if c2: results.append([dossier, c / c2, c]) else: results.append([dossier, 0, 0]) results.sort(key=lambda r: (-r[1], -r[2])) lines = [] for i, r in enumerate(results): dossier, c, c2 = r lines.append( L.span( ".list-group-item.list-group-item-action.flex-column.align-items-start" ) / f"{i+1}: {dossier} avec {round(c*100, 2)}% de votes pour ({c2} votes)" ) return HttpResponse( template([ L.h2 / "Top des lois promulguées par le pourcentage de votes pour", L.div(".list-group") / lines, ]))
def list(exts, page, pages, name, exts_count, files_count, total_size): def _page(p): name = ('pages/' + str(p) if p > 1 else 'index') + '.html' link = L.a(href='/' + name) / (' %d ' % p) if p == page: return L.strong / link return link return _base(( L.div(style="text-align: center") / ( L.strong / _add_commas(exts_count), ' extensions, ', L.strong / _add_commas(files_count), ' versions, ', L.strong / _sizeof_fmt(total_size), ' stored', L.br, 'Last update: ' + datetime.datetime.now().strftime('%Y-%m-%d'), ), L.div(style="text-align: center") / ( 'Pages:', *(_page(p) for p in range(1, pages)), '(ordered by # of users)' ), L.hr, *(_ext(ext) for ext in exts), ))
def top_contre_pourcentage(request): results = [] for dep in Depute.objects.all(): c = Vote.objects.filter(scrutin_dossier=None, scrutin__article__isnull=True, depute=dep, position='contre').count() c2 = Vote.objects.filter(scrutin_dossier=None, scrutin__article__isnull=True, depute=dep).count() if c2: results.append([dep, c / c2, c]) else: results.append([dep, 0, 0]) results.sort(key=lambda r: (-r[1], -r[2])) lines = [] for i, r in enumerate(results): dep, c, c2 = r lines.append( L.span( ".list-group-item.list-group-item-action.flex-column.align-items-start" ) / f"{i+1}: {dep} ({dep.groupe}) avec {round(c*100, 2)}% de votes contre ({c2} votes)" ) return HttpResponse( template([ L.h2 / "Top des députés qui ont votés contre les lois promulguées", L.div(".list-group") / lines, ]))
def test_shortcut(self): self.assertEqual(str(L.span('.hello')), '<span class="hello"></span>') self.assertEqual(str(L.span('.hello.world')), '<span class="hello world"></span>') self.assertEqual(str(L.span('#world.hello')), '<span class="hello" id="world"></span>') self.assertEqual(str(L.span('#what')), '<span id="what"></span>')
def list(exts, page, pages, name, exts_count, files_count, total_size): def _page(p): name = ('pages/' + str(p) if p > 1 else 'index') + '.html' link = L.a(href='/' + name) / (' %d ' % p) if p == page: return L.strong / link return link return _base(( L.div(style="text-align: center") / ( L.strong / _add_commas(exts_count), ' extensions, ', L.strong / _add_commas(files_count), ' versions, ', L.strong / _sizeof_fmt(total_size), ' stored', L.br, 'Last update: ' + datetime.datetime.now().strftime('%Y-%m-%d'), ), L.div(style="text-align: center") / ('Pages:', *(_page(p) for p in range(1, pages)), '(ordered by # of users)'), L.hr, *(_simple_ext(ext) for ext in exts), ))
def test_escaping(self): self.assertEqual(str(L.div(id='hello & ; " \'')), '<div id="hello & ; " '"></div>') self.assertEqual( str(L.h1 / '<script>alert("h4x0r")</script>'), '<h1><script>alert("h4x0r")</script></h1>') self.assertEqual( str(L.button(onclick=raw('alert(\'follow the rabbit\')'))), '<button onclick="alert(\'follow the rabbit\')"></button>')
def test_attributes(self): self.assertEqual(str(L.h1(class_='hello') / 'world'), '<h1 class="hello">world</h1>') self.assertEqual(str(L.input(id='hello', value='world')), '<input id="hello" value="world"/>') self.assertEqual(str(L.input(what='')), '<input what/>') self.assertEqual(str(L.input(what=None)), '<input what/>') self.assertEqual(str(L.input(data_trigger='666')), '<input data-trigger="666"/>')
def ext(ext): return _base((_ext( ext ), L.p('.description') / _nl2br(ext['full_description']), L.small / (L.a( '.removal-request', href='mailto:[email protected]?subject=Extension removal request&body=' + str(ext.get('ext_id'))) / 'request removal from the archive', ), L.hr, L.pre('.pprint') / json.dumps(ext, indent=2, sort_keys=True)), title_prefix=ext['name'] + ' - ')
def _render_vote(vote, count): if vote: if vote.position == 'pour': return L.small(".badge.badge-success") / vote.position elif vote.position == 'contre': return L.small(".badge.badge-danger") / vote.position elif vote.position == 'abstention': return L.small(".badge.badge-warning") / vote.position else: return L.small(".badge.badge-info") / f"absent(e) sur {count}"
def ext(ext): return _base(( _ext(ext), L.p('.description') / _nl2br(ext['full_description']), L.small / ( 'Are you the owner of the extension ?', L.a('.removal-request', href='mailto:[email protected]?subject=crx.dam.io - Contact as owner&body=' + str(ext.get('ext_id')) + '%0A%0A%0Aproof you are the owner of this extension (own an email related to the extension / dashboard screenshot):') / 'Contact as owner', ), L.hr, L.pre('.pprint') / json.dumps(ext, indent=2, sort_keys=True) ), title_prefix=ext['name']+ ' - ')
def ext(ext): return _base(( _ext(ext), L.p('.description') / _nl2br(ext['full_description']), L.small / ( 'Are you the owner of the extension ?', L.a('.removal-request', href='mailto:[email protected]?subject=Extension removal request&body=' + str(ext.get('ext_id')) + '%0A%0A%0Aproof you are the owner of this extension:') / 'request removal from crx.dam.io', ), L.hr, L.pre('.pprint') / json.dumps(ext, indent=2, sort_keys=True) ), title_prefix=ext['name']+ ' - ')
def _render_breadcrumb(els): def _el_url(el): nonlocal els if hasattr(el, 'url'): if el == els[0]: return el.url() else: return el.url(els[0]) return L.ol(".breadcrumb") / [( (L.li('.breadcrumb-item') / L.a(href=_el_url(el)) / str(el)) if el != els[-1] else (L.li('.breadcrumb-item.active') / str(el))) for el in els]
def depute(request, dep_id): dep = Depute.objects.get(identifiant=dep_id) dossiers = Dossier.objects.filter( date_promulgation__isnull=False).order_by("-date_promulgation", "titre") return HttpResponse( template([ _render_breadcrumb([dep]), L.p / ( L.a(href=dep.url() + "/lois-en-cours") / L.button(".btn.btn-warning") / "voir lois en cours", ' ', L.a(href=dep.url() + "/autres-votes") / L.button(".btn.btn-warning") / "voir autres votes", ), L.h2 / ["Lois", L.small(".text-muted") / " promulguées"], L.div(".list-group") / [ L. a(".list-group-item.list-group-item-action.flex-column.align-items-start", href=dos.url(dep)) / [ L.div(".d-flex.w-100.justify-content-between") / [ L.h5(".mb-1") / dos.titre, _display_depute_vote(dos, dep), ] ] for dos in dossiers ] ]))
def ext(ext): return _base((_ext( ext ), L.p('.description') / _nl2br(ext['full_description']), L.small / ( 'Are you the owner of the extension ?', L. a('.removal-request', href= 'mailto:[email protected]?subject=crx.dam.io - Contact as owner&body=' + str(ext.get('ext_id')) + '%0A%0A%0Aproof you are the owner of this extension (own an email related to the extension / dashboard screenshot):' ) / 'Contact as owner', ), L.hr, L.pre('.pprint') / json.dumps(ext, indent=2, sort_keys=True)), title_prefix=ext['name'] + ' - ')
def tpl_base(*content, title=''): return render(( raw('<!DOCTYPE html>'), L.html / ( L.head / ( L.meta(charset='utf-8'), L.meta(name="viewport",content="width=device-width, initial-scale=1"), L.title / (((title + ' - ') if title else '') + 'Petal'), L.link(rel='stylesheet', href="/static/css/bootstrap.css"), L.link(rel='stylesheet', href="/static/css/base.css"), ), L.body / content, ), ))
def depute_scrutin(request, dep_id, scrutin_id): dep = Depute.objects.get(identifiant=dep_id) scrutin = Scrutin.objects.get(id=scrutin_id) return HttpResponse( template([ _render_breadcrumb( [dep, scrutin.dossier, scrutin.etape, scrutin.objet]), L.span('.badge.badge-info') / ( 'Le ', scrutin.date, (' ', scrutin.heure) if scrutin.heure else None, ), ((L.p / L.a(href=scrutin.url_an) / L.button(".btn.btn-info") / "scrutin") if scrutin else None), ]))
def index(requests): app = apps.get_app_config('core') html_names = [] for model_name, model in app.models.items(): html_names.append(L.li / L.a(href='/'+model_name + '/') / model_name) return HttpResponse(str(L.ul / html_names))
def model(requests, model_name): app = apps.get_app_config('core') model = app.models[model_name] return HttpResponse(str(L.ul / ( ( L.li / L.a(href='/'+model_name+'/'+obj.pk) / str(obj) ) for obj in model.objects.all() )))
def deputes_inactifs(request): deputes_inactifs = Depute.objects.filter(actif=False).order_by( Lower('nom'), 'prenom') return HttpResponse( template([ L.h2 / ["Députés ", L.small(".text-muted") / " inactifs"], L.div(".list-group") / [ L. a(".list-group-item.list-group-item-action.flex-column.align-items-start", href=dep.url()) / [ L.div(".d-flex.w-100.justify-content-between") / [ L.h5(".mb-1") / f"{dep.nom}, {dep.prenom}", L.small / f"{dep.groupe}" ] ] for dep in deputes_inactifs ] ]))
def lois_en_cours(request, dep_id): dep = Depute.objects.get(identifiant=dep_id) dossiers = Dossier.objects.filter(date_promulgation__isnull=True) return HttpResponse( template([ _render_breadcrumb([dep, 'Lois en cours']), L.h2 / ["Lois", L.small(".text-muted") / " en cours d'étude"], L.div(".list-group") / [ L. a(".list-group-item.list-group-item-action.flex-column.align-items-start", href=dos.url(dep)) / [ L.div(".d-flex.w-100.justify-content-between") / [ L.h5(".mb-1") / dos.titre, _display_depute_vote(dos, dep), ] ] for dos in dossiers ] ]))
def depute_article(request, dep_id, etape_id, article): dep = Depute.objects.get(identifiant=dep_id) etape = Etape.objects.get(identifiant=etape_id) dos = etape.dossier try: scrutin = etape.scrutin_set.filter(article=article).first() except: scrutin = None return HttpResponse( template([ _render_breadcrumb([dep, dos, etape, article]), (L.span('.badge.badge-info') / ( 'Le ', scrutin.date, (' ', scrutin.heure) if scrutin.heure else None, ), ) if scrutin else None, ((L.p / L.a(href=scrutin.url_an) / L.button(".btn.btn-info") / "scrutin") if scrutin else None), ]))
def autres_votes(request, dep_id): dep = Depute.objects.get(identifiant=dep_id) scrutins = Scrutin.objects.filter(dossier__isnull=True, etape__isnull=True) return HttpResponse( template([ _render_breadcrumb([dep, 'Autres votes']), L.h2 / [ "Autres votes", ], L.div(".list-group") / [ L. a(".list-group-item.list-group-item-action.flex-column.align-items-start", href=dep.url() + '/scrutin/' + str(scrutin.id)) / [ L.div(".d-flex.w-100.justify-content-between") / [ L.h5(".mb-1") / scrutin.objet, _display_scrutin_vote(dep, scrutin), ] ] for scrutin in scrutins ] ]))
def depute_dossier(request, dep_id, dos_id): dep = Depute.objects.get(identifiant=dep_id) dos = Dossier.objects.get(identifiant=dos_id) etapes = Etape.objects.filter(dossier=dos).order_by("-date") return HttpResponse( template([ _render_breadcrumb([dep, dos]), L.p / L.a(href=dos.url_an()) / L.button(".btn.btn-info") / "dossier législatif", L.h2 / [ "Étapes", ], L.div(".list-group") / [ L. a(".list-group-item.list-group-item-action.flex-column.align-items-start", href=etape.url(dep)) / [ L.div(".d-flex.w-100.justify-content-between") / [ L.h5(".mb-1") / etape.titre, _display_etape_vote(etape, dep), ] ] for etape in etapes ] ]))
def homepage(request): deputes = Depute.objects.filter(actif=True).order_by( Lower('nom'), 'prenom') return HttpResponse( template([ raw(""" <div class="alert alert-dismissible alert-info"> <p>Ce site permet de retrouver facilement les votes de vos députés</p> <p>Vous pouvez trouver votre député par circonscription sur <a href="https://www.nosdeputes.fr/circonscription" class="alert-link">NosDéputés.fr</a></p> </div> """), L.p / L.a(href="/deputes/inactifs") / L.button(".btn.btn-warning") / "voir députés inactifs", L.h2 / ["Députés ", L.small(".text-muted") / " actifs"], L.div(".list-group") / [ L. a(".list-group-item.list-group-item-action.flex-column.align-items-start", href=dep.url()) / [ L.div(".d-flex.w-100.justify-content-between") / [ L.h5(".mb-1") / f"{dep.nom}, {dep.prenom}", L.small / f"{dep.groupe}" ] ] for dep in deputes ], ]))
def _ext(ext): return L.div / ( L.small('.extlink') / ( L.a(href='/ext/%s.html' % ext['ext_id']) / ('#' + ext['ext_id']) ), L.h2(id=ext['ext_id']) / (L.a(href=ext['url'] if ext['url'] else '') / ext['name']), L.small / _add_commas(ext['user_count']), L.ul / (( L.li / ( L.a(href=file['storage_url']) / ( file['name'].replace('.zip', ''), ' - ', L.small / (' ' + _sizeof_fmt(file['size'])), ' - ', L.small / (file['created']), ), ' ', L.small / ( L.a(target='_blank', rel='noreferrer', href=VIEW_SOURCE_URL + file['storage_url']) / 'view source' ), ) ) for file in ext['files']) )
def _ext(ext): return L.div / ( L.small('.extlink') / ( L.a(href='/ext/%s.html' % ext['ext_id']) / ('#' + ext['ext_id']) ), L.h2(id=ext['ext_id']) / (L.a(href=ext['url'] if ext['url'] else '') / ext['name']), L.small / _add_commas(ext['user_count']), L.ul / (( L.li / ( L.a(href=file['storage_url']) / ( file['name'].replace('.zip', ''), ' - ', L.small / (' ' + _sizeof_fmt(file['size'])), ), ' ', L.small / ( L.a(target='_blank', rel='noreferrer', href=VIEW_SOURCE_URL + file['storage_url']) / 'view source' ), ) ) for file in ext['files']) )
def top_pour(request): results = [] for dep in Depute.objects.all(): c = Vote.objects.filter(scrutin_dossier=None, scrutin__article__isnull=True, depute=dep, position='pour').count() results.append([dep, c]) results.sort(key=lambda r: -r[1]) lines = [] for i, r in enumerate(results): dep, c = r lines.append( L.span( ".list-group-item.list-group-item-action.flex-column.align-items-start" ) / f"{i+1}: {dep} ({dep.groupe}) avec {c} votes pour") return HttpResponse( template([ L.h2 / "Top des députés qui ont votés pour les lois promulguées", L.div(".list-group") / lines, ]))
def test_raise(self): # no children for void tags with self.assertRaises(LysException): L.br / L.p # only str or raw() attributes values with self.assertRaises(LysException): L.button(data_id=123) # invalid shortcuts with self.assertRaises(LysException): L.span('.foo.hello world') with self.assertRaises(LysException): L.span(',hello')
def _base(content, title_prefix=''): return str(L.html / ( L.head / ( L.meta(charset="utf-8"), L.meta(content="width=device-width, initial-scale=1", name="viewport"), L.title / (title_prefix + "Chrome Extensions Archive"), L.link(href="/style.css", media="screen", rel="stylesheet", type="text/css"), ), L.body() / ( L.a(href='/') / (L.h1 / "Chrome Extensions Archive"), L.div(style='text-align: right') / ( L.a(href="https://github.com/mdamien/chrome-extensions-archive") / "github.com/mdamien/chrome-extensions-archive" ), L.hr, content, ), ))
def _base(content='', title_prefix=''): return str(L.html / ( L.head / ( L.meta(charset="utf-8"), L.meta(content="width=device-width, initial-scale=1", name="viewport"), L.title / (title_prefix + "Chrome Extensions Archive"), L.link(href="/style.css", media="screen", rel="stylesheet", type="text/css"), ), L.body() / ( L.a(href='/') / (L.h1 / "Chrome Extensions Archive"), L.div(style='text-align: right') / ( L.a(href="https://github.com/mdamien/chrome-extensions-archive") / "github.com/mdamien/chrome-extensions-archive" ), L.hr, content, ), ))
def basic_frame(*content, title=''): return render(( raw('<!DOCTYPE html>'), L.html / ( L.head / ( L.meta(charset='utf-8'), L.title / (((title + ' - ') if title else '') + 'Linkage' + (' Enterprise' if settings.LINKAGE_ENTERPRISE else '')), L.link(rel='icon', type='image/png', href='/static/img/favicon.png'), L.link(rel='stylesheet', href="/static/css/bootstrap.css"), L.link(rel='stylesheet', href="/static/css/balloon.css"), L.link(rel='stylesheet', href="/static/css/rc-slider.css"), L.link(rel='stylesheet', href="/static/css/base.css"), L.script / raw("var LINKAGE_ENTERPRISE = %s;" % ('true' if settings.LINKAGE_ENTERPRISE else 'false')), ), L.body / content, ), ))
def test_raise(self): # no re-assignement of children with self.assertRaises(LysException): L.h1 / L.a / 'WeLcOmE-HoMe.Com' # no children for void tags with self.assertRaises(LysException): L.br / L.p # only str or raw() attributes values with self.assertRaises(LysException): L.button(data_id=123) # invalid shortcuts with self.assertRaises(LysException): L.span('.foo.hello world') with self.assertRaises(LysException): L.span(',hello')
def _page(p): name = ('pages/' + str(p) if p > 1 else 'index') + '.html' link = L.a(href='/' + name) / (' %d ' % p) if p == page: return L.strong / link return link
def _simple_ext(ext): return L.div / (L.a(href='/ext/%s.html' % ext['ext_id']) / ext.get('name'))
def _nl2br(text): return ((t, L.br()) for t in text.split('\n') if t)