* Remove old addInfluence view and replace it by a new add view (which allows you...
authorMatthijs Kooijman <matthijs@stdin.nl>
Wed, 23 Jan 2008 12:50:08 +0000 (13:50 +0100)
committerMatthijs Kooijman <matthijs@stdin.nl>
Wed, 23 Jan 2008 12:50:08 +0000 (13:50 +0100)
influences/views.py
urls.py

index 4450efadc8e863fe825e9e5871d9cb0faf42fca8..1b988dd43227baae8d1b731169ff6e9f21d25ecd 100644 (file)
@@ -4,20 +4,25 @@ from ee.influences.models import Character
 from ee.influences.models import Influence
 from django import newforms as forms
 
-class AddInfluenceForm(forms.Form):
-       contact     = forms.CharField(max_length=255, help_text='Which contact will you ask?')
-       description = forms.CharField(help_text='What do you want to achieve')
+class InfluenceForm(forms.ModelForm):
+       class Meta:
+               model = Influence
+               fields = ('character', 'contact', 'description')
 
-def addinfluence(request, character_id):
-       character = get_object_or_404(Character, id=character_id)
+def add(request, character_id):
        message = None
+
        if (request.method == "POST"):
-               f = AddInfluenceForm(request.POST)
+               f = InfluenceForm(request.POST)
                if (f.is_valid()):
-                       inf = character.influence_set.create(contact = f.clean_data['contact'], description = f.clean_data['description'])
+                       f.save()
                        message = "Influence added"
                        f = None
        else:
-               f = AddInfluenceForm()
+               f = InfluenceForm()
+       
+       if f:
+               # Only allow characters of the current user
+               f.fields['character']._set_queryset(request.user.character_set.all())
 
-       return render_to_response('influences/character_addinfluence.html', {'character' : character, 'message' : message, 'form' : f})
+       return render_to_response('influences/character_addinfluence.html', {'message' : message, 'form' : f})
diff --git a/urls.py b/urls.py
index 24a9ec179a63e879efdcbbb16c8298b89bd913cb..cf577db4d6e00c62456786d5f910edb0f6d6082e 100644 (file)
--- a/urls.py
+++ b/urls.py
@@ -9,9 +9,9 @@ urlpatterns = patterns('',
     (r'^admin/', include('django.contrib.admin.urls')),
        (r'^events/event/(?P<event_id>\d+)/$', 'ee.events.views.event.detail'),
        (r'^events/event/(?P<event_id>\d+)/regs$', 'ee.events.views.event.regs'),
+       (r'^influences/add/(\d+)?/?$', 'ee.influences.views.add'),
        (r'^influences/$', 'django.views.generic.list_detail.object_list', chars_dict),
        (r'^influences/character/(?P<object_id>\d+)/$', 'django.views.generic.list_detail.object_detail', chars_dict),
-       (r'^influences/character/(?P<character_id>\d+)/addinfluence/$', 'ee.influences.views.addinfluence'),
        (r'^accounts/login/$', 'django.contrib.auth.views.login', {'template_name': 'base/login.html'}),
-       (r'^accounts/logout/$', 'django.contrib.auth.views.logout_then_login', {'template_name': 'base/login.html'}),
+       (r'^accounts/logout/$', 'django.contrib.auth.views.logout_then_login'),
 )