def generate_tags(n_tags): fake_generator = faker.Faker() for i in range(n_tags): while True: n_words = random.randint(1, 4) tag_name = ' '.join(fake_generator.word() for _ in range(n_words)) if Tag.objects.filter(title=tag_name).first() is None: break tag = Tag(title=tag_name) tag.save() print(f"[{i+1}/{n_tags}] Saved tag {tag}")
def handle(self): for i in range(10): user = User.objects.create_user(username='******' + str(i), password='******' + str(i + 1)) user.save() for i in range(10): profile = Profile(user=User.objects.get(username='******' + str(i)), i, date(2005 + i, i + 1, (i + 1) * 2)) profile.save() tags = [ 'django', 'python', 'shell', 'user', 'android', 'net', 'c', 'java', 'web', 'html' ] for i in tags: tag = Tag(title=i) tag.save() for i in range(10): q = Question( title= "Cannot to set datetime with input field. Always return DateTime.MinValue", text= "My programm are supposed to have date filtr and gives article with proper date. But when i input any date in my datetime field my values don't change and are always DateTime.MinValue. Idk why and how i can fix it.", is_published=True, rating=i, author=Profile.objects.get(id=i + 1), likes=Profile.objects.get(id=(i + 2) // 2), tags=Tag.objects.all()[:i]) for i in range(10): ans = Answer( text="Nice question!", added_at=date(2019, i + 1, (i + 1) * 2), question=Question.objects.get(id=i + 1), author=Profile.objects.get(id=(i + 2) // 2), ) for i in range(10): ans = Answer( text="I don't know!", added_at=date(2019, i + 2, i + 3), question=Question.objects.get(id=(i + 2) // 2), author=Profile.objects.get(id=(i + 3) // 2), )
def search_question_by_tag_keyword(keyword, questioner): """ キーワードに合致するすべてのタグのうち、自分が投稿した質問のタグと一致するもののみ取り出す """ tags = Tag.get_tags_by_name(tagname=keyword) q_list = [] for tag in tags: q_tags = QuestionTag.get_questions_by_tag(tag=tag) q = [q_tag.question for q_tag in q_tags if q_tag.question.questioner == questioner] q_list.extend(q) return list(set(q_list))
def search_replylist_by_tag_keyword(keyword, answerer): """ 自分に来た(回答したものも含む)質問のタグと一致するもののみ取り出す """ tags = Tag.get_tags_by_name(tagname=keyword) r_list = ReplyList.objects.filter(answerer=answerer) r_list_tmp = [] for tag in tags: q_tags = QuestionTag.get_questions_by_tag(tag=tag) for rl in r_list: for q_tag in q_tags: if rl.question.id == q_tag.question.id: r_list_tmp.append(rl) return list(set(r_list_tmp))
def mypage(request): """ マイページ """ # ユーザのプロファイル p = get_object_or_404(UserProfile, user=request.user) # ユーザが登録しているタグ user_tags = UserTag.objects.filter(user=request.user) # edit if request.method == 'POST': form = UserProfileEditForm(request.POST, request.FILES, instance=p) # 完了がおされたら if form.is_valid(): form.save(commit=True) # 選択されたタグがそのユーザに付加されてないタグだったら新規にQuestionTagを生成 u_tag_names = UserTag.get_user_all_tags_name(user=request.user) tags = form.cleaned_data['tag'] for q_tag in tags: if q_tag.name not in u_tag_names: UserTag.objects.create(tag=q_tag, user=request.user) # 追加されたタグ名が新規に追加されたタグだったら新規にTagとUserTagを生成 tag_name = Tag.get_all_tags_name() tag_added_name = form.cleaned_data['tag_added'] if tag_added_name != "" and tag_added_name not in tag_name: UserTag.objects.create(tag=Tag.objects.create(name=tag_added_name), user=request.user) return redirect('dotchain:mypage') pass # new else: form = UserProfileEditForm(instance=p) user_question = Question.objects.filter(questioner=request.user) user_reply = Reply.objects.filter(answerer=request.user) return render_to_response('question/mypage.html', {'form': form, 'user_tags': user_tags, 'uprof': p, 'uquestion': user_question, 'ureply': user_reply}, context_instance=RequestContext(request))
def handle(sself, *args, **options): for i in range(10): try: user = User.objects.get(username='******'+str(i)) except User.DoesNotExist: user = User.objects.create_user(username='******' + str(i), password='******' + str(i + 1)) user.is_superuser=False user.is_staff=False user.save() for i in range(10): try: profile = Profile.objects.get(user=User.objects.get(username='******' + str(i))) except Profile.DoesNotExist: profile = Profile(user=User.objects.get(username='******' + str(i)), rating=i, birthday=date(2005 + i, i + 1, (i + 1) * 2)) profile.save() tags = ['django', 'python', 'shell', 'user', 'android', 'net', 'c', 'java', 'web', 'html'] for i in tags: try: tag = Tag.objects.get(title=i) except Tag.DoesNotExist: tag = Tag(title=i) tag.save() for i in range(10): try: q = Question.objects.get(title="Cannot to set datetime with input field. Always return DateTime.MinValue" + str(i), text= str(i) + "My programm are supposed to have date filtr and gives article with proper date. But when i input any date in my datetime field my values don't change and are always DateTime.MinValue. Idk why and how i can fix it.", is_published=True, author=Profile.objects.get(id=i + 1), ) except Question.DoesNotExist: q = Question(title="Cannot to set datetime with input field. Always return DateTime.MinValue" + str(i), text=str(i) +"My programm are supposed to have date filtr and gives article with proper date. But when i input any date in my datetime field my values don't change and are always DateTime.MinValue. Idk why and how i can fix it.", date_published = datetime.now(), is_published=True, rating=i, author=Profile.objects.get(id=i + 1), #likes=likes.set(Profile.objects.get(id=(i + 2)//2)), #tags=Tag.objects.get(id=i+1) ) #q.likes.set(Profile.objects.get(id=(i + 2)//2)) #q.tags.set(Tag.objects.get(id=i+1)) q.save() q.likes.add(Profile.objects.get(id=(i + 2)//2)) q.tags.add(Tag.objects.get(id=i+1)) for i in range(10): try: ans = Answer.objects.get(text="Nice question!", added_at = date(2019, i + 1, (i + 1) * 2), question=Question.objects.get(id=i + 1), author=Profile.objects.get(id=(i + 2)//2), ) except Answer.DoesNotExist: ans = Answer(text="Nice question!", added_at = date(2019, i + 1, (i + 1) * 2), question=Question.objects.get(id=i + 1), author=Profile.objects.get(id=(i + 2)//2), ) ans.save() for i in range(10): try: ans = Answer.objects.get(text="I don't know!", added_at = date(2019, i + 2, i + 3), question=Question.objects.get(id=(i+2)//2), author=Profile.objects.get(id=(i+3)//2), ) except Answer.DoesNotExist: ans = Answer(text="I don't know!", added_at = date(2019, i + 2, i + 3), question=Question.objects.get(id=(i+2)//2), author=Profile.objects.get(id=(i+3)//2), ) ans.save()
def question_edit(request, id=None, msg=None): """ 質問ページ """ # edit # 質問の編集機能は今は下書きの場合のみ使う if id: q = get_object_or_404(Question, pk=id) # user check if q.questioner != request.user: return top_default(request, msg=m.INFO_INVALID_ACCESS) # new else: q = Question() # edit if request.method == 'POST': form = QuestionEditForm(request.POST, instance=q) if form.is_valid(): # 質問を保存 q = form.save(commit=False) q.update(questioner=request.user, draft=form.cleaned_data['draft']) if q.draft: return top_default(request, msg=m.INFO_QUESTION_SAVE_OK) # 質問の宛先(複数)を生成 div_list = form.cleaned_data['destination'] for div in div_list: QuestionDestination.objects.create(question=q, tag=div) # ランダムに質問者を選んでReplyListを生成 qa_manager = QAManager() r_list = qa_manager.make_reply_list(q, qa_manager.reply_list_update_random_except) if r_list is None: q.delete() msg = m.INFO_NO_DESTINATION return render_to_response('question/question_edit.html', {'form': form, 'id': id, 'msg': msg}, context_instance=RequestContext(request)) else: r_list.save() # 選択されたタグから、新規にQuestionTagを生成 q_tags = form.cleaned_data['tag'] for q_tag in q_tags: QuestionTag.objects.create(tag=q_tag, question=q) # 追加されたタグ名が新規に追加されたタグだったら生成 tag_name = Tag.get_all_tags_name() tag_added_name = form.cleaned_data['tag_added'] if tag_added_name != "" and tag_added_name not in tag_name: QuestionTag.objects.create(tag=Tag.objects.create(name=tag_added_name), question=q) return top_default(request, msg=m.INFO_QUESTION_SEND_OK) pass # new else: form = QuestionEditForm(instance=q, initial={'time_limit': datetime.timedelta(minutes=1)}) return render_to_response('question/question_edit.html', {'form': form, 'id': id}, context_instance=RequestContext(request))
def mypage(request): """ マイページ """ # ユーザのプロファイルを取ってくる work_place, created = WorkPlace.objects.get_or_create(name='東京', defaults=dict(name='東京', ), ) work_status, created = WorkStatus.objects.get_or_create(name='在席', defaults=dict(name='在席', ), ) division, created = Division.objects.get_or_create(code=2, name='人事', defaults=dict(code=2, name='人事')) p, created = UserProfile.objects.get_or_create(user=request.user, defaults=dict(avatar='images/icons/pepper.png', work_place=work_place, work_status=work_status, division=division, accept_question=1, ), ) # ユーザが登録しているタグを取ってくる user_tags = UserTag.objects.filter(user=request.user) # user_tags = [user_tag.tag for user_tag in user_tags] # edit if request.method == 'POST': form = UserProfileEditForm(request.POST, request.FILES, instance=p) # 完了がおされたら if form.is_valid(): print(form) r = form.save(commit=False) r.save() # 選択されたタグから、新規にQuestionTagを生成して保存 tags = form.cleaned_data['tag'] u_tag_names = [t.tag.name for t in user_tags] for q_tag in tags: if q_tag.name not in u_tag_names: # ユーザに新規に追加されたタグだったら保存 qt = UserTag() qt.tag = q_tag qt.user = request.user qt.save() # 追加されたタグ名から、新規にTagとQuestionTagを生成して保存 tag_added_name = form.cleaned_data['tag_added'] tags = Tag.objects.all() tag_name = [t.name for t in tags] if tag_added_name != "" and tag_added_name not in tag_name: # 新規に追加されたタグだったら保存 t = Tag() t.name = tag_added_name t.save() qt = UserTag() qt.tag = t qt.user = request.user qt.save() else: print("tag_added") print(tag_added_name) return redirect('dotchain:mypage') pass # new else: form = UserProfileEditForm(instance=p) # tag_form = UserTagEditForm(instance=t) # TODO マイページにユーザが登録済みのタグを表示しつつ、追加・編集できるようにしたい user_question = Question.objects.filter(questioner=request.user) user_reply = Reply.objects.filter(answerer=request.user) return render_to_response('question/mypage.html', {'form': form, 'user_tags': user_tags, 'uprof': p, 'uquestion': user_question, 'ureply': user_reply}, context_instance=RequestContext(request))
def question_edit(request, id=None, msg=None): """ 質問ページ """ # edit if id: q = get_object_or_404(Question, pk=id) # user check if q.questioner != request.user: print("不正なアクセスです!") return redirect('dotchain:top') # new else: q = Question() # edit if request.method == 'POST': form = QuestionEditForm(request.POST, instance=q) # 完了がおされたら if form.is_valid(): # 質問を保存 q = form.save(commit=False) q.questioner = request.user q.draft = form.cleaned_data['draft'] q.save() div_list = form.cleaned_data['destination'] for div in div_list: d = QuestionDestination() d.question = q d.tag = div d.save() # ランダムに質問者を選んでからReplyListを生成して保存 qa_manager = QAManager() r_list = qa_manager.make_reply_list(q, qa_manager.reply_list_update_random_except) if r_list is None: q.delete() msg = '宛先ユーザが見つかりませんでした。入力された質問は消去されます。\n' msg += '次の原因が考えられます。\n' msg += '・送信先にユーザがいない\n' msg += '・送信先に1日以内にログインしたユーザがいない\n' msg += '・送信先に受信拒否のユーザしかいない' return render_to_response('question/question_edit.html', {'form': form, 'id': id, 'msg': msg}, context_instance=RequestContext(request)) else: r_list.save() # 選択されたタグから、新規にQuestionTagを生成して保存 q_tags = form.cleaned_data['tag'] for q_tag in q_tags: qt = QuestionTag() qt.tag = q_tag qt.question = q qt.save() # 追加されたタグ名から、新規にTagとQuestionTagを生成して保存 tag_added_name = form.cleaned_data['tag_added'] tags = Tag.objects.all() tag_name = [t.name for t in tags] if tag_added_name != "" and tag_added_name not in tag_name: # 新規に追加されたタグだったら保存 t = Tag() t.name = tag_added_name t.save() qt = QuestionTag() qt.tag = t qt.question = q qt.save() return redirect('dotchain:top') pass # new else: form = QuestionEditForm(instance=q, initial={'time_limit': datetime.timedelta(minutes=1)}) return render_to_response('question/question_edit.html', {'form': form, 'id': id}, context_instance=RequestContext(request))
def handle(self, *args, **options): for tag in options['arg']: name = Tag(title=tag) name.save()