def _get_user_document_count(self, user): action_queryset = Action.objects.annotate( target_object_id_int=Cast( 'target_object_id', output_field=IntegerField() ), ) action_filter_kwargs = { 'verb': event_document_create.id } document_filter_kwargs = {} if not self.document_type_all: document_filter_kwargs.update( { 'document_type_id__in': self._get_document_types().values( 'pk' ), } ) if user: # Admins are always excluded if user.is_superuser or user.is_staff: return 0 if not self.user_all: users = self._get_users() | get_user_queryset().filter( groups__in=self._get_groups() ) if not users.filter(pk=user.pk).exists(): # User is not in the restricted list of users and groups return 0 else: content_type = ContentType.objects.get_for_model( model=get_user_model() ) action_filter_kwargs.update( { 'actor_object_id': user.pk, 'actor_content_type': content_type, } ) action_queryset = action_queryset.filter(**action_filter_kwargs) document_filter_kwargs.update( { 'pk__in': action_queryset.values('target_object_id_int') } ) return Document.objects.filter(**document_filter_kwargs).count()
def __init__(self, *args, **kwargs): user = kwargs.pop('user') super(UserListForm, self).__init__(*args, **kwargs) queryset = get_user_queryset().exclude(pk=user.pk) self.fields['user'] = forms.ModelChoiceField( label=_('User'), help_text=_('User to be impersonated.'), queryset=queryset ) self.fields['permanent'] = forms.BooleanField( label=_('Permanent'), help_text=_( 'If selected, disables ending impersonation.' ), required=False )
def process(self, **kwargs): if not kwargs['instance'].pk: if kwargs['instance'].file.size >= self._allowed(): if self.document_type_all or self._get_document_types().filter(pk=kwargs['instance'].document.document_type.pk).exists(): # Don't asume there is always a user in the signal. # Non interactive uploads might not include a user. if kwargs['user']: if kwargs['user'].is_superuser or kwargs['user'].is_staff: return users = self._get_users() | get_user_queryset().filter( groups__in=self._get_groups() ) if self.user_all or kwargs['user'] and users.filter(pk=kwargs['user'].pk).exists(): raise QuotaExceeded( _('Document size quota exceeded.') )
def get_fields(cls): cls.fields.update( { 'group_ids': { 'label': _('Groups'), 'class': 'mayan.apps.views.fields.FilteredModelMultipleChoiceField', 'kwargs': { 'permission': permission_quota_edit, 'required': False, 'source_model': Group }, 'help_text': _( 'Groups to which the quota will be applied.' ) }, 'user_all': { 'label': _('All users'), 'class': 'django.forms.BooleanField', 'default': False, 'help_text': _( 'Apply the quota to all users in the system, ' 'excluding admins and staff. ' 'Enabling this option, the quota will ignore the ' 'indiviual user and group selection.' ), 'required': False, }, 'user_ids': { 'label': _('Users'), 'class': 'mayan.apps.views.fields.FilteredModelMultipleChoiceField', 'kwargs': { 'permission': permission_quota_edit, 'required': False, 'source_queryset': get_user_queryset() }, 'help_text': _( 'Users to which the quota will be applied.' ) } } ) cls.field_order = list(cls.field_order) cls.field_order.insert(0, 'user_ids') cls.field_order.insert(0, 'group_ids') cls.field_order.insert(0, 'user_all') return super(GroupsUsersQuotaMixin, cls).get_fields()
def get_user(pk, request): return get_user_queryset().exclude(pk=request.user.pk).get(pk=pk)
def _get_users(self): queryset = get_user_queryset() return queryset.filter(pk__in=self.user_ids)