def mutate(root, info, question_data): errors = {} current_user = get_jwt_identity() user = UserModel.find_by_id(current_user["id"]) if not user: errors["user"] = "******" category = CategoryModel.find_by_id(question_data["category_id"]) if not category: errors["category"] = "not found" location = CityModel.find_by_id(question_data["location_id"]) if not location: errors["location"] = "not found" if errors: raise GraphQLError(json.dumps(errors)) del question_data["category_id"] del question_data["location_id"] question = QuestionModel(created_by=user, category=category, location=location, **question_data) question.save() return CreateQuestion(question=question, ok=True)
def post(self): form = ReportForm() if form.validate_on_submit(): question = Question(user_id=current_user.id, title=form.title.data, detail=form.detail.data) question.save() return redirect(url_for('user.report')) return render_template('user/question/report.html', form=form)
def create_and_save_question(title, text, is_mcq, answers, room): ''' Helper to create a question and save it to the graph :param name: the name of the question :param active: whether or not this question will be active by default :param admin_id: the user id representing the admin :param course_id: the course code / id representing the course that this question studies :return ValueError: raised if either the course or user cannot be found in the graph ''' new_question = Question(title=title, text=text, is_multiple_choice=is_mcq) new_question.save() new_question.room.connect(room) for a in answers: temp_answ = Answer(text=a.get('text'), correct=a.get('correct')) temp_answ.save() temp_answ.question.connect(new_question) return new_question
def initdb(): click.echo('Initializing the database...') User.drop_collection() Post.drop_collection() Question.drop_collection() click.echo('Generating the User..') # FIXME: for i in range(1, 10): u = User( email = fake.email(), username = fake.name(), avatar = f'http://192.168.1.106:8000/avatar/r{i}.png' ) u.set_password('helloworld') u.save() click.echo('Done') click.echo('Generating the Post..') for i in range(10): p = Post( title = fake.sentence(), content = fake.text(2000), category = random.choices(['Python', 'JavaScript','Golang', 'Flask','Vue', 'Django'])[0], tags = random.choices(['Python', 'JavaScript','Golang', 'Flask','Vue', 'Django']), ) p.save() click.echo('Done') click.echo('Generating the Comment..') for i in Post.objects.all(): for j in range(5): c = Comment( author = random.choice(User.objects.all()), content = fake.text(200), ) i.comments.append(c) i.save() click.echo('Done') click.echo('Generating the Reply..') for i in Post.objects.all(): for j in range(5): c = Comment( author = random.choice(User.objects.all()), content = fake.text(200), reply = random.choice(i.comments).cid ) i.comments.append(c) i.save() click.echo('Done') click.echo('Generating the Question..') for j in range(5): q = Question( title = fake.sentence(), author = random.choice(User.objects.all()), description = fake.text(200), tags = random.choices(['Python', 'JavaScript','Golang', 'Flask','Vue', 'Django']), ) q.save() click.echo('Done') click.echo('Generating the Answer Comment..') for i in Question.objects.all(): for j in range(5): c = Answer( author = random.choice(User.objects.all()), content = fake.text(200), ) i.answer.append(c) i.save() click.echo('Done') click.echo('Generating the Answer Reply..') for i in Question.objects.all(): for j in range(5): c = Answer( author = random.choice(User.objects.all()), content = fake.text(200), reply = random.choice(i.answer)._id ) i.answer.append(c) i.save() click.echo('Done')