'Create latest_activity_timestamp data when possible by inspecting the model.
For ExerciseLog inspect relevant AttemptLogs to infer most recent activity.
If not available, and for other Logs, fallback to completion_timestamp if available.
All attempted migrations are wrapped in try-except blocks so that this migration
does not render an installation unusable simply because this fails for any data.'

def forwards(self, orm):

    for e in orm.ExerciseLog.objects.filter(latest_activity_timestamp=None):
        try:
            alogs = orm.AttemptLog.objects.filter(user=e.user, exercise_id=e.exercise_id, context_type__in=['playlist', 'exercise'])
            if alogs:
                e.latest_activity_timestamp = sorted([a.timestamp for a in alogs])[(-1)]
                e.save()
            elif e.completion_timestamp:
                e.latest_activity_timestamp = e.completion_timestamp
                e.save()
        except:
            pass
    for v in orm.VideoLog.objects.filter(latest_activity_timestamp=None).exclude(completion_timestamp=None):
        try:
            v.latest_activity_timestamp = v.completion_timestamp
            v.save()
        except:
            pass
    for c in orm.ContentLog.objects.filter(latest_activity_timestamp=None).exclude(completion_timestamp=None):
        try:
            c.latest_activity_timestamp = c.completion_timestamp
            c.save()
        except:
            pass
