-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: Merge branch 'refactor/landscapes-shared-files' into staging
- Loading branch information
Showing
30 changed files
with
1,054 additions
and
205 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
106 changes: 106 additions & 0 deletions
106
terraso_backend/apps/core/migrations/0046_shared_resource.py
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,106 @@ | ||
# Copyright © 2023 Technology Matters | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published | ||
# by the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see https://www.gnu.org/licenses/. | ||
|
||
|
||
# Generated by Django 4.2.6 on 2023-10-13 22:10 | ||
|
||
import uuid | ||
|
||
import django.db.models.deletion | ||
import rules.contrib.models | ||
from django.conf import settings | ||
from django.db import migrations, models | ||
|
||
import apps.core.models.commons | ||
|
||
|
||
def data_entries_to_shared_resources(apps, schema_editor): | ||
ContentType = apps.get_model("contenttypes", "ContentType") | ||
LandscapeGroup = apps.get_model("core", "LandscapeGroup") | ||
SharedResource = apps.get_model("core", "SharedResource") | ||
DataEntry = apps.get_model("shared_data", "DataEntry") | ||
data_entries = DataEntry.objects.all() | ||
for data_entry in data_entries: | ||
groups = data_entry.groups.all() | ||
for group in groups: | ||
landscape_group = LandscapeGroup.objects.filter( | ||
group=group, is_default_landscape_group=True | ||
).first() | ||
if landscape_group is None: | ||
SharedResource.objects.create( | ||
source_content_type=ContentType.objects.get_for_model(data_entry), | ||
source_object_id=data_entry.id, | ||
target_content_type=ContentType.objects.get_for_model(group), | ||
target_object_id=group.id, | ||
) | ||
else: | ||
SharedResource.objects.create( | ||
source_content_type=ContentType.objects.get_for_model(data_entry), | ||
source_object_id=data_entry.id, | ||
target_content_type=ContentType.objects.get_for_model( | ||
landscape_group.landscape | ||
), | ||
target_object_id=landscape_group.landscape.id, | ||
) | ||
|
||
|
||
class Migration(migrations.Migration): | ||
dependencies = [ | ||
("contenttypes", "0002_remove_content_type_name"), | ||
("core", "0045_taxonomyterms_ecosystems_renamed"), | ||
] | ||
|
||
operations = [ | ||
migrations.CreateModel( | ||
name="SharedResource", | ||
fields=[ | ||
("deleted_at", models.DateTimeField(db_index=True, editable=False, null=True)), | ||
("deleted_by_cascade", models.BooleanField(default=False, editable=False)), | ||
( | ||
"id", | ||
models.UUIDField( | ||
default=uuid.uuid4, editable=False, primary_key=True, serialize=False | ||
), | ||
), | ||
("created_at", models.DateTimeField(auto_now_add=True)), | ||
("updated_at", models.DateTimeField(auto_now=True)), | ||
("source_object_id", models.UUIDField()), | ||
("target_object_id", models.UUIDField()), | ||
( | ||
"source_content_type", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="source_content_type", | ||
to="contenttypes.contenttype", | ||
), | ||
), | ||
( | ||
"target_content_type", | ||
models.ForeignKey( | ||
on_delete=django.db.models.deletion.CASCADE, | ||
related_name="target_content_type", | ||
to="contenttypes.contenttype", | ||
), | ||
), | ||
], | ||
options={ | ||
"ordering": ["created_at"], | ||
"get_latest_by": "-created_at", | ||
"abstract": False, | ||
}, | ||
bases=(rules.contrib.models.RulesModelMixin, models.Model), | ||
), | ||
migrations.RunPython(data_entries_to_shared_resources), | ||
] |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
# Copyright © 2023 Technology Matters | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU Affero General Public License as published | ||
# by the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU Affero General Public License for more details. | ||
# | ||
# You should have received a copy of the GNU Affero General Public License | ||
# along with this program. If not, see https://www.gnu.org/licenses/. | ||
from django.contrib.contenttypes.fields import GenericForeignKey | ||
from django.contrib.contenttypes.models import ContentType | ||
from django.db import models | ||
|
||
from apps.core.models import BaseModel | ||
|
||
|
||
class SharedResource(BaseModel): | ||
""" | ||
This model represents a shared resource. | ||
Source represents the resource that is being shared (Example: DataEntry). | ||
Target represents the resource that is receiving the shared resource (Example: Landscape). | ||
""" | ||
|
||
source = GenericForeignKey("source_content_type", "source_object_id") | ||
source_content_type = models.ForeignKey( | ||
ContentType, on_delete=models.CASCADE, related_name="source_content_type" | ||
) | ||
source_object_id = models.UUIDField() | ||
|
||
target = GenericForeignKey("target_content_type", "target_object_id") | ||
target_content_type = models.ForeignKey( | ||
ContentType, on_delete=models.CASCADE, related_name="target_content_type" | ||
) | ||
target_object_id = models.UUIDField() |
Oops, something went wrong.