def on_post(self, req, resp): user = authenticate(req, resp) if not user: return post_content = req.media.get('post') language = req.media.get('language') country = req.media.get('country') community = req.media.get('community') if not validate(post_content, language, country, resp): return if not community: community = COMMUNITY_REPO.get_public_community_number() else: community = COMMUNITY_REPO.get_community_by_id_name(community) if not community: resp.status = falcon.HTTP_400 # Bad request resp.media = {'message': 'Community id name not found.'} return community = community.get_obj()[Community.NUMBER] post = POST_REPO.new_post(user, post_content, language, country, community) USER_REPO.on_new_post(user, post) sync = SYNC_REPO.new_post(post) resp.status = falcon.HTTP_200 resp.media = post.get_public_obj()
def random_posts(self, count, language, country, community, processed=True): match = {} if language: match[Post.LANGUAGE] = language if country: match[Post.COUNTRY] = country if not community: community = COMMUNITY_REPO.get_public_community_id() start, end = COMMUNITY_REPO.get_community_number_range(community) if start == COMMUNITY_DOES_NOT_EXIST: return COMMUNITY_DOES_NOT_EXIST match[Post.COMMUNITY] = { '$gte': start, '$lt': end } # TODO ALSO RESET ALL PREEXISTING POSTS age_ranges, probabilities = self._make_age_ranges_and_probabilities( RANDOM_CASES) post_age_range_counts = collections.Counter( random.choices(age_ranges, probabilities, k=count)) posts = [] remaining_count = count for age_range in age_ranges: match[Post.CREATED_TIME] = { '$gt': age_range[0], '$lt': age_range[1] } if age_range == age_ranges[-1]: range_count = remaining_count else: range_count = post_age_range_counts.get(age_range, 0) range_posts = self._aggregate_posts(match, range_count, processed) remaining_count -= len(range_posts) posts.extend(range_posts) random.shuffle(posts) return posts
def exhibition_posts(self, count, language, country, community, day, week, month): match = {} if language: match[Post.LANGUAGE] = language if country: match[Post.COUNTRY] = country if not community: community = COMMUNITY_REPO.get_public_community_id() start, end = COMMUNITY_REPO.get_community_number_range(community) if start == COMMUNITY_DOES_NOT_EXIST: return COMMUNITY_DOES_NOT_EXIST match[Post.COMMUNITY] = {'$gte': start, '$lt': end} random_cases = [(TODAY_POSTS_AGE, day / 100), (RECENT_POSTS_AGE, week / 100), (MID_RANGE_POSTS_AGE, month / 100)] age_ranges, probabilities = self._make_age_ranges_and_probabilities( random_cases) post_age_range_counts = collections.Counter( random.choices(age_ranges, probabilities, k=count)) posts = [] remaining_count = count for age_range in age_ranges: match[Post.CREATED_TIME] = { '$gt': age_range[0], '$lt': age_range[1] } if age_range == age_ranges[-1]: range_count = remaining_count else: range_count = post_age_range_counts.get(age_range, 0) range_posts = self._aggregate_posts(match, range_count, True) remaining_count -= len(range_posts) posts.extend(range_posts) random.shuffle(posts) return posts
def on_get(self, req, resp): user = authenticate(req, resp) if not user: return count = req.get_param_as_int('count', min_value=1, max_value=MAXIMUM_RANDOM_POST_COUNT) language = req.get_param('language') country = req.get_param('country') community = req.get_param('community') if language and not ttm_util.validate_language(language): resp.status = falcon.HTTP_400 # Bad request resp.media = {'message': 'Invalid language code'} return if country and not ttm_util.validate_country(country): resp.status = falcon.HTTP_400 # Bad request resp.media = {'message': 'Invalid country code'} return if community is None or len(community) == 0: community = COMMUNITY_REPO.get_public_community_id() else: community = COMMUNITY_REPO.get_community_by_id_name(community) if community is not None: community = community.get_obj()[Community.ID] else: resp.status = falcon.HTTP_404 # Not found resp.media = {'message': 'Community not found'} return posts = POST_REPO.random_posts(count, language, country, community) if posts == COMMUNITY_DOES_NOT_EXIST: resp.status = falcon.HTTP_404 # Not found resp.media = {'message': 'Community not found'} return resp.status = falcon.HTTP_200 resp.media = {'posts': posts}
def on_get(self, req, resp): id = req.get_param('id', default=None) id_name = req.get_param('id_name', default=None) return_tree = req.get_param_as_bool('tree', default=False) if id is not None: if len(id) > 0: id = bytes.fromhex(id) else: id = None if id_name is not None and len(id_name) > 0: community = COMMUNITY_REPO.get_community_by_id_name(id_name) if community is None: resp.status = falcon.HTTP_404 # Not found resp.media = {'message': 'Community id name not found.'} return id = community.get_obj()[Community.ID] if return_tree: tree = COMMUNITY_REPO.get_community_tree(id) if tree is None: resp.status = falcon.HTTP_404 # Not found resp.media = {'message': 'Community id not found.'} return resp.status = falcon.HTTP_200 resp.media = tree return community = COMMUNITY_REPO.get_community(id) if not community: resp.status = falcon.HTTP_404 # Not found resp.media = {'message': 'Community id not found.'} return resp.status = falcon.HTTP_200 resp.media = community.get_public_obj()
def on_post(self, req, resp): token = req.get_header('token') if token != 'iamnotanasshole': resp.status = falcon.HTTP_401 # Unauthorized resp.media = { 'message': 'You do not have permission to perform this action. Please be nice.' } return parent_id = req.media.get('parent_id') name = req.media.get('name') description = req.media.get('description') id_name = req.media.get('id_name') if not parent_id: parent_id = None else: parent_id = bytes.fromhex(parent_id) community = COMMUNITY_REPO.new_community(parent_id=parent_id, name=name, description=description, id_name=id_name) if community == COMMUNITY_DOES_NOT_EXIST: resp.status = falcon.HTTP_400 # Bad request resp.media = {'message': 'Parent community id not found.'} return if community == OVERFLOWING_CHILD_COUNT: resp.status = falcon.HTTP_406 # Not acceptable resp.media = { 'message': 'This community can not have any more children.' } return if community == ID_NAME_DUPLICATE: resp.status = falcon.HTTP_406 # Not acceptable resp.media = {'message': 'Id name already exists.'} return resp.status = falcon.HTTP_200 resp.media = community.get_public_obj()
def on_get(self, req, resp): count = req.get_param_as_int('count', min_value=1, max_value=MAXIMUM_RANDOM_POST_COUNT) day = req.get_param_as_int('day', min_value=0) week = req.get_param_as_int('week', min_value=0) month = req.get_param_as_int('month', min_value=0) language = req.get_param('language') country = req.get_param('country') community = req.get_param('community') if language and not ttm_util.validate_language(language): resp.status = falcon.HTTP_400 # Bad request resp.media = {'message': 'Invalid language code'} return if country and not ttm_util.validate_country(country): resp.status = falcon.HTTP_400 # Bad request resp.media = {'message': 'Invalid country code'} return if community is None or len(community) == 0: community = COMMUNITY_REPO.get_public_community_id() else: community = bytes.fromhex(community) posts = POST_REPO.exhibition_posts(count, language, country, community, day, week, month) if posts == COMMUNITY_DOES_NOT_EXIST: resp.status = falcon.HTTP_404 # Not found resp.media = {'message': 'Community not found'} return resp.status = falcon.HTTP_200 resp.media = {'posts': posts}