Skip to content

Commit

Permalink
feat(#2737): Reformat for Flake8 and Black, use longer lines (120)
Browse files Browse the repository at this point in the history
  • Loading branch information
pbanaszkiewicz committed Feb 16, 2025
1 parent 1caa62f commit b1a6441
Show file tree
Hide file tree
Showing 256 changed files with 2,703 additions and 7,724 deletions.
1 change: 1 addition & 0 deletions .flake8
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[flake8]
max-line-length=120
exclude = migrations
16 changes: 4 additions & 12 deletions amy/api/v1/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,26 +75,18 @@ def filter_consent(
)

if value is None:
people_ids = consents.filter(term_option__isnull=True).values_list(
"person_id", flat=True
)
people_ids = consents.filter(term_option__isnull=True).values_list("person_id", flat=True)
return queryset.filter(person_id__in=people_ids)

option = TermOptionChoices.AGREE if value is True else TermOptionChoices.DECLINE
people_ids = consents.filter(term_option__option_type=option).values_list(
"person_id", flat=True
)
people_ids = consents.filter(term_option__option_type=option).values_list("person_id", flat=True)
return queryset.filter(pk__in=people_ids)


class PersonFilter(filters.FilterSet):
is_instructor = filters.BooleanFilter(
method=filter_instructors, label="Is instructor?"
)
is_instructor = filters.BooleanFilter(method=filter_instructors, label="Is instructor?")

may_contact = filters.BooleanFilter(
method=partial(filter_consent, slug=TermEnum.MAY_CONTACT), label="May contact"
)
may_contact = filters.BooleanFilter(method=partial(filter_consent, slug=TermEnum.MAY_CONTACT), label="May contact")
publish_profile = filters.BooleanFilter(
method=partial(filter_consent, slug=TermEnum.PUBLIC_PROFILE),
label="Consent to making profile public",
Expand Down
4 changes: 1 addition & 3 deletions amy/api/v1/renderers.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,7 @@ class TrainingRequestCSVColumns:

def __init__(self):
self.header = self.serializer.Meta.fields
self.labels = {
k: v for k, v in self.translation_labels.items() if k in self.header
}
self.labels = {k: v for k, v in self.translation_labels.items() if k in self.header}


class TrainingRequestCSVRenderer(CSVRenderer, TrainingRequestCSVColumns):
Expand Down
56 changes: 14 additions & 42 deletions amy/api/v1/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,7 @@ class Meta:

class AwardSerializer(serializers.ModelSerializer):
badge = serializers.SlugRelatedField(many=False, read_only=True, slug_field="name")
event = serializers.HyperlinkedRelatedField(
read_only=True, view_name="api-v1:event-detail", lookup_field="slug"
)
event = serializers.HyperlinkedRelatedField(read_only=True, view_name="api-v1:event-detail", lookup_field="slug")

class Meta:
model = Award
Expand Down Expand Up @@ -134,12 +132,8 @@ class Meta:


class TaskSerializer(serializers.ModelSerializer):
event = serializers.HyperlinkedRelatedField(
read_only=True, view_name="api-v1:event-detail", lookup_field="slug"
)
person = serializers.HyperlinkedRelatedField(
read_only=True, view_name="api-v1:person-detail"
)
event = serializers.HyperlinkedRelatedField(read_only=True, view_name="api-v1:event-detail", lookup_field="slug")
person = serializers.HyperlinkedRelatedField(read_only=True, view_name="api-v1:person-detail")
role = serializers.SlugRelatedField(many=False, read_only=True, slug_field="name")

class Meta:
Expand All @@ -164,9 +158,7 @@ class EventSerializer(serializers.ModelSerializer):
lookup_field="slug",
lookup_url_kwarg="event_slug",
)
assigned_to = serializers.HyperlinkedRelatedField(
read_only=True, view_name="api-v1:person-detail"
)
assigned_to = serializers.HyperlinkedRelatedField(read_only=True, view_name="api-v1:person-detail")
attendance = serializers.IntegerField()

class Meta:
Expand Down Expand Up @@ -196,24 +188,16 @@ class Meta:
class TrainingRequestSerializer(serializers.ModelSerializer):
state = serializers.CharField(source="get_state_display")
domains = serializers.SlugRelatedField(many=True, read_only=True, slug_field="name")
previous_involvement = serializers.SlugRelatedField(
many=True, read_only=True, slug_field="name"
)
previous_involvement = serializers.SlugRelatedField(many=True, read_only=True, slug_field="name")
previous_training = serializers.CharField(source="get_previous_training_display")
previous_experience = serializers.CharField(
source="get_previous_experience_display"
)
previous_experience = serializers.CharField(source="get_previous_experience_display")
programming_language_usage_frequency = serializers.CharField(
source="get_programming_language_usage_frequency_display"
)
checkout_intent = serializers.CharField(source="get_checkout_intent_display")
teaching_intent = serializers.CharField(source="get_teaching_intent_display")
teaching_frequency_expectation = serializers.CharField(
source="get_teaching_frequency_expectation_display"
)
max_travelling_frequency = serializers.CharField(
source="get_max_travelling_frequency_display"
)
teaching_frequency_expectation = serializers.CharField(source="get_teaching_frequency_expectation_display")
max_travelling_frequency = serializers.CharField(source="get_max_travelling_frequency_display")

class Meta:
model = TrainingRequest
Expand Down Expand Up @@ -262,12 +246,8 @@ class Meta:


class TrainingRequestWithPersonSerializer(TrainingRequestSerializer):
person = serializers.SlugRelatedField(
many=False, read_only=True, slug_field="full_name"
)
person_id = serializers.PrimaryKeyRelatedField(
many=False, read_only=True, source="person"
)
person = serializers.SlugRelatedField(many=False, read_only=True, slug_field="full_name")
person_id = serializers.PrimaryKeyRelatedField(many=False, read_only=True, source="person")
domains = serializers.SerializerMethodField()
previous_involvement = serializers.SerializerMethodField()
awards = serializers.SerializerMethodField()
Expand All @@ -277,9 +257,7 @@ def get_domains(self, obj):
return ", ".join(map(lambda x: getattr(x, "name"), obj.domains.all()))

def get_previous_involvement(self, obj):
return ", ".join(
map(lambda x: getattr(x, "name"), obj.previous_involvement.all())
)
return ", ".join(map(lambda x: getattr(x, "name"), obj.previous_involvement.all()))

def get_awards(self, obj):
if obj.person:
Expand Down Expand Up @@ -466,15 +444,9 @@ class PersonSerializerAllData(PersonSerializer):
badges = BadgeSerializer(many=True, read_only=True)
awards = AwardSerializerExpandEvent(many=True, read_only=True, source="award_set")
tasks = TaskSerializerNoPerson(many=True, read_only=True, source="task_set")
languages = serializers.SlugRelatedField(
many=True, read_only=True, slug_field="name"
)
training_requests = TrainingRequestSerializer(
many=True, read_only=True, source="trainingrequest_set"
)
training_progresses = TrainingProgressSerializer(
many=True, read_only=True, source="trainingprogress_set"
)
languages = serializers.SlugRelatedField(many=True, read_only=True, slug_field="name")
training_requests = TrainingRequestSerializer(many=True, read_only=True, source="trainingrequest_set")
training_progresses = TrainingProgressSerializer(many=True, read_only=True, source="trainingprogress_set")
consents = serializers.SerializerMethodField("get_consents")

class Meta:
Expand Down
28 changes: 7 additions & 21 deletions amy/api/v1/tests/test_api_structure.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,15 +45,11 @@ def setUp(self):
person=self.admin,
term=self.term,
).active()[0]
self.consent = Consent.reconsent(
consent=old_consent, term_option=self.term.options[0]
)
self.consent = Consent.reconsent(consent=old_consent, term_option=self.term.options[0])

self.instructor_role = Role.objects.create(name="instructor")

self.task = Task.objects.create(
event=self.event, person=self.admin, role=self.instructor_role
)
self.task = Task.objects.create(event=self.event, person=self.admin, role=self.instructor_role)

self.client.login(username="admin", password="admin")

Expand Down Expand Up @@ -112,23 +108,15 @@ def test_links_between_resources(self):
# → event-detail
event = self.client.get(reverse("api-v1:event-detail", args=[self.event.slug]))
event_links = {
"host": reverse(
"api-v1:organization-detail", args=[self.event.host.domain]
),
"administrator": reverse(
"api-v1:organization-detail", args=[self.event.administrator.domain]
),
"host": reverse("api-v1:organization-detail", args=[self.event.host.domain]),
"administrator": reverse("api-v1:organization-detail", args=[self.event.administrator.domain]),
"tasks": reverse("api-v1:event-tasks-list", args=[self.event.slug]),
"assigned_to": reverse(
"api-v1:person-detail", args=[self.event.assigned_to.pk]
),
"assigned_to": reverse("api-v1:person-detail", args=[self.event.assigned_to.pk]),
}
for attr, link in event_links.items():
self.assertIn(link, event.data[attr])

task = self.client.get(
reverse("api-v1:event-tasks-detail", args=[self.event.slug, self.task.pk])
)
task = self.client.get(reverse("api-v1:event-tasks-detail", args=[self.event.slug, self.task.pk]))
task_links = {
"person": reverse("api-v1:person-detail", args=[self.admin.pk]),
}
Expand All @@ -145,9 +133,7 @@ def test_links_between_resources(self):
for attr, link in person_links.items():
self.assertIn(link, person.data[attr])

award = self.client.get(
reverse("api-v1:person-awards-detail", args=[self.admin.pk, self.award.pk])
)
award = self.client.get(reverse("api-v1:person-awards-detail", args=[self.admin.pk, self.award.pk]))
award_links = {
"event": reverse("api-v1:event-detail", args=[self.award.event.slug]),
}
Expand Down
38 changes: 8 additions & 30 deletions amy/api/v1/tests/test_export.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,7 @@ def prepare_data(self, user):
self.user.save()

# create a fake organization
test_host = Organization.objects.create(
domain="example.com", fullname="Test Organization"
)
test_host = Organization.objects.create(domain="example.com", fullname="Test Organization")

# create an event that will later be used
event = Event.objects.create(
Expand Down Expand Up @@ -174,9 +172,7 @@ def prepare_data(self, user):
user_notes="I like trains",
)
training_request.domains.set([KnowledgeDomain.objects.first()])
training_request.previous_involvement.set(
Role.objects.filter(name="instructor")
)
training_request.previous_involvement.set(Role.objects.filter(name="instructor"))

# add some training progress
TrainingProgress.objects.create(
Expand Down Expand Up @@ -212,19 +208,13 @@ def prepare_data(self, user):
date=datetime.date(2023, 5, 31),
trainee_notes="Notes submitted by trainee",
)
terms = (
Term.objects.active()
.filter(required_type=Term.PROFILE_REQUIRE_TYPE)
.prefetch_active_options()
)
terms = Term.objects.active().filter(required_type=Term.PROFILE_REQUIRE_TYPE).prefetch_active_options()
consents = Consent.objects.filter(person=self.user).active()
consents_by_term_id = {consent.term_id: consent for consent in consents}
self.user_consents: List[Consent] = []
for term in terms:
self.user_consents.append(
Consent.reconsent(
consent=consents_by_term_id[term.pk], term_option=term.options[0]
)
Consent.reconsent(consent=consents_by_term_id[term.pk], term_option=term.options[0])
)

def test_unauthorized_access(self):
Expand Down Expand Up @@ -359,16 +349,8 @@ def test_relational_fields_structure(self):
[consent.term.slug for consent in user_consents],
[consent["term"]["slug"] for consent in data["consents"]],
)
may_publish_name = [
consent
for consent in user_consents
if consent.term.slug == "may-publish-name"
][0]
public_profile = [
consent
for consent in user_consents
if consent.term.slug == "public-profile"
][0]
may_publish_name = [consent for consent in user_consents if consent.term.slug == "may-publish-name"][0]
public_profile = [consent for consent in user_consents if consent.term.slug == "public-profile"][0]
# assert consent format -- no term option
self.assertIn(
{
Expand Down Expand Up @@ -572,9 +554,5 @@ def test_relational_fields_structure(self):
},
]
self.assertEqual(len(data["training_progresses"]), 2)
self.assertEqual(
data["training_progresses"][0], expected["training_progresses"][0]
)
self.assertEqual(
data["training_progresses"][1], expected["training_progresses"][1]
)
self.assertEqual(data["training_progresses"][0], expected["training_progresses"][0])
self.assertEqual(data["training_progresses"][1], expected["training_progresses"][1])
42 changes: 11 additions & 31 deletions amy/api/v1/tests/test_filter.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,12 @@
class TestFilter(APITestCase):
def setUp(self):
def get_option(term_slug: TermEnum, option_type: str) -> TermOption:
return [
option
for option in terms_dict[term_slug].options
if option.option_type == option_type
][0]
return [option for option in terms_dict[term_slug].options if option.option_type == option_type][0]

def get_consent(term_slug: TermEnum, person: Person) -> Consent:
return [
consent
for consent in old_consents
if consent.term.slug == term_slug and consent.person == person
][0]
return [consent for consent in old_consents if consent.term.slug == term_slug and consent.person == person][
0
]

self.admin_1 = Person.objects.create_superuser(
username="admin1",
Expand All @@ -47,23 +41,15 @@ def get_consent(term_slug: TermEnum, person: Person) -> Consent:
self.admin_2.save()

terms = (
Term.objects.filter(
slug__in=[TermEnum.MAY_CONTACT, TermEnum.PUBLIC_PROFILE]
)
Term.objects.filter(slug__in=[TermEnum.MAY_CONTACT, TermEnum.PUBLIC_PROFILE])
.active()
.prefetch_active_options()
)
terms_dict = {term.slug: term for term in terms}
may_contact_agree = get_option(TermEnum.MAY_CONTACT, TermOptionChoices.AGREE)
may_contact_decline = get_option(
TermEnum.MAY_CONTACT, TermOptionChoices.DECLINE
)
public_profile_agree = get_option(
TermEnum.PUBLIC_PROFILE, TermOptionChoices.AGREE
)
public_profile_decline = get_option(
TermEnum.PUBLIC_PROFILE, TermOptionChoices.DECLINE
)
may_contact_decline = get_option(TermEnum.MAY_CONTACT, TermOptionChoices.DECLINE)
public_profile_agree = get_option(TermEnum.PUBLIC_PROFILE, TermOptionChoices.AGREE)
public_profile_decline = get_option(TermEnum.PUBLIC_PROFILE, TermOptionChoices.DECLINE)

old_consents = (
Consent.objects.filter(
Expand Down Expand Up @@ -96,9 +82,7 @@ def get_consent(term_slug: TermEnum, person: Person) -> Consent:
def test_person_filter(self):
def assert_data(data: Dict[str, Any], expected_people) -> None:
usernames = [person_data["username"] for person_data in data["results"]]
self.assertCountEqual(
usernames, [person.username for person in expected_people]
)
self.assertCountEqual(usernames, [person.username for person in expected_people])

person_api_url = reverse("api-v1:person-list")

Expand All @@ -121,12 +105,8 @@ def assert_data(data: Dict[str, Any], expected_people) -> None:
assert_data(people_response.data, [self.admin_1])

# combined
people_response = self.client.get(
f"{person_api_url}?&may_contact=false&publish_profile=true"
)
people_response = self.client.get(f"{person_api_url}?&may_contact=false&publish_profile=true")
assert_data(people_response.data, [])

people_response = self.client.get(
f"{person_api_url}?&may_contact=true&publish_profile=true"
)
people_response = self.client.get(f"{person_api_url}?&may_contact=true&publish_profile=true")
assert_data(people_response.data, [self.admin_1])
Loading

0 comments on commit b1a6441

Please sign in to comment.