* Add a view that redirects / to /influences/.
[matthijs/projects/xerxes.git] / influences / views.py
1 from django.shortcuts import render_to_response
2 from django.shortcuts import get_object_or_404
3 from django.template import RequestContext
4 from django.utils.translation import ugettext as _
5 from django.contrib.auth.models import User
6 from django.core.urlresolvers import reverse
7 from django.http import HttpResponseRedirect
8 from ee.influences.models import Character
9 from ee.influences.models import Influence
10 from ee.tools.forms import ContextModelForm
11
12
13
14 class InfluenceForm(ContextModelForm):
15     class Meta:
16         model = Influence
17         fields = ('character', 'contact', 'summary', 'description')
18
19 class CharacterForm(ContextModelForm):
20     class Meta:
21         model = Character
22         fields = ('name')
23
24 def add(request, character_id=None):
25     initial = {}
26     # Get the current user's characters
27     chars = request.user.character_set.all()
28
29     if (character_id):
30         initial['character'] = character_id
31     elif (chars.count() == 1):
32         initial['character'] = chars[0].id
33
34     f = InfluenceForm(request=request, initial=initial)
35     if (f.is_valid()):
36         influence = f.save()
37         return HttpResponseRedirect(reverse('influences_influence_detail', args=[influence.id]))
38      
39     # Only allow characters of the current user
40     f.fields['character']._set_queryset(chars)
41
42     return render_to_response('influences/add.html', {'form' : f}, RequestContext(request))
43
44 def addCharacter(request):
45     f = CharacterForm(request=request)
46     if (f.is_valid()):
47         character = f.save(commit=False)
48         character.player = request.user
49         character.save()
50         return HttpResponseRedirect(reverse('influences_character_detail', args=[character.id]))
51
52     return render_to_response('influences/add_character.html', {'form' : f}, RequestContext(request))
53
54 def index(request):
55     characters = request.user.character_set.all()
56     influences = Influence.objects.filter(character__in=characters)
57     return render_to_response('influences/index.html', {'characters' : characters, 'influences' : influences}, RequestContext(request))