<commit_before>from flask_restless import ProcessingException
from flask_login import current_user

from server.forms import RegistrationForm
from server.models import User, Keyword, Language, Project


def project_owned_by_current_user(instance_id):
    """Preprocessor for api project DELETE endpoint which
    ensures project can only be deleted by a user with role 'owner'

    :param instance_id: id of instance to be deleted
    :raises ProcessingException: if current_user is not an owner of the project
    :return:None
    :rtype: None
    """
    project = Project.query.filter_by(id=instance_id).first()
    owners = project.get_owners()
    if current_user not in owners:
        raise ProcessingException('Only project owners can delete projects')


def validate_with_form(form_class):
    def preprocessor(data=None):
        form = form_class.from_json(data)
        if not form.validate():
            raise ProcessingException

    return preprocessor


def remove_props(props):
    def preprocessor(data=None):
        for prop in props:
            del data[prop]

    return preprocessor


def login_required_preprocessor(*args, **kwargs):
    if not current_user.is_authenticated():
        raise ProcessingException(
            description='Not Authorized',
            code=401
        )
    return True


api_config = [
    {
        'model': User,
        'methods': ['GET', 'POST', 'DELETE'],
        'preprocessors': {
            'POST': [
                validate_with_form(RegistrationForm),
                remove_props(['confirm'])
            ],
        }
    },
    {
        'model': Keyword,
        'methods': ['GET', 'POST', 'DELETE'],
        'preprocessors': {
            'POST': [login_required_preprocessor],
            'DELETE': [login_required_preprocessor]
        }
    },
    {
        'model': Language,
        'methods': ['GET', 'POST', 'DELETE'],
        'preprocessors': {
            'POST': [login_required_preprocessor],

        }
    },
    {
        'model': Project,
        'methods': ['GET', 'POST', 'DELETE'],
        'preprocessors': {
            'POST': [login_required_preprocessor],
            'DELETE': [
                login_required_preprocessor,
                project_owned_by_current_user
            ]

        },
        'include_columns': ['id',
                            'name',
                            'description',
                            'source',
                            'type',
                            'users',
                            'keywords',
                            'languages']
    }
]<commit_msg>Returning favorite users from api<commit_after>from flask_restless import ProcessingException
from flask_login import current_user

from server.forms import RegistrationForm
from server.models import User, Keyword, Language, Project


def project_owned_by_current_user(instance_id):
    """Preprocessor for api project DELETE endpoint which
    ensures project can only be deleted by a user with role 'owner'

    :param instance_id: id of instance to be deleted
    :raises ProcessingException: if current_user is not an owner of the project
    :return:None
    :rtype: None
    """
    project = Project.query.filter_by(id=instance_id).first()
    owners = project.get_owners()
    if current_user not in owners:
        raise ProcessingException('Only project owners can delete projects')


def validate_with_form(form_class):
    def preprocessor(data=None):
        form = form_class.from_json(data)
        if not form.validate():
            raise ProcessingException

    return preprocessor


def remove_props(props):
    def preprocessor(data=None):
        for prop in props:
            del data[prop]

    return preprocessor


def login_required_preprocessor(*args, **kwargs):
    if not current_user.is_authenticated():
        raise ProcessingException(
            description='Not Authorized',
            code=401
        )
    return True


api_config = [
    {
        'model': User,
        'methods': ['GET', 'POST', 'DELETE'],
        'preprocessors': {
            'POST': [
                validate_with_form(RegistrationForm),
                remove_props(['confirm'])
            ],
        }
    },
    {
        'model': Keyword,
        'methods': ['GET', 'POST', 'DELETE'],
        'preprocessors': {
            'POST': [login_required_preprocessor],
            'DELETE': [login_required_preprocessor]
        }
    },
    {
        'model': Language,
        'methods': ['GET', 'POST', 'DELETE'],
        'preprocessors': {
            'POST': [login_required_preprocessor],

        }
    },
    {
        'model': Project,
        'methods': ['GET', 'POST', 'DELETE'],
        'preprocessors': {
            'POST': [login_required_preprocessor],
            'DELETE': [
                login_required_preprocessor,
                project_owned_by_current_user
            ]

        },
        'include_columns': ['id',
                            'name',
                            'description',
                            'source',
                            'type',
                            'users',
                            'keywords',
                            'languages',
                            'favorite_users']
    }
]
{'fraction_non_alphanumeric': 0.10128253489249339, 'fraction_numerical': 0.001131648434552999, 'mean_word_length': 1.8789359391965255, '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': '709874', 'n_tokens_mistral': 1398, 'n_tokens_neox': 1357, 'n_words': 341}