1- from arango . exceptions import (
2- ArangoClientError ,
3- ArangoServerError ,
4- PermissionUpdateError ,
5- UserCreateError ,
6- )
1+ import csv
2+ from io import StringIO
3+ import json
4+ import os
5+ from pathlib import Path
6+
77from django .conf import settings
8- from django .core .management .base import BaseCommand
98from django .contrib .auth .models import User
9+ from django .core .management .base import BaseCommand
1010
11- from multinet .api .utils .arango import arango_system_db
12- from multinet .api .models import Workspace , Network , Table , TableTypeAnnotation
11+ from multinet .api .models import Network , Table , TableTypeAnnotation , Workspace
1312from multinet .api .tasks .upload .csv import process_row
1413
15- from pathlib import Path
16- from io import StringIO
17- import os
18- import csv
19- import json
20-
21-
2214
2315class Command (BaseCommand ):
24- help = " Sets up a standard development environment with example workspaces and tables."
16+ help = ' Sets up a standard development environment with example workspaces and tables.'
2517
2618 def create_workspace_if_not_exists (self , workspace_name ):
2719 try :
2820 # Check if workspace exists, if so remove it
2921 if Workspace .objects .filter (name = workspace_name ).exists ():
30- self .stdout .write (self . style . SUCCESS (
31- f" { workspace_name } already exists, removing to recreate"
32- ))
22+ self .stdout .write (
23+ self . style . SUCCESS ( f' { workspace_name } already exists, removing to recreate' )
24+ )
3325 Workspace .objects .filter (name = workspace_name ).delete ()
3426
3527 # Create the workspace
3628 Workspace .objects .create (name = workspace_name , public = True , owner = self .user )
37- self .stdout .write (self .style .SUCCESS (
38- f"{ workspace_name } created"
39- ))
29+ self .stdout .write (self .style .SUCCESS (f'{ workspace_name } created' ))
4030
4131 except Exception as e :
4232 self .stderr .write (self .style .ERROR (str (e )))
4333
4434 def create_tables_for_workspace (self , workspace_name , edge_table_name ):
4535 try :
4636 # Get the paths for all the data objects to upload
47- data_dir_path = Path (settings .BASE_DIR ) / " data" / workspace_name
48- csv_paths = list (data_dir_path .glob (" *.csv" ))
37+ data_dir_path = Path (settings .BASE_DIR ) / ' data' / workspace_name
38+ csv_paths = list (data_dir_path .glob (' *.csv' ))
4939 workspace = Workspace .objects .get (name = workspace_name )
5040
5141 for csv_path in csv_paths :
5242 filename = os .path .splitext (csv_path .name )[0 ]
53-
43+
5444 # Check if table with name exists, if so delete
5545 if Table .objects .filter (workspace = workspace , name = filename ).exists ():
56- self .stdout .write (self .style .SUCCESS (
57- f"{ workspace_name } /{ filename } already exists, removing to recreate"
58- ))
46+ self .stdout .write (
47+ self .style .SUCCESS (
48+ f'{ workspace_name } /{ filename } already exists, removing to recreate'
49+ )
50+ )
5951 Table .objects .filter (workspace = workspace , name = filename ).delete ()
6052
6153 # Open the types file and read the types in
62- types_path = data_dir_path / " types" / f" { filename } .json"
54+ types_path = data_dir_path / ' types' / f' { filename } .json'
6355 with types_path .open ('rb' ) as f :
6456 columns = json .load (f )
65-
57+
6658 # Open the csv and read in the rows
6759 with csv_path .open ('rb' ) as f :
6860 csv_rows = list (csv .DictReader (StringIO (f .read ().decode ('utf-8' ))))
@@ -72,7 +64,9 @@ def create_tables_for_workspace(self, workspace_name, edge_table_name):
7264 csv_rows [i ] = process_row (row , columns )
7365
7466 # Create the table
75- new_table = Table .objects .create (workspace = workspace , name = filename , edge = filename == edge_table_name )
67+ new_table = Table .objects .create (
68+ workspace = workspace , name = filename , edge = filename == edge_table_name
69+ )
7670
7771 # Put the rows into the table
7872 new_table .put_rows (csv_rows )
@@ -85,9 +79,7 @@ def create_tables_for_workspace(self, workspace_name, edge_table_name):
8579 ]
8680 )
8781
88- self .stdout .write (self .style .SUCCESS (
89- f"{ workspace_name } /{ filename } created"
90- ))
82+ self .stdout .write (self .style .SUCCESS (f'{ workspace_name } /{ filename } created' ))
9183
9284 except Exception as e :
9385 self .stderr .write (self .style .ERROR (str (e )))
@@ -97,10 +89,10 @@ def create_network_in_workspace(self, workspace_name, edge_table_name, node_tabl
9789 workspace = Workspace .objects .get (name = workspace_name )
9890
9991 # Create the network
100- Network .create_with_edge_definition (workspace_name , workspace , edge_table_name , node_table_names )
101- self . stdout . write ( self . style . SUCCESS (
102- f" { workspace_name } network created"
103- ))
92+ Network .create_with_edge_definition (
93+ workspace_name , workspace , edge_table_name , node_table_names
94+ )
95+ self . stdout . write ( self . style . SUCCESS ( f' { workspace_name } network created' ))
10496
10597 except Exception as e :
10698 self .stderr .write (self .style .ERROR (str (e )))
@@ -110,27 +102,28 @@ def add_arguments(self, parser):
110102
111103 def handle (self , * args , ** options ):
112104 # Check that owner exists
113- self .user = User .objects .filter (email = options [" email" ]).first ()
105+ self .user = User .objects .filter (email = options [' email' ]).first ()
114106 if self .user is None :
115- raise Exception ('User did not exist' )
107+ raise Exception ('User did not exist' )
116108
117109 # Create the workspaces
118- self .create_workspace_if_not_exists (" boston" )
119- self .create_workspace_if_not_exists (" eurovis-2019" )
120- self .create_workspace_if_not_exists (" miserables" )
121- self .create_workspace_if_not_exists (" movies" )
122- self .create_workspace_if_not_exists (" openflights" )
110+ self .create_workspace_if_not_exists (' boston' )
111+ self .create_workspace_if_not_exists (' eurovis-2019' )
112+ self .create_workspace_if_not_exists (' miserables' )
113+ self .create_workspace_if_not_exists (' movies' )
114+ self .create_workspace_if_not_exists (' openflights' )
123115
124116 # Create the tables
125- self .create_tables_for_workspace (" boston" , " membership" )
126- self .create_tables_for_workspace (" eurovis-2019" , " connections" )
127- self .create_tables_for_workspace (" miserables" , " relationships" )
128- self .create_tables_for_workspace (" openflights" , " routes" )
117+ self .create_tables_for_workspace (' boston' , ' membership' )
118+ self .create_tables_for_workspace (' eurovis-2019' , ' connections' )
119+ self .create_tables_for_workspace (' miserables' , ' relationships' )
120+ self .create_tables_for_workspace (' openflights' , ' routes' )
129121
130122 # Create the networks from the tables
131- self .create_network_in_workspace ("boston" , "membership" , ["clubs" , "members" ])
132- self .create_network_in_workspace ("eurovis-2019" , "connections" , ["people" ])
133- self .create_network_in_workspace ("miserables" , "relationships" , ["characters" ])
134- self .create_network_in_workspace ("openflights" , "routes" , ["airports" ])
135-
136- # TODO: Include movies dataset here once new network creation API is accessible internally from the API
123+ self .create_network_in_workspace ('boston' , 'membership' , ['clubs' , 'members' ])
124+ self .create_network_in_workspace ('eurovis-2019' , 'connections' , ['people' ])
125+ self .create_network_in_workspace ('miserables' , 'relationships' , ['characters' ])
126+ self .create_network_in_workspace ('openflights' , 'routes' , ['airports' ])
127+
128+ # TODO: Include movies dataset here once new network creation API
129+ # is accessible internally from the API
0 commit comments