Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create or assign labels during data upload #4900

Closed
wants to merge 9 commits into from
Closed
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
15 changes: 15 additions & 0 deletions seed/data_importer/match.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

from celery import shared_task
from celery.utils.log import get_task_logger
from django.apps import apps
from django.contrib.postgres.aggregates.general import ArrayAgg
from django.db import IntegrityError, transaction
from django.db.models import Subquery
Expand All @@ -31,6 +32,8 @@
PropertyAuditLog,
PropertyState,
PropertyView,
PropertyViewLabel,
StatusLabel,
TaxLotAuditLog,
TaxLotState,
TaxLotView,
Expand Down Expand Up @@ -793,6 +796,17 @@ def link_states(states, ViewClass, cycle, highest_ali, sub_progress_key, tuple_v
state.raw_access_level_instance = ali
view = state.promote(cycle=cycle)

# assign incoming labels to view
if view and state.incoming_labels:
incoming_label_names = state.incoming_labels.split(",")
for incoming_label_name in incoming_label_names:
incoming_label, _ = StatusLabel.objects.get_or_create(name=incoming_label_name, super_organization=cycle.organization)
if isinstance(view, PropertyView):
PropertyViewLabel.objects.get_or_create(statuslabel=incoming_label, propertyview=view)
elif isinstance(view, TaxLotView):
TaxLotViewLabel = apps.get_model("seed", "TaxLotView_labels")
TaxLotViewLabel.objects.get_or_create(statuslabel=incoming_label, taxlotview=view)

# link state
link_count = _link_matches([*existing_views_matches, view], cycle.organization_id, view, ViewClass)
if link_count == 0:
Expand Down Expand Up @@ -883,6 +897,7 @@ def save_state_match(state1, state2, priorities):

# Set the merged_state to merged
merged_state.merge_state = MERGE_STATE_MERGED

merged_state.save()

return merged_state
Expand Down
7 changes: 6 additions & 1 deletion seed/data_importer/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,11 @@ def map_row_chunk(ids, file_pk, source_type, prog_key, **kwargs):
if footprint_details.get("obj_field") and getattr(map_model_obj, footprint_details["obj_field"]) is None:
_store_raw_footprint_and_create_rule(footprint_details, table, org, import_file, original_row, map_model_obj)

# There was an error with a field being too long [> 255 chars].
# Store the incoming label names in state.incoming_labels. -ViewLabels will be created once a view is attatched
label_key = "Property Labels" if isinstance(map_model_obj, PropertyState) else "Tax Lot Labels"
if incoming_labels := map_model_obj.extra_data.pop(label_key, None):
map_model_obj.incoming_labels = incoming_labels

map_model_obj.save()

# if importing BuildingSync create a BuildingFile for the property
Expand Down Expand Up @@ -801,6 +805,7 @@ def _save_raw_data_chunk(chunk, file_pk, progress_key):
raw_property.source_type = source_type
raw_property.data_state = DATA_STATE_IMPORT
raw_property.organization = import_file.import_record.super_organization

raw_property.save()

if source_filename is not None:
Expand Down
22 changes: 22 additions & 0 deletions seed/migrations/0235_auto_20241211_0938.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.2.25 on 2024-12-11 17:38

from django.db import migrations, models


class Migration(migrations.Migration):
dependencies = [
("seed", "0234_transaction_goals"),
]

operations = [
migrations.AddField(
model_name="propertystate",
name="incoming_labels",
field=models.TextField(blank=True, null=True),
),
migrations.AddField(
model_name="taxlotstate",
name="incoming_labels",
field=models.TextField(blank=True, null=True),
),
]
1 change: 1 addition & 0 deletions seed/models/properties.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ class PropertyState(models.Model):
merge_state = models.IntegerField(choices=MERGE_STATE, default=MERGE_STATE_UNKNOWN, null=True)
raw_access_level_instance = models.ForeignKey(AccessLevelInstance, null=True, on_delete=models.SET_NULL)
raw_access_level_instance_error = models.TextField(null=True)
incoming_labels = models.TextField(null=True, blank=True)

jurisdiction_property_id = models.TextField(null=True, blank=True, db_collation="natural_sort")

Expand Down
1 change: 1 addition & 0 deletions seed/models/tax_lots.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ class TaxLotState(models.Model):
merge_state = models.IntegerField(choices=MERGE_STATE, default=MERGE_STATE_UNKNOWN, null=True)
raw_access_level_instance = models.ForeignKey(AccessLevelInstance, null=True, on_delete=models.SET_NULL)
raw_access_level_instance_error = models.TextField(null=True)
incoming_labels = models.TextField(null=True, blank=True)

custom_id_1 = models.CharField(max_length=255, null=True, blank=True, db_collation="natural_sort")

Expand Down
2 changes: 0 additions & 2 deletions seed/views/v3/import_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,6 @@ def mapping_results(self, request, pk=None):
if prop.raw_access_level_instance is not None:
prop_dict.update(prop.raw_access_level_instance.path)
prop_dict["raw_access_level_instance_error"] = prop.raw_access_level_instance_error

prop_dict = apply_display_unit_preferences(org, prop_dict)
property_results.append(prop_dict)

Expand Down Expand Up @@ -464,7 +463,6 @@ def map(self, request, pk=None):
if not import_file.exists():
return {"status": "error", "message": f"ImportFile {pk} does not exist"}

# return remap_data(import_file_id)
return JsonResponse(map_data(pk, remap, mark_as_done))

@swagger_auto_schema_org_query_param
Expand Down
Loading