|
|
| บรรทัดที่ 816: |
บรรทัดที่ 816: |
| </syntaxhighlight> | | </syntaxhighlight> |
|
| |
|
| The key player of this pattern is the function '''{{code|gen_dbcommit_resp}}''' that handles the most repeated routines related to form validation and database commit. | | The key player of this pattern is the function '''[[Common Functions#gen_dbcommit_resp|gen_dbcommit_resp]]''' that handles the most repeated routines related to form validation and database commit. |
| '''<syntaxhighlight lang='python'>
| |
| gen_dbcommit_resp(dbtype, tbname, model, form, perm_bit=None, allowed_action=None,
| |
| suppress_success_msg=False, lock_on_add=True, head_id_col="id",
| |
| get_dict=None, skip_validation=False, month=None)
| |
| </syntaxhighlight>'''
| |
| | |
| {| style="margin-left:20px;"
| |
| |- style="vertical-align:top;"
| |
| | style="width:120px;" | '''Parameters''' || '''DO NOT''' rely on argument order beyond ''perm_bit''.
| |
| * '''dbtype''' ''(str)'': Pointer to let CuneiFox grab the correct database file. Options include {{code|lang=python|'data'}}, {{code|lang=python|'table'}}, {{code|lang=python|'report'}}, and {{code|lang=python|'temp'}}.
| |
| * '''tbname''' ''(str)'': The database file name (without file extension).
| |
| * '''model''' ''(CuneiModel)'': The model to commit to.
| |
| * '''form''' ''(FlaskForm-CuneiForm)'': The form to read the committing data from.
| |
| * '''perm_bit''' ''(int)'': The integer permission bit. This form of permission control is quite rigid and works well with single-table databases.
| |
| * '''allowed_action''' ''({{code|lang=python|{'add':<bool>, 'edit':<bool>, 'delete':<bool>} }})'': A more complicated form of permission control. This allows for more precise control. However, it cannot be read directly from a permission database, but must be interpreted from the stored integer value upon use.
| |
| * '''suppress_success_msg''' ''(bool)'': Whether to show a message on action success.
| |
| * '''lock_on_add''' ''(bool)'': Lock the document upon successful add-type commit. (This works for multi-table databases by putting the current username in the '''editting''' column of the header table.)
| |
| * '''head_id_col''' ''(str)'': The name of the field unique to each entry. (The field must be present both in the form and in the database table.)
| |
| * '''get_dict''' ''(list)'': If provided, '''{{code|new_entry}}''' is returned as a dict instead of a CuneiModel instance. (Values from field names listed in this argument are returned as their integer references. This is useful for ForeignKeyFields.)
| |
| * '''skip_validation''' ''(bool)'': Whether the form validation step is skipped. '''ONLY SET THIS IF THE FORM IS VALIDATED ELSEWHERE.'''
| |
| * '''month''' ''(date or datetime)'': (Only applies for dbtype {{code|lang=python|'data'}}) The month sub-directory of the database. If not given, the program infers the month from the session time.
| |
| |- style="vertical-align:top;"
| |
| | '''Returns''' ||
| |
| * '''db_mod_code''' ''(int or {{code|lang=python|None}})'': The returned value can be:
| |
| ** '''{{code|lang=python|None}}''': Indicate a '''permission error'''.
| |
| ** '''{{code|lang=python|2}}''': For a successful/failed '''addition''' attempt.
| |
| ** '''{{code|lang=python|3}}''': For a successful/failed '''edit''' attempt.
| |
| ** '''{{code|lang=python|4}}''': For a successful/failed '''deletion''' attempt.
| |
| * '''new_entry''' ''(CuneiModel instance or dict or {{code|lang=python|None}})'': CuneiModel instance or a dict representing the successfully added/edited entry. Returned {{code|lang=python|None}} for a failed attempt.
| |
| * '''resp_dict''' ''(dict or int)'':
| |
| ** '''For a successful/failed attempt''': The {{code|data}} dict to be sent to the client-side. This function takes care of the '''{{code|lang=python|'data'}}''', '''{{code|lang=python|'flash_messages'}}''', and '''{{code|lang=python|'err'}}''' keys.
| |
| ** '''For a permission error''': Integer '''{{code|lang=python|403}}'''.
| |
| |}
| |
|
| |
|
| === Data Fetching Route === | | === Data Fetching Route === |