<s> import warnings <EOL> from django . core . exceptions import ImproperlyConfigured <EOL> from django . utils . encoding import smart_str <EOL> from fluent_comments import appsettings <EOL> from . compat import BASE_APP <EOL> try : <EOL> from django . contrib . sites . shortcuts import get_current_site <EOL> except ImportError : <EOL> from django . contrib . sites . models import get_current_site <EOL> if BASE_APP == '<STR_LIT>' : <EOL> from django . contrib . comments . moderation import moderator , CommentModerator <EOL> elif BASE_APP == '<STR_LIT>' : <EOL> from django_comments . moderation import moderator , CommentModerator <EOL> else : <EOL> raise NotImplementedError ( ) <EOL> try : <EOL> from urllib . parse import urljoin <EOL> except ImportError : <EOL> from urlparse import urljoin <EOL> try : <EOL> from akismet import Akismet <EOL> except ImportError : <EOL> Akismet = None <EOL> if appsettings . FLUENT_CONTENTS_USE_AKISMET : <EOL> warnings . warn ( "<STR_LIT>" , RuntimeWarning ) <EOL> __all__ = ( <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> ) <EOL> class FluentCommentsModerator ( CommentModerator ) : <EOL> """<STR_LIT>""" <EOL> auto_close_field = None <EOL> auto_moderate_field = None <EOL> enable_field = None <EOL> close_after = appsettings . FLUENT_COMMENTS_CLOSE_AFTER_DAYS <EOL> moderate_after = appsettings . FLUENT_COMMENTS_MODERATE_AFTER_DAYS <EOL> email_notification = False <EOL> akismet_check = appsettings . FLUENT_CONTENTS_USE_AKISMET and Akismet is not None <EOL> akismet_check_action = appsettings . FLUENT_COMMENTS_AKISMET_ACTION <EOL> def allow ( self , comment , content_object , request ) : <EOL> """<STR_LIT>""" <EOL> if not super ( FluentCommentsModerator , self ) . allow ( comment , content_object , request ) : <EOL> return False <EOL> if self . akismet_check and self . akismet_check_action == '<STR_LIT>' : <EOL> if self . _akismet_check ( comment , content_object , request ) : <EOL> return False <EOL> return True <EOL> def moderate ( self , comment , content_object , request ) : <EOL> """<STR_LIT>""" <EOL> if super ( FluentCommentsModerator , self ) . moderate ( comment , content_object , request ) : <EOL> return True <EOL> if self . akismet_check and self . akismet_check_action == '<STR_LIT>' : <EOL> if self . _akismet_check ( comment , content_object , request ) : <EOL> return True <EOL> return False <EOL> def _akismet_check ( self , comment , content_object , request ) : <EOL> """<STR_LIT>""" <EOL> AKISMET_API_KEY = appsettings . AKISMET_API_KEY <EOL> if not AKISMET_API_KEY : <EOL> raise ImproperlyConfigured ( '<STR_LIT>' ) <EOL> current_domain = get_current_site ( request ) . domain <EOL> auto_blog_url = '<STR_LIT>' . format ( request . is_secure ( ) and '<STR_LIT>' or '<STR_LIT:http>' , current_domain ) <EOL> blog_url = appsettings . AKISMET_BLOG_URL or auto_blog_url <EOL> akismet_api = Akismet ( <EOL> key = AKISMET_API_KEY , <EOL> blog_url = blog_url <EOL> ) <EOL> if akismet_api . verify_key ( ) : <EOL> akismet_data = self . _get_akismet_data ( blog_url , comment , content_object , request ) <EOL> if akismet_api . comment_check ( smart_str ( comment . comment ) , data = akismet_data , build_data = True ) : <EOL> return True <EOL> return False <EOL> def _get_akismet_data ( self , blog_url , comment , content_object , request ) : <EOL> akismet_data = { <EOL> '<STR_LIT>' : urljoin ( blog_url , content_object . get_absolute_url ( ) ) , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : getattr ( comment , '<STR_LIT:name>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : getattr ( comment , '<STR_LIT:email>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : getattr ( comment , '<STR_LIT:url>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : comment . ip_address , <EOL> '<STR_LIT>' : request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> '<STR_LIT>' : request . META . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> } <EOL> if appsettings . AKISMET_IS_TEST : <EOL> akismet_data [ '<STR_LIT>' ] = '<STR_LIT:1>' <EOL> return akismet_data <EOL> def moderate_model ( ParentModel , publication_date_field = None , enable_comments_field = None ) : <EOL> """<STR_LIT>""" <EOL> attrs = { <EOL> '<STR_LIT>' : publication_date_field , <EOL> '<STR_LIT>' : publication_date_field , <EOL> '<STR_LIT>' : enable_comments_field , <EOL> } <EOL> ModerationClass = type ( ParentModel . __name__ + '<STR_LIT>' , ( FluentCommentsModerator , ) , attrs ) <EOL> moderator . register ( ParentModel , ModerationClass ) <EOL> def get_model_moderator ( model ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return moderator . _registry [ model ] <EOL> except KeyError : <EOL> return None <EOL> def comments_are_open ( content_object ) : <EOL> """<STR_LIT>""" <EOL> moderator = get_model_moderator ( content_object . __class__ ) <EOL> if moderator is None : <EOL> return True <EOL> return CommentModerator . allow ( moderator , None , content_object , None ) <EOL> def comments_are_moderated ( content_object ) : <EOL> """<STR_LIT>""" <EOL> moderator = get_model_moderator ( content_object . __class__ ) <EOL> if moderator is None : <EOL> return False <EOL> return CommentModerator . moderate ( moderator , None , content_object , None ) </s>
