Merge branch 'production'
[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 from xerxes.tools.misc import make_choices
18
19 @login_required
20 def add_influence(request, character_id=None):
21     initial = {}
22     # Get the current user's characters
23     my_chars = request.user.character_set.all().filter(type__in=[Character.PLAYER, Character.NPC])
24     # Get all chars
25     all_chars = Character.objects.all().filter(type__in=[Character.PLAYER, Character.NPC])
26
27     # If a character_id was specified in the url, or there is only one
28     # character, preselect it.
29     if (character_id):
30         initial['initiator'] = character_id
31     elif (my_chars.count() == 1):
32         initial['initiator'] = my_chars[0].id
33
34     f = InfluenceForm(request=request, initial=initial)
35
36     # Only allow characters of the current user. Putting this here also
37     # ensures that a form will not validate when any other choice was
38     # selected (perhaps through URL crafting).
39     f.fields['initiator']._set_queryset(my_chars)
40     
41     # List the contacts of each of the current users characters, as well
42     # as all other (non-contact) characters as choices for the
43     # other_characters field.
44     char_choices = [
45         ("Contacts of %s" % c, make_choices(c.contacts.all()))
46         for c in my_chars
47         if c.contacts.all()
48     ]
49     char_choices.append(('All player characters', make_choices(all_chars)))
50     f.fields['other_characters'].choices = char_choices
51
52     if (f.is_valid()):
53         # The form was submitted, let's save it.
54         influence = f.save()
55         # Redirect to the just saved influence
56         return HttpResponseRedirect(reverse('influences_influence_detail', args=[influence.id]))
57
58     return render_to_response('influences/add_influence.html', {'form' : f}, RequestContext(request))
59
60 @login_required
61 def add_character(request):
62     f = CharacterForm(request=request)
63     if (f.is_valid()):
64         character = f.save(commit=False)
65         character.player = request.user
66         character.save()
67         return HttpResponseRedirect(reverse('influences_character_detail', args=[character.id]))
68
69     return render_to_response('influences/add_character.html', {'form' : f}, RequestContext(request))
70
71 @login_required
72 def index(request):
73     # Only show this player's characters and influences
74     characters = request.user.character_set.all()
75     influences = Influence.objects.filter(initiator__player=request.user)
76     return render_to_response('influences/index.html', {'characters' : characters, 'influences' : influences}, RequestContext(request))
77
78 #
79 # The views below are very similar to django's generic views (in fact,
80 # they used to be generic views before). However, since they all depend
81 # on the currently logged in user (for limiting the show list or
82 # performing access control), we won't actually use the generic views
83 # here.
84
85 @login_required
86 def character_list(request):
87     # Only show this player's characters
88     os = request.user.character_set.all()
89     return render_to_response('influences/character_list.html', {'object_list' : os}, RequestContext(request))
90
91 @login_required
92 def character_detail(request, object_id):
93     o = Character.objects.get(pk=object_id)
94     # Don't show other player's characters
95     if (not request.user.is_staff and o.player != request.user):
96         return HttpResponseForbidden("Forbidden -- Trying to view somebody else's character")
97     return render_to_response('influences/character_detail.html', {'object' : o}, RequestContext(request))
98
99 @login_required
100 def influence_list(request):
101     # Only show the influences related to this player's characters
102     characters = request.user.character_set.all()
103     return render_to_response('influences/influence_list.html', {'characters' : characters}, RequestContext(request))
104
105 def influence_comment_preview(request, context_processors, extra_context, **kwargs):
106     # Use a custom template
107     kwargs['template'] = 'influences/influence_comment_preview.html'
108     # The object to be show in the influence detail
109     extra_context['object'] = get_object_or_404(Influence, pk=kwargs['object_id'])
110     return _preview(request, context_processors, extra_context, **kwargs)
111
112 @login_required
113 def influence_detail(request, object_id):
114
115     o = Influence.objects.get(pk=object_id)
116     # Don't show other player's influences
117     if (not request.user.is_staff and not request.user in o.related_players):
118         return HttpResponseForbidden("Forbidden -- Trying to view influences you are not involved in.")
119
120     # Show all comments to staff, but only public comments to other
121     # users
122     comments = o.get_comments(private=request.user.is_staff)
123     
124     context  = {
125         'object' : o,
126         'comments' : comments,
127         'comment_form' : get_influence_comment_form(request.user.is_staff, None)()
128     }
129     return render_to_response('influences/influence_detail.html', context, RequestContext(request))
130
131 @login_required
132 def influence_comment(request, object_id, parent_id=None):
133     kwargs = {}
134     # Add the content_type, since we don't put in in the url explicitly
135     kwargs['content_type'] = ContentType.objects.get_for_model(Influence).id
136     # Find the comment to which we're replying, so we can get the right form for it.
137     if parent_id:
138         reply_to = get_object_or_404(ThreadedComment, id=parent_id)
139     else:
140         reply_to = None
141
142     # Find the right form class
143     kwargs['form_class']   = get_influence_comment_form(request.user.is_staff, reply_to)
144     # Override the model, so we don't get a free comment, but a normal
145     # one. We can't use threadedcomments' comment view for that, since
146     # that hardcodes the form_class.
147     kwargs['model'] = ThreadedComment
148     # Set a custom preview view
149     kwargs['preview'] = influence_comment_preview
150     if parent_id:
151         kwargs['prefix'] = "reply-to-%s" % (parent_id)
152     return free_comment(request, object_id=object_id, parent_id=parent_id, **kwargs)
153
154 # vim: set sts=4 sw=4 expandtab: