Permalink
Browse files

Using .pk instead of .id

  • Loading branch information...
1 parent 3eb263a commit ef87ca8ab91098edc59975ffffe42ccf94271050 @tabo tabo committed Mar 22, 2012
View
@@ -150,7 +150,7 @@ def dump_bulk(cls, parent=None, keep_ids=True):
if 'children' not in parentobj:
parentobj['children'] = []
parentobj['children'].append(newobj)
- lnk[node.id] = newobj
+ lnk[node.pk] = newobj
return ret
def add_child(self, **kwargs):
View
@@ -77,7 +77,7 @@ def load_bulk(cls, bulk_data, parent=None, keep_ids=False):
node_obj = parent.add_child(**node_data)
else:
node_obj = cls.add_root(**node_data)
- added.append(node_obj.id)
+ added.append(node_obj.pk)
if 'children' in node_struct:
# extending the stack with the current node as the parent of
# the new nodes
@@ -427,7 +427,7 @@ def move(self, target, pos=None): # pragma: no cover
def delete(self):
"Removes a node and all it's descendants."
- self.__class__.objects.filter(id=self.id).delete()
+ self.__class__.objects.filter(id=self.pk).delete()
def _fix_add_sibling_opts(self, pos):
"prepare the pos variable for the add_sibling method"
View
@@ -210,30 +210,30 @@ def find_problems(cls):
found_error = False
for char in node.path:
if char not in cls.alphabet:
- evil_chars.append(node.id)
+ evil_chars.append(node.pk)
found_error = True
break
if found_error:
continue
if len(node.path) % cls.steplen:
- bad_steplen.append(node.id)
+ bad_steplen.append(node.pk)
continue
try:
node.get_parent(True)
except cls.DoesNotExist:
- orphans.append(node.id)
+ orphans.append(node.pk)
continue
if node.depth != len(node.path) / cls.steplen:
- wrong_depth.append(node.id)
+ wrong_depth.append(node.pk)
continue
real_numchild = cls.objects.filter(
path__range=cls._get_children_path_interval(node.path)).extra(
where=['LENGTH(path)/%d=%d' % (cls.steplen,
node.depth + 1)]).count()
if real_numchild != node.numchild:
- wrong_numchild.append(node.id)
+ wrong_numchild.append(node.pk)
continue
return evil_chars, bad_steplen, orphans, wrong_depth, wrong_numchild
@@ -338,7 +338,7 @@ def get_tree(cls, parent=None):
if not parent.is_leaf():
return cls.objects.filter(path__startswith=parent.path,
depth__gte=parent.depth)
- return cls.objects.filter(pk=parent.id)
+ return cls.objects.filter(pk=parent.pk)
@classmethod
def get_root_nodes(cls):
@@ -447,7 +447,7 @@ def get_descendants(self):
:returns: A queryset of all the node's descendants as DFS, doesn't
include the node itself
"""
- return self.__class__.get_tree(self).exclude(pk=self.id)
+ return self.__class__.get_tree(self).exclude(pk=self.pk)
def get_prev_sibling(self):
"""
View
@@ -54,7 +54,7 @@ def delete(self, removed_ranges=None):
found = True
break
if not found:
- removed[node.id] = node
+ removed[node.pk] = node
# ok, got the minimal list of nodes to remove...
# we must also remove their descendants
@@ -382,7 +382,7 @@ def move(self, target, pos=None):
# we reload 'self' because lft/rgt may have changed
- fromobj = cls.objects.get(pk=self.id)
+ fromobj = cls.objects.get(pk=self.pk)
depthdiff = target.depth - fromobj.depth
if parent:
@@ -439,7 +439,7 @@ def load_bulk(cls, bulk_data, parent=None, keep_ids=False):
# tree, iterative preorder
added = []
if parent:
- parent_id = parent.id
+ parent_id = parent.pk
else:
parent_id = None
# stack of nodes to analize
@@ -455,11 +455,11 @@ def load_bulk(cls, bulk_data, parent=None, keep_ids=False):
node_obj = parent.add_child(**node_data)
else:
node_obj = cls.add_root(**node_data)
- added.append(node_obj.id)
+ added.append(node_obj.pk)
if 'children' in node_struct:
# extending the stack with the current node as the parent of
# the new nodes
- stack.extend([(node_obj.id, node) \
+ stack.extend([(node_obj.pk, node) \
for node in node_struct['children'][::-1]])
transaction.commit_unless_managed()
return added
@@ -523,11 +523,11 @@ def dump_bulk(cls, parent=None, keep_ids=True):
ret.append(newobj)
else:
parentobj = pyobj.get_parent()
- parentser = lnk[parentobj.id]
+ parentser = lnk[parentobj.pk]
if 'children' not in parentser:
parentser['children'] = []
parentser['children'].append(newobj)
- lnk[pyobj.id] = newobj
+ lnk[pyobj.pk] = newobj
return ret
@classmethod
@@ -540,7 +540,7 @@ def get_tree(cls, parent=None):
# return the entire tree
return cls.objects.all()
if parent.is_leaf():
- return cls.objects.filter(pk=parent.id)
+ return cls.objects.filter(pk=parent.pk)
return cls.objects.filter(
tree_id=parent.tree_id,
lft__range=(parent.lft, parent.rgt - 1))
@@ -552,7 +552,7 @@ def get_descendants(self):
"""
if self.is_leaf():
return self.__class__.objects.none()
- return self.__class__.get_tree(self).exclude(pk=self.id)
+ return self.__class__.get_tree(self).exclude(pk=self.pk)
def get_descendant_count(self):
":returns: the number of descendants of a node."
@@ -106,13 +106,13 @@ def items_for_result(cl, result, form):
def results(cl):
- parent_id = lambda n: n.get_parent().id if not n.is_root() else 0
+ parent_id = lambda n: n.get_parent().pk if not n.is_root() else 0
if cl.formset:
for res, form in zip(cl.result_list, cl.formset.forms):
- yield res.id, parent_id(res), res.get_depth(), res.get_children_count(), list(items_for_result(cl, res, form))
+ yield res.pk, parent_id(res), res.get_depth(), res.get_children_count(), list(items_for_result(cl, res, form))
else:
for res in cl.result_list:
- yield res.id, parent_id(res), res.get_depth(), res.get_children_count(), list(items_for_result(cl, res, None))
+ yield res.pk, parent_id(res), res.get_depth(), res.get_children_count(), list(items_for_result(cl, res, None))
def check_empty_dict(GET_dict):
@@ -25,13 +25,13 @@ def __line(node, request):
if 't' in request.GET and request.GET['t'] == 'id':
raw_id_fields = """
onclick="opener.dismissRelatedLookupPopup(window, '%d'); return false;"
- """ % (node.id,)
+ """ % (node.pk,)
else:
raw_id_fields = ''
return ('<input type="checkbox" class="action-select" value="%d" '
- 'name="_selected_action" /><a href="%d/" %s>%s</a>') % (node.id,
- node.id,
+ 'name="_selected_action" /><a href="%d/" %s>%s</a>') % (node.pk,
+ node.pk,
raw_id_fields,
str(node),)
View
@@ -14,28 +14,28 @@ class MP_TestNode(MP_Node):
desc = models.CharField(max_length=255)
def __unicode__(self): # pragma: no cover
- return 'Node %d' % self.id
+ return 'Node %d' % self.pk
class MP_TestNodeSomeDep(models.Model):
node = models.ForeignKey(MP_TestNode)
def __unicode__(self): # pragma: no cover
- return 'Node %d' % self.id
+ return 'Node %d' % self.pk
class NS_TestNode(NS_Node):
desc = models.CharField(max_length=255)
def __unicode__(self): # pragma: no cover
- return 'Node %d' % self.id
+ return 'Node %d' % self.pk
class NS_TestNodeSomeDep(models.Model):
node = models.ForeignKey(NS_TestNode)
def __unicode__(self): # pragma: no cover
- return 'Node %d' % self.id
+ return 'Node %d' % self.pk
class AL_TestNode(AL_Node):
@@ -47,14 +47,14 @@ class AL_TestNode(AL_Node):
desc = models.CharField(max_length=255)
def __unicode__(self): # pragma: no cover
- return 'Node %d' % self.id
+ return 'Node %d' % self.pk
class AL_TestNodeSomeDep(models.Model):
node = models.ForeignKey(AL_TestNode)
def __unicode__(self): # pragma: no cover
- return 'Node %d' % self.id
+ return 'Node %d' % self.pk
class MP_TestNodeSorted(MP_Node):
@@ -65,7 +65,7 @@ class MP_TestNodeSorted(MP_Node):
desc = models.CharField(max_length=255)
def __unicode__(self): # pragma: no cover
- return 'Node %d' % self.id
+ return 'Node %d' % self.pk
class NS_TestNodeSorted(NS_Node):
@@ -75,7 +75,7 @@ class NS_TestNodeSorted(NS_Node):
desc = models.CharField(max_length=255)
def __unicode__(self): # pragma: no cover
- return 'Node %d' % self.id
+ return 'Node %d' % self.pk
class AL_TestNodeSorted(AL_Node):
@@ -89,7 +89,7 @@ class AL_TestNodeSorted(AL_Node):
desc = models.CharField(max_length=255)
def __unicode__(self): # pragma: no cover
- return 'Node %d' % self.id
+ return 'Node %d' % self.pk
class MP_TestNodeAlphabet(MP_Node):
@@ -98,15 +98,15 @@ class MP_TestNodeAlphabet(MP_Node):
numval = models.IntegerField()
def __unicode__(self): # pragma: no cover
- return 'Node %d' % self.id
+ return 'Node %d' % self.pk
class MP_TestNodeSmallStep(MP_Node):
steplen = 1
alphabet = '0123456789'
def __unicode__(self): # pragma: no cover
- return 'Node %d' % self.id
+ return 'Node %d' % self.pk
class MP_TestNodeSortedAutoNow(MP_Node):
@@ -116,7 +116,7 @@ class MP_TestNodeSortedAutoNow(MP_Node):
node_order_by = ['created']
def __unicode__(self): # pragma: no cover
- return 'Node %d' % self.id
+ return 'Node %d' % self.pk
class MP_TestNodeShortPath(MP_Node):
@@ -125,7 +125,7 @@ class MP_TestNodeShortPath(MP_Node):
desc = models.CharField(max_length=255)
def __unicode__(self): # pragma: no cover
- return 'Node %d' % self.id
+ return 'Node %d' % self.pk
# This is how you change the default fields defined in a Django abstract class
# (in this case, MP_Node), since Django doesn't allow overriding fields, only
@@ -158,7 +158,7 @@ class MP_TestSortedNodeShortPath(MP_Node):
node_order_by = ['desc']
def __unicode__(self): # pragma: no cover
- return 'Node %d' % self.id
+ return 'Node %d' % self.pk
MP_TestSortedNodeShortPath._meta.get_field('path').max_length = 4
@@ -245,7 +245,7 @@ def _multi_get_tree_node(self):
self.model.load_bulk(BASE_DATA, node)
# the tree was modified by load_bulk, so we reload our node object
- node = self.model.objects.get(pk=node.id)
+ node = self.model.objects.get(pk=node.pk)
got = [(o.desc, o.get_depth(), o.get_children_count())
for o in self.model.get_tree(node)]
@@ -296,7 +296,7 @@ def _multi_dump_bulk_node(self):
self.model.load_bulk(BASE_DATA, node)
# the tree was modified by load_bulk, so we reload our node object
- node = self.model.objects.get(pk=node.id)
+ node = self.model.objects.get(pk=node.pk)
got = self.model.dump_bulk(node, False)
expected = [{'data':{'desc':u'231'}, 'children':BASE_DATA}]
@@ -1430,8 +1430,8 @@ def _multi_add_child_nonroot_sorted(self):
get_node = lambda node_id: self.sorted_model.objects.get(pk=node_id)
- root_id = self.sorted_model.add_root(val1=0, val2=0, desc='a').id
- node_id = get_node(root_id).add_child(val1=0, val2=0, desc='ac').id
+ root_id = self.sorted_model.add_root(val1=0, val2=0, desc='a').pk
+ node_id = get_node(root_id).add_child(val1=0, val2=0, desc='ac').pk
get_node(root_id).add_child(val1=0, val2=0, desc='aa')
get_node(root_id).add_child(val1=0, val2=0, desc='av')
get_node(node_id).add_child(val1=0, val2=0, desc='aca')
@@ -1461,8 +1461,8 @@ def _multi_move_sorted(self):
for node in root_nodes[1:]:
# because raw queries don't update django objects
- node = self.sorted_model.objects.get(pk=node.id)
- target = self.sorted_model.objects.get(pk=target.id)
+ node = self.sorted_model.objects.get(pk=node.pk)
+ target = self.sorted_model.objects.get(pk=target.pk)
node.move(target, 'sorted-child')
expected = [(1, 4, u'bcd', 1, 7),
@@ -1490,8 +1490,8 @@ def _multi_move_sortedsibling(self):
for node in root_nodes[1:]:
# because raw queries don't update django objects
- node = self.sorted_model.objects.get(pk=node.id)
- target = self.sorted_model.objects.get(pk=target.id)
+ node = self.sorted_model.objects.get(pk=node.pk)
+ target = self.sorted_model.objects.get(pk=target.pk)
node.val1 = node.val1 - 2
node.save()
@@ -1872,7 +1872,7 @@ def _multi_form_html_root_node(self):
for obj in self.model.get_tree():
if node != obj or obj.is_descendant_of(node):
rtpl += '<option value="%d">%sNode %d</option>\n' % (
- obj.id, '. . ' * (obj.get_depth() - 1), obj.id)
+ obj.pk, '. . ' * (obj.get_depth() - 1), obj.pk)
rtpl += '</select></td></tr>'
formstr = unicode(form).replace(u' selected="selected"', u'')
self.assertEqual(rtpl, formstr)
@@ -1888,7 +1888,7 @@ def _multi_form_html_leaf_node(self):
for obj in self.model.get_tree():
if node != obj or obj.is_descendant_of(node):
rtpl += '<option value="%d">%sNode %d</option>\n' % (
- obj.id, '. . ' * (obj.get_depth() - 1), obj.id)
+ obj.pk, '. . ' * (obj.get_depth() - 1), obj.pk)
rtpl += '</select></td></tr>'
formstr = unicode(form).replace(u' selected="selected"', u'')
self.assertEqual(rtpl, formstr)
@@ -1934,7 +1934,7 @@ def _multi_admin_html(self):
form = ma.get_form(request)()
ids = []
for obj in self.model.get_tree():
- ids.extend([obj.id] * 2)
+ ids.extend([obj.pk] * 2)
self.assertEqual(tpl % tuple(ids), unicode(form))

0 comments on commit ef87ca8

Please sign in to comment.