Creating child records with django-mptt - django-mptt

I have successfully completed the django-mptt tutorial. What I cannot figure out how to do is create a child of a child.
By child of a child, i mean a third level deep and more. See example below, I want to create 1.3.1, 1.3.2, 1.3.1.1
1.0 Product Z
1.1 Product A
1.2 Product B
1.3 Product P
1.3.1 Product X
1.3.1.1 Product O
1.3.2 Product Y
2.0 Product H
In the doco i found insert_node but dont understand it enough to get it working. Also i found something in the code comments (line 317) regarding insert_node that says:
NOTE: This is a low-level method; it does NOT respect ``MPTTMeta.order_insertion_by``.
In most cases you should just set the node's parent and let mptt call this during save.
Should i be using 'insert_node' or is there a better way? If 'insert_node' should be used, then can you provide an example of its use?

This can be a little confusing, I admit. But as you can read here, the order_insertion_by field should only be used, when you're after something like a standard insertion behavior, for example alphabetically ordered trees and so on as it triggers an extra db query.
However, if you want to insert a node at a specific point in your tree, you have to use either
TreeManager.insert_node or MPTTModel.insert_at, the latter being a convenience method for calling the first.
So, according to your example, this leads to the following three options to add a new 1.3.3 Product Q as last child of 1.3 Product P:
new_node = ProductNode(name='1.3.3 Product Q')
parent = ProductNode.objects.get(name='1.3 Product P')
# With `order_insertion_by`
new_node.parent = parent
new_node.save()
# With `TreeManager.insert_node`
ProductNode.objects.insert_node(new_node, parent, position='last-child', save=True)
# With `MPTTModel.insert_at`
new_node.insert_at(parent, position='last-child', save=True)

Related

Is there a supported way to get list of features used by a H2O model during its training?

This is my situation. I have over 400 features, many of which are probably useless and often zero. I would like to be able to:
train an model with a subset of those features
query that model for the features actually used to build that model
build a H2OFrame containing just those features (I get a sparse list of non-zero values for each row I want to predict.)
pass this newly constructed frame to H2OModel.predict() to get a prediction
I am pretty sure what found is unsupported but works for now (v 3.13.0.341). Is there a more robust/supported way of doing this?
model._model_json['output']['names']
The response variable appears to be the last item in this list.
In a similar vein, it would be nice to have a supported way of finding out which H2O version that the model was built under. I cannot find the version number in the json.
If you want to know which feature columns the model used after you have built a model you can do the following in python:
my_training_frame = your_model.actual_params['training_frame']
which will return some frame id
and then you can do
col_used = h2o.get_frame(my_training_frame)
col_used
EDITED (after comment was posted)
To get the columns use:
col_used.columns
Also, a quick way to check the version of a saved binary model is to try and load it into h2o, if it loads it is the same version of h2o, if it isn't you will get a warning.
you can also open the saved model file, the first line will list the version of H2O used to create it.
For a model saved as a mojo you can look at the model.ini file. It will list the version of H2O.

Getting a level of an ALV tree node?

I created an ALV TREE report, using cl_gui_alv_tree, that has 3 levels. I'm also implementing an event handler for when he double clicks a node.
My problem is that I want to take some actions only when he double clicks a node that is a root node. The event 'node_double_click' gives a node_key, but that's the index of the displayed table. How could I achieve this?
The node ID is not an index, it's the ID you assigned to the node when adding it to the tree.
If possible, I'd suggest switching to CL_SALV_TREE - not only because it is documented
and supported by SAP, but also because it comes with some query methods that are quite handy. These methods are documented as well. You can use, for example, GET_NODE to retrieve a node by its ID and then use GET_PARENT to check whether the node in question is a top-level node or has a parent node it is attached to.
I created a pattern for myself, which i am using.
lv_parent1 = node_key.
while lv_parent1 ne go_Main_tree->C_VIRTUAL_ROOT_NODE.
CALL METHOD go_main_tree->get_parent
EXPORTING
i_node_key = lv_parent1
IMPORTING
e_parent_node_key = lv_parent1.
lv_hierlevel = lv_hierlevel + 1 .
ENDWHILE.
if lv_hierlevel > 2.
“ do what You want to do
endif.

how to customize plone 4 collection to sort by multiple fields

I'm building a Plone 4.1 based site and am trying to find the best way to either sort a collection by multiple sort criteria, or at least customize a collection portlet to do so for the font page of the site. I believe the portlet uses the collection sort settings unless you choose random. Here is the section of code from the standard results in the portlet:
def _standard_results(self):
results = []
collection = self.collection()
if collection is not None:
limit = self.data.limit
if limit and limit > 0:
# pass on batching hints to the catalog
results = collection.queryCatalog(batch=True, b_size=limit)
results = results._sequence
else:
results = collection.queryCatalog()
if limit and limit > 0:
results = results[:limit]
return results
For example, I would like to be able to sort by Expiration Date if present, if not then use the Creation Date for example. Or sort by tags and Creation Date. Any feedback on the best approach to this would be appreciated.
as ross said you'll need AdvancedQuery to sort on multiple criteria.
if you just need this for the frontpage i'd suggest do create a custom portelt based on the collectionportlet.
where the collectionportlet calls collection.queryCatalog() you'll want to add some additional logic for your sorting:
>>> uids = [brain.UID for brain in collection.queryCatalog()]
>>> query = AdvancedQuery.In('UID', uids)
>>> results = catalog.evalAdvancedQuery(query, (('sortable_title', 'asc'), ('date', 'desc')
then you can use results instead of the results in your code sample above
As in this answer, multiple sort is only available through AdvancedQuery an there's no integration of AdvancedQuery into collections that I'm aware of. So basically this isn't possible unless you integrate AdvancedQuery into collections yourself, which would be a non-trivial task.
A hackish workaround might be to use plone.indexer to write an indexer that returns the right sort value according to your logic, create a new FieldIndex in the catalog (profiles/default/catalog.xml), register that new index as valid for sort criterion in profiles/default/portal_atct.xml, then use that as your sort index.

How to develop a backend for a scrum-like board

Currently I'm developing a debate module (much like a scrum/kanban board) for a GPL application (e-cidadania) and I don't have any experience with complex backends. I have developed a basic frontend for it, but now I don't know what approach I should use for the ajax and django backends to save and manipulate the table and notes.
The table can be N rows and N columns, every row and column has a name and position inside the table. Every note has also a position, text and comments (managed with the django comments framework).
I thought to store the parent element of every note (so I can place it later) and store the name of the rows and columns like CSV strings. Is that a good approach?
A screenshot of the current frontend: http: //ur1. ca/4zn4h
Update: I almost forgot, the frontend has been done with jQuery Sortables (so the user can move the note around as he likes) and CSS3.
You just need to model your domain (that is, debates that look like scrum boards) within Django. Think about it in plain English first, like this:
The has debates. These consist of criteria, organised in rows and columns in a specific order. This creates cells, which can have notes inside them.
Then you can set to work translating this into model classes. Don't worry too much about the fields they contain, the most important bit is the relationships (so the ForeignKey bits):
class Debate(models.Model):
title = ...
class Column(models.Model):
title = ...
order = ...
board = models.ForeignKey(ScrumBoard, related_name='columns')
class Row(models.Model):
title = ...
order = ...
board = models.ForeignKey(ScrumBoard, related_name='rows')
class Cell(models.Model):
column = models.ForeignKey(Column)
row = models.ForeignKey(Row)
class Note(models.Model)
text = ...
cell = models.ForeignKey(Cell)
That might be overly complex for what you need, though. I'm not an expert in the problem you're trying to solve? My suggestion, Django is quick – so start hacking, and give it a go, and if it's all wrong then you can go back a few steps, clean out your database and try again.
You might find it useful to play with South, which does database migrations for when you do things like add/remove/edit fields in your models.

How to add an attribute to a node so it is the first one

I have a Nokogiri xml node:
node = <word n='ab' v='cd'>something</word>
I want to add an attribute:
node['p']='ef'
but in such a way that it 'shows' the first in the list of attributes, like
node = <word p='ef' n='ab' v='cd'>something</word>
Is there a simple way to do this?
I don't know of any XML serializer that allows you to control the order of attributes (except by accident, relying on undocumented features of a product). It shouldn't matter; the order is only cosmetic.
When you say "the order denotes the certitude" this is very worrying, because you are attaching meaning to the order of attributes when XML is very clear that the order will in general not be maintained. You need to redesign your XML to find a different way to capture this information.

Resources