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 ref_db_name0, CuneiModel ref_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 ref_db_name1, CuneiModel ref_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, 
                                ...
                            }],
                        ...
                    ]),
                ...
            ]
  • ref_db_nameX: the name of the referenced database file.
  • ref_modelX: the model associated with a table within said database X.
  • ref_fieldXYZ: the field name actually present in the referenced table X.
  • this_model_fieldXYZ: the field name actually present in the current model that is associated with the value in ref_fieldXYZ.
  • mock_fieldXYZ: a field name not actually present in the current model, but will take on the value of ref_fieldXYZ once referencing is made.

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)
    ...
    corres_stid = IntegerField(default=0)
    ...
    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"
                                            }]])]
    class Meta:
        database = SqliteExtDatabase(None)

In this example, the static ID of our sales representative (stored as Customer.cores_stid) is referencing the Employee table at Employee.stid. Although the code and name of the representative DO NOT actually appear in the Customer table itself, one can access these value via Customer.corres_code and Customer.corres_name respectively.

Do note that in self-referencing (in this case, a customer's parent company which is yet another customer), the CuneiModel specification is simply a string 'self'.

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.