Skip to content

Commit

Permalink
render enum in table (#271)
Browse files Browse the repository at this point in the history
Co-authored-by: Grey Li <[email protected]>
  • Loading branch information
PanderMusubi and greyli authored Apr 5, 2023
1 parent 22374ff commit dc0b91e
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ Changelog

Release date: -

- Render enums in tables by their labels.
- Support creating action URLs for dict data (`#268 <https://github.com/helloflask/bootstrap-flask/issues/268>`__).


Expand Down
11 changes: 9 additions & 2 deletions examples/bootstrap4/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from enum import Enum
from flask import Flask, render_template, request, flash, Markup, redirect, url_for
from flask_wtf import FlaskForm, CSRFProtect
from wtforms.validators import DataRequired, Length, Regexp
Expand Down Expand Up @@ -121,11 +122,16 @@ class BootswatchForm(FlaskForm):
submit = SubmitField()


class MyCategory(Enum):
CAT1 = 'Category 1'
CAT2 = 'Category 2'


class Message(db.Model):
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.Text, nullable=False)
author = db.Column(db.String(100), nullable=False)
category = db.Column(db.String(100), nullable=False)
category = db.Column(db.Enum(MyCategory), default=MyCategory.CAT1, nullable=False)
draft = db.Column(db.Boolean, default=False, nullable=False)
create_time = db.Column(db.Integer, nullable=False, unique=True)

Expand All @@ -151,9 +157,10 @@ def before_first_request_func():
m = Message(
text=f'Message {i+1} {url}',
author=f'Author {i+1}',
category=f'Category {i+1}',
create_time=4321*(i+1)
)
if i % 2:
m.category = MyCategory.CAT2
if i % 4:
m.draft = True
db.session.add(m)
Expand Down
11 changes: 9 additions & 2 deletions examples/bootstrap5/app.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# -*- coding: utf-8 -*-
from enum import Enum
from flask import Flask, render_template, request, flash, Markup, redirect, url_for
from flask_wtf import FlaskForm, CSRFProtect
from wtforms.validators import DataRequired, Length, Regexp
Expand Down Expand Up @@ -125,11 +126,16 @@ class BootswatchForm(FlaskForm):
submit = SubmitField()


class MyCategory(Enum):
CAT1 = 'Category 1'
CAT2 = 'Category 2'


class Message(db.Model):
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.Text, nullable=False)
author = db.Column(db.String(100), nullable=False)
category = db.Column(db.String(100), nullable=False)
category = db.Column(db.Enum(MyCategory), default=MyCategory.CAT1, nullable=False)
draft = db.Column(db.Boolean, default=False, nullable=False)
create_time = db.Column(db.Integer, nullable=False, unique=True)

Expand All @@ -155,9 +161,10 @@ def before_first_request_func():
m = Message(
text=f'Message {i+1} {url}',
author=f'Author {i+1}',
category=f'Category {i+1}',
create_time=4321*(i+1)
)
if i % 2:
m.category = MyCategory.CAT2
if i % 4:
m.draft = True
db.session.add(m)
Expand Down
2 changes: 2 additions & 0 deletions flask_bootstrap/templates/base/table.html
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
{{ value }}
{%- endif -%}
{%- endif -%}
{%- elif value.__class__.__base__.__name__ == 'Enum' -%}
{{ value.value }}
{%- else -%}
{{ value }}
{%- endif -%}
Expand Down
8 changes: 8 additions & 0 deletions tests/test_bootstrap4/test_render_table.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from enum import Enum
from flask import render_template_string, request
from flask_sqlalchemy import SQLAlchemy
from flask_wtf import CSRFProtect
Expand Down Expand Up @@ -317,6 +318,11 @@ def dict_data_table():
assert 'href="/table/new-message"' in data


class MyCat(Enum):
CAT1 = 'Category A'
CAT2 = 'Category B'


def test_customize_icon_title_of_table_actions(app, client):

app.config['BOOTSTRAP_TABLE_VIEW_TITLE'] = 'Read'
Expand All @@ -330,6 +336,7 @@ def test_customize_icon_title_of_table_actions(app, client):
class Message(db.Model):
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.Text)
category = db.Column(db.Enum(MyCat), default=MyCat.CAT1, nullable=False)

@app.route('/table')
def test():
Expand Down Expand Up @@ -357,3 +364,4 @@ def test():
assert 'title="Update">' in data
assert 'title="Remove">' in data
assert 'title="Create">' in data
assert 'Category A' in data

0 comments on commit dc0b91e

Please sign in to comment.