<commit_before># -*- coding: utf-8; -*-
#
# This file is part of Superdesk.
#
# Copyright 2013, 2014 Sourcefabric z.u. and contributors.
#
# For the full copyright and license information, please see the
# AUTHORS and LICENSE files distributed with this source code, or
# at https://www.sourcefabric.org/superdesk/license
import json
import itertools

import superdesk
from flask import current_app as app

from superdesk import get_resource_service
from superdesk.errors import SuperdeskApiError
from superdesk.resource import Resource
from superdesk import config
from superdesk.utils import SuperdeskBaseEnum
from bson.objectid import ObjectId
from superdesk.services import BaseService
from superdesk.notification import push_notification
from superdesk.activity import add_activity, ACTIVITY_UPDATE
from superdesk.metadata.item import FAMILY_ID
from eve.utils import ParsedRequest


class DeskTypes(SuperdeskBaseEnum):
    authoring = 'authoring'
    production = 'production'


desks_schema = {
    'name': {
        'type': 'string',
        'required': True,
        'nullable': False,
        'empty': False,
        'iunique': True
    },
    'description': {
        'type': 'string'
    },
    'members': {
        'type': 'list',
        'schema': {
            'type': 'dict',
            'schema': {
                'user': Resource.rel('users', True)
            }
        }
    },
    'incoming_stage': Resource.rel('stages', True),
    'working_stage': Resource.rel('stages', True),
    'content_expiry': {
        'type': 'integer'
    },
    'source': {
        'type': 'string'
    },
    'monitoring_settings': {
        'type': 'list',
        'schema': {
            'type': 'dict',
            'schema': {
                '_id': {
                    'type': 'string',
                    'required': True
                },
                'type': {
                    'type': 'string',
                    'allowed': ['search', 'stage', 'scheduledDeskOutput', 'deskOutput', 'personal'],
                    'required': True
                },
                'max_items': {
                    'type': 'integer',
                    'required': True
                }
            }
        }
    },
    'desk_type': {
        'type': 'string',
        'default': DeskTypes.authoring.value,
        'allowed': DeskTypes.values()
    },
    'desk_metadata': {
        'type': 'dict',
    },

    'content_profiles': {
        'type': 'dict',
    },

    'desk_language': {
        'type': 'string'
    },

    'monitoring_default_view': {
        'type': 'string',
        'allowed': ['', 'list', 'swimlane', 'photogrid'],
        'required': False
    },

    'default_content_profile': Resource.rel('content_types', type='string', nullable=True),
    'default_content_template': Resource.rel('content_templates', nullable=True),
    # name of a Slack channel that may be associated with the desk
    'slack_channel_name': {
        'type': 'string'
    },

    # desk prefered vocabulary items
    'preferred_cv_items': {
        'type': 'dict',
    },
}


def init_app(app):
    endpoint_name = 'desks'
    service = DesksService(endpoint_name, backend=superdesk.get_backend())
    DesksResource(endpoint_name, app=app, service=service)
    endpoint_name = 'user_desks'
    service = UserDesksService(endpoint_name, backend=superdesk.get_backend())
    UserDesksResource(endpoint_name, app=app, service=service)
    endpoint_name = 'sluglines'
    service = SluglineDeskService(endpoint_name, backend=superdesk.get_backend())
    SluglineDesksResource(endpoint_name, app=app, service=service)


superdesk.privilege(name='desks', label='Desk Management', description='User can manage desks.')


class DesksResource(Resource):
    schema = desks_schema
    privileges = {'POST': 'desks', 'PATCH': 'desks', 'DELETE': 'desks'}


class DesksService(BaseService):
    notification_key = 'desk'

    def create(self, docs, **kwargs):
        """Creates new desk.

        Overriding to check if the desk being created has Working and Incoming Stages. If not then Working and Incoming
        Stages would be created and associates them with the desk and desk with the Working and Incoming Stages.
        Also sets desk_type.

        :return: list of desk id's
        """

        for desk in docs:
            stages_to_be_linked_with_desk = []
            stage_service = superdesk.get_resource_service('stages')
            self._ensure_unique_members(desk)

            if desk.get('content_expiry') == 0:
                desk['content_expiry'] = app.settings['CONTENT_EXPIRY_MINUTES']

            if 'working_stage' not in desk:
                stages_to_be_linked_with_desk.append('working_stage')<commit_msg>fixed: /desks should return members sorted by display name<commit_after># -*- coding: utf-8; -*-
#
# This file is part of Superdesk.
#
# Copyright 2013, 2014 Sourcefabric z.u. and contributors.
#
# For the full copyright and license information, please see the
# AUTHORS and LICENSE files distributed with this source code, or
# at https://www.sourcefabric.org/superdesk/license
import json
import itertools

import superdesk
from flask import current_app as app

from superdesk import get_resource_service
from superdesk.errors import SuperdeskApiError
from superdesk.resource import Resource
from superdesk import config
from superdesk.utils import SuperdeskBaseEnum
from bson.objectid import ObjectId
from superdesk.services import BaseService
from superdesk.notification import push_notification
from superdesk.activity import add_activity, ACTIVITY_UPDATE
from superdesk.metadata.item import FAMILY_ID
from eve.utils import ParsedRequest


class DeskTypes(SuperdeskBaseEnum):
    authoring = 'authoring'
    production = 'production'


desks_schema = {
    'name': {
        'type': 'string',
        'required': True,
        'nullable': False,
        'empty': False,
        'iunique': True
    },
    'description': {
        'type': 'string'
    },
    'members': {
        'type': 'list',
        'schema': {
            'type': 'dict',
            'schema': {
                'user': Resource.rel('users', True)
            }
        }
    },
    'incoming_stage': Resource.rel('stages', True),
    'working_stage': Resource.rel('stages', True),
    'content_expiry': {
        'type': 'integer'
    },
    'source': {
        'type': 'string'
    },
    'monitoring_settings': {
        'type': 'list',
        'schema': {
            'type': 'dict',
            'schema': {
                '_id': {
                    'type': 'string',
                    'required': True
                },
                'type': {
                    'type': 'string',
                    'allowed': ['search', 'stage', 'scheduledDeskOutput', 'deskOutput', 'personal'],
                    'required': True
                },
                'max_items': {
                    'type': 'integer',
                    'required': True
                }
            }
        }
    },
    'desk_type': {
        'type': 'string',
        'default': DeskTypes.authoring.value,
        'allowed': DeskTypes.values()
    },
    'desk_metadata': {
        'type': 'dict',
    },

    'content_profiles': {
        'type': 'dict',
    },

    'desk_language': {
        'type': 'string'
    },

    'monitoring_default_view': {
        'type': 'string',
        'allowed': ['', 'list', 'swimlane', 'photogrid'],
        'required': False
    },

    'default_content_profile': Resource.rel('content_types', type='string', nullable=True),
    'default_content_template': Resource.rel('content_templates', nullable=True),
    # name of a Slack channel that may be associated with the desk
    'slack_channel_name': {
        'type': 'string'
    },

    # desk prefered vocabulary items
    'preferred_cv_items': {
        'type': 'dict',
    },
}


def init_app(app):
    endpoint_name = 'desks'
    service = DesksService(endpoint_name, backend=superdesk.get_backend())
    DesksResource(endpoint_name, app=app, service=service)
    endpoint_name = 'user_desks'
    service = UserDesksService(endpoint_name, backend=superdesk.get_backend())
    UserDesksResource(endpoint_name, app=app, service=service)
    endpoint_name = 'sluglines'
    service = SluglineDeskService(endpoint_name, backend=superdesk.get_backend())
    SluglineDesksResource(endpoint_name, app=app, service=service)


superdesk.privilege(name='desks', label='Desk Management', description='User can manage desks.')


class DesksResource(Resource):
    schema = desks_schema
    privileges = {'POST': 'desks', 'PATCH': 'desks', 'DELETE': 'desks'}
    datasource = {'default_sort': [('name', 1)]}


class DesksService(BaseService):
    notification_key = 'desk'

    def create(self, docs, **kwargs):
        """Creates new desk.

        Overriding to check if the desk being created has Working and Incoming Stages. If not then Working and Incoming
        Stages would be created and associates them with the desk and desk with the Working and Incoming Stages.
        Also sets desk_type.

        :return: list of desk id's
        """

        for desk in docs:
            stages_to_be_linked_with_desk = []
            stage_service = superdesk.get_resource_service('stages')
            self._ensure_unique_members(desk)

            if desk.get('content_expiry') == 0:
                desk['content_expiry'] = app.settings['CONTENT_EXPIRY_MINUTES']

            if 'working_stage' not in desk:
                stages_to_be_linked_with_desk.append('working_stage')
{'fraction_non_alphanumeric': 0.12001253394610403, 'fraction_numerical': 0.002193440568205557, 'mean_word_length': 2.5397412199630316, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '7005387', 'n_tokens_mistral': 2871, 'n_tokens_neox': 2778, 'n_words': 689}