Exemple #1
0
    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
Exemple #2
0
def inquiries_create():
    '''
    Create new inquiries (web GUI).
    '''
    cnfg = ConfigurationModel.find()
    if cnfg is None:
        abort(404)
    form = InquiryCreateForm()

    if form.validate_on_submit():

        inquiries = json.loads(form.questions.data)
        # print("client inquiries")
        # print(type(inquiries))
        # print(inquiries)
        for inquiry in inquiries:
            #length_options_list = len(json.loads(inquiry['options']))
            length_options_list = len(inquiry['options'])
            print("inquiry")
            print(inquiry)
            print("liste laenge")
            print(length_options_list)
            options = inquiry['options']
            print("options")
            print(options)

            inq = ClientInquiriesModel(
                name=inquiry['name'],
                type=inquiry['type'],
                options=json.dumps(inquiry['options']),
                answer=json.dumps([0] * length_options_list),
                prr_answer=json.dumps([0] * length_options_list),
                irr_answer=json.dumps([0] * length_options_list),
                qdescription=inquiry['description'],
                responded=False,
                locked=True,
                f=cnfg.global_f,
                p=cnfg.global_p,
                q=cnfg.global_q)
            try:
                inq.save_to_db()
            except:
                return render_template('/error_pages/500.html',
                                       title='error while creating inquiry.')

        return redirect('inquiries/')
    return render_template('inquiries/create.html',
                           form=form,
                           title='create new inquiries')
Exemple #3
0
    def get(self):
        '''
        Request new surveys from the server. If the data is valid, the surveys are stored into the
        server inquiries table. If quizmode is activated, they will be also saved in the client inquiries table.
        '''
        # contact the server of the serviceprovider. get surveys if suceed
        cnfg = ConfigurationModel.find()
        if cnfg is None:
            return {
                'message': "Configuration error."
            }, 500  #internal server error
        try:
            r = requests.get(serviceprovider_surveys)
            umfragen = r.json()
            listevonsurveys = (umfragen['surveys'])
        except requests.exceptions.ConnectionError as e:
            print(e)  #debug
            return {
                'message':
                "server not available. no survey was requested: {} ".format(e)
            }, 400  #bad request

        #this creates client inquiries if quizmode is set.
        print(cnfg.quizmode)
        if cnfg.quizmode is 1:
            print("quizmode is true")
            for survey in listevonsurveys:
                inquiries = (survey['questions']
                             )  #fuer jedes dictonairy in der liste
                for inq in inquiries:
                    name = inq['name']
                    qtype = inq['type']
                    options = inq['options']
                    options_count = len(inq['options'])
                    answer = [0] * options_count
                    prr_answer = [0] * options_count
                    irr_answer = [0] * options_count
                    qdescription = inq['description']
                    responded = False
                    locked = config_locked
                    f = cnfg.global_f
                    p = cnfg.global_p
                    q = cnfg.global_q

                    if ClientInquiriesModel.find_by_name(name):
                        print("client inquiry with name " + name +
                              "already in db")  #debug
                        continue
                    else:
                        if check_type(qtype):
                            inquiry = ClientInquiriesModel(
                                name, qtype, json.dumps(options),
                                json.dumps(answer), json.dumps(prr_answer),
                                json.dumps(irr_answer), qdescription,
                                responded, locked, f, p, q)
                            inquiry.save_to_db()
                            print(
                                "quizmode on: new inquiry with name '{}' saved to client inquiry."
                                .format(name))
                        continue  #print("error: Type '{}' not correct.".format(qtype))

        # creates questions and save the to the db
        for survey in listevonsurveys:  #for every dictonairy in a list of dictionairies
            #generate surveyids, serviceprovider for the questions format
            surveyid = (survey['surveyid'])

            serviceprovider = (survey['serviceprovider'])

            #generate qid, qname, qtype, qoptions and qdescpritons for the questions format
            questions = (survey['questions'])

            for question in questions:
                qid = question['qid']
                name = question['name']
                qtype = question['type']
                options = question['options']
                qdescription = question['description']

                #save question to the db, only if it is not already known by surveyid

                #if survey is in archive, inquiries will not saved.
                if ArchiveModel.find_by_surveyid(surveyid):
                    print(
                        "survey already in archive. ".format(surveyid))  #debug
                    continue

                elif ServerInquiriesModel.already_in_db(surveyid, name):
                    print(
                        "survey and matching inquiries already in DB")  #debug
                    continue

                else:
                    print("surveyid: " + surveyid)  #debug
                    print("serviceprovider: " + serviceprovider)  #debug
                    print("qid: " + str(qid))  #debug
                    print("qname: " + name)  #debug
                    print("qtype: " + qtype)  #debug
                    print("qoptions: " + json.dumps(options))  #debug
                    print("qdescription: " + qdescription)  #debug
                    print("____________________________")  #debug
                    if check_type(qtype):
                        frage = ServerInquiriesModel(
                            qid, surveyid, serviceprovider, name, qtype,
                            json.dumps(options), qdescription, config_locked,
                            cnfg.quizmode)
                        frage.save_to_db()
                        print(
                            "new survey with surveyid '{}' available and fetched from the server."
                            .format(surveyid))
                    print("error: Type '{}' not correct.".format(qtype))

            # create a new entry in the archive:
            if not ArchiveModel.find_by_surveyid(surveyid):
                rchv = ArchiveModel(surveyid)
                rchv.save_to_db()

        return {'message': "done."}, 200  #ok