def show_group_list(self): # 상태 설정 fs.collection(Const.COL_USER).document(self.user_key).update( {Const.FIELD_STATE: Const.STATE_SELECT_GROUP}, firestore.CreateIfMissingOption(True)) # 그룹 목록 가져오기 groups = fs.collection(Const.COL_GROUP).get() group_list = [] for doc in groups: group_list.append(doc.id) group_list.append(Const.BTN_GOTO_START) rst = { "message": { "text": '그룹을 선택해 주세요' }, "keyboard": { "type": "buttons", "buttons": group_list } } return Util.send_response(rst)
def group_selected(self): fs.collection(Const.COL_USER).document(self.user_key).update( {Const.FIELD_GROUP: self.content}, firestore.CreateIfMissingOption(True)) msg = '[{}] 을(를) 선택하였습니다'.format(self.content) return Util.show_start_menu(msg)
def show_delete_restaurant(self): fs.collection(Const.COL_USER).document(self.user_key).update( {Const.FIELD_STATE: Const.STATE_DELETE_RESTAURANT}, firestore.CreateIfMissingOption(True)) user = fs.collection(Const.COL_USER).document(self.user_key).get() group = user.get(Const.FIELD_GROUP) ref = fs.collection(Const.COL_GROUP).document(group).get() restaurants = ref._data.get(Const.FIELD_RESTAURANT) restaurant_list = [] for key, val in restaurants.items(): restaurant_list.append(key) restaurant_list.append(Const.BTN_GOTO_START) rst = { "message": { "text": '삭제할 대상을 선택해 주세요' }, "keyboard": { "type": "buttons", "buttons": restaurant_list } } return Util.send_response(rst)
def add_restaurant(self): user = fs.collection(Const.COL_USER).document(self.user_key).get() group = user.get(Const.FIELD_GROUP) title = user.get(Const.FIELD_ADD_RESTAURANT_TITLE) desc = user.get(Const.FIELD_ADD_RESTAURANT_DESC) try: ref = fs.collection(Const.COL_GROUP).document(group).get() restaurant = ref._data.get(Const.FIELD_RESTAURANT) except: restaurant = {} if restaurant is None: restaurant = {} restaurant[title] = { 'desc': desc, 'user': self.user_key, 'added': Util.get_day_str() } if Util.is_img(desc): restaurant[title][Const.FIELD_IMG_SRC] = desc fs.collection(Const.COL_GROUP).document(group).update( {Const.FIELD_RESTAURANT: restaurant}, firestore.CreateIfMissingOption(True)) if Util.is_img(desc): msg = '[{}] 이(가) 등록되었습니다'.format(title) return Util.show_start_menu(msg, desc) msg = '이름:{}\n설명:{}\n등록되었습니다'.format(title, desc) return Util.show_start_menu(msg)
def show_add_restaurant(self): # state : add_restaurant # 1. 이름을 입력해 주세요 # 2. 설명을 작성해 주세요. # 상태 설정 fs.collection(Const.COL_USER).document(self.user_key).update( {Const.FIELD_STATE: Const.STATE_ADD_RESTAURANT_TITLE}, firestore.CreateIfMissingOption(True)) rst = {"message": {"text": '음식점 이름을 입력해 주세요'}} return Util.send_response(rst)
def show_add_restaurant_desc(self): # content = 음식점 이름 # 상태 설정 fs.collection(Const.COL_USER).document(self.user_key).update( {Const.FIELD_STATE: Const.STATE_ADD_RESTAURANT_DESC}, firestore.CreateIfMissingOption(True)) fs.collection(Const.COL_USER).document(self.user_key).update( {Const.FIELD_ADD_RESTAURANT_TITLE: self.content}) rst = {"message": {"text": '음식점 사진을 추가해 주세요'}} return Util.send_response(rst)
def show_add_restaurant_confirm(self): fs.collection(Const.COL_USER).document(self.user_key).update( {Const.FIELD_STATE: Const.STATE_ADD_RESTAURANT_CONFIRM}, firestore.CreateIfMissingOption(True)) fs.collection(Const.COL_USER).document(self.user_key).update( {Const.FIELD_ADD_RESTAURANT_DESC: self.content}, firestore.CreateIfMissingOption(True)) user = fs.collection(Const.COL_USER).document(self.user_key).get() title = user.get(Const.FIELD_ADD_RESTAURANT_TITLE) desc = self.content if Util.is_img(desc): rst = { "message": { "text": '입력하신 음식점은 다음과 같습니다.\n\n이름:{}\n\n등록하시겠습니까?'.format(title), "photo": { "url": desc, "width": 640, "height": 480 } }, "keyboard": { "type": "buttons", "buttons": [Const.BTN_ADD_RESTAURANT_CONFIRM, Const.BTN_GOTO_START] } } else: rst = { "message": { "text": '입력하신 음식점은 다음과 같습니다.\n\n이름:{}\n설명:{}\n\n등록하시겠습니까?'.format( title, desc) }, "keyboard": { "type": "buttons", "buttons": [Const.BTN_ADD_RESTAURANT_CONFIRM, Const.BTN_GOTO_START] } } return Util.send_response(rst)
def show_result(self): try: # 해당 그룹에서 가장 많이 나온 순서대로 보여준다. user = fs.collection(Const.COL_USER).document(self.user_key).get() user_group = user.get(Const.FIELD_GROUP) except: print('Not Found User Group') return Select(self.args).show_group_list() today = Util.get_day_str() today_format = '[{}]'.format(today) msg_list = [today_format] result_dict = {} try: result = fs.collection( Const.COL_SELECT).document(user_group).collection( Const.COL_TODAY).document(today).get() for user_key, restaurant in result._data.items(): if restaurant in result_dict: result_dict[restaurant] += 1 else: result_dict[restaurant] = 1 sorted_list = sorted(result_dict.items(), key=operator.itemgetter(1)) sorted_list.reverse() for item in sorted_list: title = item[0] count = item[1] msg_list.append('{}명 : {}'.format(count, title)) except: print('Error') msg_list.append('아직 아무도 선택하지 않았습니다.') rst = { "message": { "text": '\n'.join(msg_list) }, "keyboard": { "type": "buttons", "buttons": Const.DEFAULT_KEYBOARD } } return Util.send_response(rst)
def delete_restaurant(self): user = fs.collection(Const.COL_USER).document(self.user_key).get() group = user.get(Const.FIELD_GROUP) title = user.get(Const.FIELD_DELETE_RESTAURANT_TITLE) try: ref = fs.collection(Const.COL_GROUP).document(group).get() restaurant = ref._data.get(Const.FIELD_RESTAURANT) del restaurant[title] except: restaurant = {} fs.collection(Const.COL_GROUP).document(group).update( {Const.FIELD_RESTAURANT: restaurant}, firestore.CreateIfMissingOption(True)) msg = '[{}] 을(를) 삭제하였습니다.'.format(title) return Util.show_start_menu(msg)
def show_restaurant_list(self): # 상태 설정 fs.collection(Const.COL_USER).document(self.user_key).update( {Const.FIELD_STATE: Const.STATE_SELECT_RESTAURANT}, firestore.CreateIfMissingOption(True)) # 그룹이 없으면 그룹 선택으로 보낸다. try: user = fs.collection(Const.COL_USER).document(self.user_key).get() user_group = user.get(Const.FIELD_GROUP) except: print("NotFound user") return self.show_group_list() try: # 해당 그룹의 식당목록을 가져온다. restaurant_list = [] group = fs.collection(Const.COL_GROUP).document(user_group).get() restaurants = group.get(Const.FIELD_RESTAURANT) for key, val in restaurants.items(): restaurant_list.append(key) except: print('No RESTAURANTS') return Util.show_start_menu('등록된 식당이 없습니다') shuffle(restaurant_list) if len(restaurant_list) > Const.SHOW_COUNT: restaurant_list = restaurant_list[:Const.SHOW_COUNT] restaurant_list.append(Const.BTN_GOTO_START) rst = { "message": { "text": '식당을 선택해 주세요' }, "keyboard": { "type": "buttons", "buttons": restaurant_list } } return Util.send_response(rst)
def create_history(self, today): user = fs.collection(Const.COL_USER).document(self.user_key).get() user_group = user.get(Const.FIELD_GROUP) # 오늘 기준 # 5일 전까지 고려 title_list = [] for i in range(1, 5 + 1): ago_day = Util.get_day_str(-1 * i) max_title = self.get_max_selected_restaurant(user_group, ago_day) if max_title: title_list.append(max_title) fs.collection(Const.COL_SELECT).document(user_group).collection( Const.COL_HISTORY).document(today).update( {Const.FIELD_MAX_HISTORY_LIST: title_list}, firestore.CreateIfMissingOption(True)) return title_list
def restaurant_selected(self): user = fs.collection(Const.COL_USER).document(self.user_key).get() user_group = user.get(Const.FIELD_GROUP) today = Util.get_day_str() fs.collection(Const.COL_SELECT).document(user_group).collection( Const.COL_TODAY).document(today).update( {self.user_key: self.content}, firestore.CreateIfMissingOption(True)) msg = '[{}] 을(를) 선택하였습니다'.format(self.content) try: ref = fs.collection(Const.COL_GROUP).document(user_group).get() restaurants = ref._data.get(Const.FIELD_RESTAURANT) target = restaurants.get(self.content) img_src = target.get(Const.FIELD_IMG_SRC) except: img_src = None return Util.show_start_menu(msg, img_src)
def show_delete_restaurant_confirm(self): fs.collection(Const.COL_USER).document(self.user_key).update( {Const.FIELD_STATE: Const.STATE_DELETE_RESTAURANT}, firestore.CreateIfMissingOption(True)) fs.collection(Const.COL_USER).document(self.user_key).update( {Const.FIELD_DELETE_RESTAURANT_TITLE: self.content}, firestore.CreateIfMissingOption(True)) title = self.content rst = { "message": { "text": '[{}] 을(를) 삭제하겠습니까?'.format(title) }, "keyboard": { "type": "buttons", "buttons": [Const.BTN_DELETE_RESTAURANT_CONFIRM, Const.BTN_GOTO_START] } } return Util.send_response(rst)
def get_max_selected_restaurant(cls, user_group, day): result_dict = {} try: result = fs.collection( Const.COL_SELECT).document(user_group).collection( Const.COL_TODAY).document(day).get() for user_key, restaurant in result._data.items(): if restaurant in result_dict: result_dict[restaurant] += 1 else: result_dict[restaurant] = 1 sorted_list = sorted(result_dict.items(), key=operator.itemgetter(1)) sorted_list.reverse() title = sorted_list[0][0] except: title = None return title
def show_setting_list(self): try: user = fs.collection(Const.COL_USER).document(self.user_key).get() group = user.get(Const.FIELD_GROUP) except: return Select.show_group_list() rst = { "message": { "text": '현재 [{}] 그룹으로 설정되어 있습니다.'.format(group) }, "keyboard": { "type": "buttons", "buttons": [ Const.BTN_ADD_RESTAURANT, Const.BTN_DELETE_RESTAURANT, Const.BTN_SETTING_GROUP, Const.BTN_DOANTE, Const.BTN_GOTO_START ] } } return Util.send_response(rst)