Created
September 13, 2018 16:57
-
-
Save chisler/ea474087d56fcfc028adff73356f4b8e to your computer and use it in GitHub Desktop.
Copy BB contacts from one agency -> another of another employer.
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
from django.db import transaction | |
@transaction.atomic | |
def copy_contacts_from_agency_to_another_in_another_employer(old_agency_id, new_agency_id, copy=False): | |
from yunojuno.apps.profiles.models import AgencyProfile | |
from yunojuno.apps.core.utils.timezone import tz_now | |
old_agency = AgencyProfile.objects.get(id=old_agency_id) | |
new_agency = AgencyProfile.objects.get(id=new_agency_id) | |
new_employer = new_agency.employer | |
owned_by_recruiters = [] | |
populated = [] | |
employer_populated = [] | |
new_ones_created = {'employee': [], 'employer': []} | |
old_contacts = old_agency.contacts.all() | |
filtered = old_contacts.exclude(parent__freelancer_id__in=new_agency.employer.contacts.all().values_list('freelancer', flat=True)) | |
for employee_contact in filtered: | |
employer_contact = employee_contact.parent | |
if employer_contact.ownership: | |
owned_by_recruiters.append(employer_contact) | |
continue | |
populated.append(employee_contact) | |
employer_populated.append(employer_contact) | |
if copy: | |
employer_contact.id = None | |
employer_contact.pk = None | |
employer_contact.employer = new_employer | |
employer_contact.created_at = tz_now() | |
employer_contact.save() | |
# | |
employee_contact.conversation = None | |
employee_contact.id = None | |
employee_contact.pk = None | |
employee_contact.parent = employer_contact | |
employee_contact.employee = new_agency | |
employer_contact.created_at = tz_now() | |
employee_contact.save() | |
new_ones_created['employer'].append(employer_contact.id) | |
new_ones_created['employee'].append(employee_contact.id) | |
print(f'Owned by recruiters: {len(owned_by_recruiters)} : {[i.id for i in owned_by_recruiters]}\n\n') | |
print(f'Employee contacts: {len(populated)} : {[i.id for i in populated]}') | |
print(f'Employer contacts: {len(employer_populated)} : {[i.id for i in employer_populated]}') | |
print(f'New ones created: {new_ones_created}') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment