Skip to content

Commit

Permalink
Refactor: Refactor experiment collection to experiment model and upda…
Browse files Browse the repository at this point in the history
…te related fields (#1186)

* refactor: Rename majority of instances of the word experiment collection or collection to experiment, with the exception of slugs/urls

* Refactor: Rename ExperimentCollection model to Experiment and update related fields (add migrations)

* Refactor: Some remaining instances of the word experiment that I missed.

Update experiment-dashboard.html to use "Blocks" instead of "Experiments" in headings and captions
  • Loading branch information
drikusroor committed Jul 15, 2024
1 parent 93c3529 commit 6a2459a
Show file tree
Hide file tree
Showing 55 changed files with 403 additions and 372 deletions.
12 changes: 6 additions & 6 deletions backend/experiment/actions/tests/test_action_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from django.test import TestCase

from experiment.actions.utils import COLLECTION_KEY, get_current_collection_url, randomize_playhead
from experiment.actions.utils import EXPERIMENT_KEY, get_current_experiment_url, randomize_playhead
from experiment.models import Block
from participant.models import Participant
from section.models import Playlist
Expand All @@ -16,12 +16,12 @@ def setUp(self) -> None:
self.session = Session.objects.create(
block=self.block, participant=self.participant, playlist=self.playlist)

def test_collection_url(self):
self.assertEqual(get_current_collection_url(self.session), None)
self.session.save_json_data({COLLECTION_KEY: 'superdupercollection'})
self.assertEqual(get_current_collection_url(self.session), '/superdupercollection')
def test_experiment_url(self):
self.assertEqual(get_current_experiment_url(self.session), None)
self.session.save_json_data({EXPERIMENT_KEY: 'superduperexperiment'})
self.assertEqual(get_current_experiment_url(self.session), '/superduperexperiment')
self.participant.participant_id_url = 'participant42'
self.assertEqual(get_current_collection_url(self.session), '/superdupercollection?participant_id=participant42')
self.assertEqual(get_current_experiment_url(self.session), '/superduperexperiment?participant_id=participant42')

def test_randomize_playhead(self):
min_jitter = 5
Expand Down
16 changes: 8 additions & 8 deletions backend/experiment/actions/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,26 @@
from experiment.actions import Final
from session.models import Session

COLLECTION_KEY = 'experiment_collection'
EXPERIMENT_KEY = 'experiment'


def get_current_collection_url(session: Session) -> str:
collection_slug = session.load_json_data().get(COLLECTION_KEY)
if not collection_slug:
def get_current_experiment_url(session: Session) -> str:
experiment_slug = session.load_json_data().get(EXPERIMENT_KEY)
if not experiment_slug:
return None

if session.participant.participant_id_url:
participant_id_url = session.participant.participant_id_url
return f'/{collection_slug}?participant_id={participant_id_url}'
return f'/{experiment_slug}?participant_id={participant_id_url}'
else:
return f'/{collection_slug}'
return f'/{experiment_slug}'


def final_action_with_optional_button(session, final_text='', title=_('End'), button_text=_('Continue')):
""" given a session, a score message and an optional session dictionary from an experiment collection,
""" given a session, a score message and an optional session dictionary from an experiment,
return a Final.action, which has a button to continue to the next block if series is defined
"""
redirect_url = get_current_collection_url(session)
redirect_url = get_current_experiment_url(session)

if redirect_url:
return Final(
Expand Down
18 changes: 9 additions & 9 deletions backend/experiment/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
from django.utils.html import format_html
from experiment.models import (
Block,
ExperimentCollection,
Experiment,
Phase,
Feedback,
GroupedBlock,
SocialMediaConfig,
)
from question.admin import QuestionSeriesInline
from experiment.forms import (
ExperimentCollectionForm,
ExperimentForm,
BlockForm,
ExportForm,
TemplateForm,
Expand Down Expand Up @@ -209,15 +209,15 @@ class SocialMediaConfigInline(admin.StackedInline):
extra = 0


class ExperimentCollectionAdmin(
class ExperimentAdmin(
InlineActionsModelAdminMixin, admin.ModelAdmin):
list_display = ('name', 'slug_link', 'description_excerpt',
'dashboard', 'phases', 'active')
fields = ['slug', 'name', 'active', 'description',
'consent', 'theme_config', 'dashboard',
'about_content']
inline_actions = ['dashboard']
form = ExperimentCollectionForm
form = ExperimentForm
inlines = [
PhaseInline,
SocialMediaConfigInline,
Expand All @@ -244,7 +244,7 @@ def phases(self, obj):
slug_link.short_description = "Slug"

def dashboard(self, request, obj, parent_obj=None):
"""Open researchers dashboard for a collection"""
"""Open researchers dashboard for an experiment"""
all_blocks = obj.associated_blocks()
all_participants = obj.current_participants()
all_sessions = obj.export_sessions()
Expand All @@ -267,16 +267,16 @@ def dashboard(self, request, obj, parent_obj=None):

return render(
request,
'collection-dashboard.html',
context={'collection': obj,
'experiment-dashboard.html',
context={'experiment': obj,
'blocks': blocks,
'sessions': all_sessions,
'participants': all_participants,
'collect_data': collect_data}
)


admin.site.register(ExperimentCollection, ExperimentCollectionAdmin)
admin.site.register(Experiment, ExperimentAdmin)


class PhaseAdmin(InlineActionsModelAdminMixin, admin.ModelAdmin):
Expand All @@ -292,7 +292,7 @@ def name_link(self, obj):

def related_series(self, obj):
url = reverse(
"admin:experiment_experimentcollection_change", args=[obj.series.pk])
"admin:experiment_experiment_change", args=[obj.series.pk])
return format_html('<a href="{}">{}</a>', url, obj.series.name)

def blocks(self, obj):
Expand Down
2 changes: 1 addition & 1 deletion backend/experiment/fixtures/experiment.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[
{
"model": "experiment.ExperimentCollection",
"model": "experiment.Experiment",
"pk": 1,
"fields": {
"slug": "RhythmTestSeries",
Expand Down
14 changes: 7 additions & 7 deletions backend/experiment/forms.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from django.forms import CheckboxSelectMultiple, ModelForm, ChoiceField, Form, MultipleChoiceField, ModelMultipleChoiceField, Select, TypedMultipleChoiceField, CheckboxSelectMultiple, TextInput
from experiment.models import ExperimentCollection, Block, SocialMediaConfig
from experiment.models import Experiment, Block, SocialMediaConfig
from experiment.rules import BLOCK_RULES


Expand Down Expand Up @@ -127,20 +127,20 @@ class MarkdownPreviewTextInput(TextInput):
template_name = 'widgets/markdown_preview_text_input.html'


class ExperimentCollectionForm(ModelForm):
class ExperimentForm(ModelForm):
def __init__(self, *args, **kwargs):
super(ModelForm, self).__init__(*args, **kwargs)
self.fields['dashboard'].help_text = (
'This field will be deprecated in the nearby future. '
'Please use experiment phases for dashboard configuration. (see bottom of form). <br><br>'
'Legacy behavior: If you check "dashboard", the experiment collection will have a '
'Legacy behavior: If you check "dashboard", the experiment will have a '
'dashboard that shows all or a subgroup of related blocks along '
'with a description, footer, and about page. If you leave it unchecked, '
'the experiment collection will redirect to the first experiment.')
'the experiment will redirect to the first block.')
self.fields['about_content'].widget = MarkdownPreviewTextInput()

class Meta:
model = ExperimentCollection
model = Experiment
fields = ['slug', 'description',
'dashboard', 'about_content']

Expand Down Expand Up @@ -198,8 +198,8 @@ class Meta:
fields = ['name', 'slug', 'active', 'rules',
'rounds', 'bonus_points', 'playlists',]
help_texts = {
'description': 'A short description of the block that will be displayed on the experiment collection page and as a meta description in search engines.',
'image': 'An image that will be displayed on the experiment collection page and as a meta image in search engines.',
'description': 'A short description of the block that will be displayed on the experiment page and as a meta description in search engines.',
'image': 'An image that will be displayed on the experiment page and as a meta image in search engines.',
'consent': 'Upload an HTML (.html) or MARKDOWN (.md) file with a text to ask a user its consent<br> \
for using the block data for this instance of the block.<br> \
This field will override any consent text loaded from the rules file. <br>\
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 4.2.14 on 2024-07-12 10:14

from django.db import migrations


class Migration(migrations.Migration):

dependencies = [
('theme', '0006_headerconfig'),
('experiment', '0046_alter_socialmediaconfig_content'),
]

operations = [
migrations.RenameModel(
old_name='ExperimentCollection',
new_name='Experiment',
),
migrations.AlterModelOptions(
name='experiment',
options={'verbose_name_plural': 'Experiments'},
),
migrations.RenameField(
model_name='socialmediaconfig',
old_name='experiment_collection',
new_name='experiment',
),
]
18 changes: 9 additions & 9 deletions backend/experiment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def consent_upload_path(instance, filename):
return f'consent/{folder_name}/{filename}'


class ExperimentCollection(models.Model):
class Experiment(models.Model):
""" A model to allow nesting multiple phases with blocks into a 'parent' experiment """
name = models.CharField(max_length=64, default='')
description = models.TextField(blank=True, default='')
Expand All @@ -50,18 +50,18 @@ def __str__(self):
return self.name or self.slug

class Meta:
verbose_name_plural = "Experiment Collections"
verbose_name_plural = "Experiments"

def associated_blocks(self):
phases = self.phases.all()
return [
experiment.block for phase in phases for experiment in list(phase.blocks.all())]

def export_sessions(self):
"""export sessions for this collection"""
"""export sessions for this experiment"""
all_sessions = Session.objects.none()
for exp in self.associated_blocks():
all_sessions |= Session.objects.filter(block=exp).order_by('-started_at')
for block in self.associated_blocks():
all_sessions |= Session.objects.filter(block=block).order_by('-started_at')
return all_sessions

def current_participants(self):
Expand All @@ -74,7 +74,7 @@ def current_participants(self):

class Phase(models.Model):
name = models.CharField(max_length=64, blank=True, default='')
series = models.ForeignKey(ExperimentCollection,
series = models.ForeignKey(Experiment,
on_delete=models.CASCADE, related_name='phases')
index = models.IntegerField(default=0, help_text='Index of the phase in the series. Lower numbers come first.')
dashboard = models.BooleanField(default=False)
Expand Down Expand Up @@ -328,8 +328,8 @@ class Feedback(models.Model):


class SocialMediaConfig(models.Model):
experiment_collection = models.OneToOneField(
ExperimentCollection,
experiment = models.OneToOneField(
Experiment,
on_delete=models.CASCADE,
related_name='social_media_config'
)
Expand Down Expand Up @@ -382,4 +382,4 @@ def get_content(
)

def __str__(self):
return f"Social Media for {self.experiment_collection.name}"
return f"Social Media for {self.experiment.name}"
4 changes: 2 additions & 2 deletions backend/experiment/rules/toontjehoger_1_mozart.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from experiment.actions.styles import STYLE_TOONTJEHOGER
from .base import Base
from experiment.utils import non_breaking_spaces
from experiment.actions.utils import get_current_collection_url
from experiment.actions.utils import get_current_experiment_url

from result.utils import prepare_result

Expand Down Expand Up @@ -233,7 +233,7 @@ def get_final_round(self, session):
body=body,
heading="Het Mozart effect",
button_label="Terug naar ToontjeHoger",
button_link=get_current_collection_url(session)
button_link=get_current_experiment_url(session)
)

return [*answer_explainer, *score, final, info]
Expand Down
4 changes: 2 additions & 2 deletions backend/experiment/rules/toontjehoger_2_preverbal.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from experiment.actions.playback import ImagePlayer
from experiment.actions.styles import STYLE_NEUTRAL_INVERTED
from experiment.actions.frontend_style import FrontendStyle, EFrontendStyle
from experiment.actions.utils import get_current_collection_url
from experiment.actions.utils import get_current_experiment_url
from experiment.utils import create_player_labels
from .base import Base
from result.utils import prepare_result
Expand Down Expand Up @@ -291,7 +291,7 @@ def get_final_round(self, session):
body=body,
heading="Het eerste luisteren",
button_label="Terug naar ToontjeHoger",
button_link=get_current_collection_url(session)
button_link=get_current_experiment_url(session)
)

return [*score, final, info]
4 changes: 2 additions & 2 deletions backend/experiment/rules/toontjehoger_3_plink.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
from experiment.actions import Explainer, Step, Score, Final, Playlist, Info, Trial
from experiment.actions.playback import PlayButton
from experiment.actions.form import AutoCompleteQuestion, RadiosQuestion, Form
from experiment.actions.utils import get_current_collection_url
from experiment.actions.utils import get_current_experiment_url
from .base import Base
from experiment.utils import non_breaking_spaces
from result.utils import prepare_result
Expand Down Expand Up @@ -312,7 +312,7 @@ def get_final_round(self, session):
body=body,
heading="Muziekherkenning",
button_label="Terug naar ToontjeHoger",
button_link=get_current_collection_url(session)
button_link=get_current_experiment_url(session)
)

return [score, final, info]
4 changes: 2 additions & 2 deletions backend/experiment/rules/toontjehoger_4_absolute.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
from experiment.actions.frontend_style import FrontendStyle, EFrontendStyle
from experiment.actions.playback import Multiplayer
from experiment.actions.styles import STYLE_NEUTRAL_INVERTED
from experiment.actions.utils import get_current_collection_url
from experiment.actions.utils import get_current_experiment_url
from experiment.utils import create_player_labels
from .base import Base
from result.utils import prepare_result
Expand Down Expand Up @@ -183,7 +183,7 @@ def get_final_round(self, session):
body=body,
heading="Absoluut gehoor",
button_label="Terug naar ToontjeHoger",
button_link=get_current_collection_url(session)
button_link=get_current_experiment_url(session)
)

return [*score, final, info]
Expand Down
4 changes: 2 additions & 2 deletions backend/experiment/rules/toontjehoger_5_tempo.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from experiment.actions.frontend_style import FrontendStyle, EFrontendStyle
from experiment.actions.playback import Multiplayer
from experiment.actions.styles import STYLE_NEUTRAL_INVERTED
from experiment.actions.utils import get_current_collection_url
from experiment.actions.utils import get_current_experiment_url
from section.models import Playlist
from .base import Base
from experiment.utils import create_player_labels, non_breaking_spaces
Expand Down Expand Up @@ -252,7 +252,7 @@ def get_final_round(self, session):
body=body,
heading="Timing en tempo",
button_label="Terug naar ToontjeHoger",
button_link=get_current_collection_url(session)
button_link=get_current_experiment_url(session)
)

return [*score, final, info]
Expand Down
4 changes: 2 additions & 2 deletions backend/experiment/rules/toontjehoger_6_relative.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from experiment.actions.playback import Multiplayer
from experiment.actions.frontend_style import FrontendStyle, EFrontendStyle
from experiment.actions.styles import STYLE_BOOLEAN
from experiment.actions.utils import get_current_collection_url
from experiment.actions.utils import get_current_experiment_url
from section.models import Playlist
from .base import Base
from .toontjehoger_1_mozart import toontjehoger_ranks
Expand Down Expand Up @@ -181,7 +181,7 @@ def get_final_round(self, session):
body=body,
heading="Relatief gehoor",
button_label="Terug naar ToontjeHoger",
button_link=get_current_collection_url(session)
button_link=get_current_experiment_url(session)
)

return [*score, final, info]
4 changes: 2 additions & 2 deletions backend/experiment/rules/toontjehogerkids_1_mozart.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from django.template.loader import render_to_string
from os.path import join
from experiment.actions import Explainer, Step, Final, Info
from experiment.actions.utils import get_current_collection_url
from experiment.actions.utils import get_current_experiment_url
from .toontjehoger_1_mozart import toontjehoger_ranks, ToontjeHoger1Mozart

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -72,7 +72,7 @@ def get_final_round(self, session):
body=body,
heading="Het Mozart effect",
button_label="Terug naar ToontjeHogerKids",
button_link=get_current_collection_url(session)
button_link=get_current_experiment_url(session)
)

return [*answer_explainer, *score, final, info]
Loading

0 comments on commit 6a2459a

Please sign in to comment.