ผลต่างระหว่างรุ่นของ "Quick Start for Developers"
| บรรทัดที่ 117: | บรรทัดที่ 117: | ||
pass | pass | ||
if request.args.get("is_submit", False) == "yes": | if request.args.get("is_submit", False) == "yes": | ||
db_mod_code, new_entry, resp_dict = gen_dbcommit_resp("table", "anime", Anime, | |||
table.form, 4, suppress_success_msg=True) | |||
if db_mod_code == 4: | |||
resp_dict["entry"] = "deleted" | |||
elif not(new_entry is None): | |||
resp_dict["entry"] = [getattr(new_entry, col, None) for col in table.cnames] | |||
return jsonify(resp_dict) | |||
return render_template("cunei_pos/test_table.html", table=table) | |||
</syntaxhighlight> | </syntaxhighlight> | ||
* ตัวแปร | * ตัวแปร | ||
รุ่นแก้ไขเมื่อ 10:35, 25 กุมภาพันธ์ 2568
Start here
สร้าง Table
สร้างหน้าที่แต่ส่วนของ table โดยมีรูปร่างหน้าตาดังนี้
<รูปหน้าที่มีแต่ตาราง(image1)>
โดยไฟล์ที่จำเป็นมีดังนี้
Model.py
กำหนดรูปแบบของ database
class Anime(CuneiModel):
id = AutoIncrementField()
stid = IntegerField()
code = CharField(default="")
name = CharField(default="")
type = CharField(default="")
Publisher = CharField(default="")
writer = CharField(default="", null=True)
class Meta:
database = SqliteExtDatabase(None)
ฟังก์ชั่นการใช้งานและชนิดช้อมูล
- คำสั่งในการใช้งาน
'AutoIncrementField()': กำหนดให้มีรันตัวเลขอัตโนมัติ'null': กำหนดให้databaseสามารถเว้นว่างได้'SqliteExtDatabase': เปิดdatabase
- กำหนดชนิดของข้อมูล
'IntegerField()': ตัวแปรชนิดตัวเลขที่เป็นจำนวนเต็ม'FloatField()': ตัวแปรชนิดตัวเลขที่จุดทศนิยม'CharField()': ตัวแปรชนิดที่เป็นตัวอักษร'TextField()': ตัวแปรชนิดที่เป็นตัวอักษรและช่องข้อความขยายได้'DateField()': ?
Table.py
class Anime_table(CuneiTable):
MASTER_colsizes = [1,3,10,10,0,7,7,7]
MASTER_colnames = ["stid", "code", "name", "type", "publisher_stid",
"publisher_code", "publisher_name", "writer"]
MASTER_coltypes = ["int", "str", "str", "sel", "int", "str", "str", "str"]
MASTER_choicecols = ["type"]
MASTER_choices = [[("Comedy", "Comedy"), ("Action", "Action"),
("Fiction", "Fiction"), ("Fantasy", "Fantasy"),
("Adventure", "Adventure")]]
MASTER_schfill = {"publisher_code":["ModalPublisher:code",
"publisher_stid:stid,publisher_name:name"]}
class SubForm(FlaskForm, CuneiSubForm):
stid = StringField("")
code = StringField("", validators=[InputRequired("Required!")])
name = StringField("", validators=[InputRequired("Required!")])
type = SelectField("")
publisher_stid = StringField("Publisher StID",
validators=[InputRequired(lazy_gettext("Required!"))],
render_kw={"readonly":""})
publisher_code = StringField("Publisher Code",
validators=[InputRequired(lazy_gettext("Required!"))])
publisher_name = StringField("Publisher Name", render_kw={"readonly":""})
writer = StringField("", validators=[InputRequired("Required!")])
def validate_writer(self, writer):
if len(writer.data) <= 1:
raise ValidationError(lazy_gettext("Very short"))
ฟังก์ชั่นการใช้งานและตัวแปร
- คำสั่งในการใช้งาน
'MASTER_colsizes': ความกว้างของช่องข้อมมูล(ทุกคอลัมน์ที่อยู่ในdatabaseต้องมีช่องข้อมูลเป็นของตัวเองทั้งหมด แต่สามารถใส่ค่าเป็น 0 ได้ หากไม่อยากให้ผู้ใช้เห็น)'MASTER_colnames': หัวคอลัมน์ที่ผู้ใช้เห็น(ไม่จำเป็นต้องตรงกับที่อยู่ในdatabase)'MASTER_coltypes': ชนิดของช่องข้อมูล'MASTER_choicecols': กำหนดช่องข้อมูลที่จะทำเป็นDrop Drown'MASTER_choices': ข้อมูลที่อยู่ใน Choices'MASTER_schfill': กำหนดช่องข้อมูลที่จะใช้ค้นหาโดยการขึ้นหน้าต่างใหม่'class SubForm': ไปดูที่ Form
HTML.py
มีส่วนประกอบที่สำคัญดังนี้
{% import "cunei_forms.html" as CuneiForms with context %}
{% import "cunei_tables.html" as CuneiTables with context %}
{% import "cunei_modals.html" as CuneiModals with context %}
{% extends "layout.html" %}
{% block content %}
{{ CuneiTables.general_table_block(table, block_height) }}
{{ CuneiTables.general_table_tail(table) }}
{% endblock content %}
{% block jvs %}
{% endblock jvs %}
'block content': ส่วนที่แสดงเนื้อหา ในที่นี้เนื้อหาจะเป็นการแสดง Table'block jvs': เป็นส่วนที่การแสดงเนื้อหาjavascript
routes.py
วิธีตรง
@cunei_pos.route("/anime", methods=["GET", "POST"])
def anime():
table = Anime_Table(prefix="test_table", perm_bit=4, editable=True,
populate_route=url_for("cunei_pos.test_table", fetch="yes"),
post_route=url_for("cunei_pos.test_table", is_submit="yes"))
publisher_tb = Publisher_Table(prefix="PublisherTable", searchable=True, editable=True,
populate_route=url_for("cunei_pos.publisher", fetch="yes"),
post_route=url_for("cunei_pos.publisher", is_submit="yes"),
in_modal="ModalPublisher", modal_head=lazy_gettext("Choose Publisher"),
perm_bit=4)
table.sch_tbs = [publisher_tb]
dbpath = path.join(session["company"]["datadir"], "table", "anime.db")
db = SqliteExtDatabase(dbpath, pragmas={"journal_mode":"wal"})
if request.args.get("fetch", False) == "yes":
if request.form.get("mode") == "qsch":
pass
if request.form.get("mode") == "ncol":
pass
if request.form.get("mode") == "sel":
pass
if request.args.get("is_submit", False) == "yes":
db_mod_code, new_entry, resp_dict = gen_dbcommit_resp("table", "anime", Anime,
table.form, 4, suppress_success_msg=True)
if db_mod_code == 4:
resp_dict["entry"] = "deleted"
elif not(new_entry is None):
resp_dict["entry"] = [getattr(new_entry, col, None) for col in table.cnames]
return jsonify(resp_dict)
return render_template("cunei_pos/test_table.html", table=table)
- ตัวแปร
'prefix': ชื่อที่ใช้ตั้งเป็นIDของTable'perm_bit': สิทธิ์ในการเข้าถึงหน้านี้- - 4 สามารถ add/edit/delete
- - 3 สามารถ add/edit
- - 2 สามารถ add
- - 1 สามารถ look
'editable': เปิดให้มีการแก้ไข้ตารางได้'populate_route': เปิดให้มีการrefetchหน้าใหม่- - argumentี่ท1: สดงถึงหน้าที่ต้องการให้มีการrefetch
- - argumentที่2: เป็นคำสั่งยืนยันการrefetch
'post_route': เรียกใช้mode Submit บันทึกลงdatabase- - argumentี่ท1: แสดงถึงหน้าที่ต้องการให้มีการบันทึก
- - argumentี่ท2: เป็นคำสั่งยืนยันการบันทึก
วิธีลัด
@cunei_pos.route("/anime", methods=["GET", "POST"])
def anime():
table = Anime_Table(prefix="test_table", suppress_copy=False, editable=True)
table.sch_tbs = [Publisher_Table(prefix="PublisherTable", perm_bit=4, editable=True,
populate_route=url_for("cunei_pos.publisher", fetch="yes"),
post_route=url_for("cunei_pos.publisher", is_submit="yes"),
in_modal="ModalPublisher", modal_head=lazy_gettext("Choose Publisher"))]
dbtype, tbname = "table", "anime"
model, module = Anime, "cunei_pos"
new_default = get_default_anime
fetch_all = anime_grab_all
return single_tb_page(dbtype, tbname, model, module, table, new_default, fetch_all, request, forced_perm=4,
in_modal="ModalPublisher", modal_head=lazy_gettext("Choose Publisher"))]