|
| 1 | +from import_export import resources |
| 2 | +from import_export.fields import Field |
| 3 | +from django.utils.translation import ugettext_lazy as _ |
| 4 | + |
| 5 | +from user.models import UserProfile |
| 6 | +from django.contrib.auth.models import User |
| 7 | + |
| 8 | + |
| 9 | +class UserResource(resources.ModelResource): # pragma: no cover |
| 10 | + """ |
| 11 | + This class is basically a serializer for the django-import-export module |
| 12 | + Fields: |
| 13 | + - Stats: |
| 14 | + - last login |
| 15 | + - date joined |
| 16 | + - ODK Synced |
| 17 | + - Permissions: |
| 18 | + - Active |
| 19 | + - Staff Status |
| 20 | + - Superuser Status |
| 21 | + - Global portfolio owner |
| 22 | + - User Info |
| 23 | + - email address |
| 24 | + - name |
| 25 | + - account type |
| 26 | + - Organization |
| 27 | + - Country |
| 28 | + - Donor |
| 29 | + - Language |
| 30 | + """ |
| 31 | + # Stats |
| 32 | + last_login = Field(column_name=_('Last login date')) |
| 33 | + date_joined = Field(column_name=_('Date joined')) |
| 34 | + is_odk_synced = Field(column_name=_('Synced to ODK?')) |
| 35 | + # Permissions |
| 36 | + is_active = Field(column_name=_('Active?')) |
| 37 | + is_staff = Field(column_name=_('Staff member?')) |
| 38 | + is_superuser = Field(column_name=_('Is superuser?')) |
| 39 | + is_gpo = Field(column_name=_('Is Global Portfolio owner?')) |
| 40 | + # User Info |
| 41 | + email = Field(column_name=_('Email address')) |
| 42 | + name = Field(column_name=_('Name')) |
| 43 | + account_type = Field(column_name=_('Account type')) |
| 44 | + organization = Field(column_name=_('Organization')) |
| 45 | + country = Field(column_name=_('Country')) |
| 46 | + donor = Field(column_name=_('Donor')) |
| 47 | + language = Field(column_name=_('Language')) |
| 48 | + |
| 49 | + class Meta: |
| 50 | + model = User |
| 51 | + fields = ('id', 'name', 'email', 'account_type', 'organization', 'country', 'donor', 'groups', 'language', |
| 52 | + 'last_login', 'date_joined', 'is_odk_synced', 'is_active', 'is_staff', 'is_superuser', 'is_gpo') |
| 53 | + export_order = ('id', 'name', 'email', 'account_type', 'organization', 'country', 'donor', 'groups', 'language', |
| 54 | + 'last_login', 'date_joined', 'is_odk_synced', 'is_active', 'is_staff', 'is_superuser', 'is_gpo') |
| 55 | + |
| 56 | + def dehydrate_last_login(self, user: User): |
| 57 | + return user.last_login.date() |
| 58 | + |
| 59 | + def dehydrate_date_joined(self, user: User): |
| 60 | + return user.date_joined.date() |
| 61 | + |
| 62 | + def dehydrate_is_odk_synced(self, user: User): |
| 63 | + return user.userprofile.odk_sync |
| 64 | + |
| 65 | + def dehydrate_is_active(self, user: User): |
| 66 | + return user.is_active |
| 67 | + |
| 68 | + def dehydrate_is_staff(self, user: User): |
| 69 | + return user.is_staff |
| 70 | + |
| 71 | + def dehydrate_is_superuser(self, user: User): |
| 72 | + return user.is_superuser |
| 73 | + |
| 74 | + def dehydrate_is_gpo(self, user: User): |
| 75 | + return user.userprofile.global_portfolio_owner |
| 76 | + |
| 77 | + def dehydrate_name(self, user: User): |
| 78 | + return user.userprofile.name |
| 79 | + |
| 80 | + def dehydrate_email(self, user: User): |
| 81 | + return user.email |
| 82 | + |
| 83 | + def dehydrate_account_type(self, user: User): |
| 84 | + account_types = {x[0]: x[1] for x in UserProfile.ACCOUNT_TYPE_CHOICES} |
| 85 | + return account_types[user.userprofile.account_type] |
| 86 | + |
| 87 | + def dehydrate_organization(self, user: User): |
| 88 | + return user.userprofile.organisation.name if user.userprofile.organisation else 'None' |
| 89 | + |
| 90 | + def dehydrate_country(self, user: User): |
| 91 | + return user.userprofile.country.name if user.userprofile.country else 'None' |
| 92 | + |
| 93 | + def dehydrate_donor(self, user: User): |
| 94 | + return user.userprofile.donor.name if user.userprofile.donor else 'None' |
| 95 | + |
| 96 | + def dehydrate_language(self, user: User): |
| 97 | + return user.userprofile.language |
0 commit comments