Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2,210 changes: 1,105 additions & 1,105 deletions src/_example/django/django_demo/.forestadmin-schema.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion src/_example/django/django_demo/app/forest/order.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ async def execute_export_json(context: ActionContext, result_builder: ResultBuil
},
{
"type": ActionFieldType.COLLECTION,
"collection_name": "Customer",
"collection_name": "app_customer",
"label": "customer",
"is_required": True,
"description": "",
Expand Down
20 changes: 10 additions & 10 deletions src/_example/django/django_demo/app/forest_admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ def customize_forest(agent: DjangoAgent):
# customize_forest_logging()

# # ## ADDRESS
agent.customize_collection("Address").add_segment("highOrderDelivery", high_delivery_address_segment).rename_field(
"country", "pays"
).add_field("full_address", address_full_name_computed("country")).rename_field(
agent.customize_collection("app_address").add_segment(
"highOrderDelivery", high_delivery_address_segment
).rename_field("country", "pays").add_field("full_address", address_full_name_computed("country")).rename_field(
"full_address", "complete_address"
).replace_field_sorting(
"full_address",
Expand Down Expand Up @@ -74,7 +74,7 @@ def customize_forest(agent: DjangoAgent):
)

# cart
agent.customize_collection("Cart").add_field(
agent.customize_collection("app_cart").add_field(
"customer_id",
ComputedDefinition(
column_type=PrimitiveType.NUMBER,
Expand All @@ -98,7 +98,7 @@ def customize_forest(agent: DjangoAgent):

# # ## CUSTOMERS
# # import field ?
agent.customize_collection("Customer").add_field(
agent.customize_collection("app_customer").add_field(
"age",
{
"column_type": PrimitiveType.NUMBER,
Expand Down Expand Up @@ -160,14 +160,14 @@ def customize_forest(agent: DjangoAgent):
).add_many_to_many_relation(
# relations
"smart_billing_addresses",
"Address",
"Order",
"app_address",
"app_order",
"customer_id",
"billing_address_id",
).add_many_to_many_relation(
"smart_delivering_addresses", "Address", "Order", "customer_id", "delivering_address_id"
"smart_delivering_addresses", "app_address", "app_order", "customer_id", "delivering_address_id"
).add_one_to_many_relation(
"smart_carts", "Cart", "customer_id"
"smart_carts", "app_cart", "customer_id"
).add_hook(
# hooks
"Before",
Expand All @@ -178,7 +178,7 @@ def customize_forest(agent: DjangoAgent):
)

# # ## ORDERS
agent.customize_collection("Order").add_segment("Pending order", pending_order_segment).add_segment(
agent.customize_collection("app_order").add_segment("Pending order", pending_order_segment).add_segment(
# segment
"Delivered order",
delivered_order_segment,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

class DjangoCollection(BaseDjangoCollection):
def __init__(self, datasource: Datasource, model: Model):
super().__init__(model.__name__, datasource)
super().__init__(model._meta.db_table, datasource)
self._model = model
schema = DjangoCollectionFactory.build(model)
self.add_fields(schema["fields"])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class DjangoCollectionFactory:
@staticmethod
def _build_one_to_many(relation: ManyToOneRel) -> Optional[OneToMany]:
return {
"foreign_collection": relation.target_field.model.__name__,
"foreign_collection": relation.target_field.model._meta.db_table,
"origin_key": relation.field.attname,
"origin_key_target": relation.field.target_field.attname,
"type": FieldType.ONE_TO_MANY,
Expand All @@ -99,7 +99,7 @@ def _build_many_to_one(relation: Union[OneToOneField, ForeignKey, ManyToOneRel])
elif isinstance(relation, ForeignKey) or isinstance(relation, OneToOneField):
foreign_key = relation.attname
return {
"foreign_collection": relation.target_field.model.__name__,
"foreign_collection": relation.target_field.model._meta.db_table,
"foreign_key": foreign_key,
"foreign_key_target": relation.target_field.attname,
"type": FieldType.MANY_TO_ONE,
Expand All @@ -108,7 +108,7 @@ def _build_many_to_one(relation: Union[OneToOneField, ForeignKey, ManyToOneRel])
@staticmethod
def _build_one_to_one(relation: OneToOneRel) -> OneToOne:
return {
"foreign_collection": relation.target_field.model.__name__,
"foreign_collection": relation.target_field.model._meta.db_table,
"origin_key": relation.field.attname,
"origin_key_target": relation.field.target_field.attname,
"type": FieldType.ONE_TO_ONE,
Expand All @@ -117,14 +117,14 @@ def _build_one_to_one(relation: OneToOneRel) -> OneToOne:
@staticmethod
def _build_many_to_many(relation: Union[ManyToManyField, ManyToManyRel]) -> ManyToMany:
kwargs: Dict[str, str] = {}
kwargs["foreign_collection"] = relation.target_field.model.__name__
kwargs["foreign_collection"] = relation.target_field.model._meta.db_table

if isinstance(relation, ManyToManyField):
remote_field = relation.remote_field
elif isinstance(relation, ManyToManyRel): # reverse relation
remote_field = relation.field.remote_field

kwargs["through_collection"] = remote_field.through.__name__
kwargs["through_collection"] = remote_field.through._meta.db_table

for field in remote_field.through._meta.get_fields():
if field.is_relation is False:
Expand Down
Empty file.
20 changes: 10 additions & 10 deletions src/datasource_django/tests/test_django_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ def test_django_datasource_should_find_all_models(self):
set([c.name for c in datasource.collections]),
set(
[
"Book",
"Person",
"Rating",
"Permission",
"Group_permissions",
"Group",
"User_groups",
"User_user_permissions",
"User",
"ContentType",
"test_app_book",
"test_app_person",
"test_app_rating",
"auth_permission",
"auth_group_permissions",
"auth_group",
"auth_user_groups",
"auth_user_user_permissions",
"auth_user",
"django_content_type",
]
),
)
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ def test_build_should_handle_one_to_one_relations(self):
self.assertEqual(
restaurant_schema["fields"]["place"],
{
"foreign_collection": "Place",
"foreign_collection": "test_app_place",
"foreign_key": "place_id",
"foreign_key_target": "id",
"type": FieldType.MANY_TO_ONE,
Expand All @@ -137,7 +137,7 @@ def test_build_should_handle_one_to_one_relations(self):
self.assertEqual(
places_schema["fields"]["restaurant"],
{
"foreign_collection": "Restaurant",
"foreign_collection": "test_app_restaurant",
"origin_key": "place_id",
"origin_key_target": "id",
"type": FieldType.ONE_TO_ONE,
Expand All @@ -155,9 +155,9 @@ def test_build_should_handle_many_to_many_relations(self):
user_schema["fields"]["groups"],
{
"type": FieldType.MANY_TO_MANY,
"foreign_collection": "Group",
"foreign_collection": "auth_group",
"foreign_relation": None,
"through_collection": "User_groups",
"through_collection": "auth_user_groups",
"origin_key": "user_id",
"origin_key_target": "id",
"foreign_key": "group_id",
Expand All @@ -169,8 +169,8 @@ def test_build_should_handle_many_to_many_relations(self):
group_schema["fields"]["user"],
{
"type": FieldType.MANY_TO_MANY,
"foreign_collection": "User",
"through_collection": "User_groups",
"foreign_collection": "auth_user",
"through_collection": "auth_user_groups",
"foreign_relation": None,
"foreign_key": "user_id",
"foreign_key_target": "id",
Expand All @@ -187,7 +187,7 @@ def test_build_should_handle_many_to_one_relations(self):
self.assertEqual(
user_groups_schema["fields"]["group"],
{
"foreign_collection": "Group",
"foreign_collection": "auth_group",
"foreign_key": "group_id",
"foreign_key_target": "id",
"type": FieldType.MANY_TO_ONE,
Expand All @@ -200,7 +200,7 @@ def test_build_should_handle_one_to_many_relations(self):
self.assertEqual(
person_schema["fields"]["books"],
{
"foreign_collection": "Book",
"foreign_collection": "test_app_book",
"origin_key": "author_id",
"origin_key_target": "id",
"type": FieldType.ONE_TO_MANY,
Expand Down
Empty file removed src/django_agent/tests/__init__.py
Empty file.