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 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 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()