def issue_add_edit(request, polity_id, issue_id=None, documentcontent_id=None): polity = get_object_or_404(Polity, id=polity_id) # Make sure that user is allowed to do this. if polity.is_newissue_only_officers and request.user not in polity.officers.all(): raise PermissionDenied() if issue_id: issue = get_object_or_404(Issue, id=issue_id, polity_id=polity_id) current_content = issue.documentcontent # We don't want to edit anything that has already been processed. if issue.is_processed: raise PermissionDenied() else: issue = Issue(polity=polity) if documentcontent_id: try: current_content = DocumentContent.objects.select_related('document').get(id=documentcontent_id) except DocumentContent.DoesNotExist: raise Http404 else: current_content = None if request.method == 'POST': form = IssueForm(request.POST, instance=issue) if form.is_valid(): issue = form.save(commit=False) issue.apply_ruleset() issue.documentcontent = current_content issue.special_process_set_by = request.user if issue.special_process else None issue.save() issue.topics.clear() for topic in request.POST.getlist('topics'): issue.topics.add(topic) return redirect(reverse('issue', args=(polity_id, issue.id))) else: # Check if we need to inherit information from previous documentcontent. if not issue_id and current_content: name = current_content.name selected_topics = [] # If this is a new issue, being made from existing content, we # want to inherit the previously selected topics, and add a # version number to the name. if current_content.order > 1: name += u', %s %d' % (_(u'version'), current_content.order) selected_topics = current_content.previous_topics() form = IssueForm(instance=issue, initial={ 'name': name, 'description': current_content.comments.replace("\n", "\\n"), 'topics': selected_topics, }) else: form = IssueForm(instance=issue) # Make only topics from this polity available. form.fields['topics'].queryset = Topic.objects.filter(polity=polity) ctx = { 'polity': polity, 'issue': issue, 'form': form, 'documentcontent': current_content, 'tab': 'diff' if current_content.order > 1 else '', } return render(request, 'issue/issue_form.html', ctx)