def sync_from_local_db(args): """ Synchronize local db with remote document """ users = mongo_init().users db = DB(args.token) client = get_auth(db) sheet, wsheet = get_worksheet(client, doc_id=REGISTRATION_DOC_ID) rows = client.get_list_feed(ID(sheet), ID(wsheet.entry[0])) last = len(rows.entry) found_users = users.find({}).sort("order", pymongo.ASCENDING) #batch = gdata.spreadsheet.SpreadsheetsCellsFeed() for num, user in enumerate(found_users): row = dict([(key, val) for key, val in user.iteritems() if key != "_id"]) order = row.pop("order", last) rows.entry[order].from_dict(row) #rows.entry[order].batch_id = BatchId('update-request') client.update(rows.entry[order]) #batch.AddUpdate(rows.entry[order]) if order >= last: last += 1 print "Synced {}".format(row.get(u"email"))
def send_registrations(args): db = DB(args.token) client = get_auth(db) users = get_users(client) total = len(users) sent = 1 sheet, wsheet = get_worksheet(client, doc_id=REGISTRATION_DOC_ID) rows = client.get_list_feed(ID(sheet), ID(wsheet.entry[0])) sent_mails = set() for num, row in enumerate(rows.entry): # TODO: parse timestamp info = row.to_dict() name = info["name"].strip() email = info["email"].strip() status = info["notification"] # avoid duplicates if email in sent_mails: print u"User with email: {} already notified".format(email) continue if email not in users: print u"User with email {} skipped "\ u"because of wrong format".format(email) continue if status: print u"Notification to {} already sent".format(name) sent += 1 continue # timestamp = info["timestamp"] # company = info["company"] # position = info["position"] print u"Sending email to: {} ...".format(name) send_mail(db, info, template="registration") # updating status info["notification"] = "sent" row.from_dict(info) client.update(row) print u"Sent {} of {}.".format(sent, total) sent_mails.add(email) sent += 1
def generate_reg_ids(args): """ Generate unique registration ids """ db = DB(args.token) client = get_auth(db) sheet, wsheet = get_worksheet(client, doc_id=REGISTRATION_DOC_ID) rows = client.get_list_feed(ID(sheet), ID(wsheet.entry[0])) for num, row in enumerate(rows.entry): info = row.to_dict() # update only if info.get("registrationid") is None: info["registrationid"] = REGID() row.from_dict(info) client.update(row) print "Generated `{registrationid}` for {email}".format(**info)