示例#1
0
def sync_to_local_db(args):
    """
    Synchronize remote document with local mongo db
    """
    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]))

    for num, row in enumerate(rows.entry):
        info = row.to_dict()
        avail_user = users.find_one({"email": info["email"]})

        if not info["internalid"]:
            info["internalid"] = str(uuid.uuid4())

        if not avail_user:
            info["order"] = num
            users.insert(info)
            print "User {email} synced".format(**info)
        else:
            print "Updating {email}".format(**info)
            for key, val in info.iteritems():
                avail_user[key] = val
            print "User {email} updated".format(**info)
            users.save(avail_user)
示例#2
0
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"))
示例#3
0
def sync_to_local_db(args):
    """
    Synchronize remote document with local mongo db
    """
    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]))

    for num, row in enumerate(rows.entry):
        info = row.to_dict()
        avail_user = users.find_one({"email": info["email"]})

        if not info["internalid"]:
            info["internalid"] = str(uuid.uuid4())

        if not avail_user:
            info["order"] = num
            users.insert(info)
            print "User {email} synced".format(**info)
        else:
            print "Updating {email}".format(**info)
            for key, val in info.iteritems():
                avail_user[key] = val
            print "User {email} updated".format(**info)
            users.save(avail_user)
示例#4
0
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"))
示例#5
0
def fetch_tools():
    log.debug("fetching tools")
    client, spread_key = get_client()
    sheets = worksheet_dict(client.get_worksheets(spread_key))
    worksheet_id = sheets['tools']
    all_tools = []
    for entry in client.get_list_feed(spread_key, worksheet_id).entry:
        all_tools.append(entry.to_dict())
    log.info("fetched %d tools" % len(all_tools))
    with open(get_tools_file(), 'w') as fh:
        json.dump(all_tools, fh)
示例#6
0
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
示例#7
0
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
示例#8
0
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)
示例#9
0
def get_users(client):
    """
    Get list of users
    """

    print "Get list of registered users..."
    sheet, wsheet = get_worksheet(client, doc_id=REGISTRATION_DOC_ID)

    users_map = {}
    rows = client.get_list_feed(ID(sheet), ID(wsheet.entry[0]))

    for num, row in enumerate(rows.entry):
        data = row.to_dict()
        if data.get("email") is None:
            print u"Warning: row #{0} not proper ({1})".format(num + 1, data)
            continue

        users_map[data.get("email").strip()] = data

    return users_map
示例#10
0
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)
示例#11
0
def get_users(client):
    """
    Get list of users
    """

    print "Get list of registered users..."
    sheet, wsheet = get_worksheet(client, doc_id=REGISTRATION_DOC_ID)

    users_map = {}
    rows = client.get_list_feed(ID(sheet), ID(wsheet.entry[0]))

    for num, row in enumerate(rows.entry):
        data = row.to_dict()
        if data.get("email") is None:
            print u"Warning: row #{0} not proper ({1})".format(num + 1, data)
            continue

        users_map[data.get("email").strip()] = data

    return users_map
示例#12
0
def powerCycle2GSheet(queue):
    print "powerCycle2GSheet started"

    config = ConfigParser.RawConfigParser()
    config.read('PBMonitor.config')

    clientId = config.get('Google','clientId')
    clientSecret = config.get('Google','clientSecret')
    userAgent = config.get('Google','userAgent')
    refreshToken = config.get('Google','refreshToken')
    gSheetKey = config.get('Google', 'gSheetKey')

    clientScope='https://spreadsheets.google.com/feeds/'

    token = gdata.gauth.OAuth2Token(client_id=clientId, client_secret=clientSecret, scope=clientScope, user_agent=userAgent, refresh_token=refreshToken)
    client = gdata.spreadsheets.client.SpreadsheetsClient()
    token.authorize(client)

    entry = client.get_list_feed(gSheetKey, 'od6').entry[0]

    powerCycle = PowerCycle()

    dict = ''
    while dict != 'PBMonShutDown':
        dict = queue.get()

        if dict != 'PBMonShutDown':
            print "Read powerCycle from queue: ", dict
            for t in dict:
                entry.set_value(t.lower(),str(dict[t]))

            retry = True
            while retry:
                try:
                    client.add_list_entry(entry,gSheetKey,'od6')
                    print "Data sent to GSheet"
                    retry = False
                except:
                    retry = True
                    time.delay(60)
                    print "Transmission to GSheet failed, retrying in 60 seconds"
示例#13
0
client = gdata.spreadsheets.client.SpreadsheetsClient()
token = gdata.gauth.OAuth2Token(client_id=flow.client_id, 
                                client_secret=flow.client_secret, 
                                scope=flow.scope,
                                user_agent="chisubmit", 
                                access_token=credentials.access_token, 
                                refresh_token=credentials.refresh_token)
token.authorize(client)

# Open spreadsheet
worksheets = client.get_worksheets(SPREADSHEET_KEY)

# Get first worksheet
worksheet_id = worksheets.entry[0].get_worksheet_id()

feed = client.get_list_feed(SPREADSHEET_KEY, worksheet_id)

for row in feed.entry:
    print "============================================"
    group = row.to_dict()

    if course.teams.has_key(group[GROUPNAME]):
        print "Skipping '%s'. Already exists." % group[GROUPNAME] 
        continue
    
    print "Adding group '%s'" % group[GROUPNAME]
    
    if course.students.has_key(group[STUDENT1_CNETID]):
        print "Skipping %s %s (%s). Already exists." % (group[STUDENT1_FNAME], group[STUDENT1_LNAME], group[STUDENT1_CNETID])
        student1 = course.students[group[STUDENT1_CNETID]];
    else:    
示例#14
0
client = gdata.spreadsheets.client.SpreadsheetsClient()
token = gdata.gauth.OAuth2Token(client_id=flow.client_id,
                                client_secret=flow.client_secret,
                                scope=flow.scope,
                                user_agent="chisubmit",
                                access_token=credentials.access_token,
                                refresh_token=credentials.refresh_token)
token.authorize(client)

# Open spreadsheet
worksheets = client.get_worksheets(SPREADSHEET_KEY)

# Get first worksheet
worksheet_id = worksheets.entry[0].get_worksheet_id()

feed = client.get_list_feed(SPREADSHEET_KEY, worksheet_id)

for row in feed.entry:
    print "============================================"
    group = row.to_dict()

    if course.teams.has_key(group[GROUPNAME]):
        print "Skipping '%s'. Already exists." % group[GROUPNAME]
        continue

    print "Adding group '%s'" % group[GROUPNAME]

    if course.students.has_key(group[STUDENT1_CNETID]):
        print "Skipping %s %s (%s). Already exists." % (group[STUDENT1_FNAME],
                                                        group[STUDENT1_LNAME],
                                                        group[STUDENT1_CNETID])