def simple_ajax_form(request, url_name, instance, form_class, **kwargs): if request.method == 'POST': form = form_class(request.POST, instance=instance) if form.is_valid(): if kwargs.get('update_datestamp', False): instance.updated_at = datetime.datetime.now() if kwargs.get('update_bonafide_flag', False): instance.has_bonafide_edits = True form.save() if kwargs.get('on_success'): kwargs['on_success'](form) if kwargs.get('ajax_submit') and request_is_ajax(request): return HttpResponse('OK', content_type='text/plain') else: return HttpResponseRedirect(instance.get_absolute_url()) else: form = form_class(instance=instance) title = kwargs.get('title') if title and title.endswith(':'): clean_title = title[:-1] else: clean_title = title return render(request, 'shared/simple_form.html', { 'form': form, 'html_form_class': kwargs.get('html_form_class'), 'title': title, 'html_title': clean_title, 'action_url': reverse(url_name, args=[instance.id]), 'ajax_submit': kwargs.get('ajax_submit'), })
def replacement_view_func(request, *args, **kwargs): if request_is_ajax(request): # output the 'sorry' message on a template, # rather than doing a redirect (which screws with AJAX) return render(request, 'read_only_mode.html') else: messages.error( request, "Sorry, the website is in read-only mode at the moment. " "We'll get things back to normal as soon as possible.") return HttpResponseRedirect('/')
def add_credit(request, production_id): production = get_object_or_404(Production, id=production_id) if not production.editable_by_user(request.user): raise PermissionDenied if request.method == 'POST': nick_form = ProductionCreditedNickForm(request.POST, production=production) credit_formset = CreditFormSet(request.POST, queryset=Credit.objects.none(), prefix="credit") if nick_form.is_valid() and credit_formset.is_valid(): credits = credit_formset.save(commit=False) if credits: nick = nick_form.cleaned_data['nick'].commit() for credit in credits: credit.nick = nick credit.production = production credit.save() credits_description = ', '.join( [credit.description for credit in credits]) description = (u"Added credit for %s on %s: %s" % (nick, production, credits_description)) Edit.objects.create(action_type='add_credit', focus=production, focus2=nick.releaser, description=description, user=request.user) production.updated_at = datetime.datetime.now() production.has_bonafide_edits = True production.save() # form.log_creation(request.user) return render_credits_update(request, production) else: nick_form = ProductionCreditedNickForm(production=production) credit_formset = CreditFormSet(queryset=Credit.objects.none(), prefix="credit") if request_is_ajax(request): return render_modal_workflow( request, 'productions/add_credit.html', 'productions/add_credit.js', { 'production': production, 'nick_form': nick_form, 'credit_formset': credit_formset, }) else: return render( request, 'productions/add_credit.html', { 'production': production, 'nick_form': nick_form, 'credit_formset': credit_formset, })
def render_credits_update(request, production): if request_is_ajax(request): credits_html = render_to_string('productions/_credits.html', { 'production': production, 'credits': production.credits_for_listing(), 'editing_credits': True, 'prompt_to_edit': settings.SITE_IS_WRITEABLE and (request.user.is_staff or not production.locked), }, request=request) return render_modal_workflow(request, None, 'productions/edit_credit_done.js', { 'credits_html': credits_html, }) else: return HttpResponseRedirect(production.get_absolute_url() + "?editing=credits#credits_panel")
def create(request): if request.method == 'POST': party = Party() form = PartyForm(request.POST, instance=party) if form.is_valid(): form.save() form.log_creation(request.user) if request_is_ajax(request): return HttpResponse('OK: %s' % party.get_absolute_url(), content_type='text/plain') else: messages.success(request, 'Party added') return redirect('party', party.id) else: form = PartyForm(initial={ 'name': request.GET.get('name'), 'party_series_name': request.GET.get('party_series_name'), 'scene_org_folder': request.GET.get('scene_org_folder'), }) return render(request, 'parties/create.html', { 'form': form, 'party_series_names': [ps.name for ps in PartySeries.objects.all()], })
def ajax_base_template(request): return { 'base_template_with_ajax_option': 'minimal_base.html' if request_is_ajax(request) else 'base.html' }
def get_context_data(self, **kwargs): context = super().get_context_data(**kwargs) context['is_ajax'] = request_is_ajax(self.request) return context
def edit_credit(request, production_id, nick_id): production = get_object_or_404(Production, id=production_id) if not production.editable_by_user(request.user): raise PermissionDenied nick = get_object_or_404(Nick, id=nick_id) credits = production.credits.filter(nick=nick).extra( select={ 'category_order': "CASE WHEN category = 'Other' THEN 'zzzother' ELSE category END" }).order_by('category_order') if request.method == 'POST': nick_form = ProductionCreditedNickForm(request.POST, nick=nick, production=production) credit_formset = CreditFormSet(request.POST, queryset=credits, prefix="credit") if nick_form.is_valid() and credit_formset.is_valid(): updated_credits = credit_formset.save(commit=False) # make sure that each credit has production and nick populated for credit in updated_credits: credit.nick = nick credit.production = production credit.save() if 'nick' in nick_form.changed_data: # need to update the nick field of all credits in the set # (not just the ones that have been updated by credit_formset.save) nick = nick_form.cleaned_data['nick'].commit() credits.update(nick=nick) # since we're using commit=False we must manually delete the # deleted credits for credit in credit_formset.deleted_objects: credit.delete() production.updated_at = datetime.datetime.now() production.has_bonafide_edits = True production.save() new_credits = Credit.objects.filter(nick=nick, production=production) credits_description = ', '.join( [credit.description for credit in new_credits]) Edit.objects.create( action_type='edit_credit', focus=production, focus2=nick.releaser, description=(u"Updated %s's credit on %s: %s" % (nick, production, credits_description)), user=request.user) return render_credits_update(request, production) else: nick_form = ProductionCreditedNickForm(nick=nick, production=production) credit_formset = CreditFormSet(queryset=credits, prefix="credit") if request_is_ajax(request): return render_modal_workflow( request, 'productions/edit_credit.html', 'productions/edit_credit.js', { 'production': production, 'nick': nick, 'nick_form': nick_form, 'credit_formset': credit_formset, }) else: return render( request, 'productions/edit_credit.html', { 'production': production, 'nick': nick, 'nick_form': nick_form, 'credit_formset': credit_formset, })