Skip to content

Commit

Permalink
remove B2B IPC
Browse files Browse the repository at this point in the history
  • Loading branch information
rm03 committed Apr 24, 2024
1 parent 7cdb450 commit 4bde09e
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 62 deletions.
6 changes: 5 additions & 1 deletion backend/clubs/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -1876,7 +1876,11 @@ def get_qr(self):

signer = Signer()
token = signer.sign_object({"owner": self.owner.id, "ticket_id": str(self.id)})
qr_image = qrcode.make(token, box_size=20, border=0)
qr_image = qrcode.make(

Check warning on line 1879 in backend/clubs/models.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/models.py#L1877-L1879

Added lines #L1877 - L1879 were not covered by tests
f"https://{settings.DOMAINS[0]}/api/tickets/validate/{token}/",
box_size=20,
border=0,
)
return qr_image

def send_confirmation_email(self):
Expand Down
83 changes: 23 additions & 60 deletions backend/clubs/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@
from ics import Calendar as ICSCal
from ics import Event as ICSEvent
from ics.grammar import parse as ICSParse
from identity.permissions import B2BPermission
from jinja2 import Template
from options.models import Option
from rest_framework import filters, generics, parsers, serializers, status, viewsets
Expand Down Expand Up @@ -5163,41 +5162,22 @@ def qr(self, request, *args, **kwargs):
qr_image.save(response, "PNG")
return response

@action(detail=False, methods=["post"])
@action(detail=False, methods=["get"], url_path="validate/(?P<token>[^/.]+)")
def validate(self, request, *args, **kwargs):
"""
Validate a ticket's QR code and mark attendance. Only accessible via B2B IPC.
Validate a ticket's QR code and mark attendance.
---
requestBody:
content:
application/json:
schema:
type: object
properties:
username:
type: string
token:
type: string
required:
- username
- token
responses:
"200":
content:
application/json:
schema:
type: object
properties:
detail:
type: string
"204":
content:
application/json:
schema:
type: object
properties:
detail:
type: string
type: object
properties:
ticket:
$ref: '#/components/schemas/Ticket'
previously_scanned:
type: boolean
"400":
content:
application/json:
Expand All @@ -5216,19 +5196,7 @@ def validate(self, request, *args, **kwargs):
type: string
---
"""
user = (
get_user_model()
.objects.filter(username=request.data.get("username"))
.first()
)
token = request.data.get("token")

if not user or not token:
return Response(
{"detail": "Must provide username and token to validate QR code"},
status=status.HTTP_400_BAD_REQUEST,
)

token = kwargs.get("token")
signer = Signer()
try:
obj = signer.unsign_object(token)
Expand All @@ -5248,14 +5216,15 @@ def validate(self, request, *args, **kwargs):
{"detail": "Ticket not found"}, status=status.HTTP_400_BAD_REQUEST
)

is_owner = request.user == ticket.owner
is_officer = Membership.objects.filter(

Check warning on line 5220 in backend/clubs/views.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/views.py#L5219-L5220

Added lines #L5219 - L5220 were not covered by tests
person=user,
person=request.user,
club=ticket.event.club,
role__lte=Membership.ROLE_OFFICER,
active=True,
).exists()

if not is_officer:
if not (is_owner or is_officer):
return Response(

Check warning on line 5228 in backend/clubs/views.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/views.py#L5227-L5228

Added lines #L5227 - L5228 were not covered by tests
{"detail": "You do not have permission to scan this ticket!"},
status=status.HTTP_403_FORBIDDEN,
Expand All @@ -5266,28 +5235,22 @@ def validate(self, request, *args, **kwargs):
{"detail": "Stale token"}, status=status.HTTP_400_BAD_REQUEST
)

if ticket.attended:
return Response(
{"detail": "Ticket has already been scanned"},
status=status.HTTP_204_NO_CONTENT,
)

ticket.attended = True
ticket.transferable = False
ticket.save()
previously_scanned = ticket.attended
if not previously_scanned and is_officer:
ticket.attended = True
ticket.transferable = False
ticket.save()

Check warning on line 5242 in backend/clubs/views.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/views.py#L5238-L5242

Added lines #L5238 - L5242 were not covered by tests

return Response({"detail": "Successfully validated QR code"})
return Response(

Check warning on line 5244 in backend/clubs/views.py

View check run for this annotation

Codecov / codecov/patch

backend/clubs/views.py#L5244

Added line #L5244 was not covered by tests
{
"ticket": TicketSerializer(ticket).data,
"previously_scanned": previously_scanned,
}
)

def get_queryset(self):
return Ticket.objects.filter(owner=self.request.user.id)

def get_permissions(self):
if self.action == "validate":
self.permission_classes = [
B2BPermission("urn:pennlabs:*")
] # TODO: change this to mobile slug
return super().get_permissions()


class MemberInviteViewSet(viewsets.ModelViewSet):
"""
Expand Down
1 change: 0 additions & 1 deletion backend/pennclubs/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,6 @@
"rest_framework",
"simple_history",
"accounts.apps.AccountsConfig",
"identity.apps.IdentityConfig",
"clubs.apps.ClubsConfig",
"options.apps.OptionsConfig",
"social_django",
Expand Down

0 comments on commit 4bde09e

Please sign in to comment.