<commit_before>from bson import ObjectId
from bson.errors import InvalidId
from collections import namedtuple
from mongoengine import Document, DateTimeField, BooleanField, StringField, DecimalField, ObjectIdField
from mongoengine.errors import InvalidQueryError
from mongoengine.queryset import Q
from pymongo.errors import DuplicateKeyError

FindParams = namedtuple('FindParams', 'projection sort limit request batch_size')
FindParams.__new__.__defaults__ = (None, None, 0, None, 0)

UpdateParams = namedtuple('UpdateByIdParams', 'unset upsert request')
UpdateParams.__new__.__defaults__ = ((), False, None)

class ModelPermissionException(Exception):
    pass

class BaseModel(Document):
    meta = {'abstract': True}

    last_updated = DateTimeField()
    deleted = BooleanField()

    @classmethod
    def allowed_find_query(cls, request):
        '''Returns a query that would find only records this user has permission to see'''
        raise NotImplementedError

    @classmethod
    def allowed_update_query(cls, request):
        '''Returns a query that would find only records this user has permission to edit'''
        raise NotImplementedError

    @classmethod
    def _get_lookup_query(cls, query, allow_deleted=False):
        if 'id' in query:
            query['_id'] = query.pop('id')
        if '_id' in query and isinstance(query['_id'], (str, unicode)):
            try:
                query['_id'] = ObjectId(query['_id'])
            except InvalidId:
                pass

        if not allow_deleted:
            query['deleted'] = None

        return query

    @classmethod
    def _get_lookup_query_find(cls, query, allow_deleted=False, request=None):
        query = cls._get_lookup_query(query, allow_deleted=allow_deleted)
        if request:
            query = {
                '$and': [query, cls.allowed_find_query(request)]
            }
        return query

    @classmethod
    def _get_lookup_query_update(cls, query, allow_deleted=False, request=None):
        query = cls._get_lookup_query(query, allow_deleted=allow_deleted)
        if request:
            query = {
                '$and': [query, cls.allowed_update_query(request)]
            }
        return query

    @classmethod
    def find(cls, params=FindParams(), **kwargs):<commit_msg>Superusers are exempt from permission checks<commit_after>from bson import ObjectId
from bson.errors import InvalidId
from collections import namedtuple
from mongoengine import Document, DateTimeField, BooleanField, StringField, DecimalField, ObjectIdField
from mongoengine.errors import InvalidQueryError
from mongoengine.queryset import Q
from pymongo.errors import DuplicateKeyError

FindParams = namedtuple('FindParams', 'projection sort limit request batch_size')
FindParams.__new__.__defaults__ = (None, None, 0, None, 0)

UpdateParams = namedtuple('UpdateByIdParams', 'unset upsert request')
UpdateParams.__new__.__defaults__ = ((), False, None)

class ModelPermissionException(Exception):
    pass

class BaseModel(Document):
    meta = {'abstract': True}

    last_updated = DateTimeField()
    deleted = BooleanField()

    @classmethod
    def allowed_find_query(cls, request):
        '''Returns a query that would find only records this user has permission to see'''
        raise NotImplementedError

    @classmethod
    def allowed_update_query(cls, request):
        '''Returns a query that would find only records this user has permission to edit'''
        raise NotImplementedError

    @classmethod
    def _get_lookup_query(cls, query, allow_deleted=False):
        if 'id' in query:
            query['_id'] = query.pop('id')
        if '_id' in query and isinstance(query['_id'], (str, unicode)):
            try:
                query['_id'] = ObjectId(query['_id'])
            except InvalidId:
                pass

        if not allow_deleted:
            query['deleted'] = None

        return query

    @classmethod
    def _get_lookup_query_find(cls, query, allow_deleted=False, request=None):
        query = cls._get_lookup_query(query, allow_deleted=allow_deleted)
        if request and not request.user.is_superuser:
            query = {
                '$and': [query, cls.allowed_find_query(request)]
            }
        return query

    @classmethod
    def _get_lookup_query_update(cls, query, allow_deleted=False, request=None):
        query = cls._get_lookup_query(query, allow_deleted=allow_deleted)
        if request and not request.user.is_superuser:
            query = {
                '$and': [query, cls.allowed_update_query(request)]
            }
        return query

    @classmethod
    def find(cls, params=FindParams(), **kwargs):
{'fraction_non_alphanumeric': 0.08903088391906283, 'fraction_numerical': 0.0008519701810436634, 'mean_word_length': 3.1557522123893804, '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': '1046794', 'n_tokens_mistral': 1276, 'n_tokens_neox': 1270, 'n_words': 364}