Esempio n. 1
0
def load_data(file_loc):
    click.echo("Loading Data")
    
    col_map = {}
    sheet = xlrd.open_workbook(file_loc).sheet_by_index(0)
    for i in range(sheet.ncols):
        if sheet.cell(0, i).value in stat_summary_columns or sheet.cell(0, i).value == user_column_name:
            col_map[sheet.cell(0, i).value] = i
    for i in range(1, sheet.nrows): # i represents the row of the sheet
        user = User.query.filter_by(username = sheet.cell(i, col_map[user_column_name]).value).first()
        if not user:
            user = User()
            user.first_name = 'John'
            user.last_name = 'Doe'
            # user.employee_id = i
            user.username = sheet.cell(i, col_map[user_column_name]).value
            db.session.add(user)
            db.session.commit()
            user = User.query.filter_by(username = sheet.cell(i, col_map[user_column_name]).value).first()
        for j in range(len(stat_summary_columns)-3): # j represents the column in the sheet
            stat = Stat()
            stat.max_val = -1
            if sheet.cell(i, col_map[stat_summary_columns[j]]).value == '':
                stat.percent = -1
            else:
                stat.percent = float(sheet.cell(i, col_map[stat_summary_columns[j]]).value) / 5 * 100
            if math.isnan(stat.percent):
                stat.percent = -1
            stat.eval_date = sheet.cell(i, sheet.ncols-1).value
            if type(stat.eval_date) is not datetime.datetime:
                stat.eval_date = datetime.datetime(datetime.MINYEAR, 1, 1)
            stat.stat_group_id = j+1
            stat.user_id = user.id
            db.session.add(stat)
            db.session.commit()
        stat = Stat()
        stat.max_val = int(sheet.cell(i, col_map[stat_summary_columns[len(stat_summary_columns)-2]]).value)
        stat.percent = float(sheet.cell(i, col_map[stat_summary_columns[len(stat_summary_columns)-3]]).value / sheet.cell(i, col_map[stat_summary_columns[len(stat_summary_columns)-2]]).value) * 100
        if math.isnan(stat.percent):
            stat.percent = -1
        stat.eval_date = sheet.cell(i, col_map[stat_summary_columns[-1]])
        if type(stat.eval_date) is not datetime.datetime:
            stat.eval_date = datetime.datetime(datetime.MINYEAR, 1, 1)
        stat.stat_group_id = len(stat_groups)
        stat.user_id = user.id
        db.session.add(stat)
        db.session.commit()
    click.echo("Finished Loading Data")
Esempio n. 2
0
def add_sample_data():
    names = ['asdf1', 'asdf2', 'asdf3']
    ids = [209152, 208963, 157644]
    texts = ['Sample text1', 'Sample text2', 'Sample text3']
    for i in range(3):
        user = User()
        user.username = names[i]
        user.first_name = names[i]
        user.last_name = names[i]
        #user.employee_id = ids[i]
        post = Post()
        post.category_id = 1
        post.content = texts[i]
        post.outcome_id = 1
        post.user = user
        db.session.add(user)
        db.session.add(post)
    db.session.commit()
Esempio n. 3
0
    def post(self):
        data = request.get_json()

        if not data:
            abort(400, message='No data posted')

        timezone = pytz.timezone(data.get('timezone', 'US/Eastern'))
        facebook_id = data.get('facebook_id')
        facebook_token = data.get('facebook_access_token')
        facebook_token_expires = datetime.utcnow() + timedelta(
            seconds=data.get('facebook_access_token_expires') or 1728000)
        birthdate = data.get('birthdate')
        education = data.get('education')
        work = data.get('work')

        properties = data.get('properties')

        if not facebook_id or not facebook_token:
            abort(400, message='Missing facebook id or token')

        with redis.lock('locks:register:{}'.format(facebook_id), timeout=30):
            try:
                User.find(facebook_id=facebook_id)
                abort(400, message='Account already exists')
            except DoesNotExist:
                pass

            if not facebook_id.startswith('xxx'):
                facebook = Facebook(facebook_token, facebook_token_expires)
                user_info = self.get_me(facebook, facebook_id, facebook_token,
                                        facebook_token_expires)
                facebook_token_expires = facebook.get_token_expiration()
            else:
                user_info = {}

            logger.info('creating new user account for facebook_id {}'.format(
                facebook_id))

            user = User()
            user.key = uuid4().hex
            user.facebook_id = facebook_id
            user.facebook_token = facebook_token
            user.facebook_token_expires = facebook_token_expires

            user.email = user_info.get('email')
            if user.email:
                user.email_validated = True
                user.email_validated_date = datetime.utcnow()
                user.email_validated_status = 'validated'

            user.timezone = timezone.zone
            user.first_name = user_info.get('first_name') or user_info.get(
                'given_name')
            user.last_name = user_info.get('last_name') or user_info.get(
                'family_name')
            user.username = get_username(user.email or user.full_name)
            user.gender = user_info.get('gender')

            if not birthdate:
                birthdate = user_info.get('birthday')

            if birthdate:
                try:
                    user.birthdate = parse(birthdate)
                except:
                    logger.info('error parsing birthdate {}'.format(birthdate))

            if education:
                user.education = education

            if work:
                user.work = work

            if g.group:
                user.group_id = g.group.id

            if properties:
                for key, value in properties.items():
                    user.set_custom_property(key, value)

            user.set_custom_property('events', {'triggers': ['find_referrer']})

            platform = request.headers.get('X-Wigo-Device')
            if not platform:
                platform = request.user_agent.platform
            if platform:
                platform = platform.lower()

            if platform in ('android', 'iphone', 'ipad'):
                user.set_custom_property('platforms', [platform])

            enterprise = request.headers.get('X-Wigo-Client-Enterprise')
            if enterprise == 'true':
                user.enterprise = True

            user.save()

        g.user = user

        # if not user.email_validated and Configuration.PUSH_ENABLED:
        #     send_email_verification.delay(user.id)

        logger.info(
            'registered new account for user {} for facebook_id {}'.format(
                user.id, facebook_id))

        return self.serialize_list(User, [user])