<commit_before># Amara, universalsubtitles.org
#
# Copyright (C) 2015 Participatory Culture Foundation
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program.  If not, see http://www.gnu.org/licenses/agpl-3.0.html.


"""
Message Resource
^^^^^^^^^^^^^^^^

The message resource allows you to send messages to user and teams.

.. http:post:: /api/message/

    :<json user: Recipient User's username
    :<json team: Recipient Team's slug
    :<json subject: Subject of the message
    :<json content: Content of the message

.. note: You can only send either ``user`` or ``team``, not both
"""

from __future__ import absolute_import

from django.core.exceptions import PermissionDenied
from rest_framework import serializers
from rest_framework import status
from rest_framework import views
from rest_framework.response import Response

from auth.models import CustomUser as User
from teams.models import Team
from messages.models import Message
import teams.permissions

class MessagesSerializer(serializers.Serializer):
    user = serializers.CharField(required=False)
    team = serializers.CharField(required=False)
    subject = serializers.CharField()
    content = serializers.CharField()
    message_type = serializers.CharField()

    default_error_messages = {
        'unknown-user': "Unknown user: {user}",
        'unknown-team': "Unknown team: {team}",
        'no-user-or-team': "Must specify either user or team",
    }

    def validate_user(self, username):
        try:
            return User.objects.get(username=username)
        except User.DoesNotExist:
            self.fail('unknown-user', user=username)

    def validate_team(self, slug):
        try:
            team = Team.objects.get(slug=slug)
        except Team.DoesNotExist:
            self.fail('unknown-team', team=slug)
        if not teams.permissions.can_message_all_members(<commit_msg>Removed message_type field from the API.<commit_after># Amara, universalsubtitles.org
#
# Copyright (C) 2015 Participatory Culture Foundation
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
# Software Foundation, either version 3 of the License, or (at your option) any
# later version.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
# details.
#
# You should have received a copy of the GNU Affero General Public License along
# with this program.  If not, see http://www.gnu.org/licenses/agpl-3.0.html.


"""
Message Resource
^^^^^^^^^^^^^^^^

The message resource allows you to send messages to user and teams.

.. http:post:: /api/message/

    :<json user: Recipient User's username
    :<json team: Recipient Team's slug
    :<json subject: Subject of the message
    :<json content: Content of the message

.. note: You can only send either ``user`` or ``team``, not both
"""

from __future__ import absolute_import

from django.core.exceptions import PermissionDenied
from rest_framework import serializers
from rest_framework import status
from rest_framework import views
from rest_framework.response import Response

from auth.models import CustomUser as User
from teams.models import Team
from messages.models import Message
import teams.permissions

class MessagesSerializer(serializers.Serializer):
    user = serializers.CharField(required=False)
    team = serializers.CharField(required=False)
    subject = serializers.CharField()
    content = serializers.CharField()

    default_error_messages = {
        'unknown-user': "Unknown user: {user}",
        'unknown-team': "Unknown team: {team}",
        'no-user-or-team': "Must specify either user or team",
    }

    def validate_user(self, username):
        try:
            return User.objects.get(username=username)
        except User.DoesNotExist:
            self.fail('unknown-user', user=username)

    def validate_team(self, slug):
        try:
            team = Team.objects.get(slug=slug)
        except Team.DoesNotExist:
            self.fail('unknown-team', team=slug)
        if not teams.permissions.can_message_all_members(
{'fraction_non_alphanumeric': 0.08710873146622734, 'fraction_numerical': 0.002883031301482702, 'mean_word_length': 3.7478005865102637, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '1651579', 'n_tokens_mistral': 1313, 'n_tokens_neox': 1287, 'n_words': 504}