Skip to content
Snippets Groups Projects
Commit 13e779ab authored by Jiří Setnička's avatar Jiří Setnička
Browse files

Table: Tabulka předělána na použití Row namísto prostého dict

Umožňuje specifikvoat html_attr a již tak není potřeba hack se speciálně pojmenovaným
sloupcem. Nakonec uznávám, že tohle je lepší řešení :)
parent 9870e693
No related branches found
No related tags found
1 merge request!17Výsledkovka pomocí mo.web.table
...@@ -2,7 +2,7 @@ from flask import render_template, g, redirect, url_for, request ...@@ -2,7 +2,7 @@ from flask import render_template, g, redirect, url_for, request
from sqlalchemy.orm import aliased from sqlalchemy.orm import aliased
import mo.db as db import mo.db as db
from mo.web.table import Table, Column from mo.web.table import Table, Row, Column
from mo.web import app from mo.web import app
...@@ -38,7 +38,7 @@ def org_export_skoly(): ...@@ -38,7 +38,7 @@ def org_export_skoly():
.filter(db.Place.place_id == db.School.place_id) .filter(db.Place.place_id == db.School.place_id)
.filter(db.Place.parent == town.place_id) .filter(db.Place.parent == town.place_id)
.yield_per(100)): .yield_per(100)):
yield { yield Row(keys={
'code': p.get_code(), 'code': p.get_code(),
'name': p.name, 'name': p.name,
'red_izo': s.red_izo, 'red_izo': s.red_izo,
...@@ -49,7 +49,7 @@ def org_export_skoly(): ...@@ -49,7 +49,7 @@ def org_export_skoly():
'is_ss': int(s.is_ss), 'is_ss': int(s.is_ss),
'town_code': t.get_code(), 'town_code': t.get_code(),
'town': t.name, 'town': t.name,
} })
table = Table(school_export_columns, gen_rows(), 'skoly') table = Table(school_export_columns, gen_rows(), 'skoly')
return table.send_as(format, streaming=True) return table.send_as(format, streaming=True)
...@@ -22,7 +22,7 @@ from mo.util_format import inflect_number ...@@ -22,7 +22,7 @@ from mo.util_format import inflect_number
from mo.web import app from mo.web import app
import mo.web.util import mo.web.util
from mo.web.util import PagerForm from mo.web.util import PagerForm
from mo.web.table import CellCheckbox, Table, Column, cell_place_link, cell_user_link, cell_email_link from mo.web.table import CellCheckbox, Table, Row, Column, cell_place_link, cell_user_link, cell_email_link
import wtforms.validators as validators import wtforms.validators as validators
...@@ -416,11 +416,11 @@ def get_contestants_query( ...@@ -416,11 +416,11 @@ def get_contestants_query(
def make_contestant_table(query: Query, add_checkbox: bool = False, add_contest_column: bool = False): def make_contestant_table(query: Query, add_checkbox: bool = False, add_contest_column: bool = False):
ctants = query.all() ctants = query.all()
rows: List[dict] = [] rows: List[Row] = []
for pion, pant, ct in ctants: for pion, pant, ct in ctants:
u = pion.user u = pion.user
rows.append({ rows.append(Row(
'_html_tr': '<tr class="testuser" title="Testovací uživatel">' if u.is_test else '', keys={
'sort_key': u.sort_key(), 'sort_key': u.sort_key(),
'first_name': cell_user_link(u, u.first_name), 'first_name': cell_user_link(u, u.first_name),
'last_name': cell_user_link(u, u.last_name), 'last_name': cell_user_link(u, u.last_name),
...@@ -433,9 +433,14 @@ def make_contestant_table(query: Query, add_checkbox: bool = False, add_contest_ ...@@ -433,9 +433,14 @@ def make_contestant_table(query: Query, add_checkbox: bool = False, add_contest_
'place_code': cell_place_link(pion.place, pion.place.get_code()), 'place_code': cell_place_link(pion.place, pion.place.get_code()),
'status': pion.state.friendly_name(), 'status': pion.state.friendly_name(),
'checkbox': CellCheckbox('checked', u.user_id, False), 'checkbox': CellCheckbox('checked', u.user_id, False),
}) },
html_attr={
'class': "testuser",
'title': "Testovací uživatel",
} if u.is_test else {}
))
rows.sort(key=lambda r: r['sort_key']) rows.sort(key=lambda r: r.keys['sort_key'])
cols: Sequence[Column] = contest_list_columns cols: Sequence[Column] = contest_list_columns
if add_checkbox: if add_checkbox:
......
...@@ -4,9 +4,10 @@ from flask import Response, url_for ...@@ -4,9 +4,10 @@ from flask import Response, url_for
from html import escape from html import escape
import io import io
from markupsafe import Markup from markupsafe import Markup
from typing import Sequence, Optional, Iterable, Union from typing import Any, Dict, Sequence, Optional, Iterable, Union
import urllib.parse import urllib.parse
import werkzeug.exceptions import werkzeug.exceptions
from werkzeug.utils import html
from mo.csv import FileFormat from mo.csv import FileFormat
import mo.db as db import mo.db as db
...@@ -39,6 +40,19 @@ class Cell: ...@@ -39,6 +40,19 @@ class Cell:
return escape(self.text) return escape(self.text)
class Row:
"""Řádek tabulky, definuje klíče. Může definovat HTML atributy řádku."""
keys: Dict[str, Any]
html_attr: Dict[str, str]
def __init__(self, keys: Dict[str, Any], html_attr: Dict[str, str] = {}):
self.keys = keys
self.html_attr = html_attr
def get(self, key: str) -> Any:
return self.keys.get(key)
class CellLink(Cell): class CellLink(Cell):
url: str url: str
hint: Optional[str] hint: Optional[str]
...@@ -75,11 +89,11 @@ class CellCheckbox(Cell): ...@@ -75,11 +89,11 @@ class CellCheckbox(Cell):
class Table: class Table:
columns: Sequence[Column] columns: Sequence[Column]
rows: Iterable[dict] rows: Iterable[Row]
filename: str filename: str
show_downlink: bool show_downlink: bool
def __init__(self, columns: Sequence[Column], rows: Iterable[dict], filename: str, show_downlink: bool = True): def __init__(self, columns: Sequence[Column], rows: Iterable[Row], filename: str, show_downlink: bool = True):
self.columns = columns self.columns = columns
self.rows = rows self.rows = rows
self.filename = filename self.filename = filename
...@@ -94,10 +108,8 @@ class Table: ...@@ -94,10 +108,8 @@ class Table:
tab.append('<tbody>') tab.append('<tbody>')
for r in self.rows: for r in self.rows:
if r.get('_html_tr'): r_attr = [f' {key}="{val}"' for (key, val) in r.html_attr.items()]
tab.append(r.get('_html_tr')) tab.append(f'<tr{"".join(r_attr)}>')
else:
tab.append('<tr>')
for c in self.columns: for c in self.columns:
val = r.get(c.key) val = r.get(c.key)
if isinstance(val, Cell): if isinstance(val, Cell):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment