I am pretty new in GAE and python. The challenge is to make nested comments with, for example, 3 levels of identation.
GAE-datastore does half work for us. It stores and outputs nested structure in a needed way.
So let example be quite simple:
- Parent 1
- Child 1 of Parent 1
- Child 2 of Parent 1
- Child 1 of Child 2
- Child 2 of Child 2
- Parent 2
- Child 1 of Parent 2
- Child 1 of Child 1
- Child 2 of Parent 2
- Child 1 of Child 2
- Child 1 of Parent 2
If we keep this hierarchy in datastore (by proper using attribute “parent” when creating entities) we will able to fetch list of entities already sorted in this way:
Parent 1
Child 1 of Parent 1
Child 2 of Parent 1
Child 1 of Child 2
Child 2 of Child 2
Parent 2
Child 1 of Parent 2
Child 1 of Child 1
Child 2 of Parent 2
Child 1 of Child 2
So all we need is just make right identation. In other words we need to pass a parameter to our template for each item. Let call this paremeters 0,1,2. I have solution but I dont like it.
I count value of parameter of ident this way:
ident_value = len(CURREN_ITEM.key().to_path())/2 - 2
(Each level in GAE hierarchy path consists of 2 elements.)
So after fetching model from datastore this cycle is executing:
for model in model_list:
new_model_list.append(model) # helper list
ident_value = len(model.key().to_path())/2 - 2
if ident_value>3: ident_value = 3 # max ident = 3
setattr(new_model_list[i], 'ident', ident_value )
i += 1
And then new_model_list is passing to jinja template where class name for each item is assigned based on model.ident value.
Helper list is needed because model is instance of MyModel class that used for saving data in datastore and dont have “ident” attribute.
So the question is: Is there more elegant way to do the same thing?
my solution if somebody needed: