Return ordered dictionary to django template - json

Suppose you want to display posts as in Disqus.
(It seems you can comment on any post in any depth)
How do I pass a such data to django template and let it draw the data as Disqus?
e.g. You want to display all posts which belong to a thread in a way which reveals the parent-child relationship.
How can you do that in django? (python/json/..)

Have you looked into using django-mptt? I have found that it works quite well for this type of tree structure.

Related

is the possible for nested level preloading in GORM GOLANG?

Example
db.Preload("Orders.Orders.Orders").Preload(clause.Associations).Find(&users)
So can i put dynamically Nth level preloading in golang?
Actually what I want
If I write only Orders in preaload so I want to get n level preloading data
db.Preload("Orders").Preload(clause.Associations).Find(&users)
For an example we can take post has many comment and comment has many comments
db.Preload("comments.comments.comments").Preload(clause.Associations).Find(&users)
So instead of multiple time write comments.comments.comments
is the possible can I write with single comment ?
SO I WANT DYNAMICALLY NESTED PRELOADING IN GOLANG
THANK YOU IN ADVANCE

Squarespace access JSON properties via URL?

I know that I can access the JSON data like so http://base-template.squarespace.com/news/?format=json-pretty. But what I want to access lets says a property on that like news.items? http://base-template.squarespace.com/news/items/?format=json-pretty throw an error. Is there a way to drill into the JSON data via the URL?
Yes, you can access a collection's items using the same format=json-pretty query parameter as you mentioned.
Do note, however, that:
The URL must exist (which in your second 'news' example is not the case)
To get a list of items from a collection, you use the format=json-pretty on the collection, the scope into items from there.
When performing this type of request via Javascript, you'll probably want to simply use format=json since the 'pretty' line breaks and indenting aren't necessary in that case.
For example, using the base-template as you have already mentioned, to get the blog items you would use: https://base-template.squarespace.com/blog?format=json-pretty. Within the JSON, you'll see an 'items' array, which is the data you're looking for (See the image below for a screenshot of this.). If that website had a "/news" collection, you could do similar.
Similarly, if you wanted to view the JSON output from a specific item, you would, for example, use: https://base-template.squarespace.com/blog/2016/7/15/most-recent-sample-blog-post?format=json-pretty

Freemarker: find specific object in array of arrays

I have a complex many-to-many relationship defined. The cross-reference table is an entity, so I have Contact with a One-To-Many to ContactList, and List with a One-To-Many to Contact List. Contact List contains listID, contactID, and a few Booleans. The relationships seem to work well and on the backend I can get a list of contacts on a review list using the Spring-Data-Jpa findByContactListsIn(Set).
However, I am trying to build a list of contacts in Freemarker, and show whether they were in the current list.
Before I made an Entity out of ContactList, I had a standard Many-To-Many relationship between them, and I was able to do something like this in my .ftl:
<#if list.contacts?seq_contains(contact)>
But I needed to add some data to ContactList specifically, so I needed it to be more complicated. How can I do something similar now? I tried:
<#if list.contactLists?seq_contains(contact)
But of course that always returns false, because it is comparing two different entity types. Is there a way to find if a contact is in one of the contactList objects?
I suppose I could do some back-end trickery, but I am looking for a front-end solution to this.
Don't use ?seq_contains for finding generic object at all. It doesn't call Object.equals, instead it works like the == operator of the template language, which only allows comparing strings, numbers, booleans and dates/times, otherwise it gives you an error. Unfortunately it won't fail in your case, because POJO-s are also strings (and their string value is what toString() returns). This is an unfortunate legacy of the stock ObjectWrapper (scheduled to be fixed in FM3); not even a quirk in the template language. Ideally you get an error there. Instead, now it silently compares the return value of the toString()-s...
Your data-model should already contain what the template should actually display. FTL is not a programming language, so if you try to extract that from the data-model in it, it will be a pain. But, that the data-model contains that data can also mean that some objects in the data-model have methods that extract the data you need. As a last resort, you can add objects that just contain helper methods.
Update: Returning to ?seq_contains, if you need the Java semantics and list is a Java Collection, you can just use the Java API: list?api.contains(contact).

Sort BC WebApp on front end using a custom field

I have created a BC WebApp using the BC Open Platform API's for the back end and everything appears to work fine including rendering the list of items in a sort order of one of my custom fields.
Here is an example of what works on the back end.
var items = new BCAPI.Models.WebApp.ItemCollection(WEBAPP_NAME);
items.fetch({
order: "MyCustomField",
skip: 0,
limit: 1000,
success: onWebpAppListFetch,
error: onAPIError
});
How do I render this list on the front end sorted by one of my custom fields? Here is an example of what I am trying to use on the front end, but it does not order or sort this way.
{module_webapps order="MyCustomField" render="collection" template="/_System/apps/cms-sports-club-manager/club-rooms/layouts/club_rooms_collection.tpl" id="cms-club-roomsx" filter="all"}
Is there something that I am overlooking, or do I need to approach this in my own manual way? Perhaps I could render the list into an array, sort the array and then iterate through that to render the front end listing? The template file uses Liquid to iterate through the collection and render the HTML. Can I define an array variable, fill the array, sort the array and iterate through the array again in that same template file?
Another possibility perhaps is to output all webapp items into a JSON file each time a user creates/edits an item (from the back end), and then use the {module_json} feature on the front end to read that JSON file which "should" allow me to sort it.
Any advice on what to do (as well as what NOT to do) would be appreciated.
I have found one possible answer, but I am not sure it is the best way to do this.
I have implemented a script which performs the following steps:
Create an array holding all items (unsorted at this point)
Sort the array using any custom field
Iterate through the sorted array and document.write() each one
I have tested this a fair bit and it seems to do exactly what I want, however I have not marked this as the correct answer just yet as I would still like to find a "better" way than needing to resort to the above manual work.

Entity Framework Serialize entity to json with included related entities

I have a User entity with too many relations to other entities in the system. And I am using AngularJs and want to serialize the User entity to json with only the included entities.
Here is my select statement:
var users = unc.Users.Include("Profile").ToList();
when serializing this to json it will always result into
The operation cannot be completed because the DbContext has been disposed
I used to solve this problem by just selecting every column I need in my view like this:
var users = unc.Users.Select(x => new { x.Id ,x.Username,Role=x.Role.Name,x.Email,x.Profile.Name,x.UpdatedAt,x.CreatedAt}).ToList();
but this is too hard and much code to write. I am looking for the ideal or a better solution.
Thanks
I'v found a decent solution here.
https://efjson.codeplex.com/
This will serialize your entity without including related entities unless you wanted to include those entities. This will avoid entering into circular loops caused by entities calling each other back through reversed attributes.
Also serializing related entities you want will make it easy to serialize an entity and through the JSON back to scripts like AngularJs.
Hope this will make other people happy too :)