def get_members_data(): raw_data = get_all_records("All Members", "Main View") ret = {} ret["All Members:Main View"] = [] for row in raw_data: fields = row["fields"] if "first_name" in fields or "last_name" in fields: retrow = { "fields": { "first_name" :fields.get("first_name"), "last_name" :fields.get("last_name"), "chapter_id" :fields.get("chapter_id"), }, "id": row["id"], } ret["All Members:Main View"].append(retrow) else: # well we can't really do anything without a name... pass raw_data = get_all_records("Chapters", "Main View") ret["Chapters:Main View"] = [] for row in raw_data: fields = row["fields"] if "Name" in fields: retrow = { "fields": { "Name" :fields.get("Name"), "All Members" :fields.get("All Members"), }, "id": row["id"], } ret["Chapters:Main View"].append(retrow) return ret
def get_members_data(): raw_data = get_all_records("All Members", "Main View") ret = {} ret["All Members:Main View"] = [] for row in raw_data: fields = row["fields"] if "first_name" in fields or "last_name" in fields: retrow = { "fields": { "first_name": fields.get("first_name"), "last_name": fields.get("last_name"), "chapter_id": fields.get("chapter_id"), }, "id": row["id"], } ret["All Members:Main View"].append(retrow) else: # well we can't really do anything without a name... pass raw_data = get_all_records("Chapters", "Main View") ret["Chapters:Main View"] = [] for row in raw_data: fields = row["fields"] if "Name" in fields: retrow = { "fields": { "Name": fields.get("Name"), "All Members": fields.get("All Members"), }, "id": row["id"], } ret["Chapters:Main View"].append(retrow) return ret
def get_chapter_data(): raw_data = get_all_records("Chapters", "Main View") ret = [] for row in raw_data: fields = row["fields"] if "Name" in fields and "Location" in fields: retrow = {} lat, lng = get_lat_long(fields["Location"]) if lat and lng: retrow["lat"] = lat retrow["long"] = lng else: continue retrow["name"] = fields["Name"] retrow["facebook"] = fields.get("Facebook") retrow["email"] = fields.get("Contact Email") retrow["youtube"] = fields.get("YouTube Channel") retrow["organizers"] = fields.get("Organizers Page") retrow["calendar"] = fields.get("Calendar") # special case: DxE SLC has their own website. They're likely to be # the only one to have their own for a while, so it's not worth it # to make a new column for website. We'll artificially inject their # site here. if fields["Name"] == "Salt Lake City": retrow["website"] = "http://dxeslc.org/" ret.append(retrow) else: # well we can't really do anything without a name and location... pass return ret
def sync_airtable_to_mailing_list(): # chapters is all of the chapters in the "Chapters" table. chapters = airtable.get_all_records("Chapters", "Main View") if not chapters: raise Exception("Expected chapter to not be empty") chapter_to_mailing_list = create_chapter_to_mailing_list(chapters) if not chapter_to_mailing_list: raise Exception("Expected chapter_to_mailing_list to not be empty") members = airtable.get_all_records("All Members", "Main View") if not members: raise Exception("Expected members to not be empty") members_to_add = get_members_to_add(members) errors = [] directory = get_directory() for m in members_to_add: valid_chapters_to_add = [] for c in m.chapters_to_add: if c not in chapter_to_mailing_list: continue ml = chapter_to_mailing_list[c] try: directory.members().insert(groupKey=ml, body={ "email": m.email.strip() }).execute() valid_chapters_to_add.append(c) except HttpError as e: print e content = json.loads(e.content) if "Member already exists" in content["error"]["message"]: # Add member if the error is that they already exist. valid_chapters_to_add.append(c) print "Adding member to {} in Airtable anyway.".format(c) if not valid_chapters_to_add: continue # Don't update the member if they weren't added to any chapter mailing lists. # Update member in airtable. valid_chapters_to_add += m.chapters_added r = airtable.update_record( "All Members", m.airtable_id, {ADDED_TO_MAILING_LIST_COL: valid_chapters_to_add}) if r.status_code != 200: raise Exception( "Expected '200' from airtable.update_record: {}, {}, {}". format(m.airtable_id, valid_chapters_to_add, r.text)) print "done!"
def sync_airtable_to_mailing_list(): # chapters is all of the chapters in the "Chapters" table. chapters = airtable.get_all_records("Chapters", "Main View") if not chapters: raise Exception("Expected chapter to not be empty") chapter_to_mailing_list = create_chapter_to_mailing_list(chapters) if not chapter_to_mailing_list: raise Exception("Expected chapter_to_mailing_list to not be empty") members = airtable.get_all_records("All Members", "Main View") if not members: raise Exception("Expected members to not be empty") members_to_add = get_members_to_add(members) errors = [] directory = get_directory() for m in members_to_add: valid_chapters_to_add = [] for c in m.chapters_to_add: if c not in chapter_to_mailing_list: continue ml = chapter_to_mailing_list[c] try: directory.members().insert(groupKey=ml, body={"email": m.email.strip()}).execute() valid_chapters_to_add.append(c) except HttpError as e: print e content = json.loads(e.content) if "Member already exists" in content["error"]["message"]: # Add member if the error is that they already exist. valid_chapters_to_add.append(c) print "Adding member to {} in Airtable anyway.".format(c) if not valid_chapters_to_add: continue # Don't update the member if they weren't added to any chapter mailing lists. # Update member in airtable. valid_chapters_to_add += m.chapters_added r = airtable.update_record("All Members", m.airtable_id, {ADDED_TO_MAILING_LIST_COL: valid_chapters_to_add}) if r.status_code != 200: raise Exception("Expected '200' from airtable.update_record: {}, {}, {}".format(m.airtable_id, valid_chapters_to_add, r.text)) print "done!"
def backup_all_tables(): output_dir = tempfile.mkdtemp() fname = "{}.zip".format( datetime.datetime.now().strftime('base_backup_%Y-%m-%d_%H:%M:%S')) fpath = os.path.join(output_dir, fname) with zipfile.ZipFile(fpath, 'w', compression=zipfile.ZIP_DEFLATED) as zf: for table in airtable.TABLES: records = airtable.get_all_records(table, "Main View") zf.writestr("{}.json".format(table), json.dumps(records)) conn = S3Connection(S3_ACCESS_KEY, S3_SECRET_KEY) b = conn.get_bucket(S3_BUCKET) k = Key(b) k.key = S3_BACKUP_DIR + "/" + fname k.set_contents_from_filename(fpath)
def backup_all_tables(): output_dir = tempfile.mkdtemp() fname = "{}.zip".format(datetime.datetime.now().strftime('base_backup_%Y-%m-%d_%H:%M:%S')) fpath = os.path.join(output_dir, fname) with zipfile.ZipFile( fpath, 'w', compression=zipfile.ZIP_DEFLATED ) as zf: for table in airtable.TABLES: records = airtable.get_all_records(table, "Main View") zf.writestr("{}.json".format(table), json.dumps(records)) conn = S3Connection(S3_ACCESS_KEY, S3_SECRET_KEY) b = conn.get_bucket(S3_BUCKET) k = Key(b) k.key = S3_BACKUP_DIR + "/" + fname k.set_contents_from_filename(fpath)