def create_question(content: str, author_id: int, id: int = 1): # fmt: off question = Question(id=id, content=content, author_id=author_id) # fmt: on question.save() return question
def edit(request, survey_id): s = get_object_or_404(Survey, pk=survey_id) # only admins and survey owner can view this page if request.user.is_staff or (request.user.is_authenticated() and s.owner == request.user): if request.method == "POST": # editing is only allowed when survey status is 'ACTIVE' if s.status == s.STATUS_ACTIVE: errors = {} for k, v in request.POST.items(): if k.startswith("surveyTitle"): s.title = v s.save() elif k.startswith("questionName"): k, pk = k.split('-') q = s.question_set.get(pk=pk) q.question = v q.save() elif k.startswith("questionType"): k, pk = k.split('-') q = s.question_set.get(pk=pk) q.type = int(v) q.save() elif k.startswith("choice"): k, pk = k.split('-') c = Choice.objects.get(pk=pk) if v and v != "Add new choice": c.choice = v c.save() else: c.delete() elif k.startswith("newChoice"): k, pk = k.split('-') if v and v != "Add new choice" and v != "": q = s.question_set.get(pk=pk) c = Choice() c.question = q c.choice = v c.votes = 0 c.save() elif k == "newQuestion" and v and v != "Enter a new question": q = Question() q.survey = s q.question = v if request.POST["newQuestionType"]: q.type = int(request.POST["newQuestionType"]) else: q.type = 1 q.save() if 'publish' in request.POST: if s.question_set.all().count() == 0: errors['publishError'] = "You cannot publish an empty survey. Create some questions first." return render_to_response('surveys/edit/edit.html', {'survey': s, 'today':datetime.datetime.now(), 'tomorrow': datetime.datetime.now() + datetime.timedelta(days=1), 'errors': errors}, context_instance=RequestContext(request)) if 'resultDisplay' in request.POST and 'endtime' in request.POST: if not s.publish(resultDisplay=request.POST["resultDisplay"], endtime=dateutil.parser.parse(request.POST["endtime"], dayfirst=True)): return HttpResponseRedirect(reverse('surveys.views.edit', args=(s.id,))) # actions for published surveys elif s.status == s.STATUS_PUBLISHED: if "end" in request.POST: ACTION_CONFIRM = 0 ACTION_END = 1 if "confirm" in request.POST: s.end() return render_to_response('surveys/edit/end.html', {'survey': s, 'action': ACTION_END}, context_instance=RequestContext(request)) if "negative" in request.POST: return HttpResponseRedirect(reverse('surveys.views.edit', args=(s.id,))) else: return render_to_response('surveys/edit/end.html', {'survey': s, 'action': ACTION_CONFIRM}, context_instance=RequestContext(request)) if "unpublish" in request.POST: if request.user.is_staff: s.status = s.STATUS_ACTIVE s.save() elif s.status == s.STATUS_INACTIVE: if "undelete" in request.POST: if request.user.is_staff: s.status = s.STATUS_ACTIVE s.save() if not s.status == s.STATUS_INACTIVE: if "delete" in request.POST: if request.user.is_staff: s.status = s.STATUS_INACTIVE s.save() if "ban" in request.POST: if request.user.is_staff: s.owner.is_active = False s.owner.save() elif "unban" in request.POST: if request.user.is_staff: s.owner.is_active = True s.owner.save() return HttpResponseRedirect(reverse('surveys.views.edit', args=(s.id,))) return render_to_response('surveys/edit/edit.html', {'survey': s, 'today':datetime.datetime.now(), 'tomorrow': datetime.datetime.now() + datetime.timedelta(days=1)}, context_instance=RequestContext(request)) else: return render_to_response('surveys/edit/error.html', {'survey': s,})
def edit(request, survey_id): s = get_object_or_404(Survey, pk=survey_id) # only admins and survey owner can view this page if request.user.is_staff or (request.user.is_authenticated() and s.owner == request.user): if request.method == "POST": # editing is only allowed when survey status is 'ACTIVE' if s.status == s.STATUS_ACTIVE: errors = {} for k, v in request.POST.items(): if k.startswith("surveyTitle"): s.title = v s.save() elif k.startswith("questionName"): k, pk = k.split('-') q = s.question_set.get(pk=pk) q.question = v q.save() elif k.startswith("questionType"): k, pk = k.split('-') q = s.question_set.get(pk=pk) q.type = int(v) q.save() elif k.startswith("choice"): k, pk = k.split('-') c = Choice.objects.get(pk=pk) if v and v != "Add new choice": c.choice = v c.save() else: c.delete() elif k.startswith("newChoice"): k, pk = k.split('-') if v and v != "Add new choice" and v != "": q = s.question_set.get(pk=pk) c = Choice() c.question = q c.choice = v c.votes = 0 c.save() elif k == "newQuestion" and v and v != "Enter a new question": q = Question() q.survey = s q.question = v if request.POST["newQuestionType"]: q.type = int(request.POST["newQuestionType"]) else: q.type = 1 q.save() if 'publish' in request.POST: if s.question_set.all().count() == 0: errors[ 'publishError'] = "You cannot publish an empty survey. Create some questions first." return render_to_response( 'surveys/edit/edit.html', { 'survey': s, 'today': datetime.datetime.now(), 'tomorrow': datetime.datetime.now() + datetime.timedelta(days=1), 'errors': errors }, context_instance=RequestContext(request)) if 'resultDisplay' in request.POST and 'endtime' in request.POST: if not s.publish( resultDisplay=request.POST["resultDisplay"], endtime=dateutil.parser.parse( request.POST["endtime"], dayfirst=True)): return HttpResponseRedirect( reverse('surveys.views.edit', args=(s.id, ))) # actions for published surveys elif s.status == s.STATUS_PUBLISHED: if "end" in request.POST: ACTION_CONFIRM = 0 ACTION_END = 1 if "confirm" in request.POST: s.end() return render_to_response( 'surveys/edit/end.html', { 'survey': s, 'action': ACTION_END }, context_instance=RequestContext(request)) if "negative" in request.POST: return HttpResponseRedirect( reverse('surveys.views.edit', args=(s.id, ))) else: return render_to_response( 'surveys/edit/end.html', { 'survey': s, 'action': ACTION_CONFIRM }, context_instance=RequestContext(request)) if "unpublish" in request.POST: if request.user.is_staff: s.status = s.STATUS_ACTIVE s.save() elif s.status == s.STATUS_INACTIVE: if "undelete" in request.POST: if request.user.is_staff: s.status = s.STATUS_ACTIVE s.save() if not s.status == s.STATUS_INACTIVE: if "delete" in request.POST: if request.user.is_staff: s.status = s.STATUS_INACTIVE s.save() if "ban" in request.POST: if request.user.is_staff: s.owner.is_active = False s.owner.save() elif "unban" in request.POST: if request.user.is_staff: s.owner.is_active = True s.owner.save() return HttpResponseRedirect( reverse('surveys.views.edit', args=(s.id, ))) return render_to_response( 'surveys/edit/edit.html', { 'survey': s, 'today': datetime.datetime.now(), 'tomorrow': datetime.datetime.now() + datetime.timedelta(days=1) }, context_instance=RequestContext(request)) else: return render_to_response('surveys/edit/error.html', { 'survey': s, })
def new_question(request): body = json.loads(request.body) question = Question(content=body["content"]) question.save() return JsonResponse({"data": body})