Flask - SQLAlchemy italicizing text imported from db - flask

I have a application that I am pulling data from fields in MySQL database. In some of these fields a some of the text needs to be italicized. For example, in species.html I have a table data "species.Notes" that I am pulling out from the MySQL database. That will render something like this on the page when the application is rendered:
Example:
Tall three tip sagebrush is associated with gray horsebrush (Tetradymia canescens).
The text in parenthesis (i.e. species name) need to be italicized, and the rest needs to remain normal text. I have tried storing the information in the MySQL database with html tags around Tetradymia canescens
<i></i>
<html><i></i></htm>
but that did not work, and I have not been able to find other suggestions online.
For reference (if needed?), here is how the information is being pulled into the html page where it will be displayed.
species.html
<tr>
<th scope="row">Additional Species Information</th>
<td colspan="3">{{ species.Notes }}</td>
</tr>
<tr>

You can use Jinja filters to introduce the italics tag like this:
<td colspan="3">
{{ species.Notes|replace('(', '<i>(')|replace(')', ')</i>')|safe }}
</td>
Ideally you'd preprocess the text with a regex expression and pass it to the template with the tags already there, then you would just need the safe filter alone.

Related

Github-pages/kramdown won't render my mixed markdown/html correctly

I've got a mixed HTML/Markdown page that I thought was pretty simple, but Github-pages giving inexplicable renderings.
It's configured to use kramdown/GFM.
This is the piece of code that's getting me:
QuickFIX Settings
-----------------
* [Session](#session)
* [Validation](#validation)
* [Initiator](#initiator)
* [Acceptor](#acceptor)
* [Socket Configuration](#socketConfiguration)
* [Storage](#storage)
* [File Storage](#file)
* [Logging](#logging)
* [SSL](#ssl)
* [Sample Settings File](#sample)
<a name="session"/>
## Session
<table>
<tr>
<th>Setting</th>
<th>Description</th>
<th>Valid Values</th>
<th>Default</th>
</tr>
[more table code follows...]
When I preview the markdown code in other Markdown parsers (e.g. the Issue description parser), it's fine. But when I push it to the gh-pages branch, I get this kind of garbage:
Why are those tr-close and table-close tags there? (No table even started yet!) And why does the table actually appear under the next header?
Nothing about this makes sense!
It's an open-source project, so you can see the current live render and source right here:
page: http://quickfixn.org/tutorial/configuration.html
source: https://raw.githubusercontent.com/connamara/quickfixn/gh-pages/tutorial/configuration.md
correct rendering by github view: https://github.com/connamara/quickfixn/blob/gh-pages/tutorial/configuration.md
The reason why your table is broken is because of an unbalanced (unclosed) td in your table, effectively breaking the HTML flow:
<tr>
<td class='setting'>SendRedundantResendRequests</td>
<td class='description'>
<!-- ^-- this td element is never closed -->
If set to Y, QuickFIX will send all necessary resend requests, even if they appear redundant.
Some systems will not certify the engine unless it does this.
When set to N, QuickFIX will attempt to minimize resend requests.
This is particularly useful on high volume systems.
<td class='valid'>
<div>Y</div>
<div>N</div>
</td>
<td class='default'>N</td>
</tr>

How to use the frame attribute for table in QTextDocument, even though it isn't supported?

I'm trying to create a document using QtTextdocument:: setHtml function. The problem is according to the link below not all attributes are available for to me use from html.
http://doc.qt.io/qt-4.8/richtext-html-subset.html
Here is what I want to do
I have the following html which I want to print to a pdf using (QtTextdocument)
<table width=100% frame='box'>
<tr align='left'>
<th>For</th>
<th>Myself</th>
</tr>
<tr align='left'>
<th>Attention</th>
<th>Mother</th>
</table>
The Html produces a table with a simple frame. The problem is the attribute "frame" is not within the supported Html Subset for Qt as indicated by the link here. The table tag is supported but not the attribute frame.
Please note that I had already tried using the attribute "border" and setting it to the value "1|0" but it gave borders around the table cells as well which is not what I want.
Here is the C++ code to do it
QTextDocument *document;
QPrinter printer;
Qstring html="<table width=100% frame='box'><tr align='left'><th>For</th>"
"<th>Myself</th>"+
"</tr>"+
"<tr align='left'>"+
"<th>Attention</th>"+
"<th>Mother</th>"+
"</table>";
document->setHtml(html);
printer.setOutputFormat(QPrinter::PdfFormat);
printer.setPaperSize(QPrinter::A4);
printer.setPageMargins(QMarginsF(15, 15, 15, 15));
printer.setOutputFileName("./Report.pdf");
document->print(&printer);
My problem again
When I check the pdf, the table doesn't have the outside frame I want. Does anyone know a way around this? All I need is a black box around the table.
The Frame attribute of Table HTML tag is not supported by Qt.
Instead Border is supported. I tried with Border, I am able to see the border.
The below link has the list attributes supported for Table tag. Search for "Supported Tags" in below link.
http://doc.qt.io/qt-5/richtext-html-subset.html
You can see that for Table tag,
Supports the following attributes: border, bgcolor (Qt color names or #RRGGBB), cellspacing, cellpadding, width (absolute or relative), and height.
I tried below change in your code. I see borders.
<table width=100% border = \"2\" >
I solved it in the following manner. The idea is simply applying a black canvas under a white sheet.
I modified the html to be the following
<table width=100% style='background-color:#000;'>
<tr>
<td style='padding:1px '>
<table width= 100% style='background-color:#fff;'>
<tr align='left'>
<th>For</th>
<th>Myself</th>
</tr>
<tr align='left'>
<th>Attention</th>
<th>Mother</th>
</table>
</td>
</tr>
</table>
Now I have a black border of 1px around the content and it is displayed very nicely on the pdf. ENJOY

XSS remediation - Improper Neutralization of Script-Related HTML Tags

I'm trying to fix some XSS errors with my code. #getEmailRecord is the line that contains the problem. How do I fix a piece of code like this? The error: Improper Neutralization of Script-Related HTML Tags in a Web Page (Basic XSS). Veracode cleansing solution: coldfusion.runtime.CFPage.HTMLEditFormat
tr>
<td> </td>
<td class="left"><b>To: </b></td>
<td class="left">#getEmailRecord.EMAIL_TO#</td></tr>
<tr><td colspan="4"> </td></tr>
Thanks! This is my first time doing something like this so any help is much appreciated.
Veracode cleansing solution: coldfusion.runtime.CFPage.HTMLEditFormat The recommended solution tells you what to do. Wrap any variables which contain user supplied data that you utilize in your code in #HTMLEditFormat()#.
<td class="left">#HTMLEditFormat(getEmailRecord.EMAIL_TO)#</td></tr>
HTMLEditFormat
Description
Replaces special characters in a string with their HTML-escaped equivalents.
Add if you are on ColdFusion 10 or newer you have even more options - EncodeFor Functions

How to make a plone view that inserts other smaller views of content items?

I think this should be simple. I have a folderish TTW dexterity content item (a drop box) that contains folderish TTW dexterity items (proposals). Each proposal contains TTW dexterity reviews that have fields I want to summarize.
I can easily make a view that generates a table as indicated below for any proposal with simple modifications to the folderlisting view:
[review1 link] [criterion_1 value] [criterion-2 value]...
[review2 link] [criterion_1 value] [criterion-2 value]...
.
.
I can also generate a working table view for a drop box by modifying the folderlisting view:
[proposal1 link] [column I would like to insert the above table in for this proposal]
[proposal2 link] [column I would like to insert the above table in for this proposal]
.
.
My problem is I cannot figure out how to insert the first table into the cells in the second column of the second table. I've tried two things:
Within the view template for the dropbox listing, I tried duplicating the repeat macro of the listingmacro, giving it and all its variables new names to have it iterate on each proposal. This easily accesses all of the Dublin core schemata for each review, but I cannot get access to the dexterity fields. Everything I have tried (things that work when generating the first table) yield LocationError and AttributeError warnings. Somehow when I go down one level I lose some of the information necessary for the view template to find everything. Any suggestions?
I've also tried accessing the listing macro for the proposal, with calls like <metal use-macro="item/first_table_template_name/listing"/>. Is this even partially the right approach? It gives no errors, but also does not insert anything into my page.
Thanks.
This solution is loosely based on the examples provided by kuel: https://github.com/plone/Products.CMFPlone/blob/854be6e30d1905a7bb0f20c66fbc1ba1f628eb1b/Products/CMFPlone/skins/plone_content/folder_full_view.pt and https://github.com/plone/Products.CMFPlone/blob/b94584e2b1231c44aa34dc2beb1ed9b0c9b9e5da/Products/CMFPlone/skins/plone_content/folder_full_view_item.pt. --Thank you.
The way I found easiest to create and debug this was:
Create a minimalist template from the plone standard template folder_listing.pt which makes just the table of summarized review data for a single proposal. The template is just for a table, no header info or any other slots. This is a stripped version, but there is nothing above the first statement. A key statement that allowed access to the fields were of the form:
python: item.getObject().restrictedTraverse('criterion_1')
The table template:
<table class="review_summary listing">
<tbody><tr class="column_labels"><th>Review</th><th>Scholarly Merit</th><th>Benefits to Student</th><th>Clarity</th><th>Sum</th></tr>
<metal:listingmacro define-macro="listing">
<tal:foldercontents define="contentFilter contentFilter|request/contentFilter|nothing;
contentFilter python:contentFilter and dict(contentFilter) or {};
I kept all the standard definitions from the original template.
I have just removed them for brevity.
plone_view context/##plone;">
The following tal:sum is where I did some math on my data. If you are
not manipulating the data this would not be needed. Note that I am only
looking at the first character of the choice field.
<tal:sum define="c1_list python:[int(temp.getObject().restrictedTraverse('criterion_1')[0])
for temp in batch if temp.portal_type=='ug_small_grants_review'];
c1_length python: test(len(c1_list)<1,-1,len(c1_list));
c2_list python:[int(temp.getObject().restrictedTraverse('criterion_2')[0])
for temp in batch if temp.portal_type=='ug_small_grants_review'];
c2_length python: test(len(c2_list)<1,-1,len(c2_list));
c1_avg python: round(float(sum(c1_list))/c1_length,2);
c2_avg python: round(float(sum(c2_list))/c2_length,2);
avg_sum python: c1_avg+c2_avg;
">
<tal:listing condition="batch">
<dl metal:define-slot="entries">
<tal:entry tal:repeat="item batch" metal:define-macro="entries">
<tal:block tal:define="item_url item/getURL|item/absolute_url;
item_id item/getId|item/id;
Again, this is the standard define from the folder_listing.pt
but I've left out most of it to save space here.
item_samedate python: (item_end - item_start < 1) if item_type == 'Event' else False;">
<metal:block define-slot="entry"
The following condition is key if you can have things
other than reviews within a proposal. Make sure the
item_type is proper for your review/item.
tal:condition="python: item_type=='ug_small_grants_review'">
<tr class="review_entry"><td class="entry_info">
<dt metal:define-macro="listitem"
tal:attributes="class python:test(item_type == 'Event', 'vevent', '')">
I kept all the standard stuff from folder_listing.pt here.
</dt>
<dd tal:condition="item_description">
</dd>
</td>
The following tal:comp block is used to calculate values
across the rows because we do not know the index of the
item the way the batch is iterated.
<tal:comp define = "crit_1 python: item.getObject().restrictedTraverse('criterion_1')[0];
crit_2 python: item.getObject().restrictedTraverse('criterion_2')[0];
">
<td tal:content="structure crit_1"># here</td>
<td tal:content="structure crit_2"># here</td>
<td tal:content="structure python: int(crit_1)+int(crit_2)"># here</td>
</tal:comp>
</tr>
</metal:block>
</tal:block>
</tal:entry>
</dl>
<tr>
<th>Average</th>
<td tal:content="structure c1_avg"># here</td>
<td tal:content="structure c2_avg"># here</td>
<td tal:content="structure avg_sum"># here</td>
</tr>
</tal:listing>
</tal:sum>
<metal:empty metal:define-slot="no_items_in_listing">
<p class="discreet"
tal:condition="not: folderContents"
i18n:translate="description_no_items_in_folder">
There are currently no items in this folder.
</p>
</metal:empty>
</tal:foldercontents>
</metal:listingmacro>
</tbody></table>
Create another listing template that calls this one to fill the appropriate table cell. Again, I used a modification of the folder_listing.pt. Basically within the repeat block I put the following statement in the second column of the table:
This belongs right after the </dd> tag ending the normal item listing.
</td> <td class="review_summary">
<div tal:replace="structure python:item.getObject().ug_small_grant_review_summary_table()" />
</td>
Note that "ug_small_grant_review_summary_table" is the name I gave to the template shown in more detail above.

CFdump and Bootstrap tooltips fight each other

I attach Bootstrap tooltips via
$("[title]").tooltip({ html: true });
When I use a <cfdump>, title tags are attached all over the place. The start of the <cfdump> html looks like this
<table class="cfdump_struct">
<tr><th class="struct" colspan="2" onClick="cfdump_toggleTable(this);" style="cursor:pointer;" title="click to collapse">struct</th></tr>
<tr>
<td class="struct" onClick="cfdump_toggleRow(this);" style="cursor:pointer;" title="click to collapse">Cause</td>
<td>
Is there a way to keep, the two from stepping on eachother?
You shouldn't care because cfdump shouldn't be used in production, however you could just reduce the array returned by the jQuery selector. Not sure if this is the best way to do it, but it works:
$("[title]").filter(function(){
return ($(this).closest(".cfdump_struct").length == 0);
}).tooltip({ html: true });
It runs the filter function for each item in the array returned by the selector. If it is within the CFDUMP table (signified by the .cfdump_struct class) it will not return it. You will have to extend this to other cfdump types (queries, etc) but this should get you started.
Again, it really shouldn't matter since you shouldn't be using cfdump in production code anyway.
You can see this in action here: http://jsfiddle.net/seancoyne/rc7TL/