def test_task_finished_state(self): w1, w2 = self.load_wordbook() user, userp = self.create_user('learner') userp.amount = 2 userp.learning_wordbook = w1 userp.save() tasks = LearnTask.get_user_tasks(user) self.assertEqual(tasks.count(), 2) task = tasks[0] task.known() task.known() tasks = LearnTask.get_user_tasks(user) self.assertEqual(tasks.count(), 1) task = tasks[0] task.known() task.known() tasks = LearnTask.get_user_tasks(user) self.assertEqual(tasks.count(), 0)
def test_generate_task_not_exists_yesterday_task(self): w1, w2 = self.load_wordbook() user, userp = self.create_user('learner') userp.amount = 5 userp.learning_wordbook = w1 userp.save() # 用户在2016年9月8日学习5个单词 today = datetime.now().date() tasks = LearnTask.get_user_tasks(user, today=today) self.assertEqual(tasks.count(), 5) # 时间过得飞快,转眼间来到了9月9日 # 用户觉得自己昨天学的太少,增加了单词数量 userp.amount = 6 userp.save() today1 = today + timedelta(days=1) # 用户发现自己的任务表里面昨天的单词已经不见了 # 今天的任务是全新布置的6个单词 tasks = LearnTask.get_user_tasks(user, today=today1) #print(userp.get_diary(today1)) self.assertEqual(tasks.count(), 6) self.assertEqual(tasks.filter(build_date=today).count(), 0) self.assertEqual(tasks.filter(build_date=today1).count(), 6)
def test_too_simple(self): w1, w2 = self.load_wordbook() user, userp = self.create_user('learner') userp.amount = 1 userp.learning_wordbook = w1 userp.save() tasks = LearnTask.get_user_tasks(user) task = tasks[0] task.too_simple() self.assertTrue(task.remember) self.assertTrue(task.finished) state = task.get_learn_state() self.assertTrue(state.too_simple)
def test_generate_task_more_than_wordbook_count(self): w1, w2 = self.load_wordbook() # 创建用户 user, userp = self.create_user('learner') # 这个用户想学500个单词,可是单词书里面只有11个 userp.amount = 500 userp.learning_wordbook = w1 userp.save() self.assertEqual(userp.learning_wordbook.wordlist_set.all().count(), 11) # 用户还是不改变注意,开始了学习 tasks = LearnTask.get_user_tasks(user) # 用户发现自己今天的学习任务还是10个单词 self.assertEqual(tasks.count(), 11)
def test_unknow(self): w1, w2 = self.load_wordbook() user, userp = self.create_user('learner') userp.amount = 1 userp.learning_wordbook = w1 userp.save() tasks = LearnTask.get_user_tasks(user) task = tasks[0] task.known() self.assertTrue(task.remember) self.assertFalse(task.finished) self.assertEqual(task.get_learn_state().familiar_level, 0) task.unknown() self.assertFalse(task.remember) self.assertFalse(task.finished) self.assertEqual(task.get_learn_state().familiar_level, 0)
def learning_view(request): task = LearnTask.get_user_tasks(request.user).first() userp = get_object_or_404(UserProperty, user=request.user) if task is None: redirect(reverse('home_page')) if request.method == 'GET': if not task: return redirect(reverse('home_page')) return render(request, 'learning.html', context={ 'wordlist': task.word, 'userp': userp, 'task': task }) elif request.method == 'POST': # 判断类型 form = NoteForm() alert_text = '' alert_type = 'info' if 'add-note' not in request.POST: # 如果不是提交笔记 # 先把任务顺序改变 # 根据post类型进行相应操作 if 'too-simple' in request.POST: task.too_simple() alert_text = '你已将该单词标记为太简单,将不会再对这个单词进行学习' elif 'unknown' in request.POST: task.unknown() alert_text = '稍后将重新学习这个单词' elif 'known' in request.POST: task.known() if task.finished: alert_text = '这个单词学习完成,今天将不再出现' alert_type = 'success' else: alert_text = '这个单词稍后还需要复习一遍' else: # 添加笔记 try: task_id = int(request.POST['task_id']) task = LearnTask.objects.get(id=task_id) except: raise Http404 form = NoteForm(data=request.POST) if form.is_valid(): Note.objects.create( userproperty=userp, word=task.word, content=form.cleaned_data['content'], shared=form.cleaned_data['shared'] ) alert_text = '笔记添加成功' return render(request, 'learning_detail.html', context={ 'wordlist': task.word, 'userp': userp, 'form': form, 'task': task, 'alert_text': alert_text, 'alert_type': alert_type, 'my_notes': task.word.user_notes(request.user), 'shared_notes': task.word.shared_notes(exclude_user=request.user) })
def home_page(request): userp = get_object_or_404(UserProperty, user=request.user) LearnTask.get_user_tasks(request.user) return render(request, 'home.html', context={'userp': userp})