Пример #1
0
def indent_njk(s, width=4, first=False, blank=False, indentfirst=None):
    """Return a copy of the string with each line indented by 4 spaces."""

    # Copied from
    # https://github.com/pallets/jinja/blob/a2f5e2c7972c4d5148c1c75c724e24950d8605bc/jinja2/filters.py#L536-L580
    # to include an unreleased fix for https://github.com/pallets/jinja/pull/826
    # which causes the file upload component to escape HTML markup.
    # TODO: Remove once jinja2 2.11 is released and in use.

    if indentfirst is not None:
        first = indentfirst

    indention = u' ' * width
    newline = u'\n'

    if isinstance(s, Markup):
        indention = Markup(indention)
        newline = Markup(newline)

    s += newline  # this quirk is necessary for splitlines method

    if blank:
        rv = (newline + indention).join(s.splitlines())
    else:
        lines = s.splitlines()
        rv = lines.pop(0)

        if lines:
            rv += newline + newline.join(indention + line if line else line
                                         for line in lines)

    if first:
        rv = indention + rv

    return rv
Пример #2
0
def do_indent(s, width=4, first=False, blank=False, indentfirst=None):
    """Return a copy of the string with each line indented by 4 spaces. The
    first line and blank lines are not indented by default.

    :param width: Number of spaces to indent by.
    :param first: Don't skip indenting the first line.
    :param blank: Don't skip indenting empty lines.

    .. versionchanged:: 2.10
        Blank lines are not indented by default.

        Rename the ``indentfirst`` argument to ``first``.
    """
    if indentfirst is not None:
        warnings.warn(
            "The 'indentfirst' argument is renamed to 'first' and will"
            " be removed in version 3.0.",
            DeprecationWarning,
            stacklevel=2,
        )
        first = indentfirst

    indention = u" " * width
    newline = u"\n"

    if isinstance(s, Markup):
        indention = Markup(indention)
        newline = Markup(newline)

    s += newline  # this quirk is necessary for splitlines method

    if blank:
        rv = (newline + indention).join(s.splitlines())
    else:
        lines = s.splitlines()
        rv = lines.pop(0)

        if lines:
            rv += newline + newline.join(
                indention + line if line else line for line in lines
            )

    if first:
        rv = indention + rv

    return rv
Пример #3
0
def do_indent(
    s: str, width: t.Union[int, str] = 4, first: bool = False, blank: bool = False
) -> str:
    """Return a copy of the string with each line indented by 4 spaces. The
    first line and blank lines are not indented by default.

    :param width: Number of spaces, or a string, to indent by.
    :param first: Don't skip indenting the first line.
    :param blank: Don't skip indenting empty lines.

    .. versionchanged:: 3.0
        ``width`` can be a string.

    .. versionchanged:: 2.10
        Blank lines are not indented by default.

        Rename the ``indentfirst`` argument to ``first``.
    """
    if isinstance(width, str):
        indention = width
    else:
        indention = " " * width

    newline = "\n"

    if isinstance(s, Markup):
        indention = Markup(indention)
        newline = Markup(newline)

    s += newline  # this quirk is necessary for splitlines method

    if blank:
        rv = (newline + indention).join(s.splitlines())
    else:
        lines = s.splitlines()
        rv = lines.pop(0)

        if lines:
            rv += newline + newline.join(
                indention + line if line else line for line in lines
            )

    if first:
        rv = indention + rv

    return rv
Пример #4
0
def indent(s, width=4, first=False, blank=False, indentfirst=None):
    """
    A ported version of the "indent" filter containing a fix for indenting Markup
    objects. If the minion has Jinja version 2.11 or newer, the "indent" filter
    from upstream will be used, and this one will be ignored.
    """
    if indentfirst is not None:
        warnings.warn(
            "The 'indentfirst' argument is renamed to 'first' and will"
            " be removed in Jinja 3.0.",
            DeprecationWarning,
            stacklevel=2,
        )
        first = indentfirst

    indention = " " * width
    newline = "\n"

    if isinstance(s, Markup):
        indention = Markup(indention)
        newline = Markup(newline)

    s += newline  # this quirk is necessary for splitlines method

    if blank:
        rv = (newline + indention).join(s.splitlines())
    else:
        lines = s.splitlines()
        rv = lines.pop(0)

        if lines:
            rv += newline + newline.join(
                indention + line if line else line for line in lines
            )

    if first:
        rv = indention + rv

    return rv
Пример #5
0
	def searchform(self):
		cursor = None

		request = self.request
		cic_view = request.viewdata.cic

		topicsearch_tag = request.matchdict.get('tag')
		model_state = request.model_state
		model_state.method = None
		model_state.schema = SearchValidators

		if not model_state.validate():
			for key in model_state.form.errors:
				del model_state.form.data[key]

		search_step = model_state.value('Step', None)
		age_group_id = model_state.value('AgeGroup', None)
		language_id = model_state.value('LNID', None)
		community_ids = [x for x in model_state.value('CMID', None) or [] if x]
		community_type = model_state.value('CMType', None)
		heading1_ids = [x for x in model_state.value('GHID', None) or [] if x]
		heading2_ids = [x for x in model_state.value('GHID_2', None) or [] if x]
		group1_ids = [x for x in model_state.value('GHID_GRP', None) or [] if x]
		group2_ids = [x for x in model_state.value('GHID_GRP_2', None) or [] if x]

		community_ids = ','.join(map(str, community_ids)) if community_ids else None
		heading1_ids = ','.join(map(str, heading1_ids)) if heading1_ids else None
		group1_ids = ','.join(map(str, group1_ids)) if group1_ids else None
		heading2_ids = ','.join(map(str, heading2_ids)) if heading2_ids else None
		group2_ids = ','.join(map(str, group2_ids)) if group2_ids else None

		log.debug('heading1_ids %s', heading1_ids)

		sql = '''
			DECLARE
				@GHIDList1 varchar(max),
				@GHIDList2 varchar(max),
				@GHGroupList1 varchar(max),
				@GHGroupList2 varchar(max),
				@CMIDList varchar(max),
				@AgeGroupID int,
				@LN_ID int,
				@ViewType int

			SET @GHIDList1 = ?
			SET @GHIDList2 = ?
			SET @GHGroupList1 = ?
			SET @GHGroupList2 = ?
			SET @CMIDList = ?
			SET @AgeGroupID = ?
			SET @LN_ID = ?
			SET @ViewType = ?

			EXEC dbo.sp_CIC_View_s_TSrch @ViewType, ?, ?, @GHIDList1=@GHIDList1 OUTPUT, @GHGroupList1=@GHGroupList1 OUTPUT, @GHIDList2=@GHIDList2 OUTPUT, @GHGroupList2=@GHGroupList2 OUTPUT, @CMIDList=@CMIDList OUTPUT, @CMType=?, @AgeGroupID=@AgeGroupID OUTPUT, @LN_ID=@LN_ID OUTPUT

			SELECT @GHIDList1 AS GHID, @GHIDList2 AS GHID_2, @CMIDList AS CMID, @AgeGroupID AS AgeGroup, @LN_ID AS LNID, @GHGroupList1 AS GHID_GRP, @GHGroupList2 AS GHID_GRP_2

			EXEC dbo.sp_CIC_View_s_BSrch @ViewType
			'''

		with request.connmgr.get_connection() as conn:
			cursor = conn.execute(sql, heading1_ids, heading2_ids, group1_ids, group2_ids, community_ids, age_group_id, language_id, cic_view.ViewType, topicsearch_tag, search_step, community_type)

			topicsearch = cursor.fetchone()

			cursor.nextset()

			criteria = cursor.fetchall()

			cursor.nextset()

			formitems = cursor.fetchall()

			cursor.nextset()

			headings1 = cursor.fetchall()

			cursor.nextset()

			headings2 = cursor.fetchall()

			cursor.nextset()

			communities = cursor.fetchall()

			cursor.nextset()

			agegroups = cursor.fetchall()

			cursor.nextset()

			languages = cursor.fetchall()

			cursor.nextset()

			validated_params = cursor.fetchone()

			cursor.nextset()

			search_info = cursor.fetchone()

			cursor.close()

		searches = {
			'A': agegroups,
			'G1': headings1,
			'G2': headings2,
			'C': communities,
			'L': languages
		}

		if topicsearch is None:
			return HTTPNotFound()

		hidden_fields = [('Step', topicsearch.Step)]
		for searched_item in criteria:
			for i, field in enumerate(search_fields[searched_item.SearchType]):
				values = getattr(validated_params, field, NOT_FROM_DB)
				if values is NOT_FROM_DB:
					value = model_state.value(field)
					if value is None:
						continue
					if not isinstance(value, list):
						hidden_fields.append((field, value))
						continue
					values = value
				elif values is None:
					continue
				else:
					values = str(values).split(',')

				for value in values:
					hidden_fields.append((field, value))

		searched_for_items = [(x.SearchType, searches[x.SearchType]) for x in criteria]
		log.debug('searched_for_items %s', searched_for_items)
		joiner = Markup('</i>%s<i>') % _(' or ')
		searched_for_items = {search_type: joiner.join([x.Name for x in rs]) for search_type, rs in searched_for_items}

		title = _(topicsearch.SearchTitle, request)
		return self._create_response_namespace(title, title, dict(topicsearch=topicsearch, topicsearch_tag=topicsearch_tag, criteria=criteria, formitems=formitems, headings1=headings1, headings2=headings2, communities=communities, agegroups=agegroups, languages=languages, searches=searches, searched_for_items=searched_for_items, search_info=search_info, hidden_fields=hidden_fields, located_near=[]), no_index=True)
Пример #6
0
    def searchform(self):
        cursor = None

        request = self.request
        cic_view = request.viewdata.cic

        topicsearch_tag = request.matchdict.get("tag")
        model_state = request.model_state
        model_state.method = None
        model_state.schema = SearchValidators

        if not model_state.validate():
            for key in model_state.form.errors:
                del model_state.form.data[key]

        search_step = model_state.value("Step", None)
        age_group_id = model_state.value("AgeGroup", None)
        language_id = model_state.value("LNID", None)
        community_ids = [x for x in model_state.value("CMID", None) or [] if x]
        community_type = model_state.value("CMType", None)
        heading1_ids = [x for x in model_state.value("GHID", None) or [] if x]
        heading2_ids = [
            x for x in model_state.value("GHID_2", None) or [] if x
        ]
        group1_ids = [
            x for x in model_state.value("GHID_GRP", None) or [] if x
        ]
        group2_ids = [
            x for x in model_state.value("GHID_GRP_2", None) or [] if x
        ]

        community_ids = ",".join(map(str,
                                     community_ids)) if community_ids else None
        heading1_ids = ",".join(map(str,
                                    heading1_ids)) if heading1_ids else None
        group1_ids = ",".join(map(str, group1_ids)) if group1_ids else None
        heading2_ids = ",".join(map(str,
                                    heading2_ids)) if heading2_ids else None
        group2_ids = ",".join(map(str, group2_ids)) if group2_ids else None

        log.debug("heading1_ids %s", heading1_ids)

        sql = """
            DECLARE
                @GHIDList1 varchar(max),
                @GHIDList2 varchar(max),
                @GHGroupList1 varchar(max),
                @GHGroupList2 varchar(max),
                @CMIDList varchar(max),
                @AgeGroupID int,
                @LN_ID int,
                @ViewType int

            SET @GHIDList1 = ?
            SET @GHIDList2 = ?
            SET @GHGroupList1 = ?
            SET @GHGroupList2 = ?
            SET @CMIDList = ?
            SET @AgeGroupID = ?
            SET @LN_ID = ?
            SET @ViewType = ?

            EXEC dbo.sp_CIC_View_s_TSrch @ViewType, ?, ?, @GHIDList1=@GHIDList1 OUTPUT, @GHGroupList1=@GHGroupList1 OUTPUT, @GHIDList2=@GHIDList2 OUTPUT, @GHGroupList2=@GHGroupList2 OUTPUT, @CMIDList=@CMIDList OUTPUT, @CMType=?, @AgeGroupID=@AgeGroupID OUTPUT, @LN_ID=@LN_ID OUTPUT

            SELECT @GHIDList1 AS GHID, @GHIDList2 AS GHID_2, @CMIDList AS CMID, @AgeGroupID AS AgeGroup, @LN_ID AS LNID, @GHGroupList1 AS GHID_GRP, @GHGroupList2 AS GHID_GRP_2

            EXEC dbo.sp_CIC_View_s_BSrch @ViewType
            """

        with request.connmgr.get_connection() as conn:
            cursor = conn.execute(
                sql,
                heading1_ids,
                heading2_ids,
                group1_ids,
                group2_ids,
                community_ids,
                age_group_id,
                language_id,
                cic_view.ViewType,
                topicsearch_tag,
                search_step,
                community_type,
            )

            topicsearch = cursor.fetchone()

            cursor.nextset()

            criteria = cursor.fetchall()

            cursor.nextset()

            formitems = cursor.fetchall()

            cursor.nextset()

            headings1 = cursor.fetchall()

            cursor.nextset()

            headings2 = cursor.fetchall()

            cursor.nextset()

            communities = cursor.fetchall()

            cursor.nextset()

            agegroups = cursor.fetchall()

            cursor.nextset()

            languages = cursor.fetchall()

            cursor.nextset()

            validated_params = cursor.fetchone()

            cursor.nextset()

            search_info = cursor.fetchone()

            cursor.close()

        searches = {
            "A": agegroups,
            "G1": headings1,
            "G2": headings2,
            "C": communities,
            "L": languages,
        }

        if topicsearch is None:
            return HTTPNotFound()

        hidden_fields = [("Step", topicsearch.Step)]
        for searched_item in criteria:
            for i, field in enumerate(search_fields[searched_item.SearchType]):
                values = getattr(validated_params, field, NOT_FROM_DB)
                if values is NOT_FROM_DB:
                    value = model_state.value(field)
                    if value is None:
                        continue
                    if not isinstance(value, list):
                        hidden_fields.append((field, value))
                        continue
                    values = value
                elif values is None:
                    continue
                else:
                    values = str(values).split(",")

                for value in values:
                    hidden_fields.append((field, value))

        searched_for_items = [(x.SearchType, searches[x.SearchType])
                              for x in criteria]
        log.debug("searched_for_items %s", searched_for_items)
        joiner = Markup("</i>%s<i>") % _(" or ")
        searched_for_items = {
            search_type: joiner.join([x.Name for x in rs])
            for search_type, rs in searched_for_items
        }

        title = _(topicsearch.SearchTitle, request)
        return self._create_response_namespace(
            title,
            title,
            dict(
                topicsearch=topicsearch,
                topicsearch_tag=topicsearch_tag,
                criteria=criteria,
                formitems=formitems,
                headings1=headings1,
                headings2=headings2,
                communities=communities,
                agegroups=agegroups,
                languages=languages,
                searches=searches,
                searched_for_items=searched_for_items,
                search_info=search_info,
                hidden_fields=hidden_fields,
                located_near=[],
            ),
            no_index=True,
        )