Web2py: Database queries from within layout.html - mongodb

I'm interested in displaying recently searched terms (stored in a database) on every page of my application. At first glance, it seems like layout.html would be the best place for this function. I'd need to run database queries, and they'd look something like this:
{{import pymongo}}
{{db = pymongo.MongoClient()}}
{{result = db.collection.distinct("search_term")}}
{{etc...}}
I'm not sure it's the wisest idea. In terms of security, should I be concerned about running database queries from a view? Are there any other alternatives?

There is no security issue, as the view is executed on the server. However, a better practice would be to move that code to a model file, and then simply display the result in the layout (any objects defined in a model file will be available in the view's execution environment).
In a model file:
import pymongo
db = pymongo.MongoClient()
result = db.collection.distinct("search_term")
And then in layout.html:
{{for record in result:}}
[code to display record]
{{pass}}
Another option would be to move the code to a module and simply import and call a function in the layout (the idea is to limit the views to display-related code only, particularly since it is a little harder to read and debug Python code in the views).

Related

In Oracle Sql Developer, how can I view package_b scope (static) variables while debuging package_a?

I'm trying the debug function in Oracle Sql Developer 18.4, it looks fine until I ran into some complicate cases, one of them is, it seems I cannot view package_b.some_variable while I'm inside package_a.
See above screen, I'm currently break in package_a, which populate data into package_b (pk_DatVerify.g_model_boc and pk_DatVerify.g_model_boc_count), but the "Watches" window is not able to display any of them.
Is there any way I can see the variable value in pk_DatVerify.g_model_boc at that moment? Even if I have to manually type "dbms_output.put_line" somewhere (like in a command window), it will be much helpful!

Swift 3 class/property reference syntax - Couchbase Lite

I'm hoping I can get a little help with some syntax in Swift 3 as I'm fairly new to it and starting to dive into stuff that is just not in the simple books that I read through. I did trying hunting around Google and just seem to have a hard time finding what I'm looking for. I'm trying something with Couchbase Lite.
So in my AppDelegate class, I create a variable, as in "var database: CBLDatabase!"
In "func application(didFinishLaunchingWithOptions)" I call startSession() and that function calls openDatabase(), which then properly opens my database using:
try database = CBLManager.sharedInstance().openDatabaseNamed(dbname, with: options)
and I can get to the data within the database using a query. At this point, I'm setting up some default global variables and a couple of arrays for use elsewhere in the app.
My app has a few viewcontrollers and in one of them, I want to get back to querying the database. I would like to set up another query using the "database" object that I set up in the AppDelegate class. I thought it would be something like:
let query2 = AppDelegate.database.viewNamed("myquery").createQuery()
but the autofill never finds anything like that as I start typing out the statement. I found some help in other posts but I can never quite seem to get the right syntax.
Any help would be much appreciated.
Thanks,
David
Edit: I did make "database" a global variable by placing it above the AppDelegate class definition, but I'm not sure that is a best practice and I would like to minimize global variable use.
Instead of putting stuff in the AppDelegate, I would recommend using a singleton pattern for hosting the global variables that you need accessed across your view controllers.Basically have a "DatabaseManager" singleton class that creates the database instance and makes it available to other view controllers. Refer to this code snippet as an example.

main/delta split marker with sphinx using xmlpipe2?

we are going to have sphinx running main+delta with nosql source. So we're going to use xmlpipe2. To implement main+delta strategy we have to maintain a marker to distinguish "main rows" from "new rows".
The problem is that, unlike sql sources, with xmlpipe2 we can't tell (at least don't know how) if the indexing was successful or not. If we simply update the marker at the end of the main feed generator, and indexing fails for whatever reason, the setup is going to be in inconsistent state.
For SQL sources we have sql_query_post_index hook, how can we achieve similar thing with xmlpipe2?
You could have a wrapper around indexer. that wrapper runs indexer, captures the output, and if the index worked, then it updates your 'marker'.
Call this wrapper from cron, rather can calling indexer directly

Why does the Function Module name of a Smartform change (sometimes)?

Not really critical question, but i'm curious
I am working on a form and sometimes the generated function name is /1BCDWB/SF00000473, and sometimes /1BCDWB/SF00000472. This goes back and forth.
Does anyone know what's the idea behind this? Cuz i'm quite sure it's not a bug (or i might be wrong on that).
It is not a bug. You always have to use SSF_FUNCTION_MODULE_NAME to determine the actual function module name and call it dynamically using CALL FUNCTION l_function_module.
Smartform FMs are tracked by internal numbering and thats saved in the table STXFADMI. You would always notice the different number in Development System if you have deleted any existing Form. Similarly, you would also notice the different number in your Quality system based on the sequence the forms are imported in QAS and the forms as well (as test forms are not migrated to QAS.
Similar behavior is also true for Adobe Form generated FMs.
You need to understand that every smartform has a different interface and hence the automatically generated function module needs to have different import parameters.
Due to this reason the 'SSF*' FMs generate a FM specific for your smartform. The name of the 'generated' FM changes when you migrate from one system to another. And that's the reason why you should use a variable while calling the 'generated' fm and not hardcode it.
The same goes with Adobe form as someone has rightly said in this thread.

Resolving associated objects in SL4 RIA

Having created a standard Silverlight Business Application in VS2010 and set up a model from a SQL Server database, I have various entities and associations, among which AssetGroup and Asset are in a 1:m relationship.
Allegedly I can use dot notation to get the associated AssetGroup out of an asset instance. Through the modern miracles of deferred execution and lazy loading, I am assured, my data will be delivered the very moment that I need it.
But it doesn't work.
What are the required incantations, and do I have to slay a chicken or a goat?
This looks promising. As soon as I've tried it out I'll update.
In the question I mention a blog post containing a possible solution. That solution works, but entails changes to generated code, which is obviously as fragile as a solution gets.
Here's a robust way to apply the solution: change the code generator.
On the EDMX designer surface right-click for the context menu and choose Add Code Generation Items...
Try to improve on "Model1.tt" as a name and save the TT file.
Open the TT file.
Search for "return (" to directly find the method template you need to change.
Edit as shown.
Rebuild the solution.
Change this
return /* big hairy expression */;
to this
var entity = /* big hairy expression */;
if (!entity.IsLoaded) entity.Load();
return entity;