def question(): if request.method == 'GET': return render_template('question.html') else: title = request.form.get('title') content = request.form.get('content') questions = Question(title=title, content=content) questions.author = g.user db.session.add(questions) db.session.commit() return redirect(url_for('index'))
def questions_edit(request): context = {} form = QuetionEditForm(hide_condition=True) if request.method == 'POST': form= QuetionEditForm(request.POST, hide_condition=True) if form.is_valid(): # form.save() txt = form.cleaned_data['question_txt'] question_id = form.cleaned_data['question_id'] if question_id == '': nq = Question( user = request.user, date = timezone.now(), question = txt ) nq.save() return HttpResponseRedirect('/q/media/?q=%s'% nq.id) else: question = Question.objects.get(id=question_id) question.question = txt question.save() questions = Question.objects.all() return HttpResponseRedirect('/q/media/?q=%s'% question.id) else: args = {} args.update(csrf(request)) args['form'] = form return render_to_response('question/question_form.html', args) else: if 'q' in request.GET: question_id = request.GET['q'] question = Question.objects.get(id=question_id) form = QuetionEditForm({'question_txt':question.question, 'question_id':question_id, }, hide_condition=True) context.update({'q': question_id}) context.update({'form': form}) context.update(csrf(request)) return direct_to_template(request, 'question/question_form.html', context)
def post(self): data = request.get_json() name = data["name"] active = data["active"] active_flag = data["active_flag"] desc = data["desc"] flag = data["flag"] q_type = data["type"] integral = data["integral"] attachment = data.get('attachment', []) if not name: raise exceptions.CheckException("名称字段不允许为空") question = Question(name=name, active=active, active_flag=active_flag, desc=desc, flag=flag, type=q_type, integral=integral) db.session.add(question) db.session.flush() if attachment: for file in attachment: db.session.add( QuestionFile(question_id=question.id, filename=file["filename"], file_path=file["file_path"])) db.session.commit() if active_flag: image = data["image"] host = data["host"] # docker image 已经在主机中了 只需要创建虚拟镜像资源绑定即可 image_resource = ImageResource(question_id=question.id, host_id=host, image_id=image) db.session.add(image_resource) db.session.commit() return jsonify({})
def question_list(): """ 题库列表 和题库添加 :data :subject 题目分类 :return: """ if request.method == 'GET': page = int(request.args.get('page', 1)) page_size = int(request.args.get("page_size", 10)) subject = request.args.get("subject") query = db.session.query(Question) if subject: query = query.filter(Question.type == subject) page = query.order_by(Question.id.desc()).paginate(page=page, per_page=page_size) data = [] for item in page.items: image_resource = db.session.query(ImageResource).filter( ImageResource.question_id == item.id).one_or_none() if image_resource: resource = { "host": image_resource.host_id, "image": image_resource.image_id } else: resource = {} data.append({ "resource": resource, "id": item.id, "date_created": item.date_created.strftime("%Y-%m-%d %H:%M:%S") if item.date_created else None, "date_modified": item.date_modified.strftime("%Y-%m-%d %H:%M:%S") if item.date_modified else None, "name": item.name, 'type': item.type, "active": item.active, "flag": item.flag, "active_flag": item.active_flag, "integral": item.integral, "desc": item.desc }) return jsonify({"total": page.total, "results": data}) else: data = request.get_json() name = data["name"] active = data["active"] active_flag = data["active_flag"] desc = data["desc"] flag = data["flag"] q_type = data["type"] integral = data["integral"] question = Question(name=name, active=active, desc=desc, flag=flag, type=q_type, integral=integral) db.session.add(question) db.session.commit() if active_flag: image = data["image"] host = data["host"] # docker image 已经在主机中了 只需要创建虚拟镜像资源绑定即可 image_resource = ImageResource(question_id=question.id, host_id=host, image_id=image) db.session.add(image_resource) db.session.commit() return jsonify({})
def list_question_media(request): # Handle file upload context = {} documents = [] if request.method == 'POST': form = DocumentForm(request.POST, request.FILES) if form.is_valid(): docfile = request.FILES['docfile'] question_id = form.cleaned_data['question_id'] question = Question.objects.get(id=question_id) docfile_name = docfile.name docfile_name_changed = docfile_name.replace('.','~^~') question.docs.update({docfile_name_changed:docfile.content_type}) question.image.update({docfile_name_changed +'_url':'/static/display/s_'+docfile_name, docfile_name_changed +'_name': docfile_name, docfile_name_changed +'_content_type':docfile.content_type}) file_read = docfile.file.read() file_data = base64.b64encode(file_read) question.image.update({docfile_name_changed +'_data': file_data}) # question.save() nq = Question( user = question.user, date = question.date, question = question.question, docs = question.docs, image = question.image, audio = question.audio, other = question.other, vote_ids = question.vote_ids, ) question.delete() # todo - there is an update issue here nq.save() questions = Question.objects.all() return render_to_response('question/home.html', {'questions':questions, 'the_user': request.user, 'message': 'Media updated!' }) # Redirect to the document list after POST # return HttpResponseRedirect(reverse('data.views.list_question_media')) else: if 'q' in request.GET: question_id = request.GET['q'] form = DocumentForm({ 'question_id':question_id,}, hide_condition=True) question = Question.objects.get(id=question_id) documents = [] for docname in question.docs: file_data = question.image[docname+'_data'] file_name = question.image[docname+'_name'] orig_fname = settings.STATIC_ROOT + '/../moma_example/static/display/' + file_name new_fname = settings.STATIC_ROOT + '/../moma_example/static/display/tn_' + file_name _decode_image_to_size(file_data, orig_fname, new_fname, basewidth=128) documents.append({'docfile':{'url':question.image[docname +'_url'], 'name':question.image[docname+'_name'], 'thumb':'/static/display/tn_'+question.image[docname+'_name']}}) context.update({'q': question_id, 'question':question}) # Load documents for the list page # documents = [{'docfile':{'url':'blaaa', 'name':'Bleee'}}] context.update( {'documents': documents, 'form': form,}) context.update(csrf(request)) context.update({'the_user': request.user}) return direct_to_template(request, 'question/question_media.html', context)