Ejemplo n.º 1
0
def get_event_object_context(event_content_object, event_type):
    # Deleted objects don't have a content object any more
    if not event_content_object:
        return EventContextSpec(name=None, url=None, object_id=None)

    event_subject = get_event_subject(event_type)

    object_id = None
    object_url = None
    object_name = None
    if hasattr(event_content_object, 'id'):
        object_id = event_content_object.id

    if hasattr(event_content_object, 'unique_name'):
        object_name = event_content_object.unique_name
        if event_subject == event_subjects.PROJECT:
            object_url = unique_urls.get_project_url(object_name)
        elif event_subject == event_subjects.EXPERIMENT:
            object_url = unique_urls.get_experiment_url(object_name)
        elif event_subject == event_subjects.EXPERIMENT_GROUP:
            object_url = unique_urls.get_experiment_group_url(object_name)
        elif event_subject == event_subjects.BUILD_JOB:
            object_url = unique_urls.get_build_url(object_name)
        elif event_subject == event_subjects.JOB:
            object_url = unique_urls.get_job_url(object_name)

    elif hasattr(event_content_object, 'name'):
        object_name = event_content_object.name
    elif hasattr(event_content_object, 'username'):
        object_name = event_content_object.username
        object_url = unique_urls.get_user_url(event_content_object.username)
    return EventContextSpec(name=object_name,
                            url=object_url,
                            object_id=object_id)
Ejemplo n.º 2
0
 def test_get_experiment_group_url(self):
     unique_name = self.group.unique_name
     self.assertEqual(
         unique_name,
         '{}.{}.{}'.format(self.group.project.user.username,
                           self.group.project.name, self.group.id))
     self.assertEqual(
         get_experiment_group_url(unique_name=unique_name),
         '/{}/{}/groups/{}'.format(self.group.project.user.username,
                                   self.group.project.name, self.group.id))
Ejemplo n.º 3
0
    def test_get_event_object_context(self):
        mock_object = MagicMock()

        event_spec = EventItemContextSpec(None, None, None)
        assert event_context.get_event_object_context(
            None, 'event_type') == event_spec

        # Experiment
        mock_object.id = 1
        mock_object.unique_name = 'user.project.1'
        event_spec = EventItemContextSpec(
            mock_object.unique_name,
            'app{}'.format(get_experiment_url(mock_object.unique_name)), 1)
        assert event_context.get_event_object_context(
            mock_object, 'experiment') == event_spec

        # Experiment inside group
        mock_object.unique_name = 'user.project.2.1'
        event_spec = EventItemContextSpec(
            mock_object.unique_name,
            'app{}'.format(get_experiment_url(mock_object.unique_name)), 1)
        assert event_context.get_event_object_context(
            mock_object, 'experiment') == event_spec

        # Experiment group
        mock_object.unique_name = 'user.project.1'
        event_spec = EventItemContextSpec(
            mock_object.unique_name,
            'app{}'.format(get_experiment_group_url(mock_object.unique_name)),
            1)
        assert event_context.get_event_object_context(
            mock_object, 'experiment_group') == event_spec

        # Job
        mock_object.unique_name = 'user.project.1'
        event_spec = EventItemContextSpec(
            mock_object.unique_name,
            'app{}'.format(get_job_url(mock_object.unique_name)), 1)
        assert event_context.get_event_object_context(mock_object,
                                                      'job') == event_spec

        # Project
        mock_object.unique_name = 'user.project'
        event_spec = EventItemContextSpec(
            mock_object.unique_name,
            'app{}'.format(get_project_url(mock_object.unique_name)), 1)
        assert event_context.get_event_object_context(mock_object,
                                                      'project') == event_spec
Ejemplo n.º 4
0
def get_event_object_context(
        event_content_object: Any,
        event_type: str) -> Optional['EventItemContextSpec']:
    # Deleted objects don't have a content object any more
    if not event_content_object:
        return EventItemContextSpec(name=None, url=None, object_id=None)

    event_subject = get_event_subject(event_type)

    object_id = None
    object_url = None
    object_name = None
    if hasattr(event_content_object, 'id'):
        object_id = event_content_object.id

    if hasattr(event_content_object, 'unique_name'):
        object_name = event_content_object.unique_name
        if event_subject == event_subjects.PROJECT:
            object_url = unique_urls.get_project_url(object_name)
        elif event_subject == event_subjects.EXPERIMENT:
            object_url = unique_urls.get_experiment_url(object_name)
        elif event_subject == event_subjects.EXPERIMENT_GROUP:
            object_url = unique_urls.get_experiment_group_url(object_name)
        elif event_subject == event_subjects.BUILD_JOB:
            object_url = unique_urls.get_build_url(object_name)
        elif event_subject == event_subjects.JOB:
            object_url = unique_urls.get_job_url(object_name)

    elif hasattr(event_content_object, 'name'):
        object_name = event_content_object.name
    elif hasattr(event_content_object, 'username'):
        object_name = event_content_object.username
        object_url = unique_urls.get_user_url(event_content_object.username)

    # Set proper url
    object_url = absolute_uri('app{}'.format(object_url))
    return EventItemContextSpec(name=object_name,
                                url=object_url,
                                object_id=object_id)