def post(self,name): ''' Creates a new client inquriy, if not already existing under the same name. ''' cnfg = ConfigurationModel.find() if cnfg is None or cnfg.dsgvo != 1: return {'message': "Configuration error. Check your GDPR/DSGVO settings."}, 400 #bad request if ClientInquiriesModel.find_by_name(name): return {'message': "Inquiry with name '{}' already exists.".format(name)}, 400 #bad request #schreibe zeugs in db data = resources.parsers.ParseClientInquiriesPost.parser.parse_args() #check if description is empty description = "" if (data['qdescription'] is not None): description = data['qdescription'] # answer, prr_answer and irr_answer will set to 0 answer = [0]* len(data['options']) prr = [0]* len(data['options']) irr = [0]* len(data['options']) #validity checks if not check_type(data['type']): return {'message': "type must be 'cbx', 'mc' or 'bool'."}, 400 #bad request if (data['type'] == 'bool' and len(data['options']) != 2): return {'message': "when type 'bool' is chosen, only 2 answer options are possible."}, 400 #bad request if not check_if_bits(answer): return {'message': "only 0s and 1s allowed in answers"}, 400 #bad request if not check_fpq(config_f, config_p, config_q): return {'message': "f,p and q must have values between 0.0 and 1.0"}, 400 #bad request inquiry = ClientInquiriesModel(name, data['type'], json.dumps(data['options']), json.dumps(answer), #json.dumps(data['answer']), json.dumps(prr), json.dumps(irr), description, False, #responded is False, because inquiry is created but not answered yet. config_locked, #data['locked'], cnfg.global_f, #config_f, until first edit by the user global values are used instead of data['f'], cnfg.global_p, #config_p, #until first edit by the user global values are used instead of data['p'], cnfg.global_q) #config_q) #until first edit by the user global values are used instead of data['q']) try: inquiry.save_to_db() except: return {'message': "error while inserting inquiry with name '{}'.".format(name)}, 500 #internal server error return inquiry.tojson(), 201 #created
def settings(): ''' Configuration page (web GUI). ''' cnfg = ConfigurationModel.find() if cnfg is None: abort(404) form = SettingsForm() #sets the values in the form if request.method != 'POST': form.dsgvo.default = cnfg.dsgvo form.quiz.default = cnfg.quizmode form.f.default = cnfg.global_f form.p.default = cnfg.global_p form.q.default = cnfg.global_q form.process() if form.validate_on_submit(): dsgvo = form.dsgvo.data quiz = form.quiz.data f = form.f.data p = form.p.data q = form.q.data if not check_fpq(f, p, q): print("Only values between 0 and 1 allowed for f,p,q!") #debug flash("Only values between 0 and 1 allowed for f,p,q!") return render_template('settings.html', form=form, cnfg=cnfg, title='client settings') cnfg.dsgvo = dsgvo cnfg.quizmode = quiz cnfg.global_f = f cnfg.global_p = p cnfg.global_q = q try: cnfg.save_to_db() except: return render_template( '/error_pages/500.html', title='error while trying to save inquiries.') return render_template('settings.html', form=form, cnfg=cnfg, title='client settings')
def inquiries_privacy(id): ''' This is for privacy settings. ''' #access is only allowed if gdpr is set to true cnfg = ConfigurationModel.find() if cnfg is None: abort(404) if cnfg.dsgvo != 1: print("info works") return redirect(url_for('gdpr')) inq = ClientInquiriesModel.find_by_id(id) if inq is None: abort(404) form = PrivacyForm() if form.validate_on_submit(): #answer = form.answer.data locked = form.locked.data f = form.f.data p = convert_range(form.p.data) q = convert_range(form.q.data) if not check_fpq(f, p, q): print("Only values between 0 and 1 allowed for f,p,q!") #debug flash("Only values between 0 and 1 allowed for f,p,q!") return render_template('inquiries/privacy.html', inq=inq, form=form, title='privacy settings') inq.f = f inq.p = p inq.q = q try: inq.save_to_db() except: return render_template( '/error_pages/500.html', title='error while trying to save inquiries.') return render_template('inquiries/privacy.html', inq=inq, form=form, title='privacy')
def post(self, surveyid): ''' Creates a new report by it's surveyid. ''' data = resources.parsers.ParseTestReports.parser.parse_args() #data = Report.parser.parse_args() if not check_fpq(data['f'], data['p'], data['q']): return { 'message': "f,p and q must have values between 0.0 and 1.0" }, 400 #bad request report = ReportModel(surveyid, data['prr'], data['irr'], data['f'], data['p'], data['q'], json.dumps(data['answers'])) try: report.save_to_db() except: return {'message': "error while inserting report"}, 500 return report.tojson(), 201 # created
def put(self): ''' Allows to change the configuration. There is no post request, because the configuration is treated as singleton. ''' data = resources.parsers.ParseConfiguration.parser.parse_args() cnfg = ConfigurationModel.find() if cnfg is None: return {'message': "No configuration available"}, 400 #bad request if not check_fpq(data['global_f'],data['global_p'],data['global_q']): return {'message': "Global f,p and q must have values between 0.0 and 1.0"}, 400 #bad request cnfg.global_f = data['global_f'] cnfg.global_p = data['global_p'] cnfg.global_q = data['global_q'] cnfg.dsgvo = data['dsgvo'] cnfg.quizmode = data['quizmode'] try: cnfg.save_to_db() except: return {'message': "Error while setting configuration data."}, 500 return cnfg.tojson(), 201 # created
def post(self, surveyid): ''' Client/public REST resource. Saves a report sent by a client, only if data is valid and the referenced survey is active. ''' # print("request: ", request.args) # debug: only for testing if SurveyModel.find_active_survey_by_id(surveyid): data = resources.parsers.ParseReportsPost.parser.parse_args() survey = SurveyModel.find_survey_by_id(surveyid) # validity: check if global fpq values are correct. if not check_fpq(data['f'], data['p'], data['q']): return { 'message': "report discarded: f,p,q must have values between 0.0 and 1.0" }, 400 #bad request report = ReportModel(surveyid, data['prr'], data['irr'], data['f'], data['p'], data['q'], json.dumps(data['answers'])) # check if type is correct, check if length of answer is correct if check_incoming_report(report, survey): print("report ok") try: report.save_to_db() except: return { 'message': "error while inserting report with surveyid '{}'. ". format(surveyid) }, 500 #internal server error return report.tojson(), 201 #created return { 'message': "report not accecpted. Incoming data not valid or survey not existing." }, 400 #bad request
def put(self,name): ''' Changes a client inquiry by its name. The following values can be changed by the user: answers, description, locked, f,p and q. ''' cnfg = ConfigurationModel.find() if cnfg is None or cnfg.dsgvo != 1: return {'message': "Configuration error. Check your GDPR/DSGVO settings."}, 400 #bad request data = resources.parsers.ParseClientInquiriesPut.parser.parse_args() inquiry = ClientInquiriesModel.find_by_name(name) if inquiry is None: return {'message': "Can not change status, inquiry '{}' does not exist".format(name)}, 400 #bad request #check if description is empty description = data['qdescription'] if (data['qdescription'] is None): description = inquiry.qdescription #check if the lengt of the answer is correct (still the same). if not (len(data['answer']) == len(json.loads(inquiry.answer))): print("laenge") print(len(data['answer'])) print(len(json.loads(inquiry.answer))) return {'message': "old and new answer must have the same amount of options."}, 400 #bad request #check bool if not check_bool(inquiry.type,data['answer']): return {'message': "error: give a correct answer for type 'bool'."}, 400 #bad request #check mc if not check_mc(inquiry.type,data['answer']): return {'message': "error: only one answer options is allowed for mc questions."}, 400 #bad request #answer must be a list of 0s and 1s if not check_if_bits(data['answer']): return {'message': "only 0s and 1s allowed in answers"}, 400 #bad request # user answer must have as many values as inquiry options if (len(json.loads(inquiry.options)) is not len(data['answer'])): return {'message': "Your answer must have as many values as options are available"}, 400 #bad request if not check_fpq(data['f'],data['p'],data['q']): return {'message': "f,p and q must have values between 0.0 and 1.0"}, 400 #bad request # PRR and IRR will be only made if answer changes if (inquiry.answer != json.dumps(data['answer'])): inquiry.answer = json.dumps(data['answer']) # a PRR will be made after a answer is was changed prr = permanent_RandomizedResponse(float(data['f']),data['answer']) inquiry.prr_answer = json.dumps(prr) # a PRR will be made after a answer is was changed irr = instantaneous_RandomizedResponse(float(data['p']),float(data['q']),prr) inquiry.irr_answer = json.dumps(irr) # if a answer was given by the user, responded will be set to TRUE inquiry.responded = True inquiry.qdescription = description #data['qdescription'] inquiry.locked = data['locked'] inquiry.f = data['f'] inquiry.p = data['p'] inquiry.q = data['q'] try: inquiry.save_to_db() except: return {'message': "error while saving inquiry with name: '{}'.".format(name)}, 500 #internal server error return inquiry.tojson(), 202 #accepted