Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions backend/bittan/bittan/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
from .api.swish import debug_cancel, swish_callback, debug_make_request, debug_synchronize_request

from bittan.views.staffpage_views import create_tickets, filter_ticket_type_from_chapter_event, resend_email, staff_dashboard, update_payment, update_tickets, send_mass_email
from bittan.views import validate_ticket, get_chapter_events, start_payment, submit_form, reserve_ticket, get_session_payment_status
from bittan.views import get_chapter_events, get_session, get_session_payment_status, reserve_ticket, start_payment, submit_form, validate_ticket

_prefix = get_bittan_backend_url_path_prefix()

Expand All @@ -35,8 +35,9 @@
path(_prefix+'get_chapterevents/', get_chapter_events),
path(_prefix+'validate_ticket/', validate_ticket),
path(_prefix+"reserve_ticket/", reserve_ticket),
path(_prefix+'session_payment_status/<slug:session_id>', get_session_payment_status),
path(_prefix+"get_session/<slug:session_id>/", get_session),
path(_prefix+"submit_form/", submit_form),
path(_prefix+'session_payment_status/', get_session_payment_status),
path(_prefix+"start_payment/", start_payment),
path(_prefix+"generate_qr/<str:token>", get_qr),
path(_prefix+"accounts/login/", django_views.LoginView.as_view(), name="login"),
Expand Down
2 changes: 1 addition & 1 deletion backend/bittan/bittan/views/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
from bittan.views.start_payment import start_payment
from bittan.views.reserve_ticket import reserve_ticket
from bittan.views.get_session_payment_status import get_session_payment_status
from bittan.views.get_session import get_session
from bittan.views.submit_form import submit_form

65 changes: 65 additions & 0 deletions backend/bittan/bittan/views/get_session.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
from rest_framework.response import Response
from rest_framework.request import Request
from rest_framework.decorators import api_view
from rest_framework import serializers, status

from bittan.models import AnswerSelectedOptions, Payment

from django.utils import timezone
from django.db.models import Count, Prefetch

class TicketSerializer(serializers.Serializer):
ticket_type = serializers.PrimaryKeyRelatedField(read_only=True)
count = serializers.IntegerField()

class AnswerSelectedOptionsSerializer(serializers.ModelSerializer):
class Meta:
model = AnswerSelectedOptions
fields = ["option"]


@api_view(["GET"])
def get_session(request: Request, session_id: str) -> Response:
payment: Payment
try:
payment = Payment.objects.get(id=session_id)
except Payment.DoesNotExist:
return Response("Session not found", status=404)

tickets = payment.ticket_set
ticket_data = list(
tickets.values("ticket_type").annotate(count=Count("ticket_type"))
)

if tickets.count() > 1:
return Response({
"status": payment.status,
"tickets": ticket_data
})

ticket = tickets.first()
answers = ticket.answer_set.prefetch_related(
Prefetch(
'answerselectedoptions_set',
queryset=AnswerSelectedOptions.objects.order_by('pk'),
to_attr='selected_options_prefetched'
)
)
answer_data = []
for answer in answers:
selected_options = answer.answerselectedoptions_set.all()
options = [selected_option.question_option.pk for selected_option in selected_options]
texts = [selected_option.text for selected_option in selected_options]
answer_data.append(
{
"question_id": answer.question.pk,
"options_ids": options,
"option_texts": texts,
}
)
return Response({
"status": payment.status,
"tickets": ticket_data,
"answers": answer_data

})
67 changes: 67 additions & 0 deletions docs/backend_endpoints.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Backend endpoints

- [/get_chapterevents/](#get-chapterevents)
- [/get_session/](#get-session)
- [/reserve_ticket/](#reserve-ticket)
- [/session_payment_status/](#session-payment-status)
- [/start_payment/](#start-payment)
Expand Down Expand Up @@ -58,6 +59,72 @@ Example of response content:
]
}
```
## Get session

`GET /get_session/<payment_id>`

Retrieves the data of a session. Retrieves the associated tickets, payment status, and if there is a form attached to the event, the answers to the form.
The tickets list consits of objects with a corresponding ticket type as well as the number of tickets of this type that is associated with the session.
The answers list contains answer to the chapter event's associated form if there exists one. Each object in this list contains the question id,
a list with the selected options, as welll as a list with the texts asscioated with the option. The texts are linked to their corresponding option
by having mathing indicies in their respective list. The answers array is only attached if there exists a form for the event.

Example of response content:
```json
{
"status": "FORM_SUBMITTED",
"tickets": [
{
"ticket_type": 1,
"count": 1
}
],
"answers": [
{
"question": 1,
"options": [
1
],
"texts": [
"Wohohoh"
]
},
{
"question": 2,
"options": [
2,
3,
4
],
"texts": [
"",
"",
"Allt"
]
},
{
"question": 3,
"options": [
5
],
"texts": [
""
]
},
{
"question": 4,
"options": [
7,
8
],
"texts": [
"",
""
]
}
]
}
```
## Reserve ticket

`POST /reserve_ticket/`
Expand Down
Loading