Beispiel #1
0
def list_kittens(db: SQLAlchemy):
    session = db.session_class()
    kittens = session.query(Kitten).all()
    session.close()
    return {
        'kittens': [kitten.serialize() for kitten in kittens]
    }
Beispiel #2
0
def create_choices(db: SQLAlchemy, poll_id: int, choice_text: str):
    session = db.session_class()
    poll = session.query(Poll).get(poll_id)
    choice = Choice(poll=poll.id, choice_text=choice_text, votes=0)
    session.add(choice)
    session.commit()
    return {'choice_text': choice_text}
Beispiel #3
0
def create_kitten(db: SQLAlchemy, name: http.QueryParam):
    session = db.session_class()
    add_kitten = Kitten(name=name)
    session.add(add_kitten)
    session.commit()
    created_kitten = add_kitten.serialize()
    session.close()
    return created_kitten
Beispiel #4
0
def create_kitten(db: SQLAlchemy, name: http.QueryParam):
    session = db.session_class()
    add_kitten = Kitten(name=name)
    session.add(add_kitten)
    session.commit()
    created_kitten = add_kitten.serialize()
    session.close()
    return created_kitten
Beispiel #5
0
def delete_teacher(db: SQLAlchemy, teacher_id: int):
    session = db.session_class()
    db_teacher = session.query(Teacher).get(teacher_id)
    session.delete(db_teacher)
    session.commit()

    data = {}
    return Response(data, status=204)
Beispiel #6
0
def teacher_details(db: SQLAlchemy, teacher_id: int):
    session = db.session_class()
    db_teacher = session.query(Teacher).get(teacher_id)

    if db_teacher is None:
        return Response({}, status=404)
    data = {"result": TeacherSerializer(db_teacher)}
    return Response(data, status=201)
Beispiel #7
0
def teachers_list(db: SQLAlchemy):
    session = db.session_class()
    teachers = session.query(Teacher).all()
    data = {
        "results": [TeacherSerializer(teacher) for teacher in teachers],
        "count": len(teachers)
    }
    return Response(data, status=200)
Beispiel #8
0
def add_teacher(db: SQLAlchemy, name: str):
    session = db.session_class()
    db_teacher = Teacher(name=name)
    session.add(db_teacher)
    session.commit()

    db_teacher = session.query(Teacher).order_by("-id").first()
    data = {"created": TeacherSerializer(db_teacher)}
    return Response(data, status=201)
Beispiel #9
0
def polls(db: SQLAlchemy):
    data = []
    current_time = datetime.datetime.utcnow()
    session = db.session_class()
    polls = session.query(Poll).filter(Poll.pub_date < current_time)[:5]
    for poll in polls:
        poll_data = {}
        poll_data['question'] = poll.question
        poll_data['pub_date'] = str(poll.pub_date)
        data.append(poll_data)
    return {'polls': data}
Beispiel #10
0
def vote(db: SQLAlchemy, poll_id: int, choice_id: int):
    session = db.session_class()
    poll = session.query(Poll).get(poll_id)
    for option in poll.choice:
        if option.id == choice_id:
            choice = session.query(Choice).get(choice_id)
            temp = int(choice.votes)
            temp += 1
            choice.votes = temp
            session.add(choice)
            result = 'Vote Added'
        else:
            result = 'Wrong Choice'
        session.commit()
    return {'result': result}
Beispiel #11
0
def add_task(db: SQLAlchemy, definition: TaskDefinition) -> Response:
    """
    Add a new task. It receives its definition as an argument
    and sets an autoincremental id in the Task constructor.

    Returns the created serialized object and 201 status code on success.
    """
    if not definition:
        Response(422,
                 {'error': 'You should provide a definition of the task.'})

    task = TaskModel(definition=definition)
    session = db.session_class()
    session.add(task)
    session.commit()

    return Response(task.serialize(), status=201)
Beispiel #12
0
def polls_details(db: SQLAlchemy):
    data = []
    cdata = []
    session = db.session_class()
    polls = session.query(Poll).all()
    for poll in polls:
        poll_data = {}
        poll_data['question'] = poll.question
        poll_data['pub_date'] = str(poll.pub_date)
        if poll.choice:
            for choice in poll.choice:
                choice_data = {}
                choice_data['id'] = choice.id
                choice_data['choice_text'] = choice.choice_text
                choice_data['votes'] = choice.votes
                cdata.append(choice_data)
            poll_data['choice'] = cdata
        data.append(poll_data)
    return {'polls': data}
Beispiel #13
0
def list_kittens(db: SQLAlchemy):
    session = db.session_class()
    kittens = session.query(Kitten).all()
    session.close()
    return {'kittens': [kitten.serialize() for kitten in kittens]}
Beispiel #14
0
def create_poll(db: SQLAlchemy, question: str):
    session = db.session_class()
    poll = Poll(question=question)
    session.add(poll)
    session.commit()
    return {'question': question}
Beispiel #15
0
def list_tasks(db: SQLAlchemy) -> List[Task]:
    session = db.session_class()
    tasks = session.query(TaskModel).all()
    return [Task(t) for t in tasks]