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 report(request, id): config = get_object_or_404(Config, pk=id, status='approved') has_errors = False # 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 = config.domains.all() or config.domainrequests.all() 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': 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) p_config = Config(owner=request.user, status='suggested') issue = Issue(config=config, owner=request.user) config_form = ConfigForm(request.POST, request.FILES, instance=p_config, domain_formset=domain_formset, docurl_formset=docurl_formset, enableurl_formset=enableurl_formset) issue_form = IssueForm(request.POST, instance=issue) if issue_form.is_valid(): if issue_form.cleaned_data['show_form']: if config_form.is_valid_all(): # save forms manually because we don't want to update the # original objects config_form.save() # Save domains for form in domain_formset: if not form.cleaned_data or ( form.cleaned_data['DELETE']): continue name = form.cleaned_data['name'] domain = DomainRequest(name=name, config=p_config) domain.save() # Save DocURL formset docurl_formset.save(commit=False) for form in docurl_formset: form.instance.pk = None if not form.cleaned_data or ( form.cleaned_data['DELETE']): continue form.instance.config = p_config for desc_form in form.desc_formset: desc_form.instance.pk = None if not desc_form.cleaned_data or ( desc_form.cleaned_data['DELETE']): continue docurl_formset.save() # Save EnableURL formset enableurl_formset.save(commit=False) for form in enableurl_formset: form.instance.pk = None if not form.cleaned_data or ( form.cleaned_data['DELETE']): continue form.instance.config = p_config for inst_form in form.inst_formset: inst_form.instance.pk = None if not inst_form.cleaned_data or ( inst_form.cleaned_data['DELETE']): continue enableurl_formset.save() # Save Issue issue.updated_config = p_config issue_form.save() return HttpResponseRedirect(reverse('ispdb_show_issue', args=[issue.id])) else: has_errors = True else: issue_form.save() return HttpResponseRedirect(reverse('ispdb_show_issue', args=[issue.id])) else: domain_formset = DomainFormSet(queryset=domain_queryset) docurl_formset = DocURLFormSet(queryset=docurl_queryset, meta=request.META) enableurl_formset = EnableURLFormSet(queryset=enableurl_queryset, meta=request.META) config_form = ConfigForm(instance=config, domain_formset=domain_formset, docurl_formset=docurl_formset, enableurl_formset=enableurl_formset) issue = Issue(config=config) issue_form = IssueForm(instance=issue) return render_to_response('config/enter_config.html', { 'config_form': config_form, 'issue_form': issue_form, 'action': 'report', 'callback': reverse('ispdb_report', args=[id]), '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))