-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
### Changelog: * Feature(backend, frontend): Added ability to pass types to DependFromFkField. * Chore(backend): Requirements version bump. See merge request vst/vst-utils!616
- Loading branch information
Showing
15 changed files
with
44 additions
and
70 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 13 additions & 45 deletions
58
frontend_src/vstutils/fields/dynamic/DependFromFkField.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,40 @@ | ||
import { BaseField } from '@/vstutils/fields/base'; | ||
import { mergeDeep } from '@/vstutils/utils'; | ||
import DependFromFkFieldMixin from './DependFromFkFieldMixin.vue'; | ||
import type { Field, FieldOptions, FieldXOptions } from '@/vstutils/fields/base'; | ||
import type { InnerData, RepresentData } from '@/vstutils/utils'; | ||
import type { FieldOptions } from '@/vstutils/fields/base'; | ||
import type { DynamicFieldXOptions } from './DynamicField'; | ||
import { DynamicField } from './DynamicField'; | ||
|
||
interface XOptions extends FieldXOptions { | ||
interface XOptions extends DynamicFieldXOptions { | ||
field: string; | ||
field_attribute: string; | ||
callback?: (data: Record<string, unknown>) => Record<string, unknown>; | ||
} | ||
|
||
export class DependFromFkField extends BaseField<unknown, unknown, XOptions> { | ||
export class DependFromFkField extends DynamicField<XOptions> { | ||
dependField: string; | ||
dependFieldAttribute: string; | ||
|
||
/** | ||
* Function that is used to customize real field options | ||
*/ | ||
callback?: (data: Record<string, unknown>) => Record<string, unknown>; | ||
|
||
constructor(options: FieldOptions<XOptions, unknown>) { | ||
super(options); | ||
|
||
this.dependField = this.props.field; | ||
this.dependFieldAttribute = this.props.field_attribute; | ||
this.callback = this.props.callback; | ||
} | ||
|
||
static get mixins() { | ||
return [DependFromFkFieldMixin]; | ||
} | ||
|
||
toInner(data: RepresentData) { | ||
return this.getRealField(data).toInner(data); | ||
} | ||
|
||
toRepresent(data: InnerData) { | ||
return this.getRealField(data).toRepresent(data); | ||
// eslint-disable-next-line @typescript-eslint/no-unsafe-return | ||
return [DependFromFkFieldMixin as any]; | ||
} | ||
|
||
validateValue(data: RepresentData) { | ||
return this.getRealField(data).validateValue(data); | ||
} | ||
|
||
getFieldByFormat(format: Record<string, unknown> | string, data: Record<string, unknown>): Field { | ||
let callback_opt: Record<string, unknown> = {}; | ||
if (this.callback) { | ||
callback_opt = this.callback(data); | ||
} | ||
const realField = this.app.fieldsResolver.resolveField( | ||
mergeDeep({ format, callback_opt }), | ||
this.name, | ||
); | ||
if (!realField.model && this.model) { | ||
realField.model = this.model; | ||
} | ||
if (this.app.store.page) { | ||
realField.prepareFieldForView(this.app.store.page.view.path); | ||
} | ||
return realField; | ||
} | ||
|
||
getRealField(data: Record<string, unknown>): Field { | ||
_getParentValues(data: Record<string, unknown> = {}): Record<string, unknown> { | ||
const dependFromInstance = data[this.dependField] as Record<string, unknown> | undefined; | ||
const dependFieldValue = (dependFromInstance?.[this.dependFieldAttribute] || 'string') as | ||
| Record<string, unknown> | ||
| string; | ||
|
||
return this.getFieldByFormat(dependFieldValue, data); | ||
return { [this.dependFieldAttribute]: dependFieldValue }; | ||
} | ||
|
||
_getFromValue(data: Record<string, unknown>) { | ||
return this.getFieldByDefinition({ format: data[this.dependFieldAttribute] as string }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
# Packages needed for delayed jobs. | ||
celery[redis]==5.3.5 | ||
celery[redis]==5.3.6 | ||
django-celery-beat~=2.5.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
# pylint: disable=django-not-available | ||
__version__: str = '5.8.16' | ||
__version__: str = '5.8.17' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters