django.forms.formset_factory - python examples

Here are the examples of the python api django.forms.formset_factory taken from open source projects. By voting up you can indicate which examples are most useful and appropriate.

20 Examples 7

3 View Complete Implementation : views.py
Copyright GNU General Public License v3.0
Author : ubccr
    def get(self, request, *args, **kwargs):
        project_obj = get_object_or_404(Project, pk=self.kwargs.get('project_pk'))

        grants_to_delete = self.get_grants_to_delete(project_obj)
        context = {}

        if grants_to_delete:
            formset = formset_factory(GrantDeleteForm, max_num=len(grants_to_delete))
            formset = formset(initial=grants_to_delete, prefix='grantform')
            context['formset'] = formset

        context['project'] = project_obj
        return render(request, self.template_name, context)

3 View Complete Implementation : views.py
Copyright GNU General Public License v3.0
Author : ubccr
    def get(self, request, *args, **kwargs):
        pk = self.kwargs.get('pk')
        project_obj = get_object_or_404(Project, pk=pk)

        users_to_remove = self.get_users_to_remove(project_obj)
        context = {}

        if users_to_remove:
            formset = formset_factory(
                ProjectRemoveUserForm, max_num=len(users_to_remove))
            formset = formset(initial=users_to_remove, prefix='userform')
            context['formset'] = formset

        context['project'] = get_object_or_404(Project, pk=pk)
        return render(request, self.template_name, context)

3 View Complete Implementation : views.py
Copyright GNU General Public License v3.0
Author : ubccr
    def get(self, request, *args, **kwargs):
        project_obj = get_object_or_404(
            Project, pk=self.kwargs.get('project_pk'))

        publications_do_delete = self.get_publications_to_delete(project_obj)
        context = {}

        if publications_do_delete:
            formset = formset_factory(
                PublicationDeleteForm, max_num=len(publications_do_delete))
            formset = formset(initial=publications_do_delete,
                              prefix='publicationform')
            context['formset'] = formset

        context['project'] = project_obj
        return render(request, self.template_name, context)

3 View Complete Implementation : edit.py
Copyright Apache License 2.0
Author : Wenvki
    def _create_formset(self, extra=0):
        from django.forms import formset_factory
        #from django.forms import BaseFormSet
        FormSet = formset_factory(self.get_form_clast(), extra=extra)
        formset = FormSet(form_kwargs=self.get_form_kwargs())
        return formset

0 View Complete Implementation : forms.py
Copyright Apache License 2.0
Author : chrisjrn
def ProductsForm(category, products):
    ''' Produces an appropriate _ProductsForm subclast for the given render
    type. '''

    # Each Category.RENDER_TYPE value has a subclast here.
    cat = inventory.Category
    RENDER_TYPES = {
        cat.RENDER_TYPE_QUANsatY: _QuansatyBoxProductsForm,
        cat.RENDER_TYPE_RADIO: _RadioButtonProductsForm,
        cat.RENDER_TYPE_ITEM_QUANsatY: _ItemQuansatyProductsForm,
        cat.RENDER_TYPE_CHECKBOX: _CheckboxProductsForm,
    }

    # Produce a subclast of _ProductsForm which we can alter the base_fields on
    clast ProductsForm(RENDER_TYPES[category.render_type]):
        past

    products = list(products)
    products.sort(key=lambda prod: prod.order)

    ProductsForm.set_fields(category, products)

    if category.render_type == inventory.Category.RENDER_TYPE_ITEM_QUANsatY:
        ProductsForm = forms.formset_factory(
            ProductsForm,
            formset=_ItemQuansatyProductsFormSet,
        )

    return ProductsForm

0 View Complete Implementation : forms.py
Copyright Apache License 2.0
Author : chrisjrn
def staff_products_formset_factory(user):
    ''' Creates a formset of StaffProductsForm for the given user. '''
    form_type = staff_products_form_factory(user)
    return forms.formset_factory(form_type)

0 View Complete Implementation : views.py
Copyright GNU General Public License v3.0
Author : clayball
def status(request, sup_hosts=False, sup_ports=False, sup_events=False, sup_vulns=False, sup_malware=False, changing_db=False):
    installation_complete = False

    subnets_installed = os.path.isfile('subnets.txt')

    hosts_installed = Host.objects.all().exists()
    vulns_installed = Vulnerability.objects.all().exists()
    events_installed = Event.objects.all().exists()
    malware_installed = Malware.objects.all().exists()
    ports_installed = Host.objects.all().filter(ports__icontains='"').exists()

    if hosts_installed and vulns_installed and events_installed \
       and ports_installed and malware_installed:
        installation_complete = True

    context = {'subnets_installed' : subnets_installed,
               'hosts_installed'   : hosts_installed,
               'vulns_installed'   : vulns_installed,
               'events_installed'  : events_installed,
               'malware_installed' : malware_installed,
               'ports_installed'   : ports_installed,
               'installation_complete' : installation_complete,
               'sup_hosts'   : sup_hosts,
               'sup_ports'   : sup_ports,
               'sup_events'  : sup_events,
               'sup_vulns'   : sup_vulns,
               'sup_malware' : sup_malware,
               'changing_db' : changing_db,
               'db_type' : connection.vendor}

    if sup_events:
        context['events_form'] = formset_factory(EventForm)
        context['extra_forms'] = 1

    if sup_vulns:
        context['vulns_form'] = formset_factory(VulnForm)
        context['extra_forms'] = 1

    if sup_malware:
        context['mals_form'] = formset_factory(MalwareForm)
        context['extra_forms'] = 1

    return render(request, 'nector_home/status.html', context)

0 View Complete Implementation : views.py
Copyright GNU General Public License v3.0
Author : clayball
def submit_events(request):
    if not request.POST:
        return status(request, sup_events=True)

    extra_forms = 1

    if 'add-additional-event' in request.POST:

        extra_forms = int(request.POST['num_extra_forms']) + 1
        events_formset = formset_factory(EventForm, extra=extra_forms)

        installation_complete = False

        subnets_installed = os.path.isfile('subnets.txt')

        hosts_installed = Host.objects.all().exists()
        vulns_installed = Vulnerability.objects.all().exists()
        events_installed = Event.objects.all().exists()
        malware_installed = Malware.objects.all().exists()
        ports_installed = Host.objects.all().filter(ports__icontains='"').exists()

        if hosts_installed and vulns_installed and events_installed \
           and ports_installed and malware_installed:
            installation_complete = True

        context = {'subnets_installed' : subnets_installed,
                   'hosts_installed'   : hosts_installed,
                   'vulns_installed'   : vulns_installed,
                   'events_installed'  : events_installed,
                   'malware_installed' : malware_installed,
                   'ports_installed'   : ports_installed,
                   'installation_complete' : installation_complete,
                   'db_type' : connection.vendor}

        context['events_form'] = events_formset
        context['sup_events'] = True
        context['extra_forms'] = extra_forms

        return render(request, 'nector_home/status.html', context)

    elif 'event_file' in request.FILES:
        input_file = request.FILES['event_file'].read()
        with open('events.csv', 'w') as events_csv:
            for line in input_file:
                events_csv.write(line)
            events_csv.close()
        update_db()
        return status(request)

    else:
        extra_forms = int(request.POST['num_extra_forms'])
        events_formset = formset_factory(EventForm, extra=extra_forms)
        events_formset = events_formset(request.POST)
        with open('events.csv', 'w') as events_csv:
            events_csv.write('Request Number,Date Submitted,satle,Status,Last Edit Date,Submitted By,astignees\n')
            for f in events_formset:
                if f.is_valid():
                    try:
                        instance = f.save(commit=False)
                        instance.save()
                    except Exception as e:
                        print e
                    clean = f.cleaned_data
                    try:
                        line = '%s,%s,%s,%s,%s,%s,%s\n' % (clean['request_number'],
                                                         clean['date_submitted'],
                                                         clean['satle'],
                                                         clean['status'],
                                                         clean['date_last_edited'],
                                                         clean['submitters'],
                                                         clean['astignees'])
                        events_csv.write(line)
                    except Exception as e:
                        print e
            events_csv.close()
        return status(request)

0 View Complete Implementation : views.py
Copyright GNU General Public License v3.0
Author : clayball
def submit_vulns(request):
    if not request.POST:
        return status(request, sup_vulns=True)

    extra_forms = 1

    if 'add-additional-event' in request.POST:

        extra_forms = int(request.POST['num_extra_forms']) + 1
        vulns_formset = formset_factory(VulnForm, extra=extra_forms)

        installation_complete = False

        subnets_installed = os.path.isfile('subnets.txt')

        hosts_installed = Host.objects.all().exists()
        vulns_installed = Vulnerability.objects.all().exists()
        events_installed = Event.objects.all().exists()
        malware_installed = Malware.objects.all().exists()
        ports_installed = Host.objects.all().filter(ports__icontains='"').exists()

        if hosts_installed and vulns_installed and events_installed \
           and ports_installed and malware_installed:
            installation_complete = True

        context = {'subnets_installed' : subnets_installed,
                   'hosts_installed'   : hosts_installed,
                   'vulns_installed'   : vulns_installed,
                   'events_installed'  : events_installed,
                   'malware_installed' : malware_installed,
                   'ports_installed'   : ports_installed,
                   'installation_complete' : installation_complete,
                   'db_type' : connection.vendor}

        context['vulns_form'] = vulns_formset
        context['sup_vulns'] = True
        context['extra_forms'] = extra_forms

        return render(request, 'nector_home/status.html', context)

    elif 'vuln_file' in request.FILES:
        input_file = request.FILES['vuln_file'].read()
        with open('vulnlist.csv', 'w') as vulnlist_csv:
            for line in input_file:
                vulnlist_csv.write(line)
            vulnlist_csv.close()
        update_db()
        return status(request)

    else:
        extra_forms = int(request.POST['num_extra_forms'])
        vulns_formset = formset_factory(VulnForm, extra=extra_forms)
        vulns_formset = vulns_formset(request.POST)
        with open('vulnlist.csv', 'w') as vulnlist_csv:
            vulnlist_csv.write('"Plugin","Plugin Name","Severity","IP Address","DNS Name"\n')
            for f in vulns_formset:
                if f.is_valid():
                    clean = f.cleaned_data
                    try:
                        line = '"%s","%s","%s","%s","%s"\n' % (clean['plugin_id'],
                                                         clean['plugin_name'],
                                                         clean['severity'],
                                                         clean['ipv4_address'],
                                                         clean['host_name'],)

                        vulnlist_csv.write(line)
                    except Exception as e:
                        print e
            vulnlist_csv.close()
        update_db()
        return status(request)

0 View Complete Implementation : views.py
Copyright GNU General Public License v3.0
Author : clayball
def submit_malware(request):
    if not request.POST:
        return status(request, sup_malware=True)

    extra_forms = 1

    if 'add-additional-event' in request.POST:

        extra_forms = int(request.POST['num_extra_forms']) + 1
        malware_formset = formset_factory(MalwareForm, extra=extra_forms)

        installation_complete = False

        subnets_installed = os.path.isfile('subnets.txt')

        hosts_installed = Host.objects.all().exists()
        vulns_installed = Vulnerability.objects.all().exists()
        events_installed = Event.objects.all().exists()
        malware_installed = Malware.objects.all().exists()
        ports_installed = Host.objects.all().filter(ports__icontains='"').exists()

        if hosts_installed and vulns_installed and events_installed \
           and ports_installed and malware_installed:
            installation_complete = True

        context = {'subnets_installed' : subnets_installed,
                   'hosts_installed'   : hosts_installed,
                   'vulns_installed'   : vulns_installed,
                   'events_installed'  : events_installed,
                   'malware_installed' : malware_installed,
                   'ports_installed'   : ports_installed,
                   'installation_complete' : installation_complete,
                   'db_type' : connection.vendor}

        context['mals_form'] = malware_formset
        context['sup_malware'] = True
        context['extra_forms'] = extra_forms

        return render(request, 'nector_home/status.html', context)

    elif 'malware_file' in request.FILES:
        input_file = request.FILES['malware_file'].read()
        with open('malware.csv', 'w') as malware_csv:
            for line in input_file:
                malware_csv.write(line)
            malware_csv.close()
        update_db()
        return status(request)

    else:
        extra_forms = int(request.POST['num_extra_forms'])
        mals_formset = formset_factory(MalwareForm, extra=extra_forms)
        mals_formset = mals_formset(request.POST)
        with open('malware.csv', 'w') as malware_csv:
            malware_csv.write('AlertID,AlertType,File,Computer,NumericIP,ContactGroup,Virus,ActualAction,Comment\n')
            for f in mals_formset:
                if f.is_valid():
                    clean = f.cleaned_data
                    try:
                        line = '%s,%s,%s,%s,%s,%s,%s,%s,%s\n' % (clean['alert_id'],
                                                         clean['alert_type'],
                                                         clean['file_name'],
                                                         clean['computer'],
                                                         clean['numeric_ip'],
                                                         clean['contact_group'],
                                                         clean['virus'],
                                                         clean['actual_action'],
                                                         clean['comment'])

                        malware_csv.write(line)
                    except Exception as e:
                        print e
            malware_csv.close()
        update_db()
        return status(request)