CuneiModel

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

The main reason for the existence of CuneiModel class, frankly, is to redirect the Metadata class from the Peewee native Metadata to CuneiFox's own ThreadSafeMetadata. However, since the opportunity presented itself, a few additional functions were also written for the class as well.

Define

Defining a CuneiModel is essentially similar to declaring a normal Peewee Model. Do refer to the official documentation. Below is the CuneiModel for product unit as an example:

class Unit(CuneiModel):
    id = AutoIncrementField()
    stid = IntegerField(unique=True)
    code = CharField(unique=True)
    name = CharField(default="")
    class Meta:
        database = SqliteExtDatabase(None)

Cross-table references

CuneiFox is designed in a way that all tables do not necessarily reside in the same database file. Thus, cross-table references require a little more work. The specifications for cross-table references are set in the variable CROSS_TBS in this format:

CROSS_TBS = [
                (str db_name0, CuneiModel model0,
                    [
                        [[str ref_field000, str this_model_field000], {str ref_field001:str mock_field001, str ref_field002: str mock_field002, ...}],
                        [[str ref_field010, str this_model_field010], {str ref_field011:str mock_field011, str ref_field012: str mock_field012, ...}],
                        ...
                    ]),
                (str db_name1, CuneiModel model1,
                    [
                        [[str ref_field100, str this_model_field100], {str ref_field101:str mock_field101, str ref_field102: str mock_field102, ...}],
                        [[str ref_field110, str this_model_field110], {str ref_field111:str mock_field111, str ref_field112: str mock_field112, ...}],
                        ...
                    ]),
                ...
            ]
Let's take an example from the CuneiModel for customer:
class Customer(CuneiModel):
    id = AutoIncrementField()
    stid = IntegerField(unique=True)
    code = CharField(unique=True)
    name = CharField(default="")
    is_sub = BooleanField(default=False)
    main_stid = IntegerField()
    acccode_stid = IntegerField(default=0)
    ...
    address = TextField(default="")
    zip = CharField(default="")
    ...
    corres_stid = IntegerField(default=0)
    taxform_stid = IntegerField(default=0)
    whtdesc_stid = IntegerField(default=0)
    whtdesc_desc = CharField(default="")
    ...
    CROSS_TBS = [("acccode", AccCode, [[["stid", "acccode_stid"], {"code":"acccode_code", "name":"acccode_name"}]]),
                ("customer", "self", [[["stid", "main_stid"], {"code":"main_code", "name":"main_name"}]]),
                ("employee", Employee, [[["stid", "corres_stid"], {"code":"corres_code", "name":"corres_name"}]]),
                ("whttype", WhtType, [[["stid", "taxform_stid"], {"code":"taxform_code", "name":"taxform_name"}]]),
                ("whtdesc", WhtDesc, [[["stid", "whtdesc_stid"], {"code":"whtdesc_code", "taxrate":"rate"}]])]
    class Meta:
        database = SqliteExtDatabase(None)

Common Functions

printout

Peewee normal printout representatin of a Model object is only its ID. This behaviour is not useful in a lot of cases; hence, this function.

printout(self)

Parameters -
Returns A string showing the CuneiModel name and the content of the entry in a dict format.

m2d

This function is an easy way to convert an entry from a Peewee object form to a dict form.

m2d(self, int_key=[], custom_keys=[])

Parameters
  • int_key (list of strings): Field names which only integer representation is required. Intended for use with ForeignKeyFields.
  • custom_keys (list of strings): Field names to also include in the dict. See notes.

Returns A dict representation of the fed object.
Notes Without custom_keys given, the returned dict only contains fields defined in the CuneiModel. Use custom_keys, in cases where the object has been modified with added keys and such keys are also needed to be represented in the returned dict.

perm_m2d

This is the shortcut for m2d intended for use with Permission-type Object.