Skip to content

Commit

Permalink
Fixup. Format code with Black
Browse files Browse the repository at this point in the history
  • Loading branch information
github-actions committed Jul 10, 2024
1 parent c331882 commit fd1a1bb
Show file tree
Hide file tree
Showing 10 changed files with 128 additions and 141 deletions.
4 changes: 1 addition & 3 deletions pod/completion/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,9 +532,7 @@ def video_completion_speaker_save(request: WSGIRequest, video: Video):
data = json.dumps(some_data_to_dump)
return HttpResponse(data, content_type="application/json")
else:
context = get_video_completion_context(
video, list_speaker=list_speaker
)
context = get_video_completion_context(video, list_speaker=list_speaker)
context["page_title"] = get_completion_home_page_title(video)
messages.add_message(
request, messages.SUCCESS, _("The speaker has been saved.")
Expand Down
14 changes: 7 additions & 7 deletions pod/speaker/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,23 @@ class JobInline(admin.StackedInline):
class SpeakerAdmin(admin.ModelAdmin):
"""Admin configuration for Speaker."""

list_display = ('firstname', 'lastname')
list_display = ("firstname", "lastname")
inlines = [JobInline]


@admin.register(Job)
class JobAdmin(admin.ModelAdmin):
"""Admin configuration for Job."""

list_display = ('title', 'speaker')
list_filter = ('speaker',)
search_fields = ('title', 'speaker__firstname', 'speaker__lastname')
list_display = ("title", "speaker")
list_filter = ("speaker",)
search_fields = ("title", "speaker__firstname", "speaker__lastname")


@admin.register(JobVideo)
class JobVideoAdmin(admin.ModelAdmin):
"""Admin configuration for Video speaker by job."""

list_display = ('job', 'video')
list_filter = ('job', 'video')
search_fields = ('job__title', 'video__title')
list_display = ("job", "video")
list_filter = ("job", "video")
search_fields = ("job__title", "video__title")
4 changes: 2 additions & 2 deletions pod/speaker/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
class SpeakerConfig(AppConfig):
"""Speaker config app."""

name = 'pod.speaker'
default_auto_field = 'django.db.models.BigAutoField'
name = "pod.speaker"
default_auto_field = "django.db.models.BigAutoField"
verbose_name = _("Speaker")
14 changes: 9 additions & 5 deletions pod/speaker/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ class JobWidget(s2forms.ModelSelect2Widget):
"""Widget for selecting speaker job."""

search_fields = [
"title__icontains", "speaker__lastname__icontains", "speaker__firstname__icontains",
"title__icontains",
"speaker__lastname__icontains",
"speaker__firstname__icontains",
]


Expand All @@ -21,7 +23,7 @@ class SpeakerForm(forms.ModelForm):

class Meta:
model = Speaker
fields = ['firstname', 'lastname']
fields = ["firstname", "lastname"]

def __init__(self, *args, **kwargs):
"""Init method."""
Expand All @@ -34,7 +36,7 @@ class JobForm(forms.ModelForm):

class Meta:
model = Job
fields = ['title']
fields = ["title"]


class JobVideoForm(forms.ModelForm):
Expand All @@ -53,8 +55,10 @@ class Meta(object):
widgets = {
"job": JobWidget(
attrs={
'data-placeholder': _("You can search speaker by first name, last name and job."),
'class': 'w-100',
"data-placeholder": _(
"You can search speaker by first name, last name and job."
),
"class": "w-100",
}
)
}
Expand Down
21 changes: 7 additions & 14 deletions pod/speaker/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,8 @@ class Speaker(models.Model):
lastname (CharField): last name of speaker.
"""

firstname = models.CharField(
verbose_name=_("First name"),
max_length=100
)
lastname = models.CharField(
verbose_name=_("Last name"),
max_length=100
)
firstname = models.CharField(verbose_name=_("First name"), max_length=100)
lastname = models.CharField(verbose_name=_("Last name"), max_length=100)

class Meta:
ordering = ["lastname", "firstname"]
Expand All @@ -41,10 +35,7 @@ class Job(models.Model):
speaker (ForeignKey <Speaker>): Speaker of the job.
"""

title = models.CharField(
verbose_name=_("Title"),
max_length=100
)
title = models.CharField(verbose_name=_("Title"), max_length=100)
speaker = models.ForeignKey(Speaker, on_delete=models.CASCADE)

def __str__(self):
Expand All @@ -60,11 +51,13 @@ class JobVideo(models.Model):
video (ForeignKey <Video>): video.
"""

job = models.ForeignKey(Job, verbose_name=_("Speaker's job"), on_delete=models.CASCADE)
job = models.ForeignKey(
Job, verbose_name=_("Speaker's job"), on_delete=models.CASCADE
)
video = models.ForeignKey(Video, on_delete=models.CASCADE)

class Meta:
unique_together = ('job', 'video')
unique_together = ("job", "video")

def __str__(self):
return f"{self.job} - {self.video}"
40 changes: 8 additions & 32 deletions pod/speaker/tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,14 @@ def setUp(self):
video="test2.mp4",
duration=20,
)
speaker1 = Speaker.objects.create(
firstname="Dupont",
lastname="Pierre"
)
speaker2 = Speaker.objects.create(
firstname="Martin",
lastname="Michel"
)
job1 = Job.objects.create(
speaker=speaker1,
title="Directeur"
)
job2 = Job.objects.create(
speaker=speaker1,
title="President"
)
job3 = Job.objects.create(
speaker=speaker2,
title="Responsable"
)
JobVideo.objects.create(
video=video,
job=job1
)
JobVideo.objects.create(
video=video,
job=job2
)
JobVideo.objects.create(
video=video2,
job=job3
)
speaker1 = Speaker.objects.create(firstname="Dupont", lastname="Pierre")
speaker2 = Speaker.objects.create(firstname="Martin", lastname="Michel")
job1 = Job.objects.create(speaker=speaker1, title="Directeur")
job2 = Job.objects.create(speaker=speaker1, title="President")
job3 = Job.objects.create(speaker=speaker2, title="Responsable")
JobVideo.objects.create(video=video, job=job1)
JobVideo.objects.create(video=video, job=job2)
JobVideo.objects.create(video=video2, job=job3)

def test_attributs_full(self):
"""Test all attributs."""
Expand Down
106 changes: 55 additions & 51 deletions pod/speaker/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,95 +13,99 @@

class SpeakerViewsTest(TestCase):
"""Test case for speaker views."""

def setUp(self):
"""Set up the test environment."""
self.client = Client()
self.superuser = User.objects.create_superuser(
username='admin', password=PWD, email='[email protected]'
)
self.user = User.objects.create_user(
username='user', password=PWD
username="admin", password=PWD, email="[email protected]"
)
self.speaker = Speaker.objects.create(firstname='John', lastname='Doe')
self.job = Job.objects.create(title='Engineer', speaker=self.speaker)
self.user = User.objects.create_user(username="user", password=PWD)
self.speaker = Speaker.objects.create(firstname="John", lastname="Doe")
self.job = Job.objects.create(title="Engineer", speaker=self.speaker)

def test_speaker_management_superuser_get(self):
"""Test GET request to speaker management by a superuser."""
self.client.login(username='admin', password=PWD)
response = self.client.get(reverse('speaker:speaker_management'))
self.client.login(username="admin", password=PWD)
response = self.client.get(reverse("speaker:speaker_management"))
self.assertEqual(response.status_code, 200)
self.assertTemplateUsed(response, 'speaker/speakers_management.html')
self.assertTemplateUsed(response, "speaker/speakers_management.html")

def test_speaker_management_non_superuser_get(self):
"""Test GET request to speaker management by a non-superuser."""
self.client.login(username='user', password=PWD)
response = self.client.get(reverse('speaker:speaker_management'))
self.client.login(username="user", password=PWD)
response = self.client.get(reverse("speaker:speaker_management"))
self.assertEqual(response.status_code, 403)

def test_add_speaker(self):
"""Test adding a new speaker."""
self.client.login(username='admin', password=PWD)
self.client.login(username="admin", password=PWD)
data = {
'action': 'add',
'firstname': 'Jane',
'lastname': 'Smith',
'jobs[]': ['Teacher', 'Researcher'],
"action": "add",
"firstname": "Jane",
"lastname": "Smith",
"jobs[]": ["Teacher", "Researcher"],
}
url = reverse('speaker:speaker_management')
url = reverse("speaker:speaker_management")
response = self.client.post(url, data)
self.assertRedirects(response, reverse('speaker:speaker_management'))
self.assertTrue(Speaker.objects.filter(firstname='Jane', lastname='Smith').exists())
speaker = Speaker.objects.get(firstname='Jane', lastname='Smith')
self.assertRedirects(response, reverse("speaker:speaker_management"))
self.assertTrue(
Speaker.objects.filter(firstname="Jane", lastname="Smith").exists()
)
speaker = Speaker.objects.get(firstname="Jane", lastname="Smith")
self.assertEqual(speaker.job_set.count(), 2)

def test_delete_speaker(self):
"""Test deleting a speaker."""
self.client.login(username='admin', password=PWD)
self.client.login(username="admin", password=PWD)
data = {
'action': 'delete',
'speakerid': self.speaker.id,
"action": "delete",
"speakerid": self.speaker.id,
}
response = self.client.post(reverse('speaker:speaker_management'), data)
self.assertRedirects(response, reverse('speaker:speaker_management'))
response = self.client.post(reverse("speaker:speaker_management"), data)
self.assertRedirects(response, reverse("speaker:speaker_management"))
self.assertFalse(Speaker.objects.filter(id=self.speaker.id).exists())

def test_edit_speaker(self):
"""Test editing a speaker."""
self.client.login(username='admin', password=PWD)
self.client.login(username="admin", password=PWD)
data = {
'action': 'edit',
'speakerid': self.speaker.id,
'firstname': 'Johnny',
'lastname': 'Doe',
'jobIds[]': [self.job.id],
'jobs[]': ['Senior Engineer'],
"action": "edit",
"speakerid": self.speaker.id,
"firstname": "Johnny",
"lastname": "Doe",
"jobIds[]": [self.job.id],
"jobs[]": ["Senior Engineer"],
}
response = self.client.post(reverse('speaker:speaker_management'), data)
self.assertRedirects(response, reverse('speaker:speaker_management'))
response = self.client.post(reverse("speaker:speaker_management"), data)
self.assertRedirects(response, reverse("speaker:speaker_management"))
speaker = Speaker.objects.get(id=self.speaker.id)
self.assertEqual(speaker.firstname, 'Johnny')
self.assertEqual(speaker.firstname, "Johnny")
self.assertEqual(speaker.job_set.count(), 1)
self.assertEqual(speaker.job_set.first().title, 'Senior Engineer')
self.assertEqual(speaker.job_set.first().title, "Senior Engineer")

def test_get_speaker(self):
"""Test retrieving a speaker's details."""
self.client.login(username='admin', password=PWD)
response = self.client.get(reverse('speaker:get_speaker', args=[self.speaker.id]))
self.client.login(username="admin", password=PWD)
response = self.client.get(reverse("speaker:get_speaker", args=[self.speaker.id]))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {
'speaker': {
'id': self.speaker.id,
'firstname': self.speaker.firstname,
'lastname': self.speaker.lastname,
'jobs': [{'id': self.job.id, 'title': self.job.title}],
}
})
self.assertEqual(
response.json(),
{
"speaker": {
"id": self.speaker.id,
"firstname": self.speaker.firstname,
"lastname": self.speaker.lastname,
"jobs": [{"id": self.job.id, "title": self.job.title}],
}
},
)

def test_get_jobs(self):
"""Test retrieving jobs for a speaker."""
self.client.login(username='admin', password=PWD)
response = self.client.get(reverse('speaker:get_jobs', args=[self.speaker.id]))
self.client.login(username="admin", password=PWD)
response = self.client.get(reverse("speaker:get_jobs", args=[self.speaker.id]))
self.assertEqual(response.status_code, 200)
self.assertEqual(response.json(), {
'jobs': [{'id': self.job.id, 'title': self.job.title}]
})
self.assertEqual(
response.json(), {"jobs": [{"id": self.job.id, "title": self.job.title}]}
)
6 changes: 3 additions & 3 deletions pod/speaker/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

urlpatterns = [
path("", speaker_management, name="speaker_management"),
path("add/", add_speaker, name='add_speaker'),
path('get-speaker/<int:speaker_id>/', get_speaker, name='get_speaker'),
path('get-jobs/<int:speaker_id>/', get_jobs, name='get_jobs'),
path("add/", add_speaker, name="add_speaker"),
path("get-speaker/<int:speaker_id>/", get_speaker, name="get_speaker"),
path("get-jobs/<int:speaker_id>/", get_jobs, name="get_jobs"),
]
2 changes: 1 addition & 1 deletion pod/speaker/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def get_all_speakers() -> Optional[Speaker]:
Returns:
Optional[Speakers]: The speakers list, or None if no speaker is found.
"""
return Speaker.objects.prefetch_related('job_set').all()
return Speaker.objects.prefetch_related("job_set").all()


def get_video_speakers(video: Video) -> Optional[JobVideo]:
Expand Down
Loading

0 comments on commit fd1a1bb

Please sign in to comment.