Created
October 30, 2019 18:09
-
-
Save jvzammit/d0e824ffe022e4bdae20b4dc52768669 to your computer and use it in GitHub Desktop.
Copy CMD
This file contains hidden or 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
| """ | |
| Copies FlaggedLead model items and associated models "back" into the | |
| Lead model and associated models. | |
| Leaves FlaggedLead model items and associated models untouched. | |
| """ | |
| from django.core.management.base import BaseCommand | |
| from django.db import transaction | |
| from lead.models import ( | |
| FlaggedLead, | |
| Lead, | |
| LeadConversion, | |
| MetaData, | |
| ) | |
| @transaction.atomic | |
| def copy_flagged_lead_to_lead(flagged_lead): | |
| lead = Lead( | |
| created=flagged_lead.created, | |
| modified=flagged_lead.modified, | |
| campaign_id=flagged_lead.campaign_id, | |
| brand_id=flagged_lead.brand_id, | |
| lead_submitted=flagged_lead.lead_submitted, | |
| lead_type=flagged_lead.lead_type, | |
| user=flagged_lead.user, | |
| validation_score=flagged_lead.validation_score, | |
| originating_lead_id=flagged_lead.originating_lead_id, | |
| status=flagged_lead.status) | |
| lead.save() | |
| for fmeta in flagged_lead.data.all(): | |
| meta = MetaData( | |
| created=fmeta.created, | |
| modified=fmeta.modified, | |
| data=fmeta.data, | |
| lead_submitted=fmeta.lead_submitted, | |
| is_duplicate=fmeta.is_duplicate, | |
| visitor=fmeta.visitor, | |
| lead=lead, | |
| inventory=fmeta.inventory) | |
| if fmeta.inventory: | |
| # it is safe to add the same inventory many times | |
| lead.leadinventory_set.add(fmeta.inventory) | |
| meta.save() | |
| for fconv in flagged_lead.conversion.all(): | |
| conv = LeadConversion( | |
| created=fconv.created, | |
| modified=fconv.modified, | |
| conversion_uuid=fconv.conversion_uuid, | |
| cookie_name=fconv.cookie_name, | |
| brand_id=fconv.brand_id, | |
| timestamp=fconv.timestamp, | |
| referrer=fconv.referrer, | |
| lead=lead) | |
| conv.save() | |
| lead.save() | |
| return lead.pk | |
| class Command(BaseCommand): | |
| help = 'Copy FlaggedLead rows into Lead rows + associated models.' | |
| def add_arguments(self, parser): | |
| parser.add_argument('batch_size', type=int) | |
| def handle(self, *args, **options): | |
| batch_size = options['batch_size'] | |
| queryset = FlaggedLead.objects.all().order_by('id')[:batch_size] | |
| for flagged_lead in queryset: | |
| copy_flagged_lead_to_lead(flagged_lead) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment