Update translations.
[matthijs/projects/xerxes.git] / influences / views.py
1 from django import forms
2 from django.contrib.auth.decorators import login_required
3 from django.shortcuts import render_to_response
4 from django.shortcuts import get_object_or_404
5 from django.template import RequestContext
6 from django.utils.translation import ugettext as _
7 from django.contrib.auth.models import User
8 from django.contrib.contenttypes.models import ContentType
9 from django.core.urlresolvers import reverse
10 from django.http import HttpResponseRedirect, HttpResponseForbidden
11 from django.views.generic.list_detail import object_detail, object_list
12 from threadedcomments.models import ThreadedComment
13 from threadedcomments.views import free_comment, _preview
14 from xerxes.influences.models import Character
15 from xerxes.influences.models import Influence
16 from forms import get_influence_comment_form, InfluenceForm, CharacterForm
17
18 @login_required
19 def add_influence(request, character_id=None):
20     initial = {}
21     # Get the current user's characters
22     chars = request.user.character_set.all()
23
24     # If a character_id was specified in the url, or there is only one
25     # character, preselect it.
26     if (character_id):
27         initial['character'] = character_id
28     elif (chars.count() == 1):
29         initial['character'] = chars[0].id
30
31
32     f = InfluenceForm(request=request, initial=initial)
33
34     # Only allow characters of the current user. Putting this here also
35     # ensures that a form will not validate when any other choice was
36     # selected (perhaps through URL crafting).
37     f.fields['character']._set_queryset(chars)
38
39     if (f.is_valid()):
40         # The form was submitted, let's save it.
41         influence = f.save()
42         # Redirect to the just saved influence
43         return HttpResponseRedirect(reverse('influences_influence_detail', args=[influence.id]))
44
45     return render_to_response('influences/add_influence.html', {'form' : f}, RequestContext(request))
46
47 @login_required
48 def add_character(request):
49     f = CharacterForm(request=request)
50     if (f.is_valid()):
51         character = f.save(commit=False)
52         character.player = request.user
53         character.save()
54         return HttpResponseRedirect(reverse('influences_character_detail', args=[character.id]))
55
56     return render_to_response('influences/add_character.html', {'form' : f}, RequestContext(request))
57
58 @login_required
59 def index(request):
60     # Only show this player's characters and influences
61     characters = request.user.character_set.all()
62     influences = Influence.objects.filter(character__player=request.user)
63     return render_to_response('influences/index.html', {'characters' : characters, 'influences' : influences}, RequestContext(request))
64
65 #
66 # The views below are very similar to django's generic views (in fact,
67 # they used to be generic views before). However, since they all depend
68 # on the currently logged in user (for limiting the show list or
69 # performing access control), we won't actually use the generic views
70 # here.
71
72 @login_required
73 def character_list(request):
74     # Only show this player's characters
75     os = request.user.character_set.all()
76     return render_to_response('influences/character_list.html', {'object_list' : os}, RequestContext(request))
77
78 @login_required
79 def character_detail(request, object_id):
80     o = Character.objects.get(pk=object_id)
81     # Don't show other player's characters
82     if (not request.user.is_staff and o.player != request.user):
83         return HttpResponseForbidden("Forbidden -- Trying to view somebody else's character")
84     return render_to_response('influences/character_detail.html', {'object' : o}, RequestContext(request))
85
86 @login_required
87 def influence_list(request):
88     # Only show this player's influences
89     os = Influence.objects.filter(character__player=request.user)
90     return render_to_response('influences/influence_list.html', {'object_list' : os}, RequestContext(request))
91
92 def influence_comment_preview(request, context_processors, extra_context, **kwargs):
93     # Use a custom template
94     kwargs['template'] = 'influences/influence_comment_preview.html'
95     # The object to be show in the influence detail
96     extra_context['object'] = get_object_or_404(Influence, pk=kwargs['object_id'])
97     return _preview(request, context_processors, extra_context, **kwargs)
98
99 @login_required
100 def influence_detail(request, object_id):
101
102     o = Influence.objects.get(pk=object_id)
103     # Don't show other player's influences
104     if (not request.user.is_staff and o.character.player != request.user):
105         return HttpResponseForbidden("Forbidden -- Trying to view influences of somebody else's character")
106
107     # Show all comments to staff, but only public comments to other
108     # users
109     comments = o.get_comments(private=request.user.is_staff)
110     
111     context  = {
112         'object' : o,
113         'comments' : comments,
114         'comment_form' : get_influence_comment_form(request.user.is_staff, None)()
115     }
116     return render_to_response('influences/influence_detail.html', context, RequestContext(request))
117
118 @login_required
119 def influence_comment(request, object_id, parent_id=None):
120     kwargs = {}
121     # Add the content_type, since we don't put in in the url explicitly
122     kwargs['content_type'] = ContentType.objects.get_for_model(Influence).id
123     # Find the comment to which we're replying, so we can get the right form for it.
124     if parent_id:
125         reply_to = get_object_or_404(ThreadedComment, id=parent_id)
126     else:
127         reply_to = None
128
129     # Find the right form class
130     kwargs['form_class']   = get_influence_comment_form(request.user.is_staff, reply_to)
131     # Override the model, so we don't get a free comment, but a normal
132     # one. We can't use threadedcomments' comment view for that, since
133     # that hardcodes the form_class.
134     kwargs['model'] = ThreadedComment
135     # Set a custom preview view
136     kwargs['preview'] = influence_comment_preview
137     if parent_id:
138         kwargs['prefix'] = "reply-to-%s" % (parent_id)
139     return free_comment(request, object_id=object_id, parent_id=parent_id, **kwargs)
140
141 # vim: set sts=4 sw=4 expandtab: