-
Notifications
You must be signed in to change notification settings - Fork 2
/
forms.py
57 lines (49 loc) · 2.15 KB
/
forms.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
## Copyright (c) 2010 by Jose Antonio Martin <jantonio.martin AT gmail DOT com>
## 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: Jose Antonio Martin <jantonio.martin AT gmail DOT com>
import django.forms as forms
from django.utils.translation import ugettext_lazy as _
import condottieri_messages.models as messages
class PlayerMultipleChoiceField(forms.ModelMultipleChoiceField):
def label_from_instance(self, obj):
try:
label = obj.contender.country.name
except:
return super(PlayerMultipleChoiceField, self).label_from_instance(obj)
else:
if obj.is_excommunicated:
label = ' '.join([label, str(_("(excommunicated)"))])
return label
def letter_form_factory(sender_player, recipient_player):
game = sender_player.game
bcc_qs = game.player_set.exclude(user__isnull=True).exclude(id=sender_player.id).exclude(id=recipient_player.id)
if sender_player.is_excommunicated:
bcc_qs = bcc_qs.filter(is_excommunicated=True)
else:
if sender_player.may_excommunicate:
bcc_qs = bcc_qs.exclude(is_excommunicated=True)
class LetterForm(forms.ModelForm):
bcc = PlayerMultipleChoiceField(queryset=bcc_qs, required=False,
label=_("Additional recipients (Bcc)"))
def __init__(self, sender_player, recipient_player, **kwargs):
super(LetterForm, self).__init__(**kwargs)
self.instance.sender_player = sender_player
self.instance.recipient_player = recipient_player
class Meta:
model = messages.Letter
fields = ('bcc', 'subject', 'body',)
return LetterForm