def list_kittens(db: SQLAlchemy): session = db.session_class() kittens = session.query(Kitten).all() session.close() return { 'kittens': [kitten.serialize() for kitten in kittens] }
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}
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
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)
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)
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)
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)
def create_tables(): """ Create SQLAlchemy tables. """ from apistar.main import get_current_app from apistar.backends import SQLAlchemy app = get_current_app() db_backend = SQLAlchemy.build(settings=app.settings) db_backend.create_tables() click.echo("Tables created")
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}
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}
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)
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}
def clear_db(scope="function"): yield db_backend = SQLAlchemy.build(app.settings) db_backend.drop_tables()
def create_poll(db: SQLAlchemy, question: str): session = db.session_class() poll = Poll(question=question) session.add(poll) session.commit() return {'question': question}
def list_kittens(db: SQLAlchemy): session = db.session_class() kittens = session.query(Kitten).all() session.close() return {'kittens': [kitten.serialize() for kitten in kittens]}
def list_tasks(db: SQLAlchemy) -> List[Task]: session = db.session_class() tasks = session.query(TaskModel).all() return [Task(t) for t in tasks]