takahe/api/views/polls.py

27 lines
852 B
Python
Raw Normal View History

from django.shortcuts import get_object_or_404
from hatchway import Schema, api_view
from activities.models import Post, PostInteraction
from api import schemas
2023-02-19 10:37:02 -08:00
from api.decorators import scope_required
class PostVoteSchema(Schema):
choices: list[int]
2023-02-19 10:37:02 -08:00
@scope_required("read:statuses")
@api_view.get
def get_poll(request, id: str) -> schemas.Poll:
post = get_object_or_404(Post, pk=id, type=Post.Types.question)
return schemas.Poll.from_post(post, identity=request.identity)
2023-02-19 10:37:02 -08:00
@scope_required("write:statuses")
@api_view.post
def vote_poll(request, id: str, details: PostVoteSchema) -> schemas.Poll:
post = get_object_or_404(Post, pk=id, type=Post.Types.question)
PostInteraction.create_votes(post, request.identity, details.choices)
post.refresh_from_db()
return schemas.Poll.from_post(post, identity=request.identity)