def add(request, domain=None): DomainFormSet = modelformset_factory(DomainRequest, extra=1, max_num=10, form=DomainForm, formset=BaseDomainFormSet) DocURLFormSet = modelformset_factory(DocURL, extra=1, form=DocURLForm, formset=BaseDocURLFormSet) EnableURLFormSet = modelformset_factory(EnableURL, extra=1, form=EnableURLForm, formset=BaseEnableURLFormSet) action = 'add' has_errors = False if request.method == 'POST': # If the form has been submitted... data = request.POST docurl_formset = DocURLFormSet(request.POST, request.FILES, queryset=DocURL.objects.none(), meta=request.META) enableurl_formset = EnableURLFormSet(request.POST, request.FILES, queryset=EnableURL.objects.none(), meta=request.META) domain_formset = DomainFormSet(request.POST, request.FILES, queryset=DomainRequest.objects.none()) # did the user fill in a full form, or are they just asking for some # domains to be registered if data['asking_or_adding'] == 'asking': config_form = ConfigForm(domain_formset=domain_formset, docurl_formset=docurl_formset, enableurl_formset=enableurl_formset) action = 'ask' if domain_formset.is_valid(): domain_formset.save(commit=False) for form in domain_formset: name = form.instance.name exists = DomainRequest.objects.filter(name=name) if exists: domain = exists[0] domain.votes += 1 domain.save() else: form.save() return HttpResponseRedirect('/') # Redirect after POST else: config = Config(owner=request.user, status='requested') # A form bound to the POST data config_form = ConfigForm(request.POST, request.FILES, instance=config, domain_formset=domain_formset, docurl_formset=docurl_formset, enableurl_formset=enableurl_formset) # All validation rules pass if config_form.is_valid_all(): config_form.save_all() return HttpResponseRedirect(reverse('ispdb_details', args=[config.id])) else: has_errors = True else: docurl_formset = DocURLFormSet(queryset=DocURL.objects.none(), meta=request.META) enableurl_formset = EnableURLFormSet(queryset=EnableURL.objects.none(), meta=request.META) domain_formset = DomainFormSet(initial=[{'name': domain}], queryset=DomainRequest.objects.none()) config_form = ConfigForm(domain_formset=domain_formset, docurl_formset=docurl_formset, enableurl_formset=enableurl_formset) return render_to_response('config/enter_config.html', { 'config_form': config_form, 'action': action, 'callback': reverse('ispdb_add'), 'has_errors': has_errors }, context_instance=RequestContext(request))
def edit(request, config_id): config = get_object_or_404(Config, pk=config_id) # Validate the user if not (request.user.is_superuser or ( (config.status == 'requested' or config.status == 'suggested') and config.owner == request.user)): return HttpResponseRedirect(reverse('ispdb_login')) if config.locked: error = ("This configuration is locked. Only admins can unlock " "it.") return details(request, config.id, error=error) # Get initial data if config.domains.all(): model = Domain else: model = DomainRequest DomainFormSet = modelformset_factory(model, extra=0, max_num=10, form=DomainForm, formset=BaseDomainFormSet) domain_queryset = Domain.objects.filter(config=config) or \ DomainRequest.objects.filter(config=config) DocURLFormSet = modelformset_factory(DocURL, extra=0, form=DocURLForm, formset=BaseDocURLFormSet) docurl_queryset = config.docurl_set.all() EnableURLFormSet = modelformset_factory(EnableURL, extra=0, form=EnableURLForm, formset=BaseEnableURLFormSet) enableurl_queryset = config.enableurl_set.all() if request.method == 'POST': # A form bound to the POST data domain_formset = DomainFormSet(request.POST, request.FILES, queryset=domain_queryset) docurl_formset = DocURLFormSet(request.POST, request.FILES, queryset=docurl_queryset, meta=request.META) enableurl_formset = EnableURLFormSet(request.POST, request.FILES, queryset=enableurl_queryset, meta=request.META) config_form = ConfigForm(request.POST, request.FILES, instance=config, domain_formset=domain_formset, docurl_formset=docurl_formset, enableurl_formset=enableurl_formset) if config_form.is_valid_all(): config_form.save_all() if config.status == 'suggested': id = config.issue.all()[0].id return HttpResponseRedirect(reverse('ispdb_show_issue', args=[id])) return HttpResponseRedirect(reverse('ispdb_details', args=[config.id])) else: docurl_formset = DocURLFormSet(queryset=docurl_queryset, meta=request.META) enableurl_formset = EnableURLFormSet(queryset=enableurl_queryset, meta=request.META) domain_formset = DomainFormSet(queryset=domain_queryset) config_form = ConfigForm(instance=config, domain_formset=domain_formset, docurl_formset=docurl_formset, enableurl_formset=enableurl_formset) return render_to_response('config/enter_config.html', { 'config_form': config_form, 'action': 'edit', 'callback': reverse('ispdb_edit', args=[config.id]), }, context_instance=RequestContext(request))