minor improvements
This commit is contained in:
34
back/app/core/models.py
Normal file
34
back/app/core/models.py
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
from beanie import Document
|
||||||
|
from pydantic import Field, validator
|
||||||
|
|
||||||
|
|
||||||
|
class CrudDocument(Document):
|
||||||
|
_id: str
|
||||||
|
created_at: datetime = Field(default=datetime.utcnow(), nullable=False)
|
||||||
|
updated_at: datetime = Field(default_factory=datetime.utcnow, nullable=False)
|
||||||
|
|
||||||
|
@validator("label", always=True, check_fields=False)
|
||||||
|
def generate_label(cls, v, values, **kwargs):
|
||||||
|
return v
|
||||||
|
|
||||||
|
class Settings:
|
||||||
|
fulltext_search = []
|
||||||
|
|
||||||
|
|
||||||
|
def text_area(*args, **kwargs):
|
||||||
|
kwargs['widget'] = {
|
||||||
|
"formlyConfig": {
|
||||||
|
"type": "textarea",
|
||||||
|
"props": {
|
||||||
|
"placeholder": "Leaving this field empty will cause formData property to be `null`",
|
||||||
|
"rows": kwargs['size'] if 'size' in kwargs else 10
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Field(*args, **kwargs)
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
@@ -1,5 +1,5 @@
|
|||||||
from beanie import PydanticObjectId
|
from beanie import PydanticObjectId
|
||||||
from beanie.operators import And, Or, RegEx, Eq
|
from beanie.operators import And, RegEx, Eq
|
||||||
|
|
||||||
from fastapi import APIRouter, HTTPException
|
from fastapi import APIRouter, HTTPException
|
||||||
from fastapi_paginate import Page, Params, add_pagination
|
from fastapi_paginate import Page, Params, add_pagination
|
||||||
@@ -18,6 +18,10 @@ def parse_sort(sort_by):
|
|||||||
return fields
|
return fields
|
||||||
|
|
||||||
|
|
||||||
|
def Or(filters):
|
||||||
|
return {'$or': filters}
|
||||||
|
|
||||||
|
|
||||||
def parse_query(query: str, model):
|
def parse_query(query: str, model):
|
||||||
if query is None:
|
if query is None:
|
||||||
return {}
|
return {}
|
||||||
@@ -40,7 +44,10 @@ def parse_query(query: str, model):
|
|||||||
|
|
||||||
and_array.append(operand)
|
and_array.append(operand)
|
||||||
|
|
||||||
|
if and_array:
|
||||||
return And(and_array) if len(and_array) > 1 else and_array[0]
|
return And(and_array) if len(and_array) > 1 else and_array[0]
|
||||||
|
else:
|
||||||
|
return {}
|
||||||
|
|
||||||
|
|
||||||
def get_crud_router(model, model_create, model_read, model_update):
|
def get_crud_router(model, model_create, model_read, model_update):
|
||||||
|
|||||||
@@ -1,11 +1,13 @@
|
|||||||
from enum import Enum
|
from enum import Enum
|
||||||
from datetime import datetime, date
|
from datetime import date, datetime
|
||||||
from typing import List, Literal, Optional
|
from typing import List, Literal, Optional
|
||||||
|
|
||||||
from pymongo import TEXT, IndexModel
|
from pymongo import TEXT, IndexModel
|
||||||
from pydantic import Field, BaseModel, validator
|
from pydantic import Field, BaseModel, validator
|
||||||
from beanie import Document, Indexed
|
from beanie import Document, Indexed
|
||||||
|
|
||||||
|
from ..core.models import CrudDocument
|
||||||
|
|
||||||
|
|
||||||
class EntityType(BaseModel):
|
class EntityType(BaseModel):
|
||||||
@property
|
@property
|
||||||
@@ -55,11 +57,10 @@ class Institution(EntityType):
|
|||||||
employees: List[Employee] = Field(default=[])
|
employees: List[Employee] = Field(default=[])
|
||||||
|
|
||||||
|
|
||||||
class Entity(Document):
|
class Entity(CrudDocument):
|
||||||
_id: str
|
|
||||||
entity_data: Individual | Corporation | Institution = Field(..., discriminator='type')
|
entity_data: Individual | Corporation | Institution = Field(..., discriminator='type')
|
||||||
address: Optional[str] = ""
|
|
||||||
label: str = None
|
label: str = None
|
||||||
|
address: Optional[str] = ""
|
||||||
|
|
||||||
@validator("label", always=True)
|
@validator("label", always=True)
|
||||||
def generate_label(cls, v, values, **kwargs):
|
def generate_label(cls, v, values, **kwargs):
|
||||||
@@ -67,16 +68,11 @@ class Entity(Document):
|
|||||||
return v
|
return v
|
||||||
return values['entity_data'].label
|
return values['entity_data'].label
|
||||||
|
|
||||||
created_at: datetime = Field(default=datetime.utcnow(), nullable=False)
|
class Settings(CrudDocument.Settings):
|
||||||
updated_at: datetime = Field(default_factory=datetime.utcnow, nullable=False)
|
fulltext_search = ['label']
|
||||||
|
|
||||||
class Settings:
|
|
||||||
bson_encoders = {
|
bson_encoders = {
|
||||||
date: lambda dt: dt if hasattr(dt, 'hour')
|
date: lambda dt: dt if hasattr(dt, 'hour')
|
||||||
else datetime(year=dt.year, month=dt.month, day=dt.day,
|
else datetime(year=dt.year, month=dt.month, day=dt.day,
|
||||||
hour=0, minute=0, second=0)
|
hour=0, minute=0, second=0)
|
||||||
}
|
}
|
||||||
|
|
||||||
fulltext_search = ['label']
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -100,7 +100,7 @@ export class ListComponent implements OnInit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
onSelect(id: string) {
|
onSelect(id: string) {
|
||||||
this.router.navigate([id], {relativeTo: this.route});
|
this.router.navigate([`../${id}`], {relativeTo: this.route});
|
||||||
}
|
}
|
||||||
|
|
||||||
get listData$() {
|
get listData$() {
|
||||||
|
|||||||
@@ -31,7 +31,7 @@ import {formatDate} from "@angular/common";
|
|||||||
[resultFormatter]="formatter"
|
[resultFormatter]="formatter"
|
||||||
[editable]="false" />
|
[editable]="false" />
|
||||||
<div class="input-group-append">
|
<div class="input-group-append">
|
||||||
<button class="btn btn-success" type="button" (click)="add(foreignModal)"><span class="cil-plus btn-icon mr-2"></span> Add</button>
|
<button class="btn btn-success" type="button" (click)="add(foreignModal)"><span class="cil-plus btn-icon mr-2"></span>Create</button>
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
<div class="input-group" *ngIf="this.hasValue()">
|
<div class="input-group" *ngIf="this.hasValue()">
|
||||||
@@ -142,11 +142,11 @@ export class ForeignkeyTypeComponent extends FieldType<FieldTypeConfig> implemen
|
|||||||
}
|
}
|
||||||
|
|
||||||
hasValue() {
|
hasValue() {
|
||||||
return this.formControl.value !== undefined;
|
return this.formControl.value !== undefined && this.formControl.value !== null;
|
||||||
}
|
}
|
||||||
|
|
||||||
add(modal: any) {
|
add(modal: any) {
|
||||||
this.modalService.open(modal);
|
this.modalService.open(modal, { size: 'xl' });
|
||||||
}
|
}
|
||||||
|
|
||||||
onResourceCreated(resource_id: string) {
|
onResourceCreated(resource_id: string) {
|
||||||
@@ -155,7 +155,7 @@ export class ForeignkeyTypeComponent extends FieldType<FieldTypeConfig> implemen
|
|||||||
}
|
}
|
||||||
|
|
||||||
see(modal: any) {
|
see(modal: any) {
|
||||||
this.modalService.open(modal);
|
this.modalService.open(modal, { size: 'xl' });
|
||||||
}
|
}
|
||||||
|
|
||||||
onResourceDeleted(resource_id: string) {
|
onResourceDeleted(resource_id: string) {
|
||||||
|
|||||||
Reference in New Issue
Block a user