def create_organization(author): organization_index_page = OrganizationIndexPage.objects.get() organization_name = author["organization_name"] drupal_full_name = author["drupal_full_name"] organization_exists = Organization.objects.filter( drupal_full_name=drupal_full_name, ).exists() # Avoid duplicates if not organization_exists: try: organization = Organization( title=organization_name, drupal_full_name=drupal_full_name, ) except: print("Could not create organization:", drupal_full_name) organization_index_page.add_child(instance=organization) organization_index_page.save()
def handle(self, *args, **options): with open(options["file"]) as import_file: # Get index pages for use when saving entities meeting_index_page = MeetingIndexPage.objects.get() organization_index_page = OrganizationIndexPage.objects.get() contacts = csv.DictReader(import_file) for contact in contacts: # Check for entity type among: # - Meeting # - Organization # # Contact Subtypes include # - Monthly_Meeting_Worship_Group # - Quarterly_Regional_Meeting # - Yearly_Meeting # - Worship_Group # - Quaker_Organization # - NULL contact_type = contact["Contact Subtype"].strip() # Most of the contacts are meetings. # We will need nested logic to label the meeting based on type. meeting_types = [ "Yearly_Meeting", "Quarterly_Regional_Meeting", "Monthly_Meeting_Worship_Group", "Worship_Group", ] # Organization types contains empty string # because contacts without a value # are organizations in the spreadsheet # Make sure empty string catches the contacts without subtype. organization_types = ["Quaker_Organization", ""] contact_is_meeting = contact_type in meeting_types contact_is_organization = contact_type in organization_types print(contact["Organization Name"], contact["Contact ID"]) if contact_is_meeting: # If meeting exists, update # else create new meeting meeting_exists = Meeting.objects.filter( title=contact["Organization Name"], ).exists() meeting_type = determine_meeting_type(contact_type) if meeting_exists: meeting = Meeting.objects.get( title=contact["Organization Name"], ) meeting.meeting_type = meeting_type meeting.website = contact["Website"] meeting.phone = contact["Phone"] meeting.email = contact["Email"] meeting.civicrm_id = contact["Contact ID"] meeting.save() else: meeting = Meeting( title=contact["Organization Name"], civicrm_id=contact["Contact ID"], meeting_type=meeting_type, website=contact["Website"], phone=contact["Phone"], email=contact["Email"], ) meeting_index_page.add_child(instance=meeting) meeting_index_page.save() elif contact_is_organization: # If organization exists, update # else create new organization organization_exists = Organization.objects.filter( title=contact["Organization Name"], ).exists() if organization_exists: print("organization exists") organization = Organization.objects.get( title=contact["Organization Name"], ) organization.civicrm_id = contact["Contact ID"] organization.save() else: print("new organization") organization = Organization( title=contact["Organization Name"], civicrm_id=contact["Contact ID"], ) organization_index_page.add_child( instance=organization) organization_index_page.save() else: print(f"Contact type: '{contact_type}'") self.stdout.write("All done!")
def execute(srv, usr, pwd): # server = Server('srv1.test.ru') server = Server(srv) conn = Connection(server, auto_bind=True, user=usr, password=pwd) #'userAccountControl' can have these values: 512 Enabled Account; 514 Disabled Account; # conn.search('ou=АСУ,dc=test,dc=ru','(&(objectClass=person)(userAccountControl=66048))',SUBTREE, attributes =['cn','proxyAddresses','department','sAMAccountName', 'displayName', 'telephoneNumber', 'ipPhone', 'streetAddress','title','manager','objectGUID','company','lastLogon']) conn.search( "dc=test,dc=ru", "(&(objectCategory=Person)(!(UserAccountControl:1.2.840.113556.1.4.803:=2)))", SUBTREE, attributes=[ "cn", "proxyAddresses", "department", "sAMAccountName", "displayName", "telephoneNumber", "ipPhone", "streetAddress", "title", "manager", "objectGUID", "company", "lastLogon", ], ) # get or create default organization try: org = Organization.objects.get(name="Не указано") except: org = Organization(name="Не указано") org.save() # get or create default organization try: dep = Subdivision.objects.get(name="Не указано") except: dep = Subdivision(organization=org, name="Не указано") dep.save() for entry in conn.entries: print("") print(" -------------------------- ") print("entry.cn", entry.cn) # print('entry.objectGUID', entry.objectGUID) # search person in contact app try: displayName = entry.displayName except: displayName = entry.cn print("displayName", displayName) try: person_obj = Person.objects.get(ad_objectguid=entry.objectGUID) except: person_obj = None print("person_obj", person_obj) if person_obj: person_obj.name = displayName # person_obj.ad_objectguid = entry.objectGUID person_obj.save() else: person_obj = Person(name=displayName, ad_objectguid=entry.objectGUID) person_obj.save() print("Человек: ", person_obj) try: ldapinfo_obj = LdapInfo.objects.get(person=person_obj) except: ldapinfo_obj = None # add AD sAMAccountName if ldapinfo_obj: try: ldapinfo_obj.samaccountname = entry.sAMAccountName ldapinfo_obj.save() except: pass else: try: sAMAccountName = LdapInfo(person=person_obj, samaccountname=entry.sAMAccountName) sAMAccountName.save() except: pass # add AD lastLogon if ldapinfo_obj: try: ldapinfo_obj.lastlogon = convert_ad_timestamp(int(entry.lastLogon.value)) ldapinfo_obj.save() except: pass else: try: lastLogon = LdapInfo(person=person_obj, lastlogon=convert_ad_timestamp(int(entry.lastLogon.value))) lastLogon.save() except: pass # delete all person phones in contact app phones = Phone.objects.filter(person=person_obj) if phones: phones.delete() # add phones to contact from Active Directory user entry try: newphone = Phone(person=person_obj, number=entry.telephoneNumber) newphone.save() except: pass try: newphone = Phone(person=person_obj, number=entry.ipPhone) newphone.save() except: pass # delete all emails in contact app emails = Email.objects.filter(person=person_obj) if emails: emails.delete() # add emails to contact app from Active Directory user entry try: new_email = Email(person=person_obj, address=entry.proxyAddresses) new_email.save() except: pass # get organization try: entry_company = entry.company except: entry_company = None organization = None if entry_company: try: organization_obj = Organization.objects.get(name=entry.company) except: organization_obj = Organization(name=entry.company) organization_obj.save() else: organization_obj = org print("Организация: ", organization_obj.name) entry_company = None # get or create subdivision try: entry_subdiv = entry.department except: entry_subdiv = None subdiv = None if entry_subdiv: print("len entry.department %s: %s" % (entry.department, len(entry.department))) try: subdiv = Subdivision.objects.get(organization=organization_obj, name=entry.department) except: subdiv = Subdivision(organization=organization_obj, name=entry.department) subdiv.save() else: subdiv = dep print("Подразделение: ", subdiv.name) entry_subdiv = None # get person position try: position = entry.title except: position = None print("Должность", position) # create employee employee = None try: employee = Employee.objects.get( organization=organization_obj, subdivision=subdiv, person=person_obj, position=position ) print("Найден работник") except: employee = Employee(organization=organization_obj, subdivision=subdiv, person=person_obj, position=position) employee.save() print("Создан работник")
def execute(srv, usr, pwd): #server = Server('srv1.test.ru') server = Server(srv) #conn = Connection(server,auto_bind=True,user='******',password='******') conn = Connection(server,auto_bind=True,user=usr,password=pwd) ''' 'userAccountControl' can have these values: 512 Enabled Account 514 Disabled Account 544 Enabled, Password Not Required 546 Disabled, Password Not Required 66048 Enabled, Password Doesn't Expire 66050 Disabled, Password Doesn't Expire 66080 Enabled, Password Doesn't Expire & Not Required 66082 Disabled, Password Doesn't Expire & Not Required 262656 Enabled, Smartcard Required 262658 Disabled, Smartcard Required 262688 Enabled, Smartcard Required, Password Not Required 262690 Disabled, Smartcard Required, Password Not Required 328192 Enabled, Smartcard Required, Password Doesn't Expire 328194 Disabled, Smartcard Required, Password Doesn't Expire 328224 Enabled, Smartcard Required, Password Doesn't Expire & Not Required 328226 Disabled, Smartcard Required, Password Doesn't Expire & Not Required ''' #conn.search('ou=АСУ,dc=test,dc=ru','(&(objectClass=person)(userAccountControl=66048))',SUBTREE, attributes =['cn','proxyAddresses','department','sAMAccountName', 'displayName', 'telephoneNumber', 'ipPhone', 'streetAddress','title','manager','objectGUID','company','lastLogon']) #conn.search('dc=test,dc=ru','(&(objectCategory=Person)(!(UserAccountControl:1.2.840.113556.1.4.803:=2)))',SUBTREE, attributes =['cn','proxyAddresses','department','sAMAccountName', 'displayName', 'telephoneNumber', 'ipPhone', 'streetAddress','title','manager','objectGUID','company','lastLogon']) conn.search('dc=test,dc=ru','(&(objectCategory=Person))',SUBTREE, attributes =['cn','proxyAddresses','department','sAMAccountName', 'displayName', 'telephoneNumber', 'ipPhone', 'streetAddress','title','manager','objectGUID','company','lastLogonTimeStamp','userAccountControl']) #get or create default organization try: org = Organization.objects.get(name="Не указано") except: org = Organization(name = 'Не указано') org.save() #get or create default organization try: dep = Subdivision.objects.get(name="Не указано") except: dep = Subdivision(organization = org, name="Не указано") dep.save() out_file = 'import_cont_from_ad_out.txt' try: os.remove(out_file) except OSError: pass debug_file = open(out_file, 'w', encoding="utf-8") debug_file.write("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" %('cn','proxyAddresses','department','sAMAccountName', 'displayName', 'telephoneNumber', 'ipPhone', 'streetAddress','title','manager','objectGUID','company','lastLogon','AD-User-Account-Disabled')) for entry in conn.entries: try: out1 = entry.cn except: out1 = 'None' try: out2 = entry.proxyAddresses except: out2 = 'None' try: out3 = entry.department except: out3 = 'None' try: out4 = entry.sAMAccountName except: out4 = 'None' try: out5 = entry.displayName except: out5 = 'None' try: out6 = entry.telephoneNumber except: out6 = 'None' try: out7 = entry.ipPhone except: out7 = 'None' try: out8 = entry.streetAddress except: out8 = 'None' try: out9 = entry.title except: out9 = 'None' try: out10 = entry.manager except: out10 = 'None' try: out11 = entry.objectGUID except: out11 = 'None' try: out12 = entry.company except: out12 = 'None' try: out13 = convert_ad_timestamp(int(entry.lastLogonTimeStamp.value)) #out13 = entry.lastLogonTimeStamp except: out13 = 'None' try: out14 = entry.userAccountControl except: out14 = 'None' debug_file.write("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" %(out1, out2, out3, out4, out5, out6, out7, out8, out9, out10, out11, out12, out13, out14)) print('') print(' -------------------------- ') print('entry.cn', entry.cn) #print('entry.objectGUID', entry.objectGUID) debug_file.close()
def execute(srv, usr, pwd): #server = Server('srv1.test.ru') server = Server(srv) #conn = Connection(server,auto_bind=True,user='******',password='******') conn = Connection(server, auto_bind=True, user=usr, password=pwd) ''' 'userAccountControl' can have these values: 512 Enabled Account 514 Disabled Account 544 Enabled, Password Not Required 546 Disabled, Password Not Required 66048 Enabled, Password Doesn't Expire 66050 Disabled, Password Doesn't Expire 66080 Enabled, Password Doesn't Expire & Not Required 66082 Disabled, Password Doesn't Expire & Not Required 262656 Enabled, Smartcard Required 262658 Disabled, Smartcard Required 262688 Enabled, Smartcard Required, Password Not Required 262690 Disabled, Smartcard Required, Password Not Required 328192 Enabled, Smartcard Required, Password Doesn't Expire 328194 Disabled, Smartcard Required, Password Doesn't Expire 328224 Enabled, Smartcard Required, Password Doesn't Expire & Not Required 328226 Disabled, Smartcard Required, Password Doesn't Expire & Not Required ''' #conn.search('ou=АСУ,dc=test,dc=ru','(&(objectClass=person)(userAccountControl=66048))',SUBTREE, attributes =['cn','proxyAddresses','department','sAMAccountName', 'displayName', 'telephoneNumber', 'ipPhone', 'streetAddress','title','manager','objectGUID','company','lastLogon']) #conn.search('dc=test,dc=ru','(&(objectCategory=Person)(!(UserAccountControl:1.2.840.113556.1.4.803:=2)))',SUBTREE, attributes =['cn','proxyAddresses','department','sAMAccountName', 'displayName', 'telephoneNumber', 'ipPhone', 'streetAddress','title','manager','objectGUID','company','lastLogon']) conn.search('dc=test,dc=ru', '(&(objectCategory=Person))', SUBTREE, attributes=[ 'cn', 'proxyAddresses', 'department', 'sAMAccountName', 'displayName', 'telephoneNumber', 'ipPhone', 'streetAddress', 'title', 'manager', 'objectGUID', 'company', 'lastLogonTimeStamp', 'userAccountControl' ]) #get or create default organization try: org = Organization.objects.get(name="Не указано") except: org = Organization(name='Не указано') org.save() #get or create default organization try: dep = Subdivision.objects.get(name="Не указано") except: dep = Subdivision(organization=org, name="Не указано") dep.save() out_file = 'import_cont_from_ad_out.txt' try: os.remove(out_file) except OSError: pass debug_file = open(out_file, 'w', encoding="utf-8") debug_file.write( "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % ('cn', 'proxyAddresses', 'department', 'sAMAccountName', 'displayName', 'telephoneNumber', 'ipPhone', 'streetAddress', 'title', 'manager', 'objectGUID', 'company', 'lastLogon', 'AD-User-Account-Disabled')) for entry in conn.entries: try: out1 = entry.cn except: out1 = 'None' try: out2 = entry.proxyAddresses except: out2 = 'None' try: out3 = entry.department except: out3 = 'None' try: out4 = entry.sAMAccountName except: out4 = 'None' try: out5 = entry.displayName except: out5 = 'None' try: out6 = entry.telephoneNumber except: out6 = 'None' try: out7 = entry.ipPhone except: out7 = 'None' try: out8 = entry.streetAddress except: out8 = 'None' try: out9 = entry.title except: out9 = 'None' try: out10 = entry.manager except: out10 = 'None' try: out11 = entry.objectGUID except: out11 = 'None' try: out12 = entry.company except: out12 = 'None' try: out13 = convert_ad_timestamp(int(entry.lastLogonTimeStamp.value)) #out13 = entry.lastLogonTimeStamp except: out13 = 'None' try: out14 = entry.userAccountControl except: out14 = 'None' debug_file.write( "%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n" % (out1, out2, out3, out4, out5, out6, out7, out8, out9, out10, out11, out12, out13, out14)) print('') print(' -------------------------- ') print('entry.cn', entry.cn) #print('entry.objectGUID', entry.objectGUID) debug_file.close()
def handle(self, *args, **options): with open(options["file"]) as import_file: # Get index pages for use when saving entities meeting_index_page = MeetingIndexPage.objects.get() organization_index_page = OrganizationIndexPage.objects.get() person_index_page = PersonIndexPage.objects.get() authors = csv.DictReader(import_file) for author in authors: # Check for entity type among: # - Meeting # - Organization # - Person # with the condition to check for corrections to person names author_is_meeting = author["meeting_name"] != "" author_is_organization = author["organization_name"] != "" author_is_person = (author["family_name"] != "" or author["given_name"] != "" or author["corrected_family_name"] != "" or author["corrected_given_name"] != "") if author_is_meeting: meeting_exists = Meeting.objects.filter( title=author["meeting_name"], ).exists() # Don't create duplicate meetings if not meeting_exists: meeting = Meeting( title=author["meeting_name"], drupal_full_name=author["drupal_full_name"], ) meeting_index_page.add_child(instance=meeting) meeting_index_page.save() elif author_is_organization: organization_exists = Organization.objects.filter( title=author["organization_name"], ).exists() # Avoid duplicates if not organization_exists: organization = Organization( title=author["organization_name"], drupal_full_name=author["drupal_full_name"], ) organization_index_page.add_child( instance=organization) organization_index_page.save() elif author_is_person: author_name_corrected = ( author["corrected_family_name"] != "" or author["corrected_given_name"] != "") if author_name_corrected: given_name = author["corrected_given_name"] family_name = author["corrected_family_name"] else: given_name = author["given_name"] family_name = author["family_name"] person_exists = Person.objects.filter( given_name=given_name, family_name=family_name, ).exists() # Avoid duplicates if not person_exists: person = Person( given_name=given_name, family_name=family_name, drupal_full_name=author["drupal_full_name"], ) person_index_page.add_child(instance=person) person_index_page.save() else: print("unknown") self.stdout.write("All done!")
def execute(srv, usr, pwd): #server = Server('srv1.test.ru') server = Server(srv) conn = Connection(server, auto_bind=True, user=usr, password=pwd) #'userAccountControl' can have these values: 512 Enabled Account; 514 Disabled Account; #conn.search('ou=АСУ,dc=test,dc=ru','(&(objectClass=person)(userAccountControl=66048))',SUBTREE, attributes =['cn','proxyAddresses','department','sAMAccountName', 'displayName', 'telephoneNumber', 'ipPhone', 'streetAddress','title','manager','objectGUID','company','lastLogon']) conn.search( 'dc=test,dc=ru', '(&(objectCategory=Person)(!(UserAccountControl:1.2.840.113556.1.4.803:=2)))', SUBTREE, attributes=[ 'cn', 'proxyAddresses', 'department', 'sAMAccountName', 'displayName', 'telephoneNumber', 'ipPhone', 'streetAddress', 'title', 'manager', 'objectGUID', 'company', 'lastLogon' ]) #get or create default organization try: org = Organization.objects.get(name="Не указано") except: org = Organization(name='Не указано') org.save() #get or create default organization try: dep = Subdivision.objects.get(name="Не указано") except: dep = Subdivision(organization=org, name="Не указано") dep.save() for entry in conn.entries: print('') print(' -------------------------- ') print('entry.cn', entry.cn) #print('entry.objectGUID', entry.objectGUID) #search person in contact app try: displayName = entry.displayName except: displayName = entry.cn print('displayName', displayName) try: person_obj = Person.objects.get(ad_objectguid=entry.objectGUID) except: person_obj = None print('person_obj', person_obj) if person_obj: person_obj.name = displayName #person_obj.ad_objectguid = entry.objectGUID person_obj.save() else: person_obj = Person(name=displayName, ad_objectguid=entry.objectGUID) person_obj.save() print('Человек: ', person_obj) try: ldapinfo_obj = LdapInfo.objects.get(person=person_obj) except: ldapinfo_obj = None #add AD sAMAccountName if ldapinfo_obj: try: ldapinfo_obj.samaccountname = entry.sAMAccountName ldapinfo_obj.save() except: pass else: try: sAMAccountName = LdapInfo(person=person_obj, samaccountname=entry.sAMAccountName) sAMAccountName.save() except: pass #add AD lastLogon if ldapinfo_obj: try: ldapinfo_obj.lastlogon = convert_ad_timestamp( int(entry.lastLogon.value)) ldapinfo_obj.save() except: pass else: try: lastLogon = LdapInfo(person=person_obj, lastlogon=convert_ad_timestamp( int(entry.lastLogon.value))) lastLogon.save() except: pass #delete all person phones in contact app phones = Phone.objects.filter(person=person_obj) if phones: phones.delete() #add phones to contact from Active Directory user entry try: newphone = Phone(person=person_obj, number=entry.telephoneNumber) newphone.save() except: pass try: newphone = Phone(person=person_obj, number=entry.ipPhone) newphone.save() except: pass #delete all emails in contact app emails = Email.objects.filter(person=person_obj) if emails: emails.delete() #add emails to contact app from Active Directory user entry try: new_email = Email(person=person_obj, address=entry.proxyAddresses) new_email.save() except: pass #get organization try: entry_company = entry.company except: entry_company = None organization = None if entry_company: try: organization_obj = Organization.objects.get(name=entry.company) except: organization_obj = Organization(name=entry.company) organization_obj.save() else: organization_obj = org print('Организация: ', organization_obj.name) entry_company = None #get or create subdivision try: entry_subdiv = entry.department except: entry_subdiv = None subdiv = None if entry_subdiv: print("len entry.department %s: %s" % (entry.department, len(entry.department))) try: subdiv = Subdivision.objects.get(organization=organization_obj, name=entry.department) except: subdiv = Subdivision(organization=organization_obj, name=entry.department) subdiv.save() else: subdiv = dep print('Подразделение: ', subdiv.name) entry_subdiv = None #get person position try: position = entry.title except: position = None print('Должность', position) #create employee employee = None try: employee = Employee.objects.get(organization=organization_obj, subdivision=subdiv, person=person_obj, position=position) print('Найден работник') except: employee = Employee(organization=organization_obj, subdivision=subdiv, person=person_obj, position=position) employee.save() print('Создан работник')