def total_duration(self): cached_duration = cache.get('userrequest_duration_{}'.format(self.id)) if not cached_duration: duration = get_total_duration_dict(self.as_dict) cache.set('userrequest_duration_{}'.format(self.id), duration, 86400 * 30 * 6) return duration else: return cached_duration
def validate(self, data): # check that the user belongs to the supplied proposal if data['proposal'] not in data['submitter'].proposal_set.all(): raise serializers.ValidationError( _('You do not belong to the proposal you are trying to submit') ) # validation on the operator matching the number of requests if data['operator'] == 'SINGLE': if len(data['requests']) > 1: raise serializers.ValidationError( _("'Single' type user requests must have exactly one child request." )) elif len(data['requests']) == 1: raise serializers.ValidationError( _("'{}' type user requests must have more than one child request." .format(data['operator'].title()))) try: total_duration_dict = get_total_duration_dict(data) for tak, duration in total_duration_dict.items(): time_allocation = TimeAllocation.objects.get( semester=tak.semester, telescope_class=tak.telescope_class, proposal=data['proposal'], ) time_available = 0 if data['observation_type'] == UserRequest.NORMAL: time_available = time_allocation.std_allocation - time_allocation.std_time_used elif data['observation_type'] == UserRequest.TOO: time_available = time_allocation.too_allocation - time_allocation.too_time_used if time_available <= 0.0: raise serializers.ValidationError( _("Proposal {} does not have any time left allocated in semester {} on {} telescopes" ).format(data['proposal'], tak.semester, tak.telescope_class)) elif time_available * OVERHEAD_ALLOWANCE < (duration / 3600.0): raise serializers.ValidationError( _("Proposal {} does not have enough time allocated in semester {} on {} telescopes" ).format(data['proposal'], tak.semester, tak.telescope_class)) # validate the ipp debitting that will take place later validate_ipp(data, total_duration_dict) except ObjectDoesNotExist: raise serializers.ValidationError( _("You do not have sufficient time allocated on the resource you're requesting for this proposal." )) except TimeAllocationError as e: raise serializers.ValidationError(repr(e)) return data
def validate(self, data): # check that the user belongs to the supplied proposal user = self.context['request'].user if data['proposal'] not in user.proposal_set.all(): raise serializers.ValidationError( _('You do not belong to the proposal you are trying to submit') ) # validation on the operator matching the number of requests if data['operator'] == 'SINGLE': if len(data['requests']) > 1: raise serializers.ValidationError( _("'Single' type user requests must have exactly one child request." )) elif len(data['requests']) == 1: raise serializers.ValidationError( _("'{}' type user requests must have more than one child request." .format(data['operator'].title()))) # Check that the user has not exceeded the time limit on this membership membership = Membership.objects.get(user=user, proposal=data['proposal']) if membership.time_limit >= 0: duration = sum(d for _, d in get_request_duration_sum(data).items()) time_to_be_used = user.profile.time_used_in_proposal( data['proposal']) + duration if membership.time_limit < time_to_be_used: raise serializers.ValidationError( _('This request\'s duration will exceed the time limit set for your account on this proposal.' )) try: total_duration_dict = get_total_duration_dict(data) for tak, duration in total_duration_dict.items(): time_allocation = TimeAllocation.objects.get( semester=tak.semester, telescope_class=tak.telescope_class, instrument_name=tak.instrument_name, proposal=data['proposal'], ) time_available = 0 if data['observation_type'] == UserRequest.NORMAL: time_available = time_allocation.std_allocation - time_allocation.std_time_used elif data['observation_type'] == UserRequest.TOO: time_available = time_allocation.too_allocation - time_allocation.too_time_used # For Rapid Response observations, check if the end time of the window is within # 24 hours + the duration of the observation for request in data['requests']: windows = request.get('windows') for window in windows: if window.get('start') - timezone.now( ) > timedelta(seconds=0): raise serializers.ValidationError( _("The Rapid Response observation window start time cannot be in the future." )) if window.get('end') - timezone.now() > timedelta( seconds=(duration + 86400)): raise serializers.ValidationError( _("A Rapid Response observation must start within the next 24 hours, so the " "window end time must be within the next (24 hours + the observation duration)" )) if time_available <= 0.0: raise serializers.ValidationError( _("Proposal {} does not have any time left allocated in semester {} on {} telescopes" ).format(data['proposal'], tak.semester, tak.telescope_class)) elif time_available * OVERHEAD_ALLOWANCE < (duration / 3600.0): raise serializers.ValidationError( _("Proposal {} does not have enough time allocated in semester {} on {} telescopes" ).format(data['proposal'], tak.semester, tak.telescope_class)) # validate the ipp debitting that will take place later validate_ipp(data, total_duration_dict) except ObjectDoesNotExist: raise serializers.ValidationError( _("You do not have sufficient time allocated on the instrument you're requesting for this proposal." )) except TimeAllocationError as e: raise serializers.ValidationError(repr(e)) return data
def total_duration(self): return get_total_duration_dict(self.as_dict)