<commit_before># -*- coding: utf-8 -*-
#
# inventory/common/admin_mixins.py
#

"""
Mixins used in the Django admin.

by: <NAME>

email: <EMAIL>
"""
__docformat__ = "restructuredtext en"

from django.contrib.admin.filters import SimpleListFilter
from django.utils.translation import ugettext_lazy as _


class UserAdminMixin(object):
    """
    Admin mixin that should be used in any model implimented with dynamic
    columns.
    """
    def save_model(self, request, obj, form, change):
        """
        When saving a record from the admin the `creator` should be updated
        with the request user object if `change` is `False`. The `updater` is
        always updated withthe request user object.

        :Parameters:
          request : HttpRequest
            Django request object.
          obj : Model
            Django model object
          form : Form
            Django form object.
          change : bool
            If `True` the record was updated, if `False` the record was created.
        """
        if change is False:
            obj.creator = request.user

        obj.updater = request.user
        super(UserAdminMixin, self).save_model(request, obj, form, change)


#
# CreatorFilter
#
class CreatorFilter(SimpleListFilter):
    # Human-readable title to appear in the right sidebar.
    title = _("Creator")
    # The parameter that should be used in the query string for that filter.
    parameter_name = 'creator'

    def lookups(self, request, model_admin):
        """
        Must be overridden to return a list of tuples (value, verbose value)
        """
        return set([(obj.creator.id, obj.creator_producer())
                    for obj in model_admin.model.objects.all()])

    def queryset(self, request, queryset):<commit_msg>Added a mixin for setting the creator and updater in inlines for the admin.<commit_after># -*- coding: utf-8 -*-
#
# inventory/common/admin_mixins.py
#

"""
Mixins used in the Django admin.

by: <NAME>

email: <EMAIL>
"""
__docformat__ = "restructuredtext en"

from django.contrib.admin.filters import SimpleListFilter
from django.utils.translation import ugettext_lazy as _


class UserAdminMixin(object):
    """
    This mixin should be placed in the MRO of an admin class.
    """
    def save_model(self, request, obj, form, change):
        """
        When saving a record from the admin the `creator` should be updated
        with the request user object if `change` is `False`. The `updater` is
        always updated withthe request user object.

        :Parameters:
          request : HttpRequest
            Django request object.
          obj : Model
            Django model object
          form : Form
            Django form object.
          change : bool
            If `True` the record was updated, if `False` the record was created.
        """
        if change is False:
            obj.creator = request.user

        obj.updater = request.user
        super(UserAdminMixin, self).save_model(request, obj, form, change)


class UserInlineAdminMixin(object):
    """
    This mixin shopuld be placed in the MRO of the parent admin class not
    the inline classes.
    """

    def save_formset(self, request, form, formset, change):
        for i_form in formset.forms:
            if not change or i_form.instance.pk is None:
                i_form.instance.creator = request.user

            i_form.instance.updater = request.user

        super(UserInlineAdminMixin, self).save_formset(
            request, form, formset, change)


#
# CreatorFilter
#
class CreatorFilter(SimpleListFilter):
    # Human-readable title to appear in the right sidebar.
    title = _("Creator")
    # The parameter that should be used in the query string for that filter.
    parameter_name = 'creator'

    def lookups(self, request, model_admin):
        """
        Must be overridden to return a list of tuples (value, verbose value)
        """
        return set([(obj.creator.id, obj.creator_producer())
                    for obj in model_admin.model.objects.all()])

    def queryset(self, request, queryset):
{'fraction_non_alphanumeric': 0.08604764219737482, 'fraction_numerical': 0.0004861448711716091, 'mean_word_length': 2.440635451505017, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7226767', 'n_tokens_mistral': 1161, 'n_tokens_neox': 1134, 'n_words': 407}