66from rest_framework .exceptions import NotFound
77
88from apps .app_registry import APP_REGISTRY
9- from apps .models import Apps , AppStatus
9+ from apps .models import Apps , BaseAppInstance
1010from studio .utils import get_logger
1111
1212logger = get_logger (__name__ )
@@ -30,20 +30,38 @@ def list_apps(self, request):
3030 # TODO: MAKE SURE THAT THIS IS FILTERED BASED ON ACCESS
3131 for model_class in APP_REGISTRY .iter_orm_models ():
3232 # Loop over all models, and check if they have the access and description field
33+ # Note: It is not possible to use BaseAppInstance.objects.get_app_instances_not_deleted here
34+ # This is the reason for looping over model_class instead
3335 if hasattr (model_class , "description" ) and hasattr (model_class , "access" ):
34- queryset = model_class .objects .filter (~ Q (app_status__status = "Deleted" ), access = "public" ).values (
35- "id" , "name" , "app_id" , "url" , "description" , "created_on" , "updated_on" , "app_status"
36+ queryset = model_class .objects .filter (access = "public" ).values (
37+ "id" ,
38+ "name" ,
39+ "app_id" ,
40+ "url" ,
41+ "description" ,
42+ "created_on" ,
43+ "updated_on" ,
44+ "latest_user_action" ,
45+ "k8s_user_app_status" ,
3646 )
37- # using a dictionary to avoid duplicates for shiny apps
47+ # Using a dictionary to avoid duplicates for shiny apps
3848 for item in queryset :
39- list_apps_dict [item ["id" ]] = item
49+ # Do not include deleted apps
50+ app_status = BaseAppInstance .convert_to_app_status (
51+ item .get ("latest_user_action" ),
52+ item .get ("k8s_user_app_status" ),
53+ )
54+ if app_status != "Deleted" :
55+ item ["app_status" ] = app_status
56+ list_apps_dict [item ["id" ]] = item
4057
4158 # Order the combined list by "created_on"
4259 list_apps = sorted (list_apps_dict .values (), key = lambda x : x ["created_on" ], reverse = True )
4360
4461 for app in list_apps :
62+ assert app ["app_status" ] != "Deleted"
63+
4564 app ["app_type" ] = Apps .objects .get (id = app ["app_id" ]).name
46- app ["app_status" ] = AppStatus .objects .get (pk = app ["app_status" ]).status
4765
4866 # Add the previous url key located at app.table_field.url to support clients using the previous schema
4967 app ["table_field" ] = {"url" : app ["url" ]}
@@ -71,8 +89,18 @@ def retrieve(self, request, app_slug=None, pk=None):
7189
7290 try :
7391 queryset = model_class .objects .all ().values (
74- "id" , "name" , "app_id" , "url" , "description" , "created_on" , "updated_on" , "access" , "app_status"
92+ "id" ,
93+ "name" ,
94+ "app_id" ,
95+ "url" ,
96+ "description" ,
97+ "created_on" ,
98+ "updated_on" ,
99+ "access" ,
100+ "latest_user_action" ,
101+ "k8s_user_app_status" ,
75102 )
103+
76104 logger .info ("Queryset: %s" , queryset )
77105 except FieldError as e :
78106 message = f"Error in field: { e } "
@@ -84,24 +112,26 @@ def retrieve(self, request, app_slug=None, pk=None):
84112 logger .error ("App instance is not available" )
85113 raise NotFound ("App instance is not available" )
86114
87- app_status_pk = app_instance . get ( "app_status" , None )
88- logger . info ( "DID WE GET HERE?!" )
89- if app_status_pk is None :
90- raise NotFound ( "App status is not available" )
115+ app_status = BaseAppInstance . convert_to_app_status (
116+ app_instance . get ( "latest_user_action" ),
117+ app_instance . get ( "k8s_user_app_status" ),
118+ )
91119
92- app_status = AppStatus .objects .get (pk = app_status_pk )
93- if app_status .status == "Deleted" :
94- logger .error ("This app has been deleted" )
120+ if app_status == "Deleted" :
121+ logger .info ("This app has been deleted" )
95122 raise NotFound ("This app has been deleted" )
96123
124+ app_instance ["app_status" ] = app_status
125+
97126 if app_instance .get ("access" , False ) != "public" :
98- logger .error ("This app is non-existent or not public" )
99127 raise NotFound ("This app is non-existent or not public" )
100128
101- app_instance ["app_status" ] = app_status .status
129+ app_type_info = Apps .objects .get (id = app_instance ["app_id" ])
130+ app_instance ["app_type" ] = app_type_info .name
131+
132+ # Remove misleading app_id from the final output because it only refers to the app type
133+ del app_instance ["app_id" ]
102134
103- add_data = Apps .objects .get (id = app_instance ["app_id" ])
104- app_instance ["app_type" ] = add_data .name
105135 data = {"app" : app_instance }
106136 logger .info ("API call successful" )
107137 return JsonResponse (data )
0 commit comments