def create_amendment(request, bill_id, segment_id): segment = _get_segment_or_404(bill_id, segment_id) if not segment.is_editable(): messages.error( request, ugettext("Cannot submit proposals to {object}.").format( object=segment)) return redirect_to_segment_at_bill_page(segment) form_factory = CitizenAmendmentCreationForm form_initial_data = { 'content': segment.content, } if request.method == 'POST': form = form_factory(request.POST, initial=form_initial_data) if form.is_valid(): amendment = form.save(commit=False) amendment.bill = segment.bill amendment.order = 0 amendment.type = segment.type amendment.number = segment.number amendment.parent = segment.parent amendment.replaced = segment amendment.author = request.user amendment.original = False # TODO(NAME) what if the content is empty? or exactly like the original? # i suggest flash + ignore amendment.save() comment = form.cleaned_data.get('comment') if comment: comment = create_comment(request, amendment, request.user, comment) messages.success( request, ugettext("{object_type} submitted.").format( object_type=capfirst(BillSegment._meta.verbose_name))) return redirect(amendment.get_absolute_url()) else: form = form_factory(initial=form_initial_data) return render(request, 'bill/create_amendment.html', context=dict( form=form, segment=segment, ))
def create_amendment(request, bill_id, segment_id): segment = _get_segment_or_404(bill_id, segment_id) if not segment.is_editable(): return redirect('show_bill', pk=bill_id) if not segment.bill.status == 'published': return redirect('show_segment', bill_id=bill_id, segment_id=segment_id) form_factory = CitizenAmendmentCreationForm form_initial_data = { 'content': segment.content, } if request.method == 'POST': form = form_factory(request.POST, initial=form_initial_data) if form.is_valid(): amendment = form.save(commit=False) amendment.bill = segment.bill amendment.order = 0 amendment.type = segment.type amendment.number = segment.number amendment.parent = segment.parent amendment.replaced = segment amendment.author = request.user amendment.original = False # TODO what if the content is empty? or exactly like the original? i suggest flash + ignore amendment.save() comment = form.cleaned_data.get('comment') if comment: comment = create_comment(request, amendment, request.user, comment) messages.success(request, ugettext("{object_type} submitted.").format( object_type=capfirst(BillSegment._meta.verbose_name))) return redirect(amendment.get_absolute_url()) else: form = form_factory(initial=form_initial_data) return render(request, 'bill/create_amendment.html', context=dict( form=form, segment=segment, ))
def form_valid(self, form): amendment = BillSegment() amendment.bill_id = self.kwargs['bill_id'] amendment.type = form.cleaned_data['type'] amendment.parent = form.cleaned_data['parent'] amendment.author = self.request.user amendment.original = False amendment.content = form.cleaned_data['content'] amendment.save() comment = form.cleaned_data.get('comment') if comment: comment = create_comment(self.request, amendment, self.request.user, comment) messages.success(self.request, ugettext("{object_type} submitted.").format( object_type=capfirst(BillSegment._meta.verbose_name))) return redirect('show_bill', self.kwargs['bill_id'])