Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes | Private Member Functions
MoinMoin.security.textcha.TextCha Class Reference

List of all members.

Public Member Functions

def __init__
def is_enabled
def check_answer
def render
def check_answer_from_form

Public Attributes

 request
 user_info
 textchas
 question
 answer_regex
 answer_re

Private Member Functions

def _get_textchas
def _init_qa
def _make_form_values
def _extract_form_values

Detailed Description

Text CAPTCHA support 

Definition at line 32 of file textcha.py.


Constructor & Destructor Documentation

def MoinMoin.security.textcha.TextCha.__init__ (   self,
  request,
  question = None 
)
Initialize the TextCha.

    @param request: the request object
    @param question: see _init_qa()

Definition at line 35 of file textcha.py.

00035 
00036     def __init__(self, request, question=None):
00037         """ Initialize the TextCha.
00038 
00039             @param request: the request object
00040             @param question: see _init_qa()
00041         """
00042         self.request = request
00043         self.user_info = request.user.valid and request.user.name or request.remote_addr
00044         self.textchas = self._get_textchas()
00045         self._init_qa(question)


Member Function Documentation

def MoinMoin.security.textcha.TextCha._extract_form_values (   self,
  form = None 
) [private]

Definition at line 142 of file textcha.py.

00142 
00143     def _extract_form_values(self, form=None):
00144         if form is None:
00145             form = self.request.form
00146         question = form.get('textcha-question')
00147         given_answer = form.get('textcha-answer', u'')
00148         return question, given_answer

Here is the caller graph for this function:

get textchas from the wiki config for the user's language (or default_language or en) 

Definition at line 46 of file textcha.py.

00046 
00047     def _get_textchas(self):
00048         """ get textchas from the wiki config for the user's language (or default_language or en) """
00049         request = self.request
00050         groups = request.groups
00051         cfg = request.cfg
00052         user = request.user
00053         disabled_group = cfg.textchas_disabled_group
00054         if disabled_group and user.name and user.name in groups.get(disabled_group, []):
00055             return None
00056         textchas = cfg.textchas
00057         if textchas:
00058             lang = user.language or request.lang
00059             logging.debug(u"TextCha: user.language == '%s'." % lang)
00060             if lang not in textchas:
00061                 lang = cfg.language_default
00062                 logging.debug(u"TextCha: fallback to language_default == '%s'." % lang)
00063                 if lang not in textchas:
00064                     logging.error(u"TextCha: The textchas do not have content for language_default == '%s'! Falling back to English." % lang)
00065                     lang = 'en'
00066                     if lang not in textchas:
00067                         logging.error(u"TextCha: The textchas do not have content for 'en', auto-disabling textchas!")
00068                         cfg.textchas = None
00069                         lang = None
00070         else:
00071             lang = None
00072         if lang is None:
00073             return None
00074         else:
00075             logging.debug(u"TextCha: using lang = '%s'" % lang)
00076             return textchas[lang]

def MoinMoin.security.textcha.TextCha._init_qa (   self,
  question = None 
) [private]
Initialize the question / answer.

 @param question: If given, the given question will be used.
          If None, a new question will be generated.

Definition at line 77 of file textcha.py.

00077 
00078     def _init_qa(self, question=None):
00079         """ Initialize the question / answer.
00080 
00081          @param question: If given, the given question will be used.
00082                           If None, a new question will be generated.
00083         """
00084         if self.is_enabled():
00085             if question is None:
00086                 self.question = random.choice(self.textchas.keys())
00087             else:
00088                 self.question = question
00089             try:
00090                 self.answer_regex = self.textchas[self.question]
00091                 self.answer_re = re.compile(self.answer_regex, re.U|re.I)
00092             except KeyError:
00093                 # this question does not exist, thus there is no answer
00094                 self.answer_regex = ur"[Never match for cheaters]"
00095                 self.answer_re = None
00096                 logging.warning(u"TextCha: Non-existing question '%s'. User '%s' trying to cheat?" % (
00097                                 self.question, self.user_info))
00098             except re.error:
00099                 logging.error(u"TextCha: Invalid regex in answer for question '%s'" % self.question)
00100                 self._init_qa()

Here is the call graph for this function:

Here is the caller graph for this function:

def MoinMoin.security.textcha.TextCha._make_form_values (   self,
  question,
  given_answer 
) [private]

Definition at line 137 of file textcha.py.

00137 
00138     def _make_form_values(self, question, given_answer):
00139         question_form = wikiutil.escape(question, True)
00140         given_answer_form = wikiutil.escape(given_answer, True)
00141         return question_form, given_answer_form

Here is the caller graph for this function:

def MoinMoin.security.textcha.TextCha.check_answer (   self,
  given_answer 
)
check if the given answer to the question is correct 

Definition at line 117 of file textcha.py.

00117 
00118     def check_answer(self, given_answer):
00119         """ check if the given answer to the question is correct """
00120         if self.is_enabled():
00121             if self.answer_re is not None:
00122                 success = self.answer_re.match(given_answer.strip()) is not None
00123             else:
00124                 # someone trying to cheat!?
00125                 success = False
00126             success_status = success and u"success" or u"failure"
00127             logging.info(u"TextCha: %s (u='%s', a='%s', re='%s', q='%s')" % (
00128                              success_status,
00129                              self.user_info,
00130                              given_answer,
00131                              self.answer_regex,
00132                              self.question,
00133                              ))
00134             return success
00135         else:
00136             return True

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 171 of file textcha.py.

00171 
00172     def check_answer_from_form(self, form=None):
00173         if self.is_enabled():
00174             question, given_answer = self._extract_form_values(form)
00175             self._init_qa(question)
00176             return self.check_answer(given_answer)
00177         else:
00178             return True
00179 

Here is the call graph for this function:

check if textchas are enabled.

    They can be disabled for all languages if you use textchas = None or = {},
    also they can be disabled for some specific language, like:
    textchas = {
'en': {
    'some question': 'some answer',
    # ...
},
'de': {}, # having no questions for 'de' means disabling textchas for 'de'
# ...
    }

Definition at line 101 of file textcha.py.

00101 
00102     def is_enabled(self):
00103         """ check if textchas are enabled.
00104 
00105             They can be disabled for all languages if you use textchas = None or = {},
00106             also they can be disabled for some specific language, like:
00107             textchas = {
00108                 'en': {
00109                     'some question': 'some answer',
00110                     # ...
00111                 },
00112                 'de': {}, # having no questions for 'de' means disabling textchas for 'de'
00113                 # ...
00114             }
00115         """
00116         return not not self.textchas # we don't want to return the dict

Here is the caller graph for this function:

def MoinMoin.security.textcha.TextCha.render (   self,
  form = None 
)
Checks if textchas are enabled and returns HTML for one,
    or an empty string if they are not enabled.

    @return: unicode result html

Definition at line 149 of file textcha.py.

00149 
00150     def render(self, form=None):
00151         """ Checks if textchas are enabled and returns HTML for one,
00152             or an empty string if they are not enabled.
00153 
00154             @return: unicode result html
00155         """
00156         if self.is_enabled():
00157             question, given_answer = self._extract_form_values(form)
00158             if question is None:
00159                 question = self.question
00160             question_form, given_answer_form = self._make_form_values(question, given_answer)
00161             result = u"""
00162 <div id="textcha">
00163 <span id="textcha-question">%s</span>
00164 <input type="hidden" name="textcha-question" value="%s">
00165 <input id="textcha-answer" type="text" name="textcha-answer" value="%s" size="20" maxlength="80">
00166 </div>
00167 """ % (wikiutil.escape(question), question_form, given_answer_form)
00168         else:
00169             result = u''
00170         return result

Here is the call graph for this function:


Member Data Documentation

Definition at line 90 of file textcha.py.

Definition at line 89 of file textcha.py.

Definition at line 85 of file textcha.py.

Definition at line 41 of file textcha.py.

Definition at line 43 of file textcha.py.

Definition at line 42 of file textcha.py.


The documentation for this class was generated from the following file: