Home > Enterprise >  replacing token with a value
replacing token with a value

Time:09-28

i am trying to replace token by checking whether the token is valid and then taking out the details using that token .

eg: {"jwt":"asdahasjkaiubdkjsdjasdajkdjakdon","hostel":"BCJ bhawan","room_no":"300"......}

something like this i will receive

how can i replace that token portion with the value in serializer1

but i am unable to merge them together

here is my views.py

class leaveview(APIView):
    def post(self,request):
        token = request.data['jwt']
        if not token:
            raise AuthenticationFailed('Unauthenticated')
        
        try:
            payload = jwt.decode(token,'secret',algorithms=['HS256'])
        except jwt.ExpiredSignatureError:
            raise AuthenticationFailed('Unauthenticated')
        
        user=User.objects.filter(id=payload['id']).first()
        serializer1=UserSerializers(user)


        serializer2 = leaveSerializers(data=request.data)
        serializer2.is_valid(raise_exception=True)
        serializer=serializer1 serializer2
        serializer.save()
        return Response(serializer.data)


models.py

class leave(models.Model):
    name=models.CharField(max_length=100)
    father_name=models.CharField(max_length=100,null=True)
    
    
    branch=models.CharField(max_length=40,null=True)
    coer_id=models.CharField(max_length=12,unique=True,null=True)
    
    hostel = models.ForeignKey(hostel_manager,on_delete=models.CASCADE)
    room_no = models.CharField(max_length=10)
    where_to = models.CharField(max_length=100)
    reason = models.CharField(max_length=300)
    start_date = models.CharField(max_length = 100,null=True)
    end_date = models.CharField(max_length=100,null=True)
    phone_regex=RegexValidator(regex=r'^\ ?1?\d{9,15}$', message="Phone number must be entered in the format: ' 9999999999'. Up to 12 digits allowed.")
    phone_number = models.CharField(validators=[phone_regex], max_length=17)

serializer.py

class leaveSerializers(serializers.ModelSerializer):
    class Meta:
        model = leave
        fields = ['id','hostel','room_no','where_to','reason','time_period','phone_number','name','father_name','branch','coer_id']

CodePudding user response:

Two things first, you have two problems in your questions.

  • Firstly you want to replace a token with a value.
  • Secondly you want to merge serializer together.

In watching your code, we assume that you're using the jwt auth system from DRF.

Therefore you could simply use something as follow to retrieve the user and be sure that the user is authenticated :

from rest_framework.decorators import api_view, permission_classes
from rest_framework.permissions import IsAuthenticated
...

class leaveview(APIView):
    @permission_classes([IsAuthenticated])
    def post(self,request):
        ...
        user = request.user
        ...

Then you let DRF handling jwt auth without hussle.

For your merging issue, it's not a right idea to force uniting things from different nature in such way.

You would have to make serialize your data :

...
serializer1 = UserSerializers(user)
serializer1_data = UserSerializers(user).data
...
serializer2.is_valid(raise_exception=True)
merged_data = {**serializer1_data, **serializer2.data}
return Response(data=merged_data)

Above should be a working example, the ball is on your side to ease your code.

  • Related