Skip to content

Commit

Permalink
add two actions to the admin
Browse files Browse the repository at this point in the history
  • Loading branch information
jefer94 committed Jul 9, 2024
1 parent 1740d90 commit 8a7d3d9
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 5 deletions.
15 changes: 10 additions & 5 deletions breathecode/mentorship/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,20 @@
logger = logging.getLogger(__name__)


def use_google_meet(modeladmin, request, queryset):
queryset.update(video_provider="GOOGLE_MEET")


def use_daily(modeladmin, request, queryset):
queryset.update(video_provider="DAILY")


@admin.register(MentorshipService)
class ServiceAdmin(admin.ModelAdmin):
list_display = ["slug", "name", "status", "academy"]
search_fields = ["slug", "name"]
list_filter = ["academy__slug", "status"]
# raw_id_fields = ['academy', 'github_user']
# actions = [sync_issues, generate_bill]

# def full_name(self, obj):
# return obj.user.first_name + ' ' + obj.user.last_name
actions = [use_google_meet, use_daily]


class MentorForm(forms.ModelForm):
Expand Down Expand Up @@ -361,3 +365,4 @@ class AcademyMentorshipSettingsAdmin(admin.ModelAdmin):
"video_provider",
)
list_filter = ["academy", "language", "allow_mentee_to_extend", "allow_mentors_to_extend", "video_provider"]
actions = [use_google_meet, use_daily]
99 changes: 99 additions & 0 deletions breathecode/mentorship/tests/admin/tests_use_daily.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"""
Test mentorships
"""

import pytest

from breathecode.mentorship.admin import use_daily
from capyc.rest_framework import pytest as capy


@pytest.mark.fixture(autouse=True)
def setup(db):
yield


@pytest.mark.parametrize("model_path", ["mentorship.MentorshipService", "mentorship.AcademyMentorshipSettings"])
def test_no_items(database: capy.Database, model_path: str):
Model = database.get_model(model_path)
queryset = Model.objects.filter()

use_daily(None, None, queryset)

assert database.list_of(model_path) == []


@pytest.mark.django_db(reset_sequences=True)
@pytest.mark.parametrize(
"model_path, model_name, includes_academy",
[
("mentorship.MentorshipService", "mentorship_service", False),
("mentorship.AcademyMentorshipSettings", "academy_mentorship_settings", True),
],
)
def test_two_items__changed(
database: capy.Database, model_path: str, model_name: str, format: capy.Format, includes_academy: bool
):
extra = {
"city": 1,
"country": 1,
}

if includes_academy:
extra["academy"] = (2, {"name": "Test Academy"})
extra[model_name] = [{"video_provider": "GOOGLE_MEET", "academy_id": n + 1} for n in range(2)]

else:
extra[model_name] = (2, {"video_provider": "GOOGLE_MEET"})

model = database.create(**extra)
Model = database.get_model(model_path)
queryset = Model.objects.filter()

use_daily(None, None, queryset)

assert database.list_of(model_path) == [
{
**format.to_obj_repr(model[model_name][n]),
"video_provider": "DAILY",
}
for n in range(2)
]


@pytest.mark.django_db(reset_sequences=True)
@pytest.mark.parametrize(
"model_path, model_name, includes_academy",
[
("mentorship.MentorshipService", "mentorship_service", False),
("mentorship.AcademyMentorshipSettings", "academy_mentorship_settings", True),
],
)
def test_two_items__did_not_changed(
database: capy.Database, model_path: str, model_name: str, format: capy.Format, includes_academy: bool
):
extra = {
"city": 1,
"country": 1,
}

if includes_academy:
extra["academy"] = (2, {"name": "Test Academy"})
extra[model_name] = [{"video_provider": "DAILY", "academy_id": n + 1} for n in range(2)]

else:
extra[model_name] = (2, {"video_provider": "DAILY"})

model = database.create(**extra)
Model = database.get_model(model_path)
queryset = Model.objects.filter()

use_daily(None, None, queryset)

assert database.list_of(model_path) == [
{
**format.to_obj_repr(model[model_name][n]),
"video_provider": "DAILY",
}
for n in range(2)
]
99 changes: 99 additions & 0 deletions breathecode/mentorship/tests/admin/tests_use_google_meet.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
"""
Test mentorships
"""

import pytest

from breathecode.mentorship.admin import use_google_meet
from capyc.rest_framework import pytest as capy


@pytest.mark.fixture(autouse=True)
def setup(db):
yield


@pytest.mark.parametrize("model_path", ["mentorship.MentorshipService", "mentorship.AcademyMentorshipSettings"])
def test_no_items(database: capy.Database, model_path: str):
Model = database.get_model(model_path)
queryset = Model.objects.filter()

use_google_meet(None, None, queryset)

assert database.list_of(model_path) == []


@pytest.mark.django_db(reset_sequences=True)
@pytest.mark.parametrize(
"model_path, model_name, includes_academy",
[
("mentorship.MentorshipService", "mentorship_service", False),
("mentorship.AcademyMentorshipSettings", "academy_mentorship_settings", True),
],
)
def test_two_items__changed(
database: capy.Database, model_path: str, model_name: str, format: capy.Format, includes_academy: bool
):
extra = {
"city": 1,
"country": 1,
}

if includes_academy:
extra["academy"] = (2, {"name": "Test Academy"})
extra[model_name] = [{"video_provider": "DAILY", "academy_id": n + 1} for n in range(2)]

else:
extra[model_name] = (2, {"video_provider": "DAILY"})

model = database.create(**extra)
Model = database.get_model(model_path)
queryset = Model.objects.filter()

use_google_meet(None, None, queryset)

assert database.list_of(model_path) == [
{
**format.to_obj_repr(model[model_name][n]),
"video_provider": "GOOGLE_MEET",
}
for n in range(2)
]


@pytest.mark.django_db(reset_sequences=True)
@pytest.mark.parametrize(
"model_path, model_name, includes_academy",
[
("mentorship.MentorshipService", "mentorship_service", False),
("mentorship.AcademyMentorshipSettings", "academy_mentorship_settings", True),
],
)
def test_two_items__did_not_changed(
database: capy.Database, model_path: str, model_name: str, format: capy.Format, includes_academy: bool
):
extra = {
"city": 1,
"country": 1,
}

if includes_academy:
extra["academy"] = (2, {"name": "Test Academy"})
extra[model_name] = [{"video_provider": "GOOGLE_MEET", "academy_id": n + 1} for n in range(2)]

else:
extra[model_name] = (2, {"video_provider": "GOOGLE_MEET"})

model = database.create(**extra)
Model = database.get_model(model_path)
queryset = Model.objects.filter()

use_google_meet(None, None, queryset)

assert database.list_of(model_path) == [
{
**format.to_obj_repr(model[model_name][n]),
"video_provider": "GOOGLE_MEET",
}
for n in range(2)
]

0 comments on commit 8a7d3d9

Please sign in to comment.