def backwards(self, orm): # Shorthand for StudentClassRegModuleInfo model from frozen ORM scrmi_model = orm['modules.StudentClassRegModuleInfo'] # Save the verb names verb_map = {} name_map = {} for item in scrmi_model.objects.all().values_list('id', 'signup_verb_id'): verb_map[item[0]] = item[1] for id in verb_map: name_map[id] = RegistrationType.objects.get(id=verb_map[id]).name # Delete the verbs (need to allow null values) db.start_transaction() db.alter_column('modules_studentclassregmoduleinfo', 'signup_verb_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['program.RegistrationType'], null=True)) for item in scrmi_model.objects.all(): item.signup_verb = None item.save() db.commit_transaction() # Changing field 'StudentClassRegModuleInfo.signup_verb' db.alter_column('modules_studentclassregmoduleinfo', 'signup_verb_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['datatree.DataTree'], null=True)) db.start_transaction() # Change verb IDs back to DataTrees for item in scrmi_model.objects.all(): item.signup_verb_id = DataTree.get_by_uri('V/Flags/Registration/%s' % name_map[item.id], create=True).id item.save() db.commit_transaction() db.alter_column('modules_studentclassregmoduleinfo', 'signup_verb_id', self.gf('django.db.models.fields.related.ForeignKey')(to=orm['datatree.DataTree']))
def _get_node(request, uri): """ Gets a request and a uri, if the uri is cached in the request, returns the node. Else gets and returns the node. """ if not hasattr(request, '_datatree_nodes'): request._datatree_nodes = {} if uri in request._datatree_nodes: return request._datatree_nodes[uri] node = DataTree.get_by_uri(uri) request._datatree_nodes[uri] = node return node
def test_get_by_uri(self): """ Test DataTree.get_by_uri() for known corner cases """ path = "test/get/by/uri/node" self.assertEqual(DataTree.objects.filter(uri=path).count(), 0) # Test creating some nodes node = DataTree.get_by_uri(path, create=True) self.assertEqual(DataTree.objects.filter(uri=path).count(), 1) self.assertEqual(node.uri, path) # Try a child node child_path = path + "/child" child = DataTree.get_by_uri(child_path, create=True) self.assertEqual(DataTree.objects.filter(uri__startswith=path).count(), 2) self.assertEqual(child.get_uri(), child_path) # Try getting the parent node uri_path = "/".join(path.split('/')[:-1]) uri = DataTree.get_by_uri(uri_path, create=True) self.assertEqual(DataTree.objects.filter(uri__startswith=uri_path).count(), 3) self.assertEqual(uri.uri, uri_path) # Try renaming a node: uri.name = "uri2" uri.save() # Make sure the URI is marked dirty (or got updated properly elsewhere) self.assertTrue((uri.uri_correct == False) or (uri.uri == 'test/get/by/uri2')) # Make sure the URI's of the child nodes are updated self.assertEqual(DataTree.get_by_uri("test/get/by/uri2", create=True).get_uri(), "test/get/by/uri2") self.assertEqual(DataTree.get_by_uri("test/get/by/uri2/node", create=True).get_uri(), "test/get/by/uri2/node") self.assertEqual(DataTree.get_by_uri("test/get/by/uri2/node/child", create=True).get_uri(), "test/get/by/uri2/node/child") # Is it marked clean now that get_uri() has been called? self.assertEqual(DataTree.objects.get(uri="test/get/by/uri2").uri_correct, True) # Try deleting a node; make sure it goes away child.delete() try: node = DataTree.get_by_uri("test/get/by/uri2/node/child") except DataTree.DoesNotExist: node = None self.assertEqual(node, None)
def genTemplate(): """ Generates the DataTree tree nodes listed in 'templates' above, including implicit parents (ie. given Q/Foo/Bar, will autogenerate Q/Foo and Q as well, even if they aren't listed) Returns a list of DataTree nodes corresponding exactly (in order, target, etc.) to the names in templates """ from esp.datatree.models import DataTree, GetNode, QTree, get_lowest_parent, StringToPerm, PermToString node_list = [DataTree.get_by_uri(i, create=True) for i in tree_template] # Special URI changes to override default tree structure (i.e. URIs start with '/') for n in node_list: n.expire_uri() Q_node = DataTree.objects.get(uri='Q') V_node = DataTree.objects.get(uri='V') Q_node.uri = 'Q' V_node.uri = 'V' # We can't use node_list again, since expire_uri doesn't modify the python object. for n in DataTree.objects.filter(uri_correct=False): n.get_uri()
def render(self, name, value, attrs={}): # Load HTML from template from django.conf import settings self.attrs.update(attrs) try: initial_node = DataTree.objects.get(id=int(value)) except (ValueError, TypeError, DataTree.DoesNotExist): initial_node = None try: root = DataTree.get_by_uri(self.attrs['root_uri'], create=False) except DataTree.DoesNotExist: root = None context = { 'field_name': name, 'initial_node': initial_node, 'root': root } return loader.render_to_string( settings.TEMPLATE_DIRS[0] + '/datatree/tree_select.html', context)