<commit_before>## Copyright (c) 2010 by <NAME> <<EMAIL> AT <EMAIL> DOT <EMAIL>>
## 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.txt>.
##
## This license is also included in the file COPYING
##
## AUTHOR: <NAME> <<EMAIL>>

import datetime

from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response, get_object_or_404, redirect
from django.http import Http404, HttpResponseRedirect
from django.template import RequestContext
from django.utils.translation import ugettext as _
from django.utils.decorators import method_decorator
from django.core.urlresolvers import reverse
from django.conf import global_settings

from django.views.generic.list import ListView

from messages.utils import format_quote
from messages.models import Message

from django.contrib import messages

from machiavelli.models import Player, Game
from machiavelli.views import base_context

from condottieri_messages.exceptions import LetterError

import condottieri_messages.forms as forms
from condottieri_messages.models import Letter

class LoginRequiredMixin(object):
	""" Mixin to check that the user has authenticated.
	(Always the first mixin in class)
	"""
	@method_decorator(login_required)
	def dispatch(self, *args, **kwargs):
		return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)

def check_errors(request, game, sender_player, recipient_player):
	msg = None
	if sender_player.eliminated or recipient_player.eliminated:
		msg = _("Eliminated players cannot send or receive letters")
	## if the game is inactive, return 404 error
	elif game.phase == 0:
		msg = _("You cannot send letters in an inactive game.")
	## check if the sender has excommunicated the recipient
	elif sender_player.may_excommunicate and recipient_player.is_excommunicated:
		msg = _("You cannot write to a country that you have excommunicated.")
	else:
		return True
	raise LetterError(msg)

@login_required
def compose(request, sender_id=None, recipient_id=None, letter_id=None):
	if sender_id and recipient_id:
		## check that the sender is legitimate
		sender_player = get_object_or_404(Player, user=request.user, id=sender_id)
		game = sender_player.game
		recipient_player = get_object_or_404(Player, id=recipient_id, game=game)
		parent = Letter.objects.none()
	elif letter_id:
		parent = get_object_or_404(Letter, id=letter_id)
		if parent.sender != request.user and parent.recipient != request.user:
			raise Http404
		sender_player = parent.recipient_player
		recipient_player = parent.sender_player
		game = sender_player.game
	else:
		raise Http404
	if game.is_team_game or not game.configuration.letters:
		raise Http404
	context = base_context(request, game, sender_player)
	try:
		check_errors(request, game, sender_player, recipient_player)
	except LetterError, e:
		messages.error(request, e.value)
		return redirect(game)
	## try to find a common language for the two players
	common_language_code = None
	sender_lang = sender_player.user.get_profile().spokenlanguage_set.values_list('code', flat=True)
	recipient_lang = recipient_player.user.get_profile().spokenlanguage_set.values_list('code', flat=True)
	for lang in sender_lang:
		if lang in recipient_lang:
			common_language_code = lang
			break
	if common_language_code is not None:
		lang_dict = dict(global_settings.LANGUAGES)
		if common_language_code in lang_dict.keys():
			common_language = lang_dict[common_language_code]
		context.update({'common_language': common_language })
	LetterForm = forms.letter_form_factory(sender_player, recipient_player)
	if request.method == 'POST':
		letter_form = LetterForm(sender_player, recipient_player, data=request.POST)
		if letter_form.is_valid():
			bcc_errors = False
			bcc = letter_form.cleaned_data["bcc"]
			excom = False
			for r in bcc:
				try:
					check_errors(request, game, sender_player, r)<commit_msg>Enable letters in team games<commit_after>## Copyright (c) 2010 by <NAME> <<EMAIL>>
## 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.txt>.
##
## This license is also included in the file COPYING
##
## AUTHOR: <NAME> <<EMAIL>>

import datetime

from django.contrib.auth.decorators import login_required
from django.shortcuts import render_to_response, get_object_or_404, redirect
from django.http import Http404, HttpResponseRedirect
from django.template import RequestContext
from django.utils.translation import ugettext as _
from django.utils.decorators import method_decorator
from django.core.urlresolvers import reverse
from django.conf import global_settings

from django.views.generic.list import ListView

from messages.utils import format_quote
from messages.models import Message

from django.contrib import messages

from machiavelli.models import Player, Game
from machiavelli.views import base_context

from condottieri_messages.exceptions import LetterError

import condottieri_messages.forms as forms
from condottieri_messages.models import Letter

class LoginRequiredMixin(object):
	""" Mixin to check that the user has authenticated.
	(Always the first mixin in class)
	"""
	@method_decorator(login_required)
	def dispatch(self, *args, **kwargs):
		return super(LoginRequiredMixin, self).dispatch(*args, **kwargs)

def check_errors(request, game, sender_player, recipient_player):
	msg = None
	if sender_player.eliminated or recipient_player.eliminated:
		msg = _("Eliminated players cannot send or receive letters")
	## if the game is inactive, return 404 error
	elif game.phase == 0:
		msg = _("You cannot send letters in an inactive game.")
	## check if the sender has excommunicated the recipient
	elif sender_player.may_excommunicate and recipient_player.is_excommunicated:
		msg = _("You cannot write to a country that you have excommunicated.")
	else:
		return True
	raise LetterError(msg)

@login_required
def compose(request, sender_id=None, recipient_id=None, letter_id=None):
	if sender_id and recipient_id:
		## check that the sender is legitimate
		sender_player = get_object_or_404(Player, user=request.user, id=sender_id)
		game = sender_player.game
		recipient_player = get_object_or_404(Player, id=recipient_id, game=game)
		parent = Letter.objects.none()
	elif letter_id:
		parent = get_object_or_404(Letter, id=letter_id)
		if parent.sender != request.user and parent.recipient != request.user:
			raise Http404
		sender_player = parent.recipient_player
		recipient_player = parent.sender_player
		game = sender_player.game
	else:
		raise Http404
	if not game.configuration.letters:
		raise Http404
	context = base_context(request, game, sender_player)
	try:
		check_errors(request, game, sender_player, recipient_player)
	except LetterError, e:
		messages.error(request, e.value)
		return redirect(game)
	## try to find a common language for the two players
	common_language_code = None
	sender_lang = sender_player.user.get_profile().spokenlanguage_set.values_list('code', flat=True)
	recipient_lang = recipient_player.user.get_profile().spokenlanguage_set.values_list('code', flat=True)
	for lang in sender_lang:
		if lang in recipient_lang:
			common_language_code = lang
			break
	if common_language_code is not None:
		lang_dict = dict(global_settings.LANGUAGES)
		if common_language_code in lang_dict.keys():
			common_language = lang_dict[common_language_code]
		context.update({'common_language': common_language })
	LetterForm = forms.letter_form_factory(sender_player, recipient_player)
	if request.method == 'POST':
		letter_form = LetterForm(sender_player, recipient_player, data=request.POST)
		if letter_form.is_valid():
			bcc_errors = False
			bcc = letter_form.cleaned_data["bcc"]
			excom = False
			for r in bcc:
				try:
					check_errors(request, game, sender_player, r)
{'fraction_non_alphanumeric': 0.07837201479986546, 'fraction_numerical': 0.007399932727884292, 'mean_word_length': 5.482558139534884, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '1704880', 'n_tokens_mistral': 2758, 'n_tokens_neox': 2569, 'n_words': 875}