def to_python(self, value): if not backportTimeZone(): return super(DateTimeUtcField, self).to_python(value) if value is None: return value if isinstance(value, datetime.datetime): if timezone.is_naive(value): value = timezone.make_aware(value, timezone.utc) return value if isinstance(value, datetime.date): value = datetime.datetime(value.year, value.month, value.day) if settings.USE_TZ: # For backwards compatibility, interpret naive datetimes in # local time. This won't work during DST change, but we can't # do much about it, so we let the exceptions percolate up the # call stack. warnings.warn( u"DateTimeField received a naive datetime (%s)" u" while time zone support is active." u" Converting from default time zone to UTC." % value, RuntimeWarning) default_timezone = timezone.get_default_timezone() value = timezone.make_aware(value, default_timezone) return value value = smart_str(value) try: parsed = parse_datetime(value) if parsed is not None: return parsed except ValueError: msg = self.error_messages['invalid_datetime'] % value raise exceptions.ValidationError(msg) try: parsed = parse_date(value) if parsed is not None: return datetime.datetime(parsed.year, parsed.month, parsed.day) except ValueError: msg = self.error_messages['invalid_date'] % value raise exceptions.ValidationError(msg) msg = self.error_messages['invalid'] % value raise exceptions.ValidationError(msg)
def to_python(self, value): if not backportTimeZone(): return super(DateTimeUtcField, self).to_python(value) if value is None: return value if isinstance(value, datetime.datetime): if timezone.is_naive(value): value = timezone.make_aware(value, timezone.utc) return value if isinstance(value, datetime.date): value = datetime.datetime(value.year, value.month, value.day) if settings.USE_TZ: # For backwards compatibility, interpret naive datetimes in # local time. This won't work during DST change, but we can't # do much about it, so we let the exceptions percolate up the # call stack. warnings.warn(u"DateTimeField received a naive datetime (%s)" u" while time zone support is active." u" Converting from default time zone to UTC." % value, RuntimeWarning) default_timezone = timezone.get_default_timezone() value = timezone.make_aware(value, default_timezone) return value value = smart_str(value) try: parsed = parse_datetime(value) if parsed is not None: return parsed except ValueError: msg = self.error_messages['invalid_datetime'] % value raise exceptions.ValidationError(msg) try: parsed = parse_date(value) if parsed is not None: return datetime.datetime(parsed.year, parsed.month, parsed.day) except ValueError: msg = self.error_messages['invalid_date'] % value raise exceptions.ValidationError(msg) msg = self.error_messages['invalid'] % value raise exceptions.ValidationError(msg)
def displayRecordedVideo(request, flightName=None, sourceShortName=None, time=None): """ TODO flightName is actually groupName """ """ Returns first segment of all sources that are part of a given episode. Used for both playing back videos from active episode and also for playing videos associated with each note. """ requestedTime = "" active = False episode = {} if time is not None: # TODO: this is a duplicate path for playing back video at a certain time, it is legacy from PLRP # and was not fully working there; merge these 2 ways of playing back from a time. # time is passed as string (yy-mm-dd hh:mm:ss) try: requestedTime = datetime.datetime.strptime(time, "%Y-%m-%d %H:%M:%S") except: requestedTime = dateparse.parse_datetime(time) requestedTime = util.pythonDatetimeToJSON( requestedTime) # util.convertUtcToLocal(requestedTime)) GET_ACTIVE_EPISODE_METHOD = getClassByName( settings.XGDS_VIDEO_GET_ACTIVE_EPISODE) activeepisode = GET_ACTIVE_EPISODE_METHOD() # this happens when user clicks on a flight name to view video if flightName: GET_EPISODE_FROM_NAME_METHOD = getClassByName( settings.XGDS_VIDEO_GET_EPISODE_FROM_NAME) episode = GET_EPISODE_FROM_NAME_METHOD(flightName) if (episode != None and episode == activeepisode): active = True # this happens when user looks for live recorded if not episode: episode = activeepisode active = True if not episode: message = 'Video not found' if flightName: message += ' for ' + flightName else: message += ': no live video' messages.add_message(request, messages.ERROR, message) return redirect(reverse('error')) if episode: if not flightName: flightName = episode.shortName # get the segments segments = episode.videosegment_set.all() if not segments: msg = 'Video segments not found ' if flightName: msg = msg + flightName return recordedVideoError(request, msg) sourceShortName = str(sourceShortName) if sourceShortName: try: source = SOURCE_MODEL.get().objects.get(shortName=sourceShortName) segments = segments.filter(source=source) except: pass sources = [] segmentsDict = {} # dictionary of segments (in JSON) within given episode sourceVehicle = {} index = 0 distinctSources = segments.values('source').distinct() for sourceDict in distinctSources: source = SOURCE_MODEL.get().objects.get(pk=sourceDict['source']) sources.append(source) sourceVehicle[source.shortName] = source.vehicleName sourceSegments = segments.filter(source=source) segmentsDict[source.shortName] = [ seg.getDict() for seg in sourceSegments ] form = buildNoteForm([episode], source, request, {'index': index}) source.form = form index = index + 1 if flightName: if flightName.find('_') == -1: fullFlightName = flightName + "_" + sources[0].shortName else: fullFlightName = flightName GET_TIMEZONE_FROM_NAME_METHOD = getClassByName( settings.XGDS_VIDEO_GET_TIMEZONE_FROM_NAME) flightTimezone = GET_TIMEZONE_FROM_NAME_METHOD(str(fullFlightName)) else: flightTimezone = GET_TIMEZONE_FROM_NAME_METHOD(None) if not segmentsDict: return recordedVideoError(request, "No video segments found " + flightName) segmentsJson = json.dumps(segmentsDict, sort_keys=True, indent=4, cls=DatetimeJsonEncoder) episodeJson = json.dumps(episode.getDict()) theTemplate = 'xgds_video/video_recorded_playbacks.html' if active: theTemplate = 'xgds_video/video_active_playbacks.html' ctx = { 'segmentsJson': segmentsJson, 'episode': episode, 'isLive': active, 'episodeJson': episodeJson, 'noteTimeStamp': requestedTime, # in string format yy-mm-dd hh:mm:ss (in utc. converted to local time in js) 'sources': sources, 'flightName': flightName, 'flightTZ': flightTimezone, 'sourceVehicle': json.dumps(sourceVehicle), 'SSE': settings.XGDS_SSE } if settings.XGDS_VIDEO_EXTRA_VIDEO_CONTEXT: extraVideoContextFn = getClassByName( settings.XGDS_VIDEO_EXTRA_VIDEO_CONTEXT) extraVideoContextFn(ctx) return render_to_response(theTemplate, ctx, context_instance=RequestContext(request))
def displayRecordedVideo(request, flightName=None, sourceShortName=None, time=None): """ TODO flightName is actually groupName """ """ Returns first segment of all sources that are part of a given episode. Used for both playing back videos from active episode and also for playing videos associated with each note. """ requestedTime = "" active = False episode = {} if time is not None: # TODO: this is a duplicate path for playing back video at a certain time, it is legacy from PLRP # and was not fully working there; merge these 2 ways of playing back from a time. # time is passed as string (yy-mm-dd hh:mm:ss) try: requestedTime = datetime.datetime.strptime(time, "%Y-%m-%d %H:%M:%S") except: requestedTime = dateparse.parse_datetime(time) requestedTime = util.pythonDatetimeToJSON(requestedTime) # util.convertUtcToLocal(requestedTime)) GET_ACTIVE_EPISODE_METHOD = getClassByName(settings.XGDS_VIDEO_GET_ACTIVE_EPISODE) activeepisode = GET_ACTIVE_EPISODE_METHOD() # this happens when user clicks on a flight name to view video if flightName: GET_EPISODE_FROM_NAME_METHOD = getClassByName(settings.XGDS_VIDEO_GET_EPISODE_FROM_NAME) episode = GET_EPISODE_FROM_NAME_METHOD(flightName) if (episode != None and episode == activeepisode): active = True # this happens when user looks for live recorded if not episode: episode = activeepisode active = True if not episode: message = 'Video not found' if flightName: message += ' for ' + flightName else: message += ': no live video' messages.add_message(request, messages.ERROR, message) return redirect(reverse('error')) if episode: if not flightName: flightName = episode.shortName # get the segments segments = episode.videosegment_set.all() if not segments: msg = 'Video segments not found ' if flightName: msg = msg + flightName return recordedVideoError(request, msg) sourceShortName = str(sourceShortName) if sourceShortName == 'None': sourceShortName = None if sourceShortName: try: source = SOURCE_MODEL.get().objects.get(shortName=sourceShortName) segments = segments.filter(source=source) except: pass sources = [] segmentsDict = {} # dictionary of segments (in JSON) within given episode sourceVehicle = {} index = 0 distinctSources = segments.values('source').distinct() for sourceDict in distinctSources: source = SOURCE_MODEL.get().objects.get(pk=sourceDict['source']) sources.append(source) sourceVehicle[source.shortName] = source.vehicleName sourceSegments = segments.filter(source=source) segmentsDict[source.shortName] = [seg.getDict() for seg in sourceSegments] form = buildNoteForm([episode], source, request, {'index':index}) source.form = form index = index + 1 if flightName: if flightName.find('_') == -1: fullFlightName = flightName + "_" + sources[0].shortName else: fullFlightName = flightName GET_TIMEZONE_FROM_NAME_METHOD = getClassByName(settings.XGDS_VIDEO_GET_TIMEZONE_FROM_NAME) flightTimezone = GET_TIMEZONE_FROM_NAME_METHOD(str(fullFlightName)) else: flightTimezone = GET_TIMEZONE_FROM_NAME_METHOD(None) if not segmentsDict: return recordedVideoError(request, "No video segments found " + flightName) segmentsJson = json.dumps(segmentsDict, sort_keys=True, indent=4, cls=DatetimeJsonEncoder) episodeJson = json.dumps(episode.getDict()) theTemplate = 'xgds_video/map_recorded_playbacks.html' if active: theTemplate = 'xgds_video/map_active_playbacks.html' noteModelName = str(NOTE_MODEL.get().cls_type()) noteForm = getClassByName(settings.XGDS_NOTES_BUILD_NOTES_FORM)({'vehicle__name':sourceShortName, 'flight__group_name':flightName}) ctx = { 'segmentsJson': segmentsJson, 'episode': episode, 'isLive': active, 'episodeJson': episodeJson, 'noteTimeStamp': requestedTime, # in string format yy-mm-dd hh:mm:ss (in utc. converted to local time in js) 'sources': sources, 'flightName': flightName, 'flightTZ': flightTimezone, 'sourceVehicle': json.dumps(sourceVehicle), 'SSE': settings.XGDS_SSE, 'modelName': noteModelName, 'searchModelDict': {noteModelName:settings.XGDS_MAP_SERVER_JS_MAP[noteModelName]}, 'searchForms': {noteModelName: [noteForm,settings.XGDS_MAP_SERVER_JS_MAP[noteModelName]] }, 'app': 'xgds_map_server/js/search/mapViewerSearchApp.js', 'templates': get_handlebars_templates(list(settings.XGDS_MAP_SERVER_HANDLEBARS_DIRS), 'XGDS_MAP_SERVER_HANDLEBARS_DIRS'), } if settings.XGDS_VIDEO_EXTRA_VIDEO_CONTEXT: extraVideoContextFn = getClassByName(settings.XGDS_VIDEO_EXTRA_VIDEO_CONTEXT) extraVideoContextFn(ctx) return render_to_response(theTemplate, ctx, context_instance=RequestContext(request))
def getVideoContext(request, flightName=None, sourceShortName=None, time=None): requestedTime = "" active = False episode = {} if time is not None: # TODO: this is a duplicate path for playing back video at a certain time, it is legacy from PLRP # and was not fully working there; merge these 2 ways of playing back from a time. # time is passed as string (yy-mm-dd hh:mm:ss) try: requestedTime = datetime.datetime.strptime(time, "%Y-%m-%d %H:%M:%S") except: try: requestedTime = datetime.datetime.strptime( time, "%Y-%m-%d %H:%M:%S+00:00") except: requestedTime = dateparse.parse_datetime(time) GET_ACTIVE_EPISODE_METHOD = getClassByName( settings.XGDS_VIDEO_GET_ACTIVE_EPISODE) activeepisode = GET_ACTIVE_EPISODE_METHOD() # this happens when user clicks on a flight name to view video if flightName: try: GET_EPISODE_FROM_NAME_METHOD = getClassByName( settings.XGDS_VIDEO_GET_EPISODE_FROM_NAME) episode = GET_EPISODE_FROM_NAME_METHOD(flightName) if not episode: raise Exception('no episode') if episode and episode == activeepisode: active = True except: return {} # this happens when user looks for live recorded if not episode: episode = activeepisode active = True if not episode: message = 'Video not found' if flightName: message += ' for ' + flightName else: message += ': no live video' messages.add_message(request, messages.ERROR, message) return redirect(reverse('error')) if episode: if not flightName: flightName = episode.shortName # get the segments segments = episode.videosegment_set.all() if not segments: msg = 'Video segments not found ' if flightName: msg = msg + flightName return recordedVideoError(request, msg) sourceShortName = str(sourceShortName) if sourceShortName == 'None': sourceShortName = None if sourceShortName: try: source = SOURCE_MODEL.get().objects.get(shortName=sourceShortName) segments = segments.filter(source=source) except: pass sources = [] segmentsDict = {} # dictionary of segments (in JSON) within given episode sourceVehicle = {} index = 0 distinctSources = segments.values('source').distinct() for sourceDict in distinctSources: source = SOURCE_MODEL.get().objects.get(pk=sourceDict['source']) sources.append(source) sourceVehicle[source.shortName] = source.vehicleName sourceSegments = segments.filter(source=source) segmentsDict[source.shortName] = [ seg.getDict() for seg in sourceSegments ] form = buildNoteForm([episode], source, request, {'index': index}) source.form = form index = index + 1 if flightName: if flightName.find('_') == -1: fullFlightName = flightName + "_" + sources[0].shortName else: fullFlightName = flightName GET_TIMEZONE_FROM_NAME_METHOD = getClassByName( settings.XGDS_VIDEO_GET_TIMEZONE_FROM_NAME) flightTimezone = GET_TIMEZONE_FROM_NAME_METHOD(str(fullFlightName)) else: flightTimezone = GET_TIMEZONE_FROM_NAME_METHOD(None) if not segmentsDict: return recordedVideoError(request, "No video segments found " + flightName) segmentsJson = json.dumps(segmentsDict, sort_keys=True, indent=4, cls=DatetimeJsonEncoder) episodeJson = json.dumps(episode.getDict(), cls=DatetimeJsonEncoder) noteModelName = str(NOTE_MODEL.get().cls_type()) noteForm = getClassByName(settings.XGDS_NOTES_BUILD_NOTES_FORM)( { 'vehicle__name': sourceShortName, 'flight__group_name': episode.shortName } ) #TODO this assumes the episode short name and group flight name are the same ctx = { 'segmentsJson': segmentsJson, 'isLive': active, 'episode': episode, 'episodeJson': episodeJson, 'noteTimeStamp': requestedTime, # in string format yy-mm-dd hh:mm:ss (in utc. converted to local time in js) 'sources': sources, 'flightName': flightName, 'flightTZ': flightTimezone, 'sourceVehicle': json.dumps(sourceVehicle), 'SSE': settings.XGDS_SSE, 'modelName': noteModelName, 'searchModelDict': { noteModelName: settings.XGDS_MAP_SERVER_JS_MAP[noteModelName] }, 'searchForms': { noteModelName: [noteForm, settings.XGDS_MAP_SERVER_JS_MAP[noteModelName]] }, 'app': 'xgds_video/js/mapVideoApp.js', 'templates': get_handlebars_templates( list(settings.XGDS_MAP_SERVER_HANDLEBARS_DIRS), 'XGDS_MAP_SERVER_HANDLEBARS_DIRS'), } if settings.XGDS_VIDEO_EXTRA_VIDEO_CONTEXT: extraVideoContextFn = getClassByName( settings.XGDS_VIDEO_EXTRA_VIDEO_CONTEXT) extraVideoContextFn(ctx) return ctx