Limit the choices for the other_characters field.
authorMatthijs Kooijman <matthijs@stdin.nl>
Tue, 3 Feb 2009 22:14:34 +0000 (23:14 +0100)
committerMatthijs Kooijman <matthijs@stdin.nl>
Tue, 3 Feb 2009 22:14:34 +0000 (23:14 +0100)
influences/forms.py
influences/views.py

index 56e31ca471334d36ed8e446be6ed9536465e48c9..e1a6db381f29159742c9aef477491bab3b0e3b63 100644 (file)
@@ -67,7 +67,11 @@ def _get_influence_comment_form(allow_markup, allow_public, allow_private):
         raise Exception("Unsupported configuration")
 
 class InfluenceForm(ContextModelForm):
         raise Exception("Unsupported configuration")
 
 class InfluenceForm(ContextModelForm):
-    other_characters = ModelMultipleChoiceField(queryset=Character.objects.all(), widget=DropDownMultiple)
+    # Manually define this field so we can select the DropDownMultiple
+    # widget. However, we leave the queryset empty, which characters can
+    # be selected depends on the logged in user and should be set by
+    # setting the choices property in the view.
+    other_characters = ModelMultipleChoiceField(queryset=Character.objects.none(), widget=DropDownMultiple)
     class Meta:
         model = Influence
         fields = ('initiator', 'summary', 'other_characters', 'other_contacts', 'description')
     class Meta:
         model = Influence
         fields = ('initiator', 'summary', 'other_characters', 'other_contacts', 'description')
index ed23bb24d92c7c45560a05a1da94d5f013735c8b..334bfc70d076f71455dc7329e1282c5d1a928d84 100644 (file)
@@ -14,27 +14,40 @@ from threadedcomments.views import free_comment, _preview
 from xerxes.influences.models import Character
 from xerxes.influences.models import Influence
 from forms import get_influence_comment_form, InfluenceForm, CharacterForm
 from xerxes.influences.models import Character
 from xerxes.influences.models import Influence
 from forms import get_influence_comment_form, InfluenceForm, CharacterForm
+from xerxes.tools.misc import make_choices
 
 @login_required
 def add_influence(request, character_id=None):
     initial = {}
     # Get the current user's characters
 
 @login_required
 def add_influence(request, character_id=None):
     initial = {}
     # Get the current user's characters
-    chars = request.user.character_set.all()
+    my_chars = request.user.character_set.all().filter(type__in=['P', 'N'])
+    # Get all chars
+    all_chars = Character.objects.all().filter(type__in=['P', 'N'])
 
     # If a character_id was specified in the url, or there is only one
     # character, preselect it.
     if (character_id):
         initial['initiator'] = character_id
 
     # If a character_id was specified in the url, or there is only one
     # character, preselect it.
     if (character_id):
         initial['initiator'] = character_id
-    elif (chars.count() == 1):
-        initial['initiator'] = chars[0].id
-
+    elif (my_chars.count() == 1):
+        initial['initiator'] = my_chars[0].id
 
     f = InfluenceForm(request=request, initial=initial)
 
     # Only allow characters of the current user. Putting this here also
     # ensures that a form will not validate when any other choice was
     # selected (perhaps through URL crafting).
 
     f = InfluenceForm(request=request, initial=initial)
 
     # Only allow characters of the current user. Putting this here also
     # ensures that a form will not validate when any other choice was
     # selected (perhaps through URL crafting).
-    f.fields['initiator']._set_queryset(chars)
+    f.fields['initiator']._set_queryset(my_chars)
+    
+    # List the contacts of each of the current users characters, as well
+    # as all other (non-contact) characters as choices for the
+    # other_characters field.
+    char_choices = [
+        ("Contacts of %s" % c, make_choices(c.contacts.all()))
+        for c in my_chars
+        if c.contacts.all()
+    ]
+    char_choices.append(('All player characters', make_choices(all_chars)))
+    f.fields['other_characters'].choices = char_choices
 
     if (f.is_valid()):
         # The form was submitted, let's save it.
 
     if (f.is_valid()):
         # The form was submitted, let's save it.