Quick Start for Developers

จาก คูนิฟ็อกซ์ วิกิ

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 หรือเนื้อหาที่ต้องการแสดง

routes.py

@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)      # If necessary.
  • ตัวแปร
    • prefix

สร้าง Form - non Database

สร้าง Form - Database