Skip to content

Commit

Permalink
Add recently Used filter in AnalysisFramework
Browse files Browse the repository at this point in the history
  • Loading branch information
RobertKoikai authored and thenav56 committed Nov 6, 2023
1 parent 30ec85f commit 2fff12b
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 3 deletions.
19 changes: 18 additions & 1 deletion apps/analysis_framework/filter_set.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,13 @@
from .models import (
AnalysisFramework,
)
from entry.models import Entry
from django.utils import timezone
from datetime import timedelta


class AnalysisFrameworkFilterSet(UserResourceFilterSet):

class Meta:
model = AnalysisFramework
fields = ('id', 'title', 'description', 'created_at',)
Expand All @@ -24,12 +28,17 @@ class Meta:
},
}


# ----------------------------- Graphql Filters ---------------------------------------


class AnalysisFrameworkGqFilterSet(UserResourceGqlFilterSet):
search = django_filters.CharFilter(method='search_filter')
is_current_user_member = django_filters.BooleanFilter(
field_name='is_current_user_member', method='filter_with_membership')
recently_used = django_filters.BooleanFilter(
method='filter_recently_used',
label='Recently Used',
)

class Meta:
model = AnalysisFramework
Expand All @@ -50,3 +59,11 @@ def filter_with_membership(self, queryset, _, value):
afs = afs.exclude(filter_query)
return queryset.filter(id__in=afs)
return queryset

def filter_recently_used(self, queryset, name, value):
if value:
# Calculate the date for "recent" usage (e.g., within the last 6 months or 180 days)
recent_usage_cutoff = timezone.now() - timedelta(days=180)
entries_qs = Entry.objects.filter(modified_at__gte=recent_usage_cutoff)
return queryset.filter(id__in=entries_qs.values('analysis_framework'))
return queryset
36 changes: 36 additions & 0 deletions apps/analysis_framework/tests/test_filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

from analysis_framework.filter_set import AnalysisFrameworkGqFilterSet
from analysis_framework.factories import AnalysisFrameworkFactory
from entry.factories import EntryFactory
from lead.factories import LeadFactory

from django.utils import timezone
from datetime import timedelta


class TestAnalysisFrameworkFilter(GraphQLTestCase):
Expand All @@ -21,3 +26,34 @@ def test_search_filter(self):
expected,
obtained
)

def test_filter_recently_used(self):
af1, af2, af3, _ = AnalysisFrameworkFactory.create_batch(4)
lead1, lead2 = LeadFactory.create_batch(2)
now = timezone.now()
# Within date range window
EntryFactory.create(
analysis_framework=af1,
lead=lead1,
modified_at=now - timedelta(days=90),
)
EntryFactory.create(
analysis_framework=af2,
lead=lead2,
modified_at=now - timedelta(days=30),
)
# Outside date range window
EntryFactory.create(
analysis_framework=af3,
lead=lead1,
modified_at=now - timedelta(days=190),
)
# Make sure we only get af1, af2
obtained = set(list(
self
.filter_class(data={'recently_used': True})
.qs
.values_list('id', flat=True)
))
expected = set([af1.pk, af2.pk])
self.assertEqual(obtained, expected)
4 changes: 2 additions & 2 deletions schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -2842,8 +2842,8 @@ type Query {
user(id: ID!): UserType
users(id: Float, search: String, membersExcludeProject: ID, membersExcludeFramework: ID, membersExcludeUsergroup: ID, page: Int = 1, ordering: String, pageSize: Int): UserListType
analysisFramework(id: ID!): AnalysisFrameworkDetailType
analysisFrameworks(id: Float, createdAt: DateTime, createdAtGte: DateTime, createdAtLte: DateTime, modifiedAt: DateTime, modifiedAtGte: DateTime, modifiedAtLte: DateTime, createdBy: [ID!], modifiedBy: [ID!], search: String, isCurrentUserMember: Boolean, page: Int = 1, ordering: String, pageSize: Int): AnalysisFrameworkListType
publicAnalysisFrameworks(id: Float, createdAt: DateTime, createdAtGte: DateTime, createdAtLte: DateTime, modifiedAt: DateTime, modifiedAtGte: DateTime, modifiedAtLte: DateTime, createdBy: [ID!], modifiedBy: [ID!], search: String, isCurrentUserMember: Boolean, page: Int = 1, ordering: String, pageSize: Int): PublicAnalysisFrameworkListType
analysisFrameworks(id: Float, createdAt: DateTime, createdAtGte: DateTime, createdAtLte: DateTime, modifiedAt: DateTime, modifiedAtGte: DateTime, modifiedAtLte: DateTime, createdBy: [ID!], modifiedBy: [ID!], search: String, isCurrentUserMember: Boolean, recentlyUsed: Boolean, page: Int = 1, ordering: String, pageSize: Int): AnalysisFrameworkListType
publicAnalysisFrameworks(id: Float, createdAt: DateTime, createdAtGte: DateTime, createdAtLte: DateTime, modifiedAt: DateTime, modifiedAtGte: DateTime, modifiedAtLte: DateTime, createdBy: [ID!], modifiedBy: [ID!], search: String, isCurrentUserMember: Boolean, recentlyUsed: Boolean, page: Int = 1, ordering: String, pageSize: Int): PublicAnalysisFrameworkListType
project(id: ID!): ProjectDetailType
projects(createdAt: DateTime, createdAtGte: DateTime, createdAtLte: DateTime, modifiedAt: DateTime, modifiedAtGte: DateTime, modifiedAtLte: DateTime, createdBy: [ID!], modifiedBy: [ID!], ids: [ID!], excludeIds: [ID!], status: ProjectStatusEnum, organizations: [ID!], analysisFrameworks: [ID!], regions: [ID!], search: String, isCurrentUserMember: Boolean, hasPermissionAccess: ProjectPermission, ordering: [ProjectOrderingEnum!], isTest: Boolean, page: Int = 1, pageSize: Int): ProjectListType
recentProjects: [ProjectDetailType!]
Expand Down

0 comments on commit 2fff12b

Please sign in to comment.