<commit_before>from django.core.urlresolvers import reverse
from smartmin.views import SmartCRUDL, SmartListView, SmartUpdateView, SmartCreateView
from dash.dashblocks.models import DashBlockType, DashBlock, DashBlockImage
from dash.orgs.views import OrgObjPermsMixin, OrgPermsMixin
from django.utils.translation import ugettext_lazy as _

class DashBlockTypeCRUDL(SmartCRUDL):
    model = DashBlockType
    actions = ('create', 'update', 'list')

    class List(SmartListView):
        title = _("Content Types")
        fields = ('name', 'slug', 'description')
        link_fields = ('name',)


class DashBlockFormMixin(object):

    def get_type(self):
        if 'type' in self.request.REQUEST:
            return DashBlockType.objects.get(id=self.request.REQUEST.get('type'))
        return None

    def get_context_data(self, *args, **kwargs):
        context = super(DashBlockFormMixin, self).get_context_data(*args, **kwargs)
        context['type'] = self.get_type()
        return context

    def get_success_url(self):
        return "%s?type=%d" % (reverse('dashblocks.dashblock_list'), self.object.dashblock_type.id)

    def derive_exclude(self):
        exclude = super(DashBlockFormMixin, self).derive_exclude()

        block_type = self.get_type()
        if block_type:
            if not self.request.user.has_perm(self.permission):
                exclude.append('dashblock_type')

            if not block_type.has_summary:
                exclude.append('summary')

            if not block_type.has_video:
                exclude.append('video_id')

            if not block_type.has_title:
                exclude.append('title')

            if not block_type.has_tags:
                exclude.append('tags')

            if not block_type.has_image:
                exclude.append('image')

            if not block_type.has_link:
                exclude.append('link')

            if not block_type.has_color:
                exclude.append('color')

        # if this user does not have global permissins, remove org as a field
        if not self.request.user.has_perm(self.permission):
            exclude.append('org')

        return exclude

    def pre_save(self, obj):
        obj = super(DashBlockFormMixin, self).pre_save(obj)

        block_type = self.get_type()
        if block_type:
            obj.dashblock_type = block_type

        # if the user doesn't have global permissions, set the org appropriately
        if not self.request.user.has_perm(self.permission):
            obj.org = self.request.org

        obj.space_tags()
        return obj
class DashBlockCRUDL(SmartCRUDL):
    model = DashBlock
    permissions = True
    actions = ('create', 'update', 'list')

    class List(OrgPermsMixin, SmartListView):
        fields = ('title', 'priority', 'dashblock_type', 'tags')
        link_fields = ('title',)
        default_order = '-modified_on'
        search_fields = ('title__icontains', 'content__icontains', 'summary__icontains')

        def derive_fields(self):
            fields = super(DashBlockCRUDL.List, self).derive_exclude()

            block_type = self.get_type()
            if block_type:
                if not block_type.has_tags:
                    return ('title', 'priority', 'dashblock_type')

            return ('title', 'priority', 'dashblock_type', 'tags')

        def get_title(self, obj):
            block_type = self.get_type()
            if block_type:
                if not block_type.has_title:
                    return unicode(obj)
            return obj.title

        def derive_title(self):
            type = self.get_type()
            if not type:
                return _("Content Blocks")
            else:<commit_msg>Re-implement derive_fields in dashblock list<commit_after>from django.core.urlresolvers import reverse
from smartmin.views import SmartCRUDL, SmartListView, SmartUpdateView, SmartCreateView
from dash.dashblocks.models import DashBlockType, DashBlock, DashBlockImage
from dash.orgs.views import OrgObjPermsMixin, OrgPermsMixin
from django.utils.translation import ugettext_lazy as _

class DashBlockTypeCRUDL(SmartCRUDL):
    model = DashBlockType
    actions = ('create', 'update', 'list')

    class List(SmartListView):
        title = _("Content Types")
        fields = ('name', 'slug', 'description')
        link_fields = ('name',)


class DashBlockFormMixin(object):

    def get_type(self):
        if 'type' in self.request.REQUEST:
            return DashBlockType.objects.get(id=self.request.REQUEST.get('type'))
        return None

    def get_context_data(self, *args, **kwargs):
        context = super(DashBlockFormMixin, self).get_context_data(*args, **kwargs)
        context['type'] = self.get_type()
        return context

    def get_success_url(self):
        return "%s?type=%d" % (reverse('dashblocks.dashblock_list'), self.object.dashblock_type.id)

    def derive_exclude(self):
        exclude = super(DashBlockFormMixin, self).derive_exclude()

        block_type = self.get_type()
        if block_type:
            if not self.request.user.has_perm(self.permission):
                exclude.append('dashblock_type')

            if not block_type.has_summary:
                exclude.append('summary')

            if not block_type.has_video:
                exclude.append('video_id')

            if not block_type.has_title:
                exclude.append('title')

            if not block_type.has_tags:
                exclude.append('tags')

            if not block_type.has_image:
                exclude.append('image')

            if not block_type.has_link:
                exclude.append('link')

            if not block_type.has_color:
                exclude.append('color')

        # if this user does not have global permissins, remove org as a field
        if not self.request.user.has_perm(self.permission):
            exclude.append('org')

        return exclude

    def pre_save(self, obj):
        obj = super(DashBlockFormMixin, self).pre_save(obj)

        block_type = self.get_type()
        if block_type:
            obj.dashblock_type = block_type

        # if the user doesn't have global permissions, set the org appropriately
        if not self.request.user.has_perm(self.permission):
            obj.org = self.request.org

        obj.space_tags()
        return obj
class DashBlockCRUDL(SmartCRUDL):
    model = DashBlock
    permissions = True
    actions = ('create', 'update', 'list')

    class List(OrgPermsMixin, SmartListView):
        fields = ('title', 'priority', 'dashblock_type', 'tags')
        link_fields = ('title',)
        default_order = '-modified_on'
        search_fields = ('title__icontains', 'content__icontains', 'summary__icontains')

        def derive_fields(self):
            fields = super(DashBlockCRUDL.List, self).derive_fields()
            block_type = self.get_type()
            if block_type:
                if not block_type.has_tags:
                    fields = type(fields)(x for x in fields if x != 'tags')
            return fields

        def get_title(self, obj):
            block_type = self.get_type()
            if block_type:
                if not block_type.has_title:
                    return unicode(obj)
            return obj.title

        def derive_title(self):
            type = self.get_type()
            if not type:
                return _("Content Blocks")
            else:
{'fraction_non_alphanumeric': 0.09940971290582237, 'fraction_numerical': 0.0, 'mean_word_length': 2.310390763765542, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '7625216', 'n_tokens_mistral': 2179, 'n_tokens_neox': 2068, 'n_words': 483}