def draw_field(v: FieldView, locale: Locale, theme: Theme, numbers_right: bool = False, border: bool = False): name_label, view = v header = py_.chain(locale.value).map_( lambda x: "{: ^{}}".format(x, CELL_WIDTH)).join().value() name_header = '{:<{}}'.format(name_label, CELL_WIDTH * len(locale.value)) if numbers_right: name_header = ' '.join([name_header, ' ' * CELL_WIDTH]) header = ' '.join([header, ' ' * CELL_WIDTH]) else: header = ' '.join([' ' * CELL_WIDTH, header]) name_header = ' '.join([' ' * CELL_WIDTH, name_header]) sub_header = None if border: sub_header = "─" * CELL_WIDTH * len(locale.value) if numbers_right: sub_header += '─┐ ' + ' ' * CELL_WIDTH header += ' ' name_header += ' ' else: sub_header = ' ' * CELL_WIDTH + ' ┌─' + sub_header header = ' ' + header name_header = ' ' + name_header raw_rows = py_.map_( view, lambda row: py_.chain(row).map_(theme.value.get).join().value()) line_numbers_fmt = '{{: {}{}}}'.format('<' if numbers_right else '>', CELL_WIDTH) line_numbers = py_.map_(list(range(1, FIELD_DIMENSIONS.i + 1)), line_numbers_fmt.format) if numbers_right: columns = py_.zip_(raw_rows, line_numbers) else: columns = py_.zip_(line_numbers, raw_rows) column_separator = ' ' if border: column_separator = ' │ ' lines = [name_header, header] if border: lines.append(sub_header) lines += py_.map_(columns, column_separator.join) return py_.join(lines, '\n')
def frontpage(): """Render the front-page with the latest Knowledge Packages added.""" # retrieving the required values latest_records = get_latest_knowledge_packages(3) engagement_priority_topics_available = get_engagement_priority_topics_available( params={ "q": "props.icon:labels*", "size": 25 }).to_dict() # selecting only items with icons available py_.set( engagement_priority_topics_available, "hits.hits", (py_.chain(engagement_priority_topics_available).get( "hits.hits", []).filter(lambda x: py_.get(x, "props.icon") is not None). map(lambda x: py_.set_( x, "props.icon", url_for("static", filename=x["props"]["icon"]))) ).value(), ) # rendering! return render_template( "geo_knowledge_hub/frontpage/frontpage.html", latest_records=latest_records, engagement_priority_topics_available= engagement_priority_topics_available, )
def prepare_record_topics( record: Dict, record_engagement_priorities_metadata: List[Dict]) -> List: """Prepare the record topics (Engagement priorities and target users) to use into the UI. Note: In the created topics list, we only include the engagement priorities without icon, since these items are presented in a image carousel. Args: record (Dict): Record Object serialized as UI Dict. record_engagement_priorities_metadata (List[Dict]): List of engagement priorities metadata object (as dict). Returns: List: List with the topics associated with the record """ # preparing the engagement priorities topics default_scheme = "Engagement Priorities" # for engagements # getting the engagement objects with titles l10n engagement_titles_l10n = py_.get(record, "ui.engagement_priorities", []) # indexing the l10n objects engagement_titles_l10n = {x["id"]: x for x in engagement_titles_l10n} record_engagement_priorities = ( py_.chain(record_engagement_priorities_metadata).filter( lambda x: py_.get(x, "props.icon") == "").map( lambda x: { "scheme": default_scheme, "title": engagement_titles_l10n[x["id"]]["title_l10n"], "model_field": "metadata.engagement_priorities", })).value() # preparing the target users topics default_scheme = "Target Audience" # for users # getting the target audience with titles l10n target_audiences = (py_.chain(record).get("ui.target_audiences", []).map( lambda x: { "scheme": default_scheme, "title": x["title_l10n"], "model_field": "metadata.target_audiences", })).value() return py_.mapcat([target_audiences, record_engagement_priorities])
def group_by_keys(dicts: Iterable[Dict[str, T]], default_value: Optional[T] = None) -> Dict[str, List[T]]: all_keys = py_.chain(dicts).flat_map(lambda d: list(d.keys())).uniq().value() # ^ это эквивалентно вот этому: # all_keys = py_.flat_map(dicts, lambda d: list(d.keys())) # all_keys = py_.uniq(all_keys) values = py_.map_(all_keys, lambda key: py_.invoke_map(dicts, 'get', key, default_value)) return py_.zip_object(all_keys, values)
def chain(self): """Return pydash chaining instance with items returned by :meth:`all`. See Also: `pydash's <http://pydash.readthedocs.org/>`_ documentation on `chaining <http://pydash.readthedocs.org/en/latest/chaining.html>`_ """ return py_.chain(self.all())
def get_engagement_priority_from_record( identity: Identity, record: Record) -> Union[None, List[Dict]]: """Retrieve the Engagement Priority metadata associated with a record. Args: identity (flask_principal.Identity): User identity record (invenio_records.Record): Record API Object from where the engagement priorities must be extracted. Returns: Union[None, List[Dict]]: None or the engagement priorities metadata (as dict). """ # getting the engagement priority topics result = None record_engagement_priorities = py_.get(record, "metadata.engagement_priorities", []) record_engagement_priorities_ids = py_.map(record_engagement_priorities, lambda x: x["id"]) if record_engagement_priorities_ids: record_engagement_priorities = vocabulary_service.read_many( identity=identity, type="engagementprioritiestypes", ids=record_engagement_priorities_ids, ).to_dict() result = (py_.chain(record_engagement_priorities).get( "hits.hits", []).map( lambda x: py_.set_( x, "props.icon", url_for("static", filename=py_.get(x, "props.icon")), ) if py_.get(x, "props.icon") != "" else x, )).value() return result