1
+ import logging
1
2
import pathlib
2
3
from functools import cached_property
3
4
4
5
import yaml
6
+ from core .utils .db import fast_first
5
7
from rest_framework import serializers
6
8
7
- from .models import ProductTourInteractionData , UserProductTour
9
+ from .models import ProductTourInteractionData , ProductTourState , UserProductTour
10
+
11
+ logger = logging .getLogger (__name__ )
8
12
9
13
PRODUCT_TOURS_CONFIGS_DIR = pathlib .Path (__file__ ).parent / 'configs'
10
14
11
15
12
16
class UserProductTourSerializer (serializers .ModelSerializer ):
17
+ # steps is a list of steps in the tour loaded from the yaml file
13
18
steps = serializers .SerializerMethodField (read_only = True )
19
+ # awaiting is a boolean that indicates if the tour is awaiting other tours in the list of "dependencies"
20
+ awaiting = serializers .SerializerMethodField (read_only = True )
14
21
15
22
class Meta :
16
23
model = UserProductTour
@@ -29,14 +36,25 @@ def validate_name(self, value):
29
36
30
37
return value
31
38
39
+ @cached_property
32
40
def load_tour_config (self ):
33
41
# TODO: get product tour from yaml file. Later we move it to remote storage, e.g. S3
34
42
filepath = PRODUCT_TOURS_CONFIGS_DIR / f'{ self .context ["name" ]} .yml'
35
43
with open (filepath , 'r' ) as f :
36
44
return yaml .safe_load (f )
37
45
46
+ def get_awaiting (self , obj ):
47
+ config = self .load_tour_config
48
+ dependencies = config .get ('dependencies' , [])
49
+ for dependency in dependencies :
50
+ tour = fast_first (UserProductTour .objects .filter (user = self .context ['request' ].user , name = dependency ))
51
+ if not tour or tour .state != ProductTourState .COMPLETED :
52
+ logger .info (f'Tour { dependency } is not completed: skipping tour { self .context ["name" ]} ' )
53
+ return True
54
+ return False
55
+
38
56
def get_steps (self , obj ):
39
- config = self .load_tour_config ()
57
+ config = self .load_tour_config
40
58
return config .get ('steps' , [])
41
59
42
60
def validate_interaction_data (self , value ):
0 commit comments