class ChildScript(Script): def Setup(self): self.WA_API = WaApiClient() while (not self.WA_API.ConnectAPI(self.config.get('api', 'key'))): time.sleep(5) self.processed_filename = "followup_processed.txt" self.discourse_api = DiscourseClient( self.config.get('discourse_api', 'site'), api_username=self.config.get('discourse_api', 'username'), api_key=self.config.get('discourse_api', 'key')) self.member_levels = [ 'Monthly', '6 months', '12 months', "Scholarship (monthly)", "Scholarship (6 months)", "Scholarship (12 months)" ] self.group_name = 'MakeICT_Members' self.member_group_id = \ self.discourse_api._get("/groups/{0}.json".format(self.group_name))['group']['id'] def SyncUser(self, d_user, wa_contacts): # print(self.discourse_api.user_emails(d_user['user']['username'])) print(d_user['username'], ':', d_user['emails'], ':', [c['Email'] for c in wa_contacts]) is_member = False is_active = False # assert wa_contacts['Email'] in d_user['user']['emails'] for contact in wa_contacts: try: is_member = contact['MembershipLevel'][ 'Name'] in self.member_levels except KeyError: print("No WA membership level found") try: is_active = contact['Status'] == 'Active' and contact[ 'MembershipEnabled'] except KeyError: print("WA contact is not active") # If an active WildApricot account is found, stop looking and sync if is_member and is_active: break if is_member and is_active: print("User is an active member, adding user to group") try: if d_user['primary_group_id'] == self.member_group_id: print("User is already in group") return else: print("User is not in group") except KeyError: print("User has no primary group") try: self.discourse_api.add_group_member(self.member_group_id, d_user['username']) except DiscourseClientError: print("Failed to add user to group") else: print("user is not an active member") if (self.member_group_id == d_user['primary_group_id']): print("removing user from group") self.discourse_api.delete_group_member(self.member_group_id, d_user['id']) else: print("user is not in group") def SyncUsers(self, disc_users, wa_users): for u in disc_users: # if u['user']['username'] != '': # continue u = self.discourse_api.user_all(u['user']['id']) emails = self.discourse_api.user_emails(u['username']) email_list = [emails['email']] email_list += emails['secondary_emails'] u['emails'] = email_list found_contacts = [] for contact in wa_users: if contact['Email'] in u['emails']: print("Found WA user for %s" % contact['Email']) found_contacts.append(contact) self.SyncUser(u, found_contacts) time.sleep(2) def SyncAllUsers(self): response = self.discourse_api._get( '/directory_items.json?period=all&order=post_count') discourse_users = response['directory_items'] while response['meta']['total_rows_directory_items'] > len( discourse_users): response = self.discourse_api._get( response['meta']['load_more_directory_items']) discourse_users += response['directory_items'] print(response['meta']['total_rows_directory_items']) print(len(discourse_users)) wa_contacts = self.WA_API.GetAllContacts() self.SyncUsers(discourse_users, wa_contacts) def SyncNewUsers(self): discourse_users = self.discourse_api.list_users("new") wa_contacts = self.WA_API.GetAllContacts() self.SyncUsers(discourse_users, wa_contacts) def Run(self): self.SyncAllUsers()
if 'Too Many Requests' in ex.message: logger.error('Too Many Requests!' + ex.message, exc_info=True) break logger.warning("Unhandled error: " + ex.message, exc_info=True) new_user = discourse.create_user( user['full_name'], email.split('@')[0], email, ''.join(random.sample(string.lowercase, 8)), active='true') continue finally: # get discourse user_id list_users = discourse.list_users(type='active', filter=email) if list_users != None: discourse_id = list_users[0]['id'] # insert line into matching table match_user = """ insert into match_users ( useresponse_id, email, drupal_login, discourse_id, registration_ip ) values (%s, %s, %s, %s, %s)