From 7c34c2ba911dbc944f556defd560463fd5a1cf60 Mon Sep 17 00:00:00 2001 From: ewandor Date: Sun, 12 Feb 2023 20:12:19 +0100 Subject: [PATCH] loading draft from template --- back/app/template/models.py | 8 ++ .../views/base-view/new/new.component.html | 1 + .../app/views/base-view/new/new.component.ts | 8 +- .../app/views/contracts/contracts.module.ts | 19 ++++- .../app/views/contracts/drafts.component.ts | 84 +++++++++++++++++-- .../src/common/crud/card/card.component.ts | 16 +++- .../src/common/crud/types/dictionary.type.ts | 17 ++-- 7 files changed, 130 insertions(+), 23 deletions(-) diff --git a/back/app/template/models.py b/back/app/template/models.py index 7c76deb3..00a05e20 100644 --- a/back/app/template/models.py +++ b/back/app/template/models.py @@ -56,6 +56,7 @@ class ProvisionTemplateReference(BaseModel): class ContractTemplate(CrudDocument): name: str title: str + label: str = "" parties: List[PartyTemplate] = [] provisions: List[ProvisionTemplateReference] = Field( default=[], @@ -65,3 +66,10 @@ class ContractTemplate(CrudDocument): default=[], format="dictionary", ) + + @validator("label", always=True) + def generate_label(cls, v, values, **kwargs): + return "{} - \"{}\"".format(values['name'], unescape(remove_html_tags(values['title']))) + + class Settings(CrudDocument.Settings): + fulltext_search = ['name', 'title'] diff --git a/front/app/src/app/views/base-view/new/new.component.html b/front/app/src/app/views/base-view/new/new.component.html index 1b31f652..af05054f 100644 --- a/front/app/src/app/views/base-view/new/new.component.html +++ b/front/app/src/app/views/base-view/new/new.component.html @@ -1,6 +1,7 @@ \ No newline at end of file diff --git a/front/app/src/app/views/base-view/new/new.component.ts b/front/app/src/app/views/base-view/new/new.component.ts index 143d7f85..b80e76a9 100644 --- a/front/app/src/app/views/base-view/new/new.component.ts +++ b/front/app/src/app/views/base-view/new/new.component.ts @@ -6,11 +6,11 @@ import {FlashmessagesService} from "../../../layout/flashmessages/flashmessages. templateUrl: 'new.component.html' }) export class BaseCrudNewComponent { - @Input() resource: string | undefined; - @Input() schema: string | undefined; - @Input() model = {}; + @Input() resource: string | undefined; + @Input() schema: string | undefined; + @Input() model: {} = {}; constructor( - public flashService: FlashmessagesService + public flashService: FlashmessagesService ) {} } \ No newline at end of file diff --git a/front/app/src/app/views/contracts/contracts.module.ts b/front/app/src/app/views/contracts/contracts.module.ts index 833e1282..d65fc7a6 100644 --- a/front/app/src/app/views/contracts/contracts.module.ts +++ b/front/app/src/app/views/contracts/contracts.module.ts @@ -3,21 +3,32 @@ import { NgModule } from '@angular/core'; import { BaseViewModule } from "../base-view/base-view.module"; import { ContractsRoutingModule } from './contracts-routing.module'; -import { DraftCardComponent, DraftListComponent, DraftNewComponent } from "./drafts.component"; - +import { DraftCardComponent, DraftListComponent, DraftNewComponent, DraftNewFormComponent } from "./drafts.component"; +import { FormlyModule } from "@ngx-formly/core"; +import { FormlyBootstrapModule } from "@ngx-formly/bootstrap"; +import { ForeignkeyTypeComponent } from "@common/crud/types/foreignkey.type"; +import { CrudService } from "@common/crud/crud.service"; @NgModule({ imports: [ CommonModule, BaseViewModule, - ContractsRoutingModule + ContractsRoutingModule, + FormlyModule.forRoot({ + types: [ + { name: 'foreign-key', component: ForeignkeyTypeComponent } + ] + }), + FormlyBootstrapModule, ], declarations: [ DraftListComponent, DraftNewComponent, DraftCardComponent, - ] + DraftNewFormComponent + ], + providers: [CrudService] }) export class ContractsModule { } diff --git a/front/app/src/app/views/contracts/drafts.component.ts b/front/app/src/app/views/contracts/drafts.component.ts index 89d53718..8aecd863 100644 --- a/front/app/src/app/views/contracts/drafts.component.ts +++ b/front/app/src/app/views/contracts/drafts.component.ts @@ -1,26 +1,92 @@ -import { Component } from '@angular/core'; +import { Component, Input, OnInit } from '@angular/core'; +import { FormlyFieldConfig } from "@ngx-formly/core"; +import { FormGroup} from "@angular/forms"; +import { CrudFormlyJsonschemaService } from "@common/crud/crud-formly-jsonschema.service"; +import { CrudService } from "@common/crud/crud.service"; export class BaseEntitiesComponent { - protected resource: string = "contract-draft"; - protected schema: string = "ContractDraft"; + protected resource: string = "contract-draft"; + protected schema: string = "ContractDraft"; } @Component({ - templateUrl: '../base-view/templates/list.template.html' + templateUrl: '../base-view/templates/list.template.html' }) export class DraftListComponent extends BaseEntitiesComponent { - columns = []; + columns = []; } @Component({ - templateUrl: '../base-view/templates/new.template.html' + selector: 'draft-new-form', + template: `` }) -export class DraftNewComponent extends BaseEntitiesComponent { +export class DraftNewFormComponent extends BaseEntitiesComponent { + @Input() value: {} = {}; } @Component({ - templateUrl: '../base-view/templates/card.template.html' + selector: 'draft-new', + template: ` + + + ` +}) +export class DraftNewComponent extends BaseEntitiesComponent implements OnInit { + templateModel: {} = {}; + temaplateFormfields: FormlyFieldConfig[] = []; + temaplateForm: FormGroup = new FormGroup({}); + + fieldJson = { + type: "object", + properties: { + template_id: { + type: "string", + title: "Find a template", + foreignKey: { + reference: { + resource: "template/contract", + schema: "ContractTemplate" + } + } + } + }, + } + + constructor(private formlyJsonschema: CrudFormlyJsonschemaService, + private crudService: CrudService + ) { + super(); + } + + ngOnInit() { + // @ts-ignore + this.temaplateFormfields = [this.formlyJsonschema.toFieldConfig(this.fieldJson)]; + this.temaplateForm.valueChanges.subscribe((values) => { + if (values.template_id !== undefined) { + this.crudService.get("template/contract", values.template_id).subscribe((templateModel) => { + delete templateModel._id; + delete templateModel.created_at; + delete templateModel.updated_at; + delete templateModel.label; + const provisions = []; + for (const p of templateModel.provisions) { + provisions.push({ + provision: { type: "template", provision_template_id: p.provision_template_id} + }) + } + templateModel.provisions = provisions; + this.templateModel = templateModel; + }); + } else { + this.templateModel = {}; + } + }) + } +} + +@Component({ + templateUrl: '../base-view/templates/card.template.html' }) export class DraftCardComponent extends BaseEntitiesComponent{ -} \ No newline at end of file +} diff --git a/front/app/src/common/crud/card/card.component.ts b/front/app/src/common/crud/card/card.component.ts index e054237c..1b8b3b09 100644 --- a/front/app/src/common/crud/card/card.component.ts +++ b/front/app/src/common/crud/card/card.component.ts @@ -17,9 +17,22 @@ export class CardComponent implements OnInit { @Input() resource: string | undefined; @Input() resource_id: string | null = null; @Input() schema: string | undefined; - @Input() model = {}; @Input() is_modal: Boolean = false; + private _model: {} = {}; + + @Input() set model(value: any) { + this._model = value; + if (Object.keys(this.form.controls).length) { + delete value._id; + this.form.patchValue(value); + } + } + + get model(): any { + return this._model; + } + @Output() resourceCreated: EventEmitter = new EventEmitter(); @Output() resourceUpdated: EventEmitter = new EventEmitter(); @Output() resourceDeleted: EventEmitter = new EventEmitter(); @@ -91,6 +104,7 @@ export class CardComponent implements OnInit { error: (err) => this.error.emit("Error creating the entity:" + err) }); } else { + model._id = this.resource_id; this.crudService.update(this.resource!, model).subscribe( { next: (model: any) => { this.model = model; diff --git a/front/app/src/common/crud/types/dictionary.type.ts b/front/app/src/common/crud/types/dictionary.type.ts index d876baa9..9b49bcec 100644 --- a/front/app/src/common/crud/types/dictionary.type.ts +++ b/front/app/src/common/crud/types/dictionary.type.ts @@ -1,6 +1,6 @@ import { Component, OnInit } from '@angular/core'; -import {FieldType, FieldTypeConfig, FormlyFieldConfig, FormlyFormOptions} from '@ngx-formly/core'; -import {FormControl, FormGroup, FormArray} from "@angular/forms"; +import { FieldType, FieldTypeConfig, FormlyFieldConfig } from '@ngx-formly/core'; +import { FormGroup } from "@angular/forms"; import {DictionaryService} from "./dictionary.service"; import {JSONSchema7} from "json-schema"; import {FormlyJsonschema} from "@ngx-formly/core/json-schema"; @@ -58,7 +58,10 @@ export class DictionaryTypeComponent extends FieldType implemen this.parameterFields = [this.formlyJsonschema.toFieldConfig( this.getFormSchema(parameters) )]; - this.parameterForm.setValue(this.parameterModel, {emitEvent: false}); + + if (Object.keys(this.parameterForm.controls).length) { + this.parameterForm.setValue(this.parameterModel, {emitEvent: false}); + } }); this.parameterForm.valueChanges.subscribe((values) => { @@ -70,6 +73,12 @@ export class DictionaryTypeComponent extends FieldType implemen } this.formControl.setValue(formValue) }) + + this.formControl.valueChanges.subscribe((values) => { + for (const entry of values) { + this.parameterModel[entry.key] = entry.value; + } + }) } getFormSchema(properties: string[]) { @@ -92,6 +101,4 @@ export class DictionaryTypeComponent extends FieldType implemen return schema } - - }