Example #1
0
def check_db_status():
    spinner = Halo(text='Populating Database', spinner='dots')
    spinner.start()
    if not os.path.exists(DB_PATH):
        table_sql = """CREATE TABLE IF NOT EXISTS questions (
                                            id text PRIMARY KEY,
                                            q_text text NOT NULL,
                                            answer text,
                                            category text
                                        );"""
        create_DB(DB_PATH)
        create_table(table_sql)
        BASE_QUESTIONS_PATH = root_dir_path + '/data/base_questions.csv'
        if os.path.exists(BASE_QUESTIONS_PATH):
            df = pd.read_csv(BASE_QUESTIONS_PATH, header=0)
            df = df.dropna()
            for index, row in df.iterrows():
                # print ('{} -> {}'.format(type(row['Question']), type(row['Answer'])))
                q = Question(row['Question'])
                q.create_question()
                q.answer_question(row['Answer'])
    spinner.stop_and_persist(symbol='✅ '.encode('utf-8'),
                             text='Populated/Loaded Database')
Example #2
0
def jcreate():
    current_app.logger.debug("jcreate. " + str(request.json))
    
    if current_user.id >= 0:
        
        schema = {
                    "type" : "object",
                    "properties" : {            
                        "id" : {"type" : "integer", "maxLength" : 8, "optional" : False},
                        "quizid" : {"type" : "integer", "maxLength" : 8, "optional" : False},
                        "qtext" : {"type" : "string", "maxLength" : 4096, "optional" : False},                        
                        "lat" : {"type" : "number", "maxLength" : 12, "optional" : False},
                        "lon" : {"type" : "number", "maxLength" : 12, "optional" : False},
                        }
                    }
                
        v = Draft4Validator(schema)
        errors = sorted(v.iter_errors(request.json), key = lambda e: e.path)

        if len(errors) == 0:        
        
            qtext = request.json['qtext']
            quiz_id = request.json['quizid']
            latitude = request.json['lat']
            longitude = request.json['lon']

            new_question = Question.create_question(quiz_id, current_user.id,\
                                                   qtext, qtext, latitude, longitude)

            db.session.commit()
                    
            result = {'jstaus' : 'OK', 'id' : new_question.qid}
            return jsonify(result)
        else:
            msg = "Error in json"
            current_app.logger.warning(msg)
            return jsonify({"status" : "ERROR", "message" : msg})
    else:
        msg = "You should be logged in to create a question"
        current_app.logger.warning(msg)
        return jsonify({"status" : "ERROR", "message" : msg})