arches_extensions.management.commands.bulk-update-tile

 1import uuid
 2
 3from django.core.management.base import BaseCommand
 4
 5from arches_extensions.utils import user_confirms
 6from arches.app.models.models import Node
 7from arches.app.models.tile import Tile
 8
 9class Command(BaseCommand):
10    """
11    Facilitates bulk updates to nodes across the database.
12
13    .. warning::
14        This command is a work-in-progress
15    """
16
17    def __init__(self, *args, **kwargs):
18        self.help = self.__doc__
19
20    def add_arguments(self, parser):
21        parser.add_argument("node",
22            help='specify the name of the node whose values will be updated.'
23        )
24        parser.add_argument("--set-value",
25            help='the value to assign to the nodes.'
26        )
27        parser.add_argument("--set-empty",
28            action="store_true",
29            default=False,
30            help='set all node values to empty. can be used to initialize '\
31                 'newly created nodes. ERASES ALL EXISTING VALUES.'
32        )
33
34    def handle(self, *args, **options):
35
36        if options["set_empty"]:
37            value = None
38        elif options["set_value"]:
39            value = options["set_value"]
40
41        try:
42            id = uuid.UUID(options["node"])
43            nodes = Node.objects.filter(nodeid=id)
44        except ValueError:
45            nodes = Node.objects.filter(name=options["node"])
46        if len(nodes) == 0:
47            print("cancelling, no nodes match this name.")
48            exit()
49        elif len(nodes) > 1:
50            print("multiple nodes match this name:")
51            for node in nodes:
52                print(f"{node.name} - {node.pk} - {node.graph.name}")
53            print("\nAll of these nodes will be updated. To choose a specific "\
54                  "one, rerun this command using the node id instead of name.")
55            if not user_confirms(message="proceed?", default=False):
56                print("cancelled")
57                exit()
58
59        for node in nodes:
60
61            print(f"{node.name} - {node.pk} - {node.graph.name}")
62            tiles = Tile.objects.filter(nodegroup_id=node.nodegroup)
63
64            nodeid = str(node.pk)
65            for t in tiles:
66                old_value = t.data.get(nodeid, "<no previously saved value>")
67
68                if options["set_empty"] or options["set_value"]:
69                    if options["set_empty"]:
70                        new_value = None
71                    elif options["set_value"]:
72                        new_value = options["set_value"]
73                    Tile().update_node_value(nodeid, new_value, tileid=t.tileid)
74                    print(f"{t.resourceinstance_id}: {old_value} --> {new_value}")
75                else:
76                    print(f"{t.resourceinstance_id}: {old_value}")
77
78            print(f"  tiles: {tiles.count()}")
class Command(django.core.management.base.BaseCommand):
10class Command(BaseCommand):
11    """
12    Facilitates bulk updates to nodes across the database.
13
14    .. warning::
15        This command is a work-in-progress
16    """
17
18    def __init__(self, *args, **kwargs):
19        self.help = self.__doc__
20
21    def add_arguments(self, parser):
22        parser.add_argument("node",
23            help='specify the name of the node whose values will be updated.'
24        )
25        parser.add_argument("--set-value",
26            help='the value to assign to the nodes.'
27        )
28        parser.add_argument("--set-empty",
29            action="store_true",
30            default=False,
31            help='set all node values to empty. can be used to initialize '\
32                 'newly created nodes. ERASES ALL EXISTING VALUES.'
33        )
34
35    def handle(self, *args, **options):
36
37        if options["set_empty"]:
38            value = None
39        elif options["set_value"]:
40            value = options["set_value"]
41
42        try:
43            id = uuid.UUID(options["node"])
44            nodes = Node.objects.filter(nodeid=id)
45        except ValueError:
46            nodes = Node.objects.filter(name=options["node"])
47        if len(nodes) == 0:
48            print("cancelling, no nodes match this name.")
49            exit()
50        elif len(nodes) > 1:
51            print("multiple nodes match this name:")
52            for node in nodes:
53                print(f"{node.name} - {node.pk} - {node.graph.name}")
54            print("\nAll of these nodes will be updated. To choose a specific "\
55                  "one, rerun this command using the node id instead of name.")
56            if not user_confirms(message="proceed?", default=False):
57                print("cancelled")
58                exit()
59
60        for node in nodes:
61
62            print(f"{node.name} - {node.pk} - {node.graph.name}")
63            tiles = Tile.objects.filter(nodegroup_id=node.nodegroup)
64
65            nodeid = str(node.pk)
66            for t in tiles:
67                old_value = t.data.get(nodeid, "<no previously saved value>")
68
69                if options["set_empty"] or options["set_value"]:
70                    if options["set_empty"]:
71                        new_value = None
72                    elif options["set_value"]:
73                        new_value = options["set_value"]
74                    Tile().update_node_value(nodeid, new_value, tileid=t.tileid)
75                    print(f"{t.resourceinstance_id}: {old_value} --> {new_value}")
76                else:
77                    print(f"{t.resourceinstance_id}: {old_value}")
78
79            print(f"  tiles: {tiles.count()}")

Facilitates bulk updates to nodes across the database.

This command is a work-in-progress

Command(*args, **kwargs)
18    def __init__(self, *args, **kwargs):
19        self.help = self.__doc__
help = ''
def add_arguments(self, parser):
21    def add_arguments(self, parser):
22        parser.add_argument("node",
23            help='specify the name of the node whose values will be updated.'
24        )
25        parser.add_argument("--set-value",
26            help='the value to assign to the nodes.'
27        )
28        parser.add_argument("--set-empty",
29            action="store_true",
30            default=False,
31            help='set all node values to empty. can be used to initialize '\
32                 'newly created nodes. ERASES ALL EXISTING VALUES.'
33        )

Entry point for subclassed commands to add custom arguments.

def handle(self, *args, **options):
35    def handle(self, *args, **options):
36
37        if options["set_empty"]:
38            value = None
39        elif options["set_value"]:
40            value = options["set_value"]
41
42        try:
43            id = uuid.UUID(options["node"])
44            nodes = Node.objects.filter(nodeid=id)
45        except ValueError:
46            nodes = Node.objects.filter(name=options["node"])
47        if len(nodes) == 0:
48            print("cancelling, no nodes match this name.")
49            exit()
50        elif len(nodes) > 1:
51            print("multiple nodes match this name:")
52            for node in nodes:
53                print(f"{node.name} - {node.pk} - {node.graph.name}")
54            print("\nAll of these nodes will be updated. To choose a specific "\
55                  "one, rerun this command using the node id instead of name.")
56            if not user_confirms(message="proceed?", default=False):
57                print("cancelled")
58                exit()
59
60        for node in nodes:
61
62            print(f"{node.name} - {node.pk} - {node.graph.name}")
63            tiles = Tile.objects.filter(nodegroup_id=node.nodegroup)
64
65            nodeid = str(node.pk)
66            for t in tiles:
67                old_value = t.data.get(nodeid, "<no previously saved value>")
68
69                if options["set_empty"] or options["set_value"]:
70                    if options["set_empty"]:
71                        new_value = None
72                    elif options["set_value"]:
73                        new_value = options["set_value"]
74                    Tile().update_node_value(nodeid, new_value, tileid=t.tileid)
75                    print(f"{t.resourceinstance_id}: {old_value} --> {new_value}")
76                else:
77                    print(f"{t.resourceinstance_id}: {old_value}")
78
79            print(f"  tiles: {tiles.count()}")

The actual logic of the command. Subclasses must implement this method.