def add_topic(request, board_id, title, body): """Insert a topic to the database. :param request: A serialized request :type:`dict` returned from :meth:`fanboi2.utils.serialize_request`. :param board_id: An :type:`int` referencing board ID. :param title: A :type:`str` topic title. :param body: A :type:`str` topic body. :type request: dict :type board_id: int :type title: str :type body: str :rtype: tuple """ ip_address = request['remote_addr'] country_code = geoip.country_code(ip_address) country_scope = 'country:%s' % (str(country_code).lower()) with transaction.manager: board = DBSession.query(Board).get(board_id) board_scope = 'board:%s' % (board.slug, ) if DBSession.query(RuleBan).\ filter(RuleBan.listed(ip_address, scopes=(board_scope,))).\ count() > 0: return 'failure', 'ban_rejected' override = {} rule_override = DBSession.query(RuleOverride).filter( RuleOverride.listed(ip_address, scopes=(board_scope,))).\ first() if rule_override is not None: override = rule_override.override board_status = override.get('status', board.status) if board_status != 'open': return 'failure', 'status_rejected', board_status if checklist.enabled(country_scope, 'akismet') and \ akismet.spam(request, body): return 'failure', 'spam_rejected' if checklist.enabled(country_scope, 'dnsbl') and \ dnsbl.listed(ip_address): return 'failure', 'dnsbl_rejected' if checklist.enabled(country_scope, 'proxy_detect') and \ proxy_detector.detect(ip_address): return 'failure', 'proxy_rejected' post = Post(body=body, ip_address=ip_address) post.topic = Topic(board=board, title=title) DBSession.add(post) DBSession.flush() return 'topic', post.topic_id
def add_topic(request, board_id, title, body): """Insert a topic to the database. :param request: A serialized request :type:`dict` returned from :meth:`fanboi2.utils.serialize_request`. :param board_id: An :type:`int` referencing board ID. :param title: A :type:`str` topic title. :param body: A :type:`str` topic body. :type request: dict :type board_id: int :type title: str :type body: str :rtype: tuple """ with transaction.manager: board = DBSession.query(Board).get(board_id) scope = 'board:%s' % (board.slug,) if DBSession.query(RuleBan).\ filter(RuleBan.listed(request['remote_addr'], scopes=(scope,))).\ count() > 0: return 'failure', 'ban_rejected' override = {} rule_override = DBSession.query(RuleOverride).filter( RuleOverride.listed(request['remote_addr'], scopes=(scope,))).\ first() if rule_override is not None: override = rule_override.override board_status = override.get('status', board.status) if board_status != 'open': return 'failure', 'status_rejected', board_status if akismet.spam(request, body): return 'failure', 'spam_rejected' if dnsbl.listed(request['remote_addr']): return 'failure', 'dnsbl_rejected' if proxy_detector.detect(request['remote_addr']): return 'failure', 'proxy_rejected' post = Post(body=body, ip_address=request['remote_addr']) post.topic = Topic(board=board, title=title) DBSession.add(post) DBSession.flush() return 'topic', post.topic_id
def _get_override(request, board=None): """Returns a :type:`dict` of an override rule for the given IP address presented in request. If no override present for a user, an empty dict is returned. :param request: A :class:`pyramid.request.Request` object. :param board: A :class:`fanboi2.models.Board` object to scope. :type request: pyramid.request.Request :type board: fanboi2.models.Board :rtype: dict """ scopes = None if board is not None: scopes = ('board:%s' % (board.slug, ), ) rule_override = DBSession.query(RuleOverride).filter( RuleOverride.listed(request.remote_addr, scopes=scopes)).\ first() if rule_override is not None: return rule_override.override return {}
def _get_override(request, board=None): """Returns a :type:`dict` of an override rule for the given IP address presented in request. If no override present for a user, an empty dict is returned. :param request: A :class:`pyramid.request.Request` object. :param board: A :class:`fanboi2.models.Board` object to scope. :type request: pyramid.request.Request :type board: fanboi2.models.Board :rtype: dict """ scopes = None if board is not None: scopes = ('board:%s' % (board.slug,),) rule_override = DBSession.query(RuleOverride).filter( RuleOverride.listed(request.remote_addr, scopes=scopes)).\ first() if rule_override is not None: return rule_override.override return {}
def add_post(self, request, topic_id, body, bumped): """Insert a post to a topic. :param self: A :class:`celery.Task` object. :param request: A serialized request :type:`dict` returned from :meth:`fanboi2.utils.serialize_request`. :param topic_id: An :type:`int` referencing topic ID. :param body: A :type:`str` post body. :param bumped: A :type:`bool` specifying bump status. :type self: celery.Task :type request: dict :type topic_id: int :type body: str :type bumped: bool :rtype: tuple """ with transaction.manager: topic = DBSession.query(Topic).get(topic_id) board = topic.board scope = 'board:%s' % (board.slug,) if DBSession.query(RuleBan).\ filter(RuleBan.listed(request['remote_addr'], scopes=(scope,))).\ count() > 0: return 'failure', 'ban_rejected' if topic.status != 'open': return 'failure', 'status_rejected', topic.status override = {} rule_override = DBSession.query(RuleOverride).filter( RuleOverride.listed(request['remote_addr'], scopes=(scope,))).\ first() if rule_override is not None: override = rule_override.override board_status = override.get('status', board.status) if not board_status in ('open', 'restricted'): return 'failure', 'status_rejected', board_status if akismet.spam(request, body): return 'failure', 'spam_rejected' if dnsbl.listed(request['remote_addr']): return 'failure', 'dnsbl_rejected' if proxy_detector.detect(request['remote_addr']): return 'failure', 'proxy_rejected' post = Post( topic=topic, body=body, bumped=bumped, ip_address=request['remote_addr']) try: DBSession.add(post) DBSession.flush() except IntegrityError as e: raise self.retry(exc=e) return 'post', post.id
def add_post(self, request, topic_id, body, bumped): """Insert a post to a topic. :param self: A :class:`celery.Task` object. :param request: A serialized request :type:`dict` returned from :meth:`fanboi2.utils.serialize_request`. :param topic_id: An :type:`int` referencing topic ID. :param body: A :type:`str` post body. :param bumped: A :type:`bool` specifying bump status. :type self: celery.Task :type request: dict :type topic_id: int :type body: str :type bumped: bool :rtype: tuple """ ip_address = request['remote_addr'] country_code = geoip.country_code(ip_address) country_scope = 'country:%s' % (str(country_code).lower()) with transaction.manager: topic = DBSession.query(Topic).get(topic_id) board = topic.board board_scope = 'board:%s' % (board.slug, ) if DBSession.query(RuleBan).\ filter(RuleBan.listed(ip_address, scopes=(board_scope,))).\ count() > 0: return 'failure', 'ban_rejected' if topic.status != 'open': return 'failure', 'status_rejected', topic.status override = {} rule_override = DBSession.query(RuleOverride).filter( RuleOverride.listed(ip_address, scopes=(board_scope,))).\ first() if rule_override is not None: override = rule_override.override board_status = override.get('status', board.status) if not board_status in ('open', 'restricted'): return 'failure', 'status_rejected', board_status if checklist.enabled(country_scope, 'akismet') and \ akismet.spam(request, body): return 'failure', 'spam_rejected' if checklist.enabled(country_scope, 'dnsbl') and \ dnsbl.listed(ip_address): return 'failure', 'dnsbl_rejected' if checklist.enabled(country_scope, 'proxy_detect') and \ proxy_detector.detect(ip_address): return 'failure', 'proxy_rejected' post = Post(topic=topic, body=body, bumped=bumped, ip_address=ip_address) try: DBSession.add(post) DBSession.flush() except IntegrityError as e: raise self.retry(exc=e) return 'post', post.id
def _newRuleOverride(self, **kwargs): from fanboi2.models import RuleOverride return RuleOverride(**kwargs)