def to_internal_value(self, data): # if string or dict is given, try to use its id and convert the id to correct home municipality object if isinstance(data, str) or isinstance(data, dict): home_municipality = None if isinstance(data, str): home_municipality = get_object_or_none( ReservationHomeMunicipalityField, id=data) # if dict and key id exists if isinstance(data, dict): if "id" in data: home_municipality = get_object_or_none( ReservationHomeMunicipalityField, id=data['id']) else: raise ValidationError( _('Invalid home municipality object - id is missing.')) if not home_municipality: raise ValidationError({ 'home_municipality': { 'id': [ _('Invalid pk "{pk_value}" - object does not exist.' ).format(pk_value=data) ] } }) data = home_municipality return data else: return super().to_internal_value(data)
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) data = self.get_initial() resource = None # try to find out the related resource using initial data if that is given resource_id = data.get('resource') if data else None if resource_id: resource = get_object_or_none(Resource, id=resource_id) # if that didn't work out use the reservation's old resource if such exists if not resource: if isinstance(self.instance, Reservation) and isinstance( self.instance.resource, Resource): resource = self.instance.resource # set extra fields required if the related resource is found and it needs manual confirmation if resource and resource.need_manual_confirmation: for field_name in RESERVATION_EXTRA_FIELDS: self.fields[field_name].read_only = False can_approve = resource.can_approve_reservations( self.context['request'].user) staff_event = data.get('staff_event', False) and can_approve for field_name in REQUIRED_RESERVATION_EXTRA_FIELDS: required = True if not staff_event else field_name in ( 'reserver_name', 'event_description') self.fields[field_name].required = required
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) data = self.get_initial() resource = None # try to find out the related resource using initial data if that is given resource_id = data.get('resource') if data else None if resource_id: resource = get_object_or_none(Resource, id=resource_id) # if that didn't work out use the reservation's old resource if such exists if not resource: if isinstance(self.instance, Reservation) and isinstance(self.instance.resource, Resource): resource = self.instance.resource # set supported and required extra fields if resource: cache = self.context.get('reservation_metadata_set_cache') supported = resource.get_supported_reservation_extra_field_names(cache=cache) required = resource.get_required_reservation_extra_field_names(cache=cache) if resource.need_manual_confirmation: # manually confirmed reservations have some extra conditions can_approve = resource.can_approve_reservations(self.context['request'].user) if data.get('staff_event', False) and can_approve: required = {'reserver_name', 'event_description'} # we don't need to remove a field here if it isn't supported, as it will be read-only and will be more # easily removed in to_representation() for field_name in supported: self.fields[field_name].read_only = False for field_name in required: self.fields[field_name].required = True
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) data = self.get_initial() resource = None # try to find out the related resource using initial data if that is given resource_id = data.get('resource') if data else None if resource_id: resource = get_object_or_none(Resource, id=resource_id) # if that didn't work out use the reservation's old resource if such exists if not resource: if isinstance(self.instance, Reservation) and isinstance(self.instance.resource, Resource): resource = self.instance.resource # set supported and required extra fields if resource: supported = resource.get_supported_reservation_extra_field_names() required = resource.get_required_reservation_extra_field_names() if resource.need_manual_confirmation: # manually confirmed reservations have some extra conditions can_approve = resource.can_approve_reservations(self.context['request'].user) if data.get('staff_event', False) and can_approve: required = {'reserver_name', 'event_description'} # we don't need to remove a field here if it isn't supported, as it will be read-only and will be more # easily removed in to_representation() for field_name in supported: self.fields[field_name].read_only = False for field_name in required: self.fields[field_name].required = True
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) data = self.get_initial() resource = None # try to find out the related resource using initial data if that is given resource_id = data.get('resource') if data else None if resource_id: resource = get_object_or_none(Resource, id=resource_id) # if that didn't work out use the reservation's old resource if such exists if not resource: if isinstance(self.instance, Reservation) and isinstance(self.instance.resource, Resource): resource = self.instance.resource # set supported and required extra fields if resource: cache = self.context.get('reservation_metadata_set_cache') supported = resource.get_supported_reservation_extra_field_names(cache=cache) required = resource.get_required_reservation_extra_field_names(cache=cache) # staff events have less requirements is_staff_event = data.get('staff_event', False) is_resource_manager = resource.is_manager(self.context['request'].user) if is_staff_event and is_resource_manager: required = {'reserver_name', 'event_description'} # we don't need to remove a field here if it isn't supported, as it will be read-only and will be more # easily removed in to_representation() for field_name in supported: self.fields[field_name].read_only = False for field_name in required: self.fields[field_name].required = True
def __init__(self, *args, **kwargs): super().__init__(*args, **kwargs) data = self.get_initial() resource = None # try to find out the related resource using initial data if that is given resource_id = data.get('resource') if data else None if resource_id: resource = get_object_or_none(Resource, id=resource_id) # if that didn't work out use the reservation's old resource if such exists if not resource: if isinstance(self.instance, Reservation) and isinstance(self.instance.resource, Resource): resource = self.instance.resource # set extra fields required if the related resource is found and it needs manual confirmation if resource and resource.need_manual_confirmation: for field_name in RESERVATION_EXTRA_FIELDS: self.fields[field_name].read_only = False can_approve = resource.can_approve_reservations(self.context['request'].user) staff_event = data.get('staff_event', False) and can_approve for field_name in REQUIRED_RESERVATION_EXTRA_FIELDS: required = True if not staff_event else field_name in ('reserver_name', 'event_description') self.fields[field_name].required = required