示例#1
0
def updateTags(id):
    data = toDict(request.data)
    if request.method == 'PUT':
        try:
            Questions.updateTags(id, data['tags'])
            app.logger.info(data['tags'])
            response = json.dumps({
                'success': True,
                'status': 'OK',
                'message': 'Tags updated.'
            })
        except KeyError:
            response = json.dumps({
                'success': False,
                'status': 'FAILURE',
                'message': 'Tags not updated.'
            })
    else:
        response = json.dumps({
            'success': False,
            'status': 'FAILURE',
            'message': 'Not proper keys.'
        })

    return response
示例#2
0
    def test_db_questions(self):
        question1=Questions(question='Intest', answer='Intest')
        db.session.add(question1)
        db.session.commit()
        question=Questions.query.all()
        assert repr(question)=='[Question: Intest\r\nAnswer: Intest\r\nID: 1\r\n]'

                 

                        
示例#3
0
def questionsIDRoute(id):
    data = toDict(
        request.data
    )  # toDict takes the request data and converts it to a dictionary

    success = False  # assume the response is unsucessful
    message = ""  # assume an empty message
    status = ""  # accepted statues: 'OK', 'DENIED', 'FAILURE', 'WARNING', 'INVALID'
    response = {}  # assume the response is empty dict() for now

    if request.method == 'PUT':
        if data['actions'] == 'ups':
            success = Questions.incrementUps(id)
        if data['actions'] == 'downs':
            success = Questions.incrementDowns(id)

    response = json.dumps({
        'success': success,
        'status': status,
        'message': message
    })
    print(response)
    return response
示例#4
0
 def setUp(self):
     db.create_all()
     sample1 = Questions(id=1, question='What?')
     sample2 = Employees(name='John Doe')
     sample3 = Answers(id=1,
                       answer='1',
                       question_id=1,
                       employee_id='John Doe')
     db.session.add(sample1)
     db.session.commit()
     db.session.add(sample2)
     db.session.commit()
     db.session.add(sample3)
     db.session.commit()
示例#5
0
文件: routes.py 项目: pstyp/projectqa
def questions():
    questionData=Questions.query.all()
    form = QuestionForm()
    if form.validate_on_submit():
        questionData = Questions(
            question = form.question.data,
            answer = form.answer.data,
                    )

        db.session.add(questionData)
        db.session.commit()

        return redirect(url_for('questions'))
    else:
        print(form.errors)
        
    return render_template('questions.html', title='questions', questions=questionData, form=form)
示例#6
0
#!/usr/bin/python3

from application import db
from application.models import Questions, Employees, Answers

db.session.remove()
db.drop_all()
db.create_all()

question1 = Questions(question='When I am working, I think about nothing else.')
question2 = Questions(question='I get carried away by my work.')
question3 = Questions(question='When I am working, I forget everything else around me.')
question4 = Questions(question='I am totally immersed in my work.')
question5 = Questions(question='My work gives me a good feeling.')
question6 = Questions(question='I do my work with a lot of enjoyment.')
question7 = Questions(question='I feel happy during my work.')
question8 = Questions(question='I feel cheerful when I am working.')
question9 = Questions(question='I would still do this work, even if I received less pay.')
question10 = Questions(question='I find that I also want to work in my free time.')
question11 = Questions(question='I work because I enjoy it.')
question12 = Questions(question='When I am working on something, I am doing it for myself.')
question13 = Questions(question='I get my motivation from the work itself, and not from the reward for it.')
db.session.add_all([question1, question2, question3, question4, question5, question6, question7, question8, question9,
                   question10, question11, question12, question13])
db.session.commit()
示例#7
0
def questionsRoute():
    data = toDict(
        request.data
    )  # toDict takes the request data and converts it to a dictionary

    success = False  # assume the response is unsucessful
    message = ""  # assume an empty message
    status = ""  # accepted statues: 'OK', 'DENIED', 'FAILURE', 'WARNING', 'INVALID'
    response = {}  # assume the response is empty dict() for now
    question = {}
    questions = []

    if validateQuestionRequest(data):
        message = "Invalid data request object (title, text, engineer, user_id)."
        status = "FAILURE"
        # make the response a json object
        response = json.dumps({
            'success': success,
            'status': status,
            'message': message,
            'question': question
        })
    elif request.method == 'POST':
        # get tags from post request, if not set then there are no tags
        try:
            tags = data['tags']
        except:
            tags = ''

        # Create a user and find our whether it is successful or not
        question = Questions.createQuestion(data['title'],
                                            data['text'],
                                            data['engineer'],
                                            data['user_id'],
                                            tags=tags)

        print("---------------------")
        app.logger.info(question)
        print("---------------------")

        if question is not None:
            success = True
            status = "OK"
            message = "Question added."
            question = questionResponse(question)
        else:
            success = False
            status = "FAILURE"
            message = "Error."

        # make the response a json object
        response = json.dumps({
            'success': success,
            'status': status,
            'message': message,
            'question': question
        })
    elif request.method == 'GET':
        # url get request
        questionArgs = request.args.to_dict()

        # get logged in user id, if it's set then continue, else set it to -1
        try:
            id = int(questionArgs['loggedin_id'])
        except:
            id = -1
        app.logger.info("USER ID (-1 means logged out): " + str(id))

        if 'question_id' in questionArgs:
            q = Questions.getQuestion(questionArgs['question_id'], id)
            app.logger.info(q)
            question = questionResponse(q, id)
            app.logger.info(question)
            success = True
            status = 'OK'
            message = 'Question with anwsers.'
        elif 'user_id' in questionArgs and 'engineer' not in questionArgs:
            questions = Questions.getQuestionsByUser(questionArgs['user_id'],
                                                     id)
            success = True
            status = 'OK'
            message = 'List of several questions by user_id'
        elif 'user_id' in questionArgs and 'engineer' in questionArgs:
            questions = Questions.getQuestionsByBoth(questionArgs['engineer'],
                                                     questionArgs['user_id'],
                                                     id)
            success = True
            status = 'OK'
            message = 'List of several questions by user_id'
        elif 'engineer' in questionArgs:
            questions = Questions.getQuestionsByEngineer(
                questionArgs['engineer'], id)
            success = True
            status = 'OK'
            message = 'List of several questions by engineer'
        else:
            questions = Questions.getQuestions(id)
            success = True
            status = 'OK'
            message = 'List of several questions'

        if 'sort' in questionArgs and 'reverse' in questionArgs:
            questions = sorted(questions,
                               key=lambda k: k[questionArgs['sort']],
                               reverse=int(questionArgs['reverse']))
        elif 'sort' in questionArgs:
            questions = sorted(questions,
                               key=lambda k: k[questionArgs['sort']])

        if question:
            response = json.dumps({
                'success': success,
                'status': status,
                'message': message,
                'question': question
            })
        else:
            response = json.dumps({
                'success': success,
                'status': status,
                'message': message,
                'questions': questions
            })

    else:
        success = False
        status = "WARNING"
        message = "HTTP method invalid."
        response = json.dumps({
            'success': success,
            'status': status,
            'message': message
        })

    return response