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
20 def add_influence(request, character_id=None):
22 # Get the current user's characters
23 my_chars = request.user.character_set.all().filter(type__in=[Character.PLAYER, Character.NPC])
25 all_chars = Character.objects.all().filter(type__in=[Character.PLAYER, Character.NPC])
27 # If a character_id was specified in the url, or there is only one
28 # character, preselect it.
30 initial['initiator'] = character_id
31 elif (my_chars.count() == 1):
32 initial['initiator'] = my_chars[0].id
34 f = InfluenceForm(request=request, initial=initial)
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)
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.
45 ("Contacts of %s" % c, make_choices(c.contacts.all()))
49 char_choices.append(('All player characters', make_choices(all_chars)))
50 f.fields['other_characters'].choices = char_choices
53 # The form was submitted, let's save it.
55 # Redirect to the just saved influence
56 return HttpResponseRedirect(reverse('influences_influence_detail', args=[influence.id]))
58 return render_to_response('influences/add_influence.html', {'form' : f}, RequestContext(request))
61 def add_character(request):
62 f = CharacterForm(request=request)
64 character = f.save(commit=False)
65 character.player = request.user
67 return HttpResponseRedirect(reverse('influences_character_detail', args=[character.id]))
69 return render_to_response('influences/add_character.html', {'form' : f}, RequestContext(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))
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
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))
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))
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))
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)
113 def influence_detail(request, object_id):
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.")
120 # Show all comments to staff, but only public comments to other
122 comments = o.get_comments(private=request.user.is_staff)
126 'comments' : comments,
127 'comment_form' : get_influence_comment_form(request.user.is_staff, None)()
129 return render_to_response('influences/influence_detail.html', context, RequestContext(request))
132 def influence_comment(request, object_id, parent_id=None):
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.
138 reply_to = get_object_or_404(ThreadedComment, id=parent_id)
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
151 kwargs['prefix'] = "reply-to-%s" % (parent_id)
152 return free_comment(request, object_id=object_id, parent_id=parent_id, **kwargs)
154 # vim: set sts=4 sw=4 expandtab: