from models import Reservation
from setup import ma

from marshmallow import fields

class ReservationSchema(ma.ModelSchema):
    user = fields.Method('get_user')

    def get_user(self, reservation):
        user = self.context
        if user.admin or reservation.user == user:
            return reservation.user.id
        else:
            return None

    class Meta:
        model = Reservation

reservation_schema = ReservationSchema()
reservations_schema = ReservationSchema(many=True)

from models import Reservation
from setup import ma

from marshmallow import fields

class ReservationSchema(ma.ModelSchema):
    user = fields.Method('get_user')

    def get_user(self, reservation, **kwargs):
        user = self.context['user']
        if user.admin or reservation.user == user:
            return reservation.user.id
        else:
            return None

    class Meta:
        model = Reservation

reservation_schema = ReservationSchema()
reservations_schema = ReservationSchema(many=True)

Fix user in reservations responses
Fix user in reservations responses

  from models import Reservation
  from setup import ma
  
  from marshmallow import fields
  
  class ReservationSchema(ma.ModelSchema):
      user = fields.Method('get_user')
  
-     def get_user(self, reservation):
+     def get_user(self, reservation, **kwargs):
-         user = self.context
+         user = self.context['user']
          if user.admin or reservation.user == user:
              return reservation.user.id
          else:
              return None
  
      class Meta:
          model = Reservation
  
  reservation_schema = ReservationSchema()
  reservations_schema = ReservationSchema(many=True)
  
Fix user in reservations responses
## Code Before:
from models import Reservation
from setup import ma

from marshmallow import fields

class ReservationSchema(ma.ModelSchema):
    user = fields.Method('get_user')

    def get_user(self, reservation):
        user = self.context
        if user.admin or reservation.user == user:
            return reservation.user.id
        else:
            return None

    class Meta:
        model = Reservation

reservation_schema = ReservationSchema()
reservations_schema = ReservationSchema(many=True)

## Instruction:
Fix user in reservations responses
## Code After:
from models import Reservation
from setup import ma

from marshmallow import fields

class ReservationSchema(ma.ModelSchema):
    user = fields.Method('get_user')

    def get_user(self, reservation, **kwargs):
        user = self.context['user']
        if user.admin or reservation.user == user:
            return reservation.user.id
        else:
            return None

    class Meta:
        model = Reservation

reservation_schema = ReservationSchema()
reservations_schema = ReservationSchema(many=True)
