def get_shared_context(self, _from, _to, *args, **kwargs): """ Return some things that are in all templates. Additional kwargs passed here will end up in the context passsed to templates. """ current_pair_settings, orig_pair_opts = current_app.config.resolve_original_pair( _from, _to) o_pair = orig_pair_opts.get('orig_pair') if orig_pair_opts.get('orig_pair') != (): orig_from, orig_to = o_pair else: orig_from, orig_to = _from, _to current_search_variant = False if 'search_variant_type' in kwargs: _t = kwargs['search_variant_type'] variants = [ v for v in current_pair_settings.get('search_variants') if v.get('type') == _t ] if len(variants) > 0: current_search_variant = variants[0] shared_context = { 'display_swap': self.get_reverse_pair(_from, _to), 'current_pair_settings': current_pair_settings, 'current_variant_options': orig_pair_opts.get('variant_options'), 'current_search_variant': current_search_variant, 'current_locale': get_locale(), '_from': _from, '_to': _to, 'orig_from': orig_from, 'orig_to': orig_to, 'last_searches': session.get('last_searches-' + current_app.config.short_name, []), } shared_context.update(**kwargs) # Render some additional templates search_info = current_app.lexicon_templates.render_individual_template( _from, 'search_info.template', **shared_context) search_form = current_app.lexicon_templates.render_individual_template( _from, 'index_search_form.template', **shared_context) shared_context['search_info_template'] = search_info shared_context['index_search_form'] = search_form shared_context.update(**orig_pair_opts) return shared_context
def force_locale(self, _from, _to): from flask.ext.babel import refresh pair_settings, orig_pair_opts = current_app.config.resolve_original_pair( _from, _to) opts = orig_pair_opts.get('variant_options') current_locale = get_locale() if opts and ('force_locale' in opts): layout_forces = opts.get('force_locale', {}) if current_locale in layout_forces: session['force_locale'] = session['locale'] = layout_forces[ current_locale] # Refresh the localization infos, and send the user back whence they # came. refresh() else: try: del session['force_locale'] except: pass return False
def check_notice(): from jinja2 import TemplateNotFound _from, _to = current_app.config.default_language_pair project_notice = current_app.lexicon_templates.render_individual_template( _from, 'notice.template', **{'current_locale': get_locale()}) return {'project_notice': project_notice}
def post(self, variant_type, _from, _to): """ The only difference here between the normal type of search is that there's an extra argument in the URL, to select the variant type. This is easy to extract, and once removed and included elsewhere in the search everything else is the same. """ self.variant_type = variant_type return self.search_args(variant_type, _from, _to, get_locale(), 'POST')
def search_to_detailed_context(self, lookup_value, **search_kwargs): # TODO: There's a big mess contained here, and part of it # relates to lexicon formatters. Slowly working on unravelling # it. # TODO: this can probably be generalized to be part of the last # function. errors = [] search_result_obj = self.do_search_to_obj(lookup_value, generate=True, **search_kwargs) template_results = [{ 'input': search_result_obj.search_term, 'lookups': search_result_obj.formatted_results_sorted # 'analyses': search_result_obj.analyses }] logIndexLookups( search_result_obj.search_term, template_results, g._from, g._to, ) show_info = False if len(errors) == 0: errors = False search_context = { 'result': search_result_obj.formatted_results_sorted, # These variables can be turned into something more general 'successful_entry_exists': search_result_obj.successful_entry_exists, 'word_searches': template_results, 'analyses': search_result_obj.analyses, 'analyses_without_lex': search_result_obj.analyses_without_lex, 'user_input': search_result_obj.search_term, 'current_locale': get_locale(), # ? 'errors': errors, # is this actually getting set? 'show_info': show_info, 'language_pairs_other_results': search_result_obj.other_results, 'debug_text': search_result_obj.debug_text } return search_context
def dispatch_request(self): redir = self.maybe_do_mobile_redirect() if redir: return redir reverse_exists = \ current_app.config.dictionaries.get( ( self.default_to , self.default_from ) , False ) current_pair_settings, orig_pair_opts = current_app.config.resolve_original_pair( self.default_from, self.default_to) template_context = self.get_shared_context(self.default_from, self.default_to) o_pair = orig_pair_opts.get('orig_pair') if orig_pair_opts.get('orig_pair') != (): orig_from, orig_to = o_pair else: orig_from, orig_to = self.default_from, self.default_to template_context.update({ 'display_swap': reverse_exists, 'swap_from': self.default_to, 'swap_to': self.default_from, 'show_info': True, 'current_pair_settings': current_pair_settings, 'current_variant_options': orig_pair_opts.get('variant_options'), 'current_locale': get_locale(), '_from': self.default_from, '_to': self.default_to, 'orig_from': orig_from, 'orig_to': orig_to, }) return render_template(self.template_name, **template_context)
def add_current_locale_code(): return {'current_locale': get_locale()}
def footer_template(): _from, _to = current_app.config.default_language_pair footer_template = current_app.lexicon_templates.render_individual_template( _from, 'footer.template', **{'current_locale': get_locale()}) return {'footer_template': footer_template}
def get(self, _from, _to, lemma): # TODO: submit and process on separate thread, optional poll # argument to not submit and just check for result # Check for cache entry here self.check_pair_exists_or_abort(_from, _to) user_input = lemma = decodeOrFail(lemma) pretty = request.args.get('pretty', False) # Generation constraints pos_filter = request.args.get('pos', False) e_node = request.args.get('e_node', False) # This is the same self.lemma_match = user_input _split = True _non_c = True _non_d = False search_kwargs = { 'split_compounds': _split, 'non_compounds_only': _non_c, 'no_derivations': _non_d, } has_analyses = False cache_key = '+'.join( [a for a in [ _from, lemma, pos_filter, e_node, ] if a]) paradigms = current_app.cache.get(cache_key.encode('utf-8')) if paradigms is None: paradigms = \ self.search_to_paradigm(user_input, detailed=True, **search_kwargs) # current_app.cache.set(cache_key.encode('utf-8'), paradigms, timeout=None) from morphology.utils import tagfilter ui_locale = get_locale() def filter_tag(f): filtered_tag = tagfilter(f.tag, g._from, g._to).split(' ') return (f.input, filtered_tag, [f.form], f.tag.tag_string) paradigms = [map(filter_tag, _p) for _p in paradigms] _tagsets = current_app.config.morphologies.get(g._from).tagsets.sets tagsets_serializer_ready = {} for key, ts in _tagsets.iteritems(): tagsets_serializer_ready[key] = [m.val for m in ts.members] if pretty: resp_fx = json_response_pretty else: resp_fx = json_response return resp_fx({ 'paradigms': paradigms, 'tagsets': tagsets_serializer_ready, 'input': { 'lemma': lemma, 'pos': pos_filter, } })