示例#1
0
 def deleteById(self, record_id):
    record = Record.objects(id_number= record_id)
    if record is not None:
        record.delete()
        return True
    else:
         return False
示例#2
0
 def get_current_record(self):
     record_id = self.get_secure_cookie("record_id")
     if not record_id:
         return None
     else:
         try:
             record = Record.objects(pk=ObjectId(record_id)).get()
         except DoesNotExist:
             self.clear_cookie("record_id")
             # self.redirect(self.get_argument("next", "/"))
             self.clear_all_cookies()
             self.render("login.html", error="请重新登录")
             raise tornado.web.Finish
         else:
             return record
示例#3
0
 def readById(self, record_id):
     record = Record.objects(id_number= record_id)
     return record
示例#4
0
    def get(self):
        """
        生成背诵列表
        使用Cookie来记录用户的Record
        :return:
        """
        user = self.get_current_user_mongo()

        # 获取上次未完成的背诵
        record = self.get_current_record()
        if record:
            # self.write_response(record.format_response())
            next_word = record.next_word
            self.set_secure_cookie("next_word", next_word)
            self.render("recite.html",
                        record=record.format_response(),
                        next_word=next_word)
            return

        # 已背的单词们
        # Pipeline
        pipeline = [
            # Stage 1
            {
                "$match": {
                    "user": user.pk,
                }
            },

            # Stage
            {
                "$unwind": "$words"
            },

            # Stage
            {
                "$match": {
                    "words.status": WORD_FINISHED
                }
            },

            # Stage
            {
                "$group": {
                    "_id": "$user",
                    "words_recited": {
                        "$addToSet": "$words.word"
                    }
                }
            },

            # Stage
            {
                "$project": {
                    "_id": 0,
                    "words_recited": 1
                }
            }
        ]
        logging.info(user.pk)
        recited_words = Record.objects(
            user=user, words__status=WORD_FINISHED).aggregate(*pipeline)
        # 如果有值, 会返回只拥有一个元素的列表
        recited_words = list(recited_words)

        query = [Q(scope__in=[user.scope])]
        if recited_words:
            query.append(Q(word__nin=recited_words[0]['words_recited']))

        query = reduce(lambda x, y: x & y, query)
        # 获取新的待背诵单词
        wait_words = Word.objects(query).limit(user.quota)
        # 记录新的背诵记录
        new_record = Record(user=user,
                            words=[{
                                "word": word.word,
                                "status": WORD_UNDONE
                            } for word in wait_words]).save()
        self.set_secure_cookie("record_id", str(new_record.id))
        next_word = new_record.words[0]['word']
        # self.set_secure_cookie("next_word", next_word)
        self.render("recite.html",
                    record=new_record.format_response(),
                    next_word=next_word)
        return
示例#5
0
    def get(self):
        """
        生成背诵列表
        使用Cookie来记录用户的Record
        :return:
        """
        user = self.get_current_user_mongo()

        # 获取上次未完成的背诵
        record = self.get_current_record()
        if record:
            # self.write_response(record.format_response())
            next_word = record.next_word
            self.set_secure_cookie("next_word", next_word)
            self.render("recite.html", record=record.format_response(), next_word=next_word)
            return

        # 已背的单词们
        # Pipeline
        pipeline = [
            # Stage 1
            {
                "$match": {
                    "user": user.pk,
                }
            },

            # Stage
            {
                "$unwind": "$words"
            },

            # Stage
            {
                "$match": {
                    "words.status": WORD_FINISHED
                }
            },

            # Stage
            {
                "$group": {
                    "_id": "$user",
                    "words_recited": {"$addToSet": "$words.word"}
                }
            },

            # Stage
            {
                "$project": {
                    "_id": 0,
                    "words_recited": 1
                }

            }
        ]
        logging.info(user.pk)
        recited_words = Record.objects(user=user, words__status=WORD_FINISHED).aggregate(*pipeline)
        # 如果有值, 会返回只拥有一个元素的列表
        recited_words = list(recited_words)

        query = [Q(scope__in=[user.scope])]
        if recited_words:
            query.append(Q(word__nin=recited_words[0]['words_recited']))

        query = reduce(lambda x, y: x & y, query)
        # 获取新的待背诵单词
        wait_words = Word.objects(query).limit(user.quota)
        # 记录新的背诵记录
        new_record = Record(
            user=user, words=[{"word": word.word, "status": WORD_UNDONE} for word in wait_words]
        ).save()
        self.set_secure_cookie("record_id", str(new_record.id))
        next_word = new_record.words[0]['word']
        # self.set_secure_cookie("next_word", next_word)
        self.render("recite.html", record=new_record.format_response(), next_word=next_word)
        return