Skip to content

Commit

Permalink
Chore: Add missing Django Experiment Phase migrations (#1164)
Browse files Browse the repository at this point in the history
* chore: Add missing Django Experiment Phase migrations and rename 'order' to 'index'

* fix(test): Fix tests after renaming column
  • Loading branch information
drikusroor authored Jul 2, 2024
1 parent 3fe7373 commit 79282db
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 16 deletions.
12 changes: 6 additions & 6 deletions backend/experiment/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class ExperimentAdmin(InlineActionsModelAdminMixin, admin.ModelAdmin):
search_fields = ['name']
inline_actions = ['export', 'export_csv']
fields = ['name', 'description', 'image',
'slug', 'url', 'hashtag', 'theme_config',
'slug', 'url', 'hashtag', 'theme_config',
'language', 'active', 'rules',
'rounds', 'bonus_points', 'playlists',
'consent']
Expand Down Expand Up @@ -104,7 +104,7 @@ def export(self, request, obj, parent_obj=None):
# create forced download response
response = HttpResponse(zip_buffer.getbuffer())
response['Content-Type'] = 'application/x-zip-compressed'
response['Content-Disposition'] = 'attachment; filename="'+obj.slug+'-'+timezone.now().isoformat()+'.zip"'
response['Content-Disposition'] = 'attachment; filename="'+obj.slug+'-'+timezone.now().isoformat()+'.zip"'
return response

export.short_description = "Export JSON"
Expand Down Expand Up @@ -228,7 +228,7 @@ def description_excerpt(self, obj):
def phases(self, obj):
phases = Phase.objects.filter(series=obj)
return format_html(', '.join([f'<a href="/admin/experiment/phase/{phase.id}/change/">{phase.name}</a>' for phase in phases]))

slug_link.short_description = "Slug"

def dashboard(self, request, obj, parent_obj=None):
Expand All @@ -249,7 +249,7 @@ def dashboard(self, request, obj, parent_obj=None):
'participant_count': len(exp.current_participants()),
'participants': exp.current_participants()
} for exp in all_experiments]

return render(
request,
'collection-dashboard.html',
Expand All @@ -265,8 +265,8 @@ def dashboard(self, request, obj, parent_obj=None):


class PhaseAdmin(InlineActionsModelAdminMixin, admin.ModelAdmin):
list_display = ('name_link', 'related_series', 'order', 'dashboard', 'randomize', 'experiments')
fields = ['name', 'series', 'order', 'dashboard', 'randomize']
list_display = ('name_link', 'related_series', 'index', 'dashboard', 'randomize', 'experiments')
fields = ['name', 'series', 'index', 'dashboard', 'randomize']
inlines = [GroupedExperimentInline]

def name_link(self, obj):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# Generated by Django 4.2.11 on 2024-07-02 08:35

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('experiment', '0039_auto_20240625_1055'),
]

operations = [
migrations.AlterModelOptions(
name='phase',
options={'ordering': ['index']},
),
migrations.RemoveField(
model_name='phase',
name='order',
),
migrations.AddField(
model_name='phase',
name='index',
field=models.IntegerField(default=0, help_text='Index of the phase in the series. Lower numbers come first.'),
),
migrations.AlterField(
model_name='phase',
name='randomize',
field=models.BooleanField(default=False, help_text='Randomize the order of the experiments in this phase.'),
),
]
4 changes: 2 additions & 2 deletions backend/experiment/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class Phase(models.Model):
name = models.CharField(max_length=64, blank=True, default='')
series = models.ForeignKey(ExperimentCollection,
on_delete=models.CASCADE, related_name='phases')
order = models.IntegerField(default=0, help_text='Order of the phase in the series. Lower numbers come first.')
index = models.IntegerField(default=0, help_text='Index of the phase in the series. Lower numbers come first.')
dashboard = models.BooleanField(default=False)
randomize = models.BooleanField(
default=False, help_text='Randomize the order of the experiments in this phase.')
Expand All @@ -86,7 +86,7 @@ def __str__(self):
return f'{compound_name}'

class Meta:
ordering = ['order']
ordering = ['index']


class GroupedExperiment(models.Model):
Expand Down
8 changes: 4 additions & 4 deletions backend/experiment/tests/test_admin_experiment.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ def setUp(self):
self.admin = ExperimentAdmin(model=Experiment,
admin_site=AdminSite
)

def test_admin_export(self):
response = self.admin.export(request, self.experiment)
zip_buffer = BytesIO(response.content)
Expand Down Expand Up @@ -225,7 +225,7 @@ def setUp(self):
def test_related_series_with_series(self):
series = ExperimentCollection.objects.create(name='Test Series')
phase = Phase.objects.create(
name='Test Group', order=1, randomize=False, series=series, dashboard=True)
name='Test Group', index=1, randomize=False, series=series, dashboard=True)
related_series = self.admin.related_series(phase)
expected_url = reverse(
"admin:experiment_experimentcollection_change", args=[series.pk])
Expand All @@ -235,14 +235,14 @@ def test_related_series_with_series(self):
def test_experiments_with_no_experiments(self):
series = ExperimentCollection.objects.create(name='Test Series')
phase = Phase.objects.create(
name='Test Group', order=1, randomize=False, dashboard=True, series=series)
name='Test Group', index=1, randomize=False, dashboard=True, series=series)
experiments = self.admin.experiments(phase)
self.assertEqual(experiments, "No experiments")

def test_experiments_with_experiments(self):
series = ExperimentCollection.objects.create(name='Test Series')
phase = Phase.objects.create(
name='Test Group', order=1, randomize=False, dashboard=True, series=series)
name='Test Group', index=1, randomize=False, dashboard=True, series=series)
experiment1 = Experiment.objects.create(name='Experiment 1', slug='experiment-1')
experiment2 = Experiment.objects.create(name='Experiment 2', slug='experiment-2')
grouped_experiment1 = GroupedExperiment.objects.create(phase=phase, experiment=experiment1)
Expand Down
6 changes: 3 additions & 3 deletions backend/experiment/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def setUpTestData(cls):
introductory_phase = Phase.objects.create(
name='introduction',
series=collection,
order=1
index=1
)
cls.experiment1 = Experiment.objects.create(
name='experiment1', slug='experiment1')
Expand All @@ -44,7 +44,7 @@ def setUpTestData(cls):
intermediate_phase = Phase.objects.create(
name='intermediate',
series=collection,
order=2
index=2
)
cls.experiment2 = Experiment.objects.create(
name='experiment2', slug='experiment2', theme_config=theme_config)
Expand All @@ -61,7 +61,7 @@ def setUpTestData(cls):
final_phase = Phase.objects.create(
name='final',
series=collection,
order=3
index=3
)
cls.experiment4 = Experiment.objects.create(
name='experiment4', slug='experiment4')
Expand Down
2 changes: 1 addition & 1 deletion backend/experiment/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def get_experiment_collection(
request.session[COLLECTION_KEY] = slug
participant = get_participant(request)
phases = list(Phase.objects.filter(
series=collection.id).order_by('order'))
series=collection.id).order_by('index'))
try:
current_phase = phases[phase_index]
serialized_phase = serialize_phase(
Expand Down

0 comments on commit 79282db

Please sign in to comment.