class MemberOrganisationCustomMixin: """ Mixin for MemberOrganisationAdmin and MemberOrganisationUserCustomAdmin """ def get_queryset(self, *args, **kwargs): return super().get_queryset(*args, **kwargs).select_related('country', ) actions = (export_as_csv_action('CSV Export', fields=[ 'gs1_cloud_username', 'country__name', ], all_objects=True), ) list_display = ( 'country_name', 'gs1_cloud_username', ) @staticmethod def country_name(mo): return mo.country.name search_fields = ('gs1_cloud_username', ) list_filter = (('country', RelatedDropdownFilter), )
class CompanyOrganisationCustomMixin(RightSideLinksAdminMixin): actions = ( export_as_csv_action( 'CSV Export', fields=[ 'id', 'company', 'uuid', 'name', 'member_organisation_id', ], all_objects=True ), ) list_display_links = ( 'uuid', 'company', 'member_organisation' ) list_display = ( 'uuid', 'name', 'company', 'member_organisation', ) search_fields = ( 'company', ) list_filter = ( ('member_organisation', RelatedDropdownFilter), ) right_side_links = [ RightSideAdminLinkInfo( name='users', url='auth/user/?q={obj.uuid}' ), RightSideAdminLinkInfo( name='products', url='products/product/?q={obj.uuid}' ), RightSideAdminLinkInfo( name='prefixes', url='prefixes/prefix/?q={obj.uuid}' ), RightSideAdminLinkInfo( name='impersonate as', url='company_organisations/companyorganisationuser/?q={obj.uuid}' ), ]
class ProfileCustomMixin: """ Mixin for ProfileModelAdmin """ def get_queryset(self, *args, **kwargs): return super().get_queryset(*args, **kwargs).select_related( 'user', 'company_organisation') actions = (export_as_csv_action('CSV Export', fields=[ 'uid', 'user__username', 'user__last_name', 'user__email', 'company_organisation', ], all_objects=True), ) list_display = ( 'username', 'last_name', 'email', 'company_organisation', ) @staticmethod def username(profile): return profile.user.username @staticmethod def email(profile): return profile.user.email @staticmethod def last_name(profile): return profile.user.last_name search_fields = ( 'user__username', 'user__email', 'user__last_name', ) list_filter = (('company_organisation', RelatedDropdownFilter), )
class ProductCustomMixin(RightSideLinksAdminMixin): actions = ( export_as_csv_action( 'CSV Export', fields=[ 'gtin', 'owner', 'description_i18n', 'created', ], all_objects=True, ), gs1_cloud_draft_action('Force change GS1 cloud state to "DRAFT"', 'DRAFT'), gs1_cloud_reactivate_action('Force reactivate products in GS1 cloud'), ) list_display = ( 'gtin', 'owner', 'description_i18n', 'gs1_cloud_state', 'created', ) search_fields = ( 'gtin', 'owner__email', 'company_organisation__uuid', ) list_filter = (('owner', RelatedDropdownFilter), ) right_side_links = [ RightSideAdminLinkInfo(name='owner', url='auth/user/{obj.owner_id}/change/'), RightSideAdminLinkInfo(name='organization', url='company_organisations/companyorganisation/' '{obj.company_organisation_id}/change'), RightSideAdminLinkInfo(name='gs1 logs', url='audit/cloudlog/?q={obj.gtin}'), ]
class UserCustomMixin(RightSideLinksAdminMixin): def get_actions(self, request): actions = super().get_actions(request) if 'delete_selected' in actions: del actions['delete_selected'] return actions def get_queryset(self, *args, **kwargs): return super().get_queryset(*args, **kwargs).select_related( 'profile__company_organisation', 'profile', 'profile__member_organisation', ) actions = (export_as_csv_action('CSV Export', fields=[ 'id', 'username', 'last_name', 'email', 'profile__company_organisation', ], all_objects=True), ) list_display = ( 'username', 'last_name', 'email', 'organisation', ) @staticmethod def organisation(user): return str(user.profile.company_organisation) search_fields = ( 'username', 'email', 'company_organisations_companyorganisation__uuid', ) list_filter = (('profile__company_organisation__member_organisation', RelatedDropdownFilter), ) readonly_fields = [ 'prefixes', ] def get_fields(self, request, obj=None): if not getattr(self, 'url_prefix', False): return None return [ 'prefixes', ] + list(super().get_fields(request, obj)) def get_readonly_fields(self, request, obj=None): if not getattr(self, 'url_prefix', False): return super().get_readonly_fields(request, obj) return [ 'prefixes', ] + list(super().get_readonly_fields(request, obj)) fieldsets = ( (None, { 'fields': ('username', 'password') }), ('Prefixes', { 'fields': readonly_fields }), (_('Personal info'), { 'fields': ('first_name', 'last_name', 'email') }), (_('Permissions'), { 'fields': ('is_active', 'is_staff', 'is_superuser', 'groups', 'user_permissions') }), (_('Important dates'), { 'fields': ('last_login', 'date_joined') }), ) @staticmethod def prefixes(user): prefixes = user.profile.company_organisation.prefix_set.all() return ','.join(pr.prefix for pr in prefixes) right_side_links = [ RightSideAdminLinkInfo(name='logs', url='audit/log/?q={obj.username}'), RightSideAdminLinkInfo(name='impersonate as', url='company_organisations/' 'companyorganisationuser/?q={obj.username}'), ]
class CompanyOrganisationUserCustomMixin(RightSideLinksAdminMixin): """ Mixin for CompanyOrganisationAdmin and CompanyOrganisationCustomAdmin """ def get_queryset(self, *args, **kwargs): return super().get_queryset(*args, **kwargs).select_related( 'user', 'organization' ) actions = ( export_as_csv_action( 'CSV Export', fields=[ 'user__username', 'user__last_name', 'user__email', 'organization__company', ], all_objects=True ), ) list_display = ( 'username', 'last_name', 'email', 'company', ) @staticmethod def company(obj): return obj.organization.company @staticmethod def username(obj): return obj.user.username @staticmethod def email(obj): return obj.user.email @staticmethod def last_name(obj): return obj.user.last_name search_fields = ( 'user__username', 'user__email', 'user__last_name', 'organization__company', 'organization__uuid', ) list_filter = ( ('organization__member_organisation', RelatedDropdownFilter), ) right_side_links = [ RightSideAdminLinkInfo( name='owner', url='auth/user/{obj.user_id}/change/' ), RightSideAdminLinkInfo( name='organization', url='company_organisations/companyorganisation/' '{obj.organization_id}/change' ), ]