<commit_before>from django.db.models import Count

from ...simplified import SimplifiedModelApi, simplified_modelapi, PermissionDenied, FieldSpec
from ..core import models
from devilry.coreutils.simplified.metabases import (SimplifiedSubjectMetaMixin,
                                                   SimplifiedFileMetaMetaMixin,
                                                   SimplifiedPeriodMetaMixin,
                                                   SimplifiedAssignmentMetaMixin,
                                                   SimplifiedAssignmentGroupMetaMixin,
                                                   SimplifiedDeadlineMetaMixin,
                                                   SimplifiedDeliveryMetaMixin,
                                                   SimplifiedStaticFeedbackMetaMixin,)



class PublishedWhereIsExaminerMixin(SimplifiedModelApi):
    """ Mixin class extended by many of the classes in the Simplified API for Examiner """

    @classmethod
    def create_searchqryset(cls, user, **kwargs):
        """ Returns all objects of this type that matches arguments
        given in ``\*\*kwargs`` where ``user`` is an examiner.

        :param user: A django user object.
        :param \*\*kwargs: A dict containing search-parameters.
        :rtype: a django queryset
        """
        return cls._meta.model.published_where_is_examiner(user)

    @classmethod
    def read_authorize(cls, user, obj):
        """ Checks if the given ``user`` is an examiner for the given 
        ``obj``, and raises ``PermissionDenied`` if not.

        :param user: A django user object.
        :param obj: An object of the type this method is used in.
        :throws PermissionDenied:
        """
        if not cls._meta.model.published_where_is_examiner(user).filter(id=obj.id):
            raise PermissionDenied()


@simplified_modelapi
class SimplifiedSubject(PublishedWhereIsExaminerMixin):
    """ Simplified wrapper for :class:`devilry.apps.core.models.Subject`. """
    class Meta(SimplifiedSubjectMetaMixin):
        """ Defines what methods an Examiner can use on a Subject object using the Simplified API """
        methods = ['search', 'read']


@simplified_modelapi
class SimplifiedPeriod(PublishedWhereIsExaminerMixin):
    """ Simplified wrapper for :class:`devilry.apps.core.models.Period`. """
    class Meta(SimplifiedPeriodMetaMixin):
        """ Defines what methods an Examiner can use on a Period object using the Simplified API """
        methods = ['search', 'read']


@simplified_modelapi
class SimplifiedAssignment(PublishedWhereIsExaminerMixin):
    """ Simplified wrapper for :class:`devilry.apps.core.models.Assignment`. """
    class Meta(SimplifiedAssignmentMetaMixin):
        """ Defines what methods an Examiner can use on an Assignment object using the Simplified API """
        methods = ['search', 'read']

    @classmethod
    def create_searchqryset(cls, user, **kwargs):
        """ Returns all Assignment objects that matches the arguments
        given in ``\*\*kwargs`` where ``user`` is an examiner.

        :param user: A django user object.
        :param \*\*kwargs: A dict containing search-parameters.
        :rtype: a django queryset
        """
        old = kwargs.pop('old', True)
        active = kwargs.pop('active', True)
        subject_short_name = kwargs.pop('subject_short_name', None)
        period_short_name = kwargs.pop('period_short_name', None)
        qryset = models.Assignment.published_where_is_examiner(user, old=old,
                                                               active=active)
        if subject_short_name and period_short_name:
            qryset = qryset.filter(parentnode__short_name=period_short_name,
                                   parentnode__parentnode__short_name=subject_short_name)
        return qryset


@simplified_modelapi
class SimplifiedAssignmentGroup(PublishedWhereIsExaminerMixin):
    """ Simplified wrapper for :class:`devilry.apps.core.models.AssignmentGroup`. """
    class Meta(SimplifiedAssignmentGroupMetaMixin):
        """ Defines what methods an Examiner can use on an AssignmentGroup object using the Simplified API """
        methods = ['search', 'read']


@simplified_modelapi
class SimplifiedDelivery(PublishedWhereIsExaminerMixin):
    """ Simplified wrapper for :class:`devilry.apps.core.models.Delivery`. """
    class Meta(SimplifiedDeliveryMetaMixin):
        """ Defines what methods an Examiner can use on a Delivery object using the Simplified API """
        methods = ['search', 'read']


@simplified_modelapi
class SimplifiedStaticFeedback(PublishedWhereIsExaminerMixin):
    """ Simplified wrapper for :class:`devilry.apps.core.models.StaticFeedback`. """
    class Meta(SimplifiedStaticFeedbackMetaMixin):
        """ Defines what methods an Examiner can use on a StaticFeedback
        object using the Simplified API. Also adds to the resultfields
        returned by search """
        methods = ['search', 'read', 'create']
        # Examiners need a few more fields than is given by
        # default in SimplifiedStaticFeedbackMetaMixin. Addition them in!
        resultfields = SimplifiedStaticFeedbackMetaMixin.resultfields + FieldSpec('points')
        editablefields = ['grade', 'is_passing_grade', 'points',
                          'rendered_view', 'delivery']

    @classmethod
    def pre_full_clean(cls, user, obj):
        if not obj.id == None:
            raise ValueError('BUG: Examiners should only have create permission on StaticFeedback.')
        obj.saved_by = user

    @classmethod
    def write_authorize(cls, user, obj):
        """ Checks if the given ``user`` is an examiner in the given
        StaticFeedback ``obj``, and raises ``PermissionDenied`` if not.

        :param user: A django user object.
        :param obj: A StaticFeedback-object.
        :throws PermissionDenied:
        """
        if not obj.delivery.deadline.assignment_group.is_examiner(user):
            raise PermissionDenied()


@simplified_modelapi
class SimplifiedDeadline(PublishedWhereIsExaminerMixin):
    """ Simplified wrapper for :class:`devilry.apps.core.models.Deadline`. """
    class Meta(SimplifiedDeadlineMetaMixin):
        """ Defines what methods an Examiner can use on a Deadline object using the Simplified API """
        methods = ['search', 'read', 'create', 'update']
        editablefields = ('text', 'deadline', 'assignment_group',
                          'feedbacks_published')

    @classmethod<commit_msg>apps.examiner.simplified. Re-enabled Deadline delete.<commit_after>from django.db.models import Count

from ...simplified import SimplifiedModelApi, simplified_modelapi, PermissionDenied, FieldSpec
from ..core import models
from devilry.coreutils.simplified.metabases import (SimplifiedSubjectMetaMixin,
                                                   SimplifiedFileMetaMetaMixin,
                                                   SimplifiedPeriodMetaMixin,
                                                   SimplifiedAssignmentMetaMixin,
                                                   SimplifiedAssignmentGroupMetaMixin,
                                                   SimplifiedDeadlineMetaMixin,
                                                   SimplifiedDeliveryMetaMixin,
                                                   SimplifiedStaticFeedbackMetaMixin,)



class PublishedWhereIsExaminerMixin(SimplifiedModelApi):
    """ Mixin class extended by many of the classes in the Simplified API for Examiner """

    @classmethod
    def create_searchqryset(cls, user, **kwargs):
        """ Returns all objects of this type that matches arguments
        given in ``\*\*kwargs`` where ``user`` is an examiner.

        :param user: A django user object.
        :param \*\*kwargs: A dict containing search-parameters.
        :rtype: a django queryset
        """
        return cls._meta.model.published_where_is_examiner(user)

    @classmethod
    def read_authorize(cls, user, obj):
        """ Checks if the given ``user`` is an examiner for the given 
        ``obj``, and raises ``PermissionDenied`` if not.

        :param user: A django user object.
        :param obj: An object of the type this method is used in.
        :throws PermissionDenied:
        """
        if not cls._meta.model.published_where_is_examiner(user).filter(id=obj.id):
            raise PermissionDenied()


@simplified_modelapi
class SimplifiedSubject(PublishedWhereIsExaminerMixin):
    """ Simplified wrapper for :class:`devilry.apps.core.models.Subject`. """
    class Meta(SimplifiedSubjectMetaMixin):
        """ Defines what methods an Examiner can use on a Subject object using the Simplified API """
        methods = ['search', 'read']


@simplified_modelapi
class SimplifiedPeriod(PublishedWhereIsExaminerMixin):
    """ Simplified wrapper for :class:`devilry.apps.core.models.Period`. """
    class Meta(SimplifiedPeriodMetaMixin):
        """ Defines what methods an Examiner can use on a Period object using the Simplified API """
        methods = ['search', 'read']


@simplified_modelapi
class SimplifiedAssignment(PublishedWhereIsExaminerMixin):
    """ Simplified wrapper for :class:`devilry.apps.core.models.Assignment`. """
    class Meta(SimplifiedAssignmentMetaMixin):
        """ Defines what methods an Examiner can use on an Assignment object using the Simplified API """
        methods = ['search', 'read']

    @classmethod
    def create_searchqryset(cls, user, **kwargs):
        """ Returns all Assignment objects that matches the arguments
        given in ``\*\*kwargs`` where ``user`` is an examiner.

        :param user: A django user object.
        :param \*\*kwargs: A dict containing search-parameters.
        :rtype: a django queryset
        """
        old = kwargs.pop('old', True)
        active = kwargs.pop('active', True)
        subject_short_name = kwargs.pop('subject_short_name', None)
        period_short_name = kwargs.pop('period_short_name', None)
        qryset = models.Assignment.published_where_is_examiner(user, old=old,
                                                               active=active)
        if subject_short_name and period_short_name:
            qryset = qryset.filter(parentnode__short_name=period_short_name,
                                   parentnode__parentnode__short_name=subject_short_name)
        return qryset


@simplified_modelapi
class SimplifiedAssignmentGroup(PublishedWhereIsExaminerMixin):
    """ Simplified wrapper for :class:`devilry.apps.core.models.AssignmentGroup`. """
    class Meta(SimplifiedAssignmentGroupMetaMixin):
        """ Defines what methods an Examiner can use on an AssignmentGroup object using the Simplified API """
        methods = ['search', 'read']


@simplified_modelapi
class SimplifiedDelivery(PublishedWhereIsExaminerMixin):
    """ Simplified wrapper for :class:`devilry.apps.core.models.Delivery`. """
    class Meta(SimplifiedDeliveryMetaMixin):
        """ Defines what methods an Examiner can use on a Delivery object using the Simplified API """
        methods = ['search', 'read']


@simplified_modelapi
class SimplifiedStaticFeedback(PublishedWhereIsExaminerMixin):
    """ Simplified wrapper for :class:`devilry.apps.core.models.StaticFeedback`. """
    class Meta(SimplifiedStaticFeedbackMetaMixin):
        """ Defines what methods an Examiner can use on a StaticFeedback
        object using the Simplified API. Also adds to the resultfields
        returned by search """
        methods = ['search', 'read', 'create']
        # Examiners need a few more fields than is given by
        # default in SimplifiedStaticFeedbackMetaMixin. Addition them in!
        resultfields = SimplifiedStaticFeedbackMetaMixin.resultfields + FieldSpec('points')
        editablefields = ['grade', 'is_passing_grade', 'points',
                          'rendered_view', 'delivery']

    @classmethod
    def pre_full_clean(cls, user, obj):
        if not obj.id == None:
            raise ValueError('BUG: Examiners should only have create permission on StaticFeedback.')
        obj.saved_by = user

    @classmethod
    def write_authorize(cls, user, obj):
        """ Checks if the given ``user`` is an examiner in the given
        StaticFeedback ``obj``, and raises ``PermissionDenied`` if not.

        :param user: A django user object.
        :param obj: A StaticFeedback-object.
        :throws PermissionDenied:
        """
        if not obj.delivery.deadline.assignment_group.is_examiner(user):
            raise PermissionDenied()


@simplified_modelapi
class SimplifiedDeadline(PublishedWhereIsExaminerMixin):
    """ Simplified wrapper for :class:`devilry.apps.core.models.Deadline`. """
    class Meta(SimplifiedDeadlineMetaMixin):
        """ Defines what methods an Examiner can use on a Deadline object using the Simplified API """
        methods = ['search', 'read', 'create', 'update', 'delete']
        editablefields = ('text', 'deadline', 'assignment_group',
                          'feedbacks_published')

    @classmethod
{'fraction_non_alphanumeric': 0.08888550083854246, 'fraction_numerical': 0.0, 'mean_word_length': 2.6789119461581605, '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': 34, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3500211', 'n_tokens_mistral': 3499, 'n_tokens_neox': 3387, 'n_words': 969}