2023-02-15 17:42:56 +00:00
|
|
|
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 18:37:02 +00:00
|
|
|
from api.decorators import scope_required
|
2023-02-15 17:42:56 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PostVoteSchema(Schema):
|
|
|
|
choices: list[int]
|
|
|
|
|
|
|
|
|
2023-02-19 18:37:02 +00:00
|
|
|
@scope_required("read:statuses")
|
2023-02-15 17:42:56 +00:00
|
|
|
@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 18:37:02 +00:00
|
|
|
@scope_required("write:statuses")
|
2023-02-15 17:42:56 +00:00
|
|
|
@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)
|