-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(django): add database operations and routes (#145)
- Loading branch information
Showing
43 changed files
with
1,371 additions
and
221 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
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,6 +1,14 @@ | ||
from django.apps import AppConfig | ||
from app.forest_admin import customize_forest | ||
from django.apps import AppConfig, apps | ||
from forestadmin.django_agent.agent import DjangoAgent | ||
|
||
|
||
class AppConfig(AppConfig): | ||
default_auto_field = "django.db.models.BigAutoField" | ||
name = "app" | ||
|
||
def ready(self) -> None: | ||
agent: DjangoAgent = apps.get_app_config("django_agent").get_agent() | ||
if agent: | ||
customize_forest(agent) | ||
agent.start() |
Empty file.
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 |
---|---|---|
@@ -0,0 +1,33 @@ | ||
from forestadmin.datasource_toolkit.context.collection_context import CollectionCustomizationContext | ||
from forestadmin.datasource_toolkit.interfaces.fields import Operator | ||
from forestadmin.datasource_toolkit.interfaces.query.aggregation import Aggregation | ||
from forestadmin.datasource_toolkit.interfaces.query.condition_tree.nodes.leaf import ConditionTreeLeaf | ||
from forestadmin.datasource_toolkit.interfaces.query.filter.unpaginated import Filter | ||
|
||
|
||
async def get_customer_spent_values(records, context: CollectionCustomizationContext): | ||
record_ids = [record["id"] for record in records] | ||
condition = Filter( | ||
{"condition_tree": ConditionTreeLeaf(field="customer_id", operator=Operator.IN, value=record_ids)} | ||
) | ||
|
||
aggregation = Aggregation( | ||
{ | ||
"operation": "Sum", | ||
"field": "amount", | ||
"groups": [ | ||
{ | ||
"field": "customer_id", | ||
} | ||
], | ||
}, | ||
) | ||
rows = await context.datasource.get_collection("Order").aggregate(context.caller, condition, aggregation) | ||
# rows = await context.datasource.get_collection("order").aggregate(context.caller, condition, aggregation) | ||
ret = [] | ||
for record in records: | ||
filtered = [*filter(lambda r: r["group"]["customer_id"] == record["id"], rows)] | ||
row = filtered[0] if len(filtered) > 0 else {} | ||
ret.append(row.get("value", 0)) | ||
|
||
return ret |
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 |
---|---|---|
@@ -0,0 +1,25 @@ | ||
from app.forest.customer import get_customer_spent_values | ||
from forestadmin.datasource_toolkit.interfaces.fields import PrimitiveType | ||
from forestadmin.django_agent.agent import DjangoAgent | ||
|
||
|
||
def customize_forest(agent: DjangoAgent): | ||
agent.customize_collection("Cart").add_field( | ||
"customer_id", | ||
{ | ||
"dependencies": ["order:customer_id"], | ||
"column_type": PrimitiveType.NUMBER, | ||
"get_values": lambda records, context: [rec["order"]["customer_id"] for rec in records], | ||
}, | ||
).emulate_field_filtering("customer_id") | ||
agent.customize_collection("Customer").add_field( | ||
"total_spending", | ||
{"column_type": PrimitiveType.NUMBER, "dependencies": ["id"], "get_values": get_customer_spent_values}, | ||
).add_one_to_many_relation("smart_carts", "Cart", "customer_id").add_many_to_many_relation( | ||
# relations | ||
"smart_billing_addresses", | ||
"Address", | ||
"Order", | ||
"customer_id", | ||
"billing_address_id", | ||
) |
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
Oops, something went wrong.