Accessing MatchedParameters from an if statement in set-body - azure-api-management

In an APIM policy for a REST API, I'm trying to create a SOAP request for the backend service.
The URI of the REST request is /GetCustomer/{accountNumber}.
In the Liquid if statement below, I'm trying to check if the MatchedParameters collection contains the account number. This doesn't work, I get the value from the else branch.
I tried a couple of variations:
{% if {{context.Request.MatchedParameters.ContainsKey("accountNumber")}} %}
{% if #(context.Request.MatchedParameters.ContainsKey("accountNumber")) %}
{% if context.Request.MatchedParameters.ContainsKey("accountNumber") %}
<set-body template="liquid">
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://my-services.com/CustomerService/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<GetCustomer>
<request>
{% if {{context.Request.MatchedParameters.ContainsKey("accountNumber")}} %}
<AccountNumber>{{context.Request.MatchedParameters["accountNumber"]}}</AccountNumber>
{% else %}
<AccountNumber xsi:nil="true" />
{% endif %}
</request>
</GetCustomer>
</soap:Body>
</soap:Envelope>
</set-body>
Leaving the if statement out works correctly, then the request is constructed as I expect:
<set-body template="liquid">
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://my-services.com/CustomerService/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soap:Body>
<GetCustomer>
<request>
<AccountNumber>{{context.Request.MatchedParameters["accountNumber"]}}</AccountNumber>
</request>
</GetCustomer>
</soap:Body>
</soap:Envelope>
</set-body>
So the question is: is it possible to access the context from within a Liquid if statement? And if so, what is the correct syntax?

I test it in my side, you just need to write the liquid like:
{% if context.Request.MatchedParameters["accountNumber"] != null %}
{% else %}
{% endif %}
And by the way: According to my test, if you specify a {accountNumber} in the request url of APIM. We have to requsest it with ...../GetCustomer/{accountNumber}, if we request it with ...../GetCustomer, it will show 404 error. So you do not need to worry about "if context.Request.MatchedParameters["accountNumber"] is exist"

Related

Condition based on the three first letters of a string?

In my Jinja template, model.DataType value can be user defined or built in. My requirenement is if model.DataType start with the three letters ARR, then do a specific operation.
Example of values:
ARRstruct124
ARR_int123
ARR123123
CCHAR
UUINT
etc.
{% set evenDataType = model.eventDataType %}
{%if evenDataType | regex_match('^ARR', ignorecase=False) %}
// do the operation
{%else%}
// do the operation
{% endif %}
With this template, I am getting the error
{%if evenDataType | regex_match('^ARR', ignorecase=False) %}
jinja2.exceptions.TemplateAssertionError: no filter named 'regex_match'
There is indeed no regex_match filter in the Jinja builtin filters. You might have found some examples using it, but this is an additional filter provided by Ansible, so it won't work outside of Ansible.
This said, your requirement does not need a regex to be fulfilled, you can use the startswith() method of a Python string.
So, you template should be:
{% set evenDataType = model.eventDataType %}
{% if evenDataType.startswith('ARR') %}
`evenDataType` starts with 'ARR'
{% else %}
`evenDataType` does not starts with 'ARR'
{% endif %}

How to detect change in HTML page after a successful redirect from form?

views.py
class ChangePasswordView(PasswordChangeView):
form_class = ChangePasswordForm
success_url = reverse_lazy("login")
login.html
{% if XXXXXX %}
<p style="color:green;">Password changed successfully! Please login.</p>
{% endif %}
Basically I want the following message to appear on my login html page if password was changed. Is there a way to detect if form was successful (by passing some parameter to HTML), or if user was redirected from certain URL to current html page?
Instead of writing a specific message, it might be easier to work with Django's message framework.
You can use the MessageSuccessMixin to add a message to the session, so:
from django.contrib.messages.views import SuccessMessageMixin
class ChangePasswordView(SuccessMessageMixin, PasswordChangeView):
form_class = ChangePasswordForm
success_url = reverse_lazy('login')
success_message = 'Password changed successfully! Please login.'
Usually on all pages you then write logic to report messages to the user, as specified in the documentation:
{% if messages %}
<ul class="messages">
{% for message in messages %}
<li{% if message.tags %} class="{{ message.tags }}"{% endif %}>{{ message }}</li>
{% endfor %}
</ul>
{% endif %}
In case there are messages, that page will deliver these to the user, and remove these from the session to prevent showing these a second time. It is thus a way to add messages that will (later) be reported to the user when they visit the next page.

How do I display a message in Jinja (flask) if there is no object data to display

I have an object in python views.py that references the events database table. When there is data, it displays the data in my html template, however, when there is no data, I cannot figure out the {% if %} function that would display the message "No data found."
I have tried Tadeck's post, but the is defined always seems to evaluate to true even if there is no data to display. Thank you for your help.
{% if events is defined %}
value of variable: {{ events }}
{% else %}
variable is not defined
{% endif %}
views.py
events = db.session.query(Eventdetails, Buyers).\
join(Buyers).\
filter(Eventdetails.events_id == event_id)
return render_template(
self.template_file, events=events, the_event=the_event,
event_id=event_id
)
You are passing events as the query. You want it to be the query results:
events = db.session.query(Eventdetails, Buyers).\
join(Buyers).\
filter(Eventdetails.events_id == event_id).all()
Things to try:
{% if events %}
{% if events|length > 0 %}
{% if events != [] %}
This will help you. When event have data then it will go inside the if condition otherwise else will be execute No data found.
{% if events %}
value of variable: {{ events }}
{% else %}
No data found.
{% endif %}

Jekyll's sitemap plugin - exclude assets pages

I'm using jekyll-sitemap plugin for Jekyll.
Is there a way to exclude .html files within the assets folder?
Some of them contain some HTML examples and I end up having things like the following in my sitemaps.xml, which doesn't make sense:
<url>
<loc>https://example.com/blog/assets/vanilla-lazyload/demos/with_picture.html</loc>
<lastmod>2017-11-18T15:05:22+01:00</lastmod>
</url>
Where with_picture.html is a demo file of a Javascript library, that comes with it when using npm install (and I can't be bother to remove those every time for every library)
According to the docs, using sitemap: false in our front matter should solve it, but it doesn't seem to be working at all.
Because I do not have any front matter in those vendor files, I'm using the Jekyll's Front Matter defaults method to do so, but without success.
# in my _config.yml
defaults:
- scope:
path: 'assets/'
values:
sitemap: false
I also tried the following path without luck:
path: "assets"
Might it be that path: 'assets' wont' take into account subfolders?
If your Jekyll version is v3.7.2 or higher and your jekyll-sitemap version is v1.2.0 or higher, this should work:
defaults:
-
scope:
path: 'assets/**'
values:
sitemap: false
The ** will match any file in the assets directory or in any of its subdirectories.
Here's the relevant section in the docs.
You can easily create your own sitemap without the plugin: Create a file called ‘sitemap.xml’ in your main jekyll folder, as in next to _post, _pages, and _includes.
All the file needs to contain is the following:
---
layout: null
---
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd" xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
{% for post in site.posts %}
<url>
<loc>{{ site.url }}{{ post.url }}</loc>
{% if post.lastmod == null %}
<lastmod>{{ post.date | date_to_xmlschema }}</lastmod>
{% else %}
<lastmod>{{ post.lastmod | date_to_xmlschema }}</lastmod>
{% endif %}
<changefreq>monthly</changefreq>
<priority>1.0</priority>
</url>
{% endfor %}
{% for page in site.pages %}
{% if page.sitemap != null and page.sitemap != empty %}
<url>
<loc>{{ site.url }}{{ page.url }}</loc>
<lastmod>{{ page.sitemap.lastmod | date_to_xmlschema }}</lastmod>
<changefreq>{{ page.sitemap.changefreq }}</changefreq>
<priority>{{ page.sitemap.priority }}</priority>
</url>
{% endif %}
{% endfor %}
</urlset>
In your _config.yml file, add :
exclude :
- assets/vanilla-lazyload/demos

Get data from data folder?

I need to get some data using a dynamic name from front matter:
{{ site.data.prd-[page.tag].title" }}
The above fails to get the string from the data folder and nothing is output.
Where am I going wrong?
I am not sure about what you are trying to do but how about make a dictionary type data in _data directory and search for a title that matches page.tag in desired pages?
_data.prd-tags.yml
For each item, we can access the key and value using index ([0]: key, [1]: value).
jekyll: "jekyll's title"
ruby: "ruby's title"
html: "html's title"
.
.
.
HTML fragment for searching and displaying a tag (if found)
Iterate over site.data.prd-tags and display title if an item that matches page.tag is found.
<div class="tag-title">
{% for tag in site.data.prd-tags %}
{% if tag[0] == page.tag %}
{{ tag[1] }}
{% else %}
Nothing found
{% endif %}
{% endfor %}
</div>