コード例 #1
0
ファイル: word.py プロジェクト: Yamaneko1031/tori-api
 def get(self, word: str) -> models.WordAll:
     """ 単語情報取得
     """
     docs = db.collection(self.collection_name).where("word", "==",
                                                      word).limit(1).get()
     if docs:
         return models.WordAll(**docs[0].to_dict())
     return
コード例 #2
0
ファイル: word.py プロジェクト: Yamaneko1031/tori-api
 def get_topic_taught(self,
                      taught: str,
                      get_ref: bool = False) -> models.WordAll:
     """ 教えた単語の中から一つピックアップして取得
     """
     doc = db.collection(
         self.collection_session).document(document_id=taught).get()
     doc_dict = doc.to_dict()
     if "teach_refs" not in doc_dict:
         return
     ref = random.choice(doc.to_dict()["teach_refs"])
     if get_ref:
         return ref
     return models.WordAll(**ref.get().to_dict())
コード例 #3
0
ファイル: word.py プロジェクト: Yamaneko1031/tori-api
    def get_common_tag_word(self, word: str, tag: str) -> models.WordAll:
        """ 共通タグの単語情報取得
        """
        data = []
        docs = db.collection("words").where("tags", "array_contains",
                                            tag).stream()
        for doc in docs:
            if doc.to_dict()["word"] != word:
                data.append(doc)

        if data:
            return models.WordAll(**random.choice(data).to_dict())

        return
コード例 #4
0
ファイル: word.py プロジェクト: Yamaneko1031/tori-api
    def get_topic_word(self,
                       taught: str = "",
                       limit: int = 20,
                       get_ref: bool = False) -> models.WordAll:
        """ 自分が教えていない単語の中から一つピックアップして取得
        """
        # get_data_list = []
        # docs = db.collection(self.collection_name).where("taught", "!=", taught).order_by(
        #     u'updated_at', direction=firestore.Query.ASCENDING).limit(limit).stream()
        # for doc in docs:
        #     word_dict = doc.to_dict()
        #     get_data_list.append({
        #         "word": word_dict["word"],
        #         "mean": word_dict["word"],
        #         "tag_list": word_dict["tag_list"],
        #     })
        get = False

        word_cnt = system_service.get_system_data()["word_cnt"]
        random_index = random.randint(0, word_cnt - 1)

        for i in range(limit):
            docs = db.collection(self.collection_name).where(
                "index", "==", random_index).limit(1).get()
            if docs:
                word_dict = docs[0].to_dict()
                if word_dict["kind"] == "NG":
                    pass
                elif "taught" in word_dict:
                    if word_dict["taught"] != taught:
                        get = True
                        break
                else:
                    get = True
                    break
            random_index = (random_index + 1) % word_cnt

        if not get:
            docs = db.collection(self.collection_name).where(
                "word", "==", "むーちゃん").limit(1).get()

        if get_ref:
            return docs[0]._reference
        return models.WordAll(**docs[0].to_dict())
コード例 #5
0
ファイル: word.py プロジェクト: Yamaneko1031/tori-api
def create_tr(transaction: Transaction, word_create: models.WordCreate,
              tags: List[models.TagAct], taught: str, collect_word: str,
              collect_unknown: str, collect_session: str):
    """ 新規単語の追加で呼び出すトランザクション処理
    """
    # unknownsコレクションにある場合は削除する
    docs = db.collection(collect_unknown).where("word", "==",
                                                word_create.word).stream()
    for doc in docs:
        transaction.delete(doc._reference)
        system_service.dec_unknown()

    set_like = 0
    set_tags = []
    set_tags_cnt = {}
    for tag in tags:
        set_tags.append(tag["text"])
        set_tags_cnt[tag["text"]] = firestore.Increment(1)
        set_like = set_like + tag["pnt"]

    # wordsコレクションに追加
    word_data = models.WordAll(**word_create.dict()).dict()
    word_data["taught"] = taught
    word_data["like"] = set_like
    word_data["tags"] = set_tags
    word_data["tags_cnt"] = set_tags_cnt
    word_data["cnt"] = firestore.Increment(1)
    word_data["index"] = system_service.get_system_data()["word_cnt"]
    word_ref = db.collection(collect_word).document()
    transaction.set(word_ref, word_data)

    # sessionsコレクション更新
    session_ref = db.collection(collect_session).document(document_id=taught)
    session_data = {
        "teach_words": firestore.ArrayUnion([word_create.word]),
        "teach_refs": firestore.ArrayUnion([word_ref]),
        "teach_cnt": firestore.Increment(1),
    }
    transaction.set(session_ref, session_data, merge=True)

    return word_ref
コード例 #6
0
ファイル: word.py プロジェクト: Yamaneko1031/tori-api
    def create(self, word_create: models.WordCreate, taught: str,
               ip_address: str) -> models.WordAll:
        """ 新規単語の追加
        """
        self.renew_ng_list()

        retData = self.get_knowns_list(mean=word_create.mean,
                                       teach_word=word_create.word,
                                       secret_tag=word_create.secret_tag)

        docs = db.collection(self.collection_name).where(
            "word", "==", word_create.word).limit(1).get()

        if docs:
            if self.update_mean(word_create.word, word_create.mean, taught):
                retData["tweet"] = self.mean_update_tweet(
                    docs[0]._reference, taught, ip_address)
            ref = docs[0]._reference
            retData["action"] = "更新"
            retData["pre_mean"] = ref.get().to_dict()["word"]
        else:
            transaction = db.transaction()
            ref = create_tr(transaction=transaction,
                            word_create=word_create,
                            taught=taught,
                            tags=retData["tags"],
                            collect_word=self.collection_name,
                            collect_unknown=self.collection_unknown,
                            collect_session=self.collection_session)
            system_service.add_word_create(word_create.word)
            retData["tweet"] = self.word_create_tweet(ref,
                                                      word_create.secret_tag,
                                                      taught, ip_address)
            retData["action"] = "新規"
            retData["pre_mean"] = ""

        retData["create"] = models.WordAll(**ref.get().to_dict())
        retData["create_ref"] = ref

        return retData