From 344b9437954ac8e0846c4932c05479704b0265e9 Mon Sep 17 00:00:00 2001 From: Matthijs Kooijman Date: Wed, 23 Jan 2008 13:50:08 +0100 Subject: [PATCH] * Remove old addInfluence view and replace it by a new add view (which allows you to specify a character to add the influence for). --- influences/views.py | 23 ++++++++++++++--------- urls.py | 4 ++-- 2 files changed, 16 insertions(+), 11 deletions(-) diff --git a/influences/views.py b/influences/views.py index 4450efa..1b988dd 100644 --- a/influences/views.py +++ b/influences/views.py @@ -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 24a9ec1..cf577db 100644 --- a/urls.py +++ b/urls.py @@ -9,9 +9,9 @@ urlpatterns = patterns('', (r'^admin/', include('django.contrib.admin.urls')), (r'^events/event/(?P\d+)/$', 'ee.events.views.event.detail'), (r'^events/event/(?P\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\d+)/$', 'django.views.generic.list_detail.object_detail', chars_dict), - (r'^influences/character/(?P\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'), ) -- 2.30.2