---
# System prepended metadata

title: validate_depression_form 修改

---

# validate_depression_form 修改

## 新增 Function
```py=359    
def get_db_subjects():
    client = MongoClient('mongodb://root:rootpassword@140.115.54.68:27017/?authMechanism=DEFAULT')
    database = client['therapist']
    subject = database['Subject']
    
    return subject

def get_session_id(tracker: Tracker):
    
    session_id = None
    metadata = tracker.latest_message.get('metadata')
    if metadata is not None and "session_id" in metadata:
        session_id = metadata["session_id"]
    return session_id

def submit_occupation(tracker: Tracker):
    
    subject_table = get_db_subjects()
    session_id = get_session_id(tracker=tracker)
    # session_id = "5ae6cd46-406b-4e08-b3ae-996415eb7a01"
    record = subject_table.find_one({"session_id": session_id})
    # 判斷是否有在 DB 裡面找到目標 session id
    if record is not None:
        job = record["job"]
    else:
        job = "other" 
        
    tracker.add_slots([SlotSet("depression_jobs", job)])
    
def submit_gender(tracker: Tracker):
    
    subject_table = get_db_subjects()
    session_id = get_session_id(tracker=tracker)
    record = subject_table.find_one({"session_id": session_id})
    if record is not None:
        gender = record["gender"]
    else:
        gender = "M" 
        
    tracker.add_slots([SlotSet("gender", gender)])

```

## 修改 class ValidateDepressionForm 

### run()
* 主要更改：423 - 424
```py=416
    async def run(
        self,
        dispatcher: "CollectingDispatcher",
        tracker: "Tracker",
        domain: "DomainDict",
    ) -> List[EventType]:
        
        submit_occupation(tracker=tracker)
        submit_gender(tracker=tracker)
            
        # tracker.latest_message.get("intent"): 抓上一個問題答案的意圖
        # tracker.latest_message.get("intent")['name'] == EXTERNAL_reminder_repeat: 成立的話就要重問
        if tracker.latest_message.get("intent") and tracker.latest_message.get("intent")['name'] == EXTERNAL_reminder_repeat:
            # REQUESTED_SLOT: 現在正在問的問題
            current_request_slot = tracker.get_slot(REQUESTED_SLOT)
            logger.info(
                f"receive reminder info with current request {current_request_slot if current_request_slot else ''}")
            # reminder repeat ask info
            ret = []
            # 判斷是不是第一次問，如果 current_request_slot 不是 None，那就不是第一句
            if current_request_slot is not None:
                counter = tracker.get_slot(REMINDER_COUNTER_SLOT)
                # if counter and counter >= REMINDER_MAX_COUNT:
                #     # reminder超過一定次數,可能用戶已經關閉，取消reminder
                #     logger.info(
                #         f"cancel reminder due to max reminder {REMINDER_MAX_COUNT}")
                #     ret.append(ReminderCancelled())
                # else:
                #     date = datetime.datetime.now() + datetime.timedelta(seconds=REMINDER_DURATION_SECONDS)
                #     entities = tracker.latest_message.get("entities")
                #     reminder = ReminderScheduled(
                #         EXTERNAL_reminder_repeat,
                #         trigger_date_time=date,
                #         entities=entities,
                #         name=f"reminder_{current_request_slot}",
                #         kill_on_user_message=True,
                #     )
                #     ret.append(reminder)
                # 清空當前的Slot的值，因為他有可能被觸發這個reminder的消息填充了
                ret.append(SlotSet(current_request_slot, None))
                ret.append(SlotSet(REMINDER_COUNTER_SLOT, counter+1))
                ret.append(SlotSet(REQUESTED_SLOT, current_request_slot))
            return ret
        # 清空reminder counter
        tracker.add_slots([SlotSet(REMINDER_COUNTER_SLOT, 0)])
        sensor_events = await ensure_fetch_sensor_expression(tracker)
        tracker.add_slots(sensor_events)
        # super function
        extraction_events = await self.extract_custom_slots(dispatcher, tracker, domain)
        tracker.add_slots(extraction_events)

        validation_events = await self.validate(dispatcher, tracker, domain)
        tracker.add_slots(validation_events)
        ################## Remove repeat question part ##################
        next_slots = await self.next_requested_slot_with_repeat(dispatcher, tracker, domain)
        if next_slots:
            validation_events.extend(next_slots)
        
        # if sensor_events:
        #     validation_events.extend(sensor_events)
        # Validation events include events for extracted slots
        return validation_events

```

### required_slots()

```py=558
JOB = tracker.get_slot('depression_jobs')
        
        if JOB is not None:
            if JOB == "student" and \
                "depression_lockself_a2" in required_slots and \
                    "overall_work" in required_slots:
                required_slots.remove("depression_lockself_a2")
                required_slots.remove("overall_work")
            if JOB == "other" and \
                "depression_lockself_a1" in required_slots and \
                    "overall_study" in required_slots:
                required_slots.remove("depression_lockself_a1")
                required_slots.remove("overall_study")
                
        SEX = tracker.get_slot('gender')
        
        if SEX is not None:
            if SEX == "M" and \
                "sex_f_satisfy" in required_slots and \
                    "sex_f_excited" in required_slots and \
                        "sex_f_inferiority" in required_slots:
                required_slots.remove("sex_f_satisfy")
                required_slots.remove("sex_f_excited")
                required_slots.remove("sex_f_inferiority")
            if SEX == "F" and \
                "sex_m_excited" in required_slots and \
                    "sex_m_satisfy" in required_slots and \
                        "sex_m_inferiority" in required_slots:
                required_slots.remove("sex_m_excited")
                required_slots.remove("sex_m_satisfy")
                required_slots.remove("sex_m_inferiority")
```