<commit_before>import logging

from django.db.models import Min, Q

from river.models import FORWARD
from river.models.proceeding import Proceeding, PENDING
from river.models.proceeding_meta import ProceedingMeta
from river.models.state import State
from river.services.config import RiverConfig
from river.services.state import StateService

__author__ = 'ahmetdal'

LOGGER = logging.getLogger(__name__)


class ProceedingService(object):
    @staticmethod
    def init_proceedings(workflow_object, field):
        content_type = RiverConfig.CONTENT_TYPE_CLASS.objects.get_for_model(workflow_object)
        for proceeding_meta in ProceedingMeta.objects.filter(transition__content_type=content_type, transition__field=field):
            proceeding, created = Proceeding.objects.update_or_create(
                meta=proceeding_meta,
                workflow_object=workflow_object,
                field=field,
                defaults={
                    'order': proceeding_meta.order,
                    'status': PENDING,
                }
            )
            proceeding.permissions.add(*proceeding_meta.permissions.all())
            proceeding.groups.add(*proceeding_meta.groups.all())

        init_state = StateService.get_initial_state(content_type, field)
        setattr(workflow_object, field, init_state)
        workflow_object.save()
        LOGGER.debug("Proceedings are initialized for workflow object %s and field %s" % (workflow_object, field))

    @staticmethod
    def get_available_proceedings(workflow_object, field, source_states, user=None, destination_state=None, god_mod=False):

        def get_proceeding(proceedings):
            min_order = proceedings.aggregate(Min('order'))['order__min']
            proceedings = proceedings.filter(order=min_order)

            if destination_state:
                proceedings = proceedings.filter(meta__transition__destination_state=destination_state)

            return proceedings

        def authorize_proceedings(proceedings):
            group_q = Q()
            for g in user.groups.all():
                group_q = group_q | Q(groups__in=[g])

            permission_q = Q()
            for p in user.user_permissions.all():
                permission_q = permission_q | Q(permissions__in=[p])

            return proceedings.filter(
                (
                    (Q(transactioner__isnull=True) | Q(transactioner=user)) &
                    (Q(permissions__isnull=True, groups__isnull=True) | permission_q | group_q)
                )
            )

        proceedings = Proceeding.objects.filter(
            workflow_object=workflow_object,
            field=field,
            meta__transition__source_state__in=source_states,
            status=PENDING,
            enabled=True
        )

        suitable_proceedings = get_proceeding(proceedings.filter(skip=False))<commit_msg>On finding proceedings, user.userpermissions was being used. Permissions are now retrieved from auth backend instead.<commit_after>import logging

from django.db.models import Min, Q

from river.models import FORWARD
from river.models.proceeding import Proceeding, PENDING
from river.models.proceeding_meta import ProceedingMeta
from river.models.state import State
from river.services.config import RiverConfig
from river.services.state import StateService
from django.contrib import auth

__author__ = 'ahmetdal'

LOGGER = logging.getLogger(__name__)


class ProceedingService(object):
    @staticmethod
    def init_proceedings(workflow_object, field):
        content_type = RiverConfig.CONTENT_TYPE_CLASS.objects.get_for_model(workflow_object)
        for proceeding_meta in ProceedingMeta.objects.filter(transition__content_type=content_type, transition__field=field):
            proceeding, created = Proceeding.objects.update_or_create(
                meta=proceeding_meta,
                workflow_object=workflow_object,
                field=field,
                defaults={
                    'order': proceeding_meta.order,
                    'status': PENDING,
                }
            )
            proceeding.permissions.add(*proceeding_meta.permissions.all())
            proceeding.groups.add(*proceeding_meta.groups.all())

        init_state = StateService.get_initial_state(content_type, field)
        setattr(workflow_object, field, init_state)
        workflow_object.save()
        LOGGER.debug("Proceedings are initialized for workflow object %s and field %s" % (workflow_object, field))

    @staticmethod
    def get_available_proceedings(workflow_object, field, source_states, user=None, destination_state=None, god_mod=False):

        def get_proceeding(proceedings):
            min_order = proceedings.aggregate(Min('order'))['order__min']
            proceedings = proceedings.filter(order=min_order)

            if destination_state:
                proceedings = proceedings.filter(meta__transition__destination_state=destination_state)

            return proceedings

        def authorize_proceedings(proceedings):
            group_q = Q()
            for g in user.groups.all():
                group_q = group_q | Q(groups__in=[g])

            permissions = []
            for backend in auth.get_backends():
                permissions.extend(backend.get_all_permissions(user))

            permission_q = Q()
            for p in permissions:
                label, codename = p.split('.')
                permission_q = permission_q | Q(permissions__content_type__app_label=label, permissions__codename=codename)

            return proceedings.filter(
                (
                    (Q(transactioner__isnull=True) | Q(transactioner=user)) &
                    (Q(permissions__isnull=True) | permission_q) &
                    (Q(groups__isnull=True) | group_q)
                )
            )

        proceedings = Proceeding.objects.filter(
            workflow_object=workflow_object,
            field=field,
            meta__transition__source_state__in=source_states,
            status=PENDING,
            enabled=True
        )

        suitable_proceedings = get_proceeding(proceedings.filter(skip=False))
{'fraction_non_alphanumeric': 0.07811993517017828, 'fraction_numerical': 0.0, 'mean_word_length': 2.6108835576360443, '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': '1526958', 'n_tokens_mistral': 1688, 'n_tokens_neox': 1597, 'n_words': 303}