def smart_get(value, cls=uni_cls, delimiter=','): """ Smart convert function, which convert value to cls. """ if issubclass(cls, uni_cls): return uni(value) if issubclass(cls, str): return value if issubclass(cls, bool): if value.lower() in ('true', 'yes', '1', 'on', 'enable'): return True elif value.lower() in ('false', 'no', '0', 'off', 'disable'): return False else: raise ValueError("%s is not boolean value" % value) if cls in (list, tuple): if value: return cls(map(lambda s: uni(s.strip()), value.split(delimiter))) return cls() else: return cls(value)
def form_send(req): locale = req.args.getfirst('locale', get_lang(req), uni) menu = req.cfg.get_static_menu(req) fdict = dict((key, uni(req.form.getvalue(key))) for key in req.form.keys()) form_obj = req.cfg.forms[req.uri.split('/')[-1]] qid, question, answer = (0, '', form_obj.answer) status = None if req.method == 'POST': if form_obj.protection: robot = True if req.form.getfirst("robot", "", str) else False if not form_obj.answer: qid = int(req.form.getfirst("qid", '0', str), 16) question, answer = robot_questions[qid] check = req.form.getfirst("answer", "", str) == answer else: robot = False check = True # endif required = [] # check email if exist if 'email' in req.form: if not Email.check(req.form.getfirst("email", fce=str)): required.append('email') for it in form_obj.required: if it not in req.form: required.append(it) if robot or not check or required: return generate_page( req, req.cfg.form_web_templates + form_obj.template + '.html', question=question, answer=answer, qid=hex(qid), form=fdict, menu=menu, lang=locale, required=required, robot=robot, check=check) # else kwargs = {'logger': req.log_info} if 'email' in req.form: kwargs['reply'] = req.form.getfirst('email', '', str) status = False try: req.smtp.send_email_txt( form_obj.subject, # subject form_obj.recipient, # recipient morias_template( req, req.cfg.form_mail_templates + form_obj.template + '.txt', form=fdict).encode('utf-8'), # body **kwargs) # logger + reply fdict = {} status = True except Exception as e: req.log_error('Mail form: %s', str(e), state.LOG_ERROR) status = False # else (reg.method != POST) if form_obj.protection and not form_obj.answer: qid = randint(0, len(robot_questions)-1) question, answer = robot_questions[qid] return generate_page( req, req.cfg.form_web_templates + form_obj.template + '.html', question=question, answer=answer, qid=hex(qid), form=fdict, menu=menu, lang=locale, status=status)
def make_uri(title): if not isinstance(title, unicode): title = uni(title) title = unidecode(title.lower()) return sub('\W+', '_', title).strip('_')