Example #1
0
    def _make_age_ranges_and_probabilities(self, random_cases):
        age_ranges = []
        probabilities = []

        remaining_probability = 1

        now = ttm_util.now()
        current_time = now

        for random_case in random_cases:
            age, probability = random_case

            relative_age = age - (now - current_time)

            age_range = (current_time - relative_age, current_time)
            age_ranges.append(age_range)

            current_time = age_range[0]

            probabilities.append(probability)
            remaining_probability -= probability

        age_ranges.append((0, current_time))
        probabilities.append(remaining_probability)

        return age_ranges, probabilities
Example #2
0
    def get_obj(self):
        if not self.companion.get(BaseEntity.CREATED_TIME):
            self.companion[BaseEntity.CREATED_TIME] = ttm_util.now()

        if not self.companion.get(BaseEntity.UPDATED_TIME):
            self.companion[BaseEntity.UPDATED_TIME] = self.companion[
                BaseEntity.CREATED_TIME]

        if not self.companion.get(BaseEntity.ID):
            self.companion[BaseEntity.ID] = uuid.uuid4().bytes

        return self.companion
Example #3
0
    def get_obj(self):
        if not self.companion.get(Report.CREATED_TIME):
            self.companion[Report.CREATED_TIME] = ttm_util.now()

        if not self.companion.get(Report.UPDATED_TIME):
            self.companion[Report.UPDATED_TIME] = self.companion[
                Report.CREATED_TIME]

        if not self.companion.get(Report.ID):
            self.companion[Report.ID] = uuid.uuid4().bytes.hex()

        return self.companion
Example #4
0
def authenticate(req, resp):
    token_id = req.get_header('token')

    if not token_id:
        resp.status = falcon.HTTP_400  # Bad request
        resp.media = {'message': "'token' not provided in header."}
        return None

    token = TOKEN_REPO.authorize(token_id)

    if not token:
        resp.status = falcon.HTTP_401  # Unauthorized
        resp.media = {'message': 'Token not found.'}
        return None

    token_obj = token.get_obj()

    now = ttm_util.now()
    then = token_obj[Token.CREATED_TIME]

    if now - then > token.get_life() * 1000:
        resp.status = falcon.HTTP_401  # Unauthorized
        resp.media = {'message': 'Token is expired'}
        return None

    user_id = token_obj[Token.USER_ID]

    user = USER_REPO.find_user_by_mongo_id(user_id)

    if not user:
        # Someone deleted the user from database without telling?!
        resp.status = falcon.HTTP_410  # Gone
        resp.media = {'message': 'User not exists. Should\'t really happen!'}
        return None

    # Further processing of user. For example maybe a deleted account? Can go here...

    return user
Example #5
0
 def on_updated(self):
     self.get_obj()[BaseEntity.UPDATED_TIME] = ttm_util.now()
Example #6
0
    def get_obj(self):
        if not self.companion.get(Like.CREATED_TIME):
            self.companion[Like.CREATED_TIME] = ttm_util.now()

        return self.companion
Example #7
0
 def on_updated(self):
     self.get_obj()[Report.UPDATED_TIME] = ttm_util.now()
Example #8
0
 def on_updated(self):
     self.get_obj()[Comment.UPDATED_TIME] = ttm_util.now()