|
4 | 4 | from django.contrib.auth import get_user_model
|
5 | 5 |
|
6 | 6 | from authentication.decorators import cognito_authenticated
|
| 7 | +from authentication.utils import get_user_id_from_token |
7 | 8 |
|
8 | 9 | from .models import Piggies
|
9 | 10 | from .serializers import PiggiesSerializer
|
10 | 11 |
|
11 | 12 |
|
12 | 13 | class PiggiesViewSet(viewsets.ViewSet):
|
13 |
| - def get_user_id_from_token(self, request): |
14 |
| - try: |
15 |
| - authorization_header = request.headers.get("Authorization") |
16 |
| - if not authorization_header: |
17 |
| - raise Exception("Authorization header not found") |
18 |
| - |
19 |
| - token = authorization_header.split()[1] |
20 |
| - decoded_token = jwt.decode(token, options={"verify_signature": False}) |
21 |
| - username = decoded_token.get("username") |
22 |
| - if not username: |
23 |
| - raise Exception("User ID not found in token") |
24 |
| - return username |
25 |
| - except jwt.DecodeError: |
26 |
| - raise Exception("Invalid token") |
27 |
| - except jwt.ExpiredSignatureError: |
28 |
| - raise Exception("Expired token") |
29 |
| - except Exception as e: |
30 |
| - raise Exception(f"Error decoding token: {e}") |
31 |
| - |
32 | 14 | @cognito_authenticated
|
33 | 15 | def list(self, request):
|
34 | 16 | try:
|
35 |
| - username = self.get_user_id_from_token(request) |
| 17 | + username = get_user_id_from_token(request) |
36 | 18 | User = get_user_model()
|
37 | 19 | users = [{"user_id": str(x.user_id), "first_name": x.first_name} for x in User.objects.all()]
|
38 | 20 |
|
@@ -60,46 +42,34 @@ def list(self, request):
|
60 | 42 | @cognito_authenticated
|
61 | 43 | def create(self, request):
|
62 | 44 | try:
|
63 |
| - username = self.get_user_id_from_token(request) |
| 45 | + username = get_user_id_from_token(request) |
64 | 46 | data = request.data.copy()
|
65 | 47 | data["username"] = username
|
66 | 48 |
|
67 | 49 | serializer = PiggiesSerializer(data=data)
|
| 50 | + |
68 | 51 | if serializer.is_valid():
|
69 | 52 | serializer.save()
|
70 | 53 | response = {
|
71 | 54 | "username": serializer.data["username"],
|
72 | 55 | "piggy": serializer.data["piggy"],
|
73 | 56 | }
|
74 |
| - return Response(data=response, status=status.HTTP_201_CREATED) |
| 57 | + |
| 58 | + serializer = PiggiesSerializer(data={"username": data["piggy"], "piggy": username}) |
| 59 | + |
| 60 | + if serializer.is_valid(): |
| 61 | + serializer.save() |
| 62 | + |
| 63 | + return Response(data=response, status=status.HTTP_201_CREATED) |
75 | 64 | return Response(serializer.errors, status=status.HTTP_400_BAD_REQUEST)
|
76 | 65 | except Exception as e:
|
77 | 66 | return Response({"error": str(e)}, status=status.HTTP_500_INTERNAL_SERVER_ERROR)
|
78 | 67 |
|
79 | 68 |
|
80 | 69 | class NotPiggiesViewSet(viewsets.ViewSet):
|
81 |
| - def get_user_id_from_token(self, request): |
82 |
| - try: |
83 |
| - authorization_header = request.headers.get("Authorization") |
84 |
| - if not authorization_header: |
85 |
| - raise Exception("Authorization header not found") |
86 |
| - |
87 |
| - token = authorization_header.split()[1] |
88 |
| - decoded_token = jwt.decode(token, options={"verify_signature": False}) |
89 |
| - username = decoded_token.get("username") |
90 |
| - if not username: |
91 |
| - raise Exception("User ID not found in token") |
92 |
| - return username |
93 |
| - except jwt.DecodeError: |
94 |
| - raise Exception("Invalid token") |
95 |
| - except jwt.ExpiredSignatureError: |
96 |
| - raise Exception("Expired token") |
97 |
| - except Exception as e: |
98 |
| - raise Exception(f"Error decoding token: {e}") |
99 |
| - |
100 | 70 | def users(self, request):
|
101 | 71 | try:
|
102 |
| - username = self.get_user_id_from_token(request) |
| 72 | + username = get_user_id_from_token(request) |
103 | 73 | User = get_user_model()
|
104 | 74 | users = [
|
105 | 75 | {"user_id": str(x.user_id), "first_name": x.first_name, "email": x.email} for x in User.objects.all()
|
|
0 commit comments