Coverage for memberships/resources.py: 70%
46 statements
« prev ^ index » next coverage.py v6.4.4, created at 2023-06-07 11:59 -0600
« prev ^ index » next coverage.py v6.4.4, created at 2023-06-07 11:59 -0600
1from import_export import resources, fields
2from logs.models import Log
3from .models import Membership, Affiliation
6class MembershipResource(resources.ModelResource):
7 last_user_with_action = fields.Field()
8 last_log_action = fields.Field()
9 last_log_date = fields.Field()
11 class Meta:
12 model = Membership
13 fields = [
14 "name",
15 "is_active",
16 "segment",
17 "last_user_with_action",
18 "last_log_action",
19 "last_log_date",
20 "created_at",
21 ]
22 export_order = ["name", "segment", "is_active", "last_user_with_action", "last_log_action", "last_log_date"]
24 def dehydrate_segment(self, membership):
25 return membership.segment.name if membership.segment else ""
27 def dehydrate_is_active(self, membership):
28 return "Si" if membership.is_active else "No"
30 def dehydrate_last_user_with_action(self, membership):
31 log = Log.objects.filter(user__in=membership.users.all()).first()
32 return f"{log.user.email}" if log else ""
34 def dehydrate_last_log_action(self, membership):
35 log = Log.objects.filter(user__in=membership.users.all()).first()
36 return f"{log.get_action_display()} - {log.content_type}" if log else ""
38 def dehydrate_last_log_date(self, membership):
39 log = Log.objects.filter(user__in=membership.users.all()).first()
40 return log.created_at.date() if log else ""
43class AffiliationResource(resources.ModelResource):
44 segment = fields.Field(attribute="membership__segment")
45 last_log_action = fields.Field()
46 last_log_date = fields.Field()
48 class Meta:
49 model = Affiliation
50 fields = ["membership", "name", "is_active", "segment", "user", "last_log_action", "last_log_date"]
51 export_order = [
52 "membership",
53 "name",
54 "segment",
55 "is_active",
56 "user",
57 "last_log_action",
58 "last_log_date",
59 ]
61 def dehydrate_segment(self, affiliation):
62 return affiliation.membership.segment.name if affiliation.membership.segment else ""
64 def dehydrate_is_active(self, affiliation):
65 return "Si" if affiliation.is_active else "No"
67 def dehydrate_user(self, affiliation):
68 return affiliation.user.email
70 def dehydrate_membership(self, affiliation):
71 return affiliation.membership.name
73 def dehydrate_last_log_action(self, affiliation):
74 log = Log.objects.filter(user=affiliation.user).first()
75 return f"{log.get_action_display()} - {log.content_type}" if log else ""
77 def dehydrate_last_log_date(self, affiliation):
78 log = Log.objects.filter(user=affiliation.user).first()
79 return log.created_at.date() if log else ""