Example #1
0
def jupd(question_id):
    current_app.logger.debug("question.jupd. - " + str(question_id))    
    
    question = Question.get_active_question_by_id(question_id)
    
    if question:
        if current_user.id == question.user_id:
            
            current_app.logger.warning(request.json)

            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},
                        "answers" : {"type": "array", "items": { "type" : "object", "properties": {"id" : {"type" : "integer", "maxLength" : 8, "optional" : False},
                                                                  "correct" : {"type" : "string", "enum" : ["T", "F"], "optional" : False}
                                                                  }}, "maxItems" : 25, "optional" : True},
                        "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)

            qid = request.json['id']
            qtext = request.json['qtext']
            answers = request.json['answers']
            latitude = request.json['lat']
            longitude = request.json['lon']

            if len(errors) == 0:
                #TODO - allow displaying unallowed tags as non html tags
                #scrubb = scrubber.Scrubber()                
                #qtext = jinja2.Markup(scrubb.scrub(qtext))
                
                current_app.logger.debug("got a question from DB, id = " + str(question_id))
                current_app.logger.debug("id = " + str(qid))
                current_app.logger.debug("qtext = '" + qtext + "'")
                current_app.logger.debug("answers = " + str(answers))
                current_app.logger.debug("latitude = " + str(latitude))
                current_app.logger.debug("longitude = " + str(longitude))

                results = QuestionResult.get_question_results_by_revision_id(question.revision_id)

                if len(results) > 0:
                    Question.update_question_by_id_and_create_revision(question_id, qtext, latitude, longitude)
                else:
                    Question.update_question_by_id(question_id, qtext, latitude, longitude)

                db.session.commit()

                current_app.logger.debug("Status - OK")
                
                result = {'staus':'OK'}
                return jsonify(result)
            else:
                if len(qtext) > 4096:
                    msg = u"Question text is too long. It must be less than 4096 symbols"
                    current_app.logger.warning(msg)
                    return jsonify({"status" : "ERROR", "message" : msg})
                elif len(answers) > 25:
                    msg = u"Number of answers must not be greater than 25"
                    current_app.logger.warning(msg)
                    return jsonify({"status" : "ERROR", "message" : msg})                    
                else:
                    msg = u"Error : "
                    for e in errors:
                        msg = msg + str(list(e.path)[len(list(e.path))-1]) + " - " + e.message.decode("UTF-8")
    
                    current_app.logger.warning(msg)
                    return jsonify({"status" : "ERROR", "message" : msg})
        else:
            msg = auth_failure_message + u"update this question"
            current_app.logger.warning(msg)
            return jsonify({"status" : "ERROR", "message" : msg})            
    else:
        msg = u"No such question found!"
        current_app.logger.warning(msg)
        return jsonify({"status" : "ERROR", "message" : msg})