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
19 def add_influence(request, character_id=None):
21 # Get the current user's characters
22 chars = request.user.character_set.all()
24 # If a character_id was specified in the url, or there is only one
25 # character, preselect it.
27 initial['initiator'] = character_id
28 elif (chars.count() == 1):
29 initial['initiator'] = chars[0].id
32 f = InfluenceForm(request=request, initial=initial)
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['initiator']._set_queryset(chars)
40 # The form was submitted, let's save it.
42 # Redirect to the just saved influence
43 return HttpResponseRedirect(reverse('influences_influence_detail', args=[influence.id]))
45 return render_to_response('influences/add_influence.html', {'form' : f}, RequestContext(request))
48 def add_character(request):
49 f = CharacterForm(request=request)
51 character = f.save(commit=False)
52 character.player = request.user
54 return HttpResponseRedirect(reverse('influences_character_detail', args=[character.id]))
56 return render_to_response('influences/add_character.html', {'form' : f}, RequestContext(request))
60 # Only show this player's characters and influences
61 characters = request.user.character_set.all()
62 influences = Influence.objects.filter(initiator__player=request.user)
63 return render_to_response('influences/index.html', {'characters' : characters, 'influences' : influences}, RequestContext(request))
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
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))
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))
87 def influence_list(request):
88 # Only show the influences related to this player's characters
89 characters = request.user.character_set.all()
90 return render_to_response('influences/influence_list.html', {'characters' : characters}, RequestContext(request))
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)
100 def influence_detail(request, object_id):
102 o = Influence.objects.get(pk=object_id)
103 # Don't show other player's influences
104 if (not request.user.is_staff and not request.user in o.related_players):
105 return HttpResponseForbidden("Forbidden -- Trying to view influences you are not involved in.")
107 # Show all comments to staff, but only public comments to other
109 comments = o.get_comments(private=request.user.is_staff)
113 'comments' : comments,
114 'comment_form' : get_influence_comment_form(request.user.is_staff, None)()
116 return render_to_response('influences/influence_detail.html', context, RequestContext(request))
119 def influence_comment(request, object_id, parent_id=None):
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.
125 reply_to = get_object_or_404(ThreadedComment, id=parent_id)
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
138 kwargs['prefix'] = "reply-to-%s" % (parent_id)
139 return free_comment(request, object_id=object_id, parent_id=parent_id, **kwargs)
141 # vim: set sts=4 sw=4 expandtab: