Esempio n. 1
0
def add_asset_view(request):
    form = None

    if request.method == 'GET':
        form = AssetForm()
    elif request.method == 'POST':
        form = AssetForm(request.POST)

        if not Asset.is_savable():
            messages.error(request,
                           'MAX_ASSETS reached. Contact Support team ;)')
            return render(request, 'add-asset.html', {'form': form})

        if form.is_valid():
            asset_args = {
                'value': encoding.unicode_escape(form.cleaned_data['value']),
                'name': form.cleaned_data['name'],
                'type': form.cleaned_data['type'],
                'criticity': form.cleaned_data['criticity'],
                'description': form.cleaned_data['description'],
                'owner': request.user,
            }
            asset = Asset(**asset_args)
            asset.save()

            # Add categories
            for cat in form.data['categories']:
                asset.categories.add(cat)
            asset.save()

            if asset.type in ['ip-range', 'ip-subnet']:
                # Create an asset group dynamically
                assetgroup_args = {
                    'name':
                    "{} assets".format(asset.name),
                    'criticity':
                    asset.criticity,
                    'description':
                    "Asset dynamically created. Imported desc: {}".format(
                        asset.description),
                    'owner':
                    request.user
                }
                asset_group = AssetGroup(**assetgroup_args)
                asset_group.save()

                # Add the asset to the new group
                asset_group.assets.add(asset)
                asset_group.save()

                # Caculate the risk grade
                asset_group.calc_risk_grade()
                asset_group.save()

            messages.success(request, 'New asset created')
            return redirect('list_assets_view')

    return render(request, 'add-asset.html', {'form': form})
Esempio n. 2
0
def add_asset_view(request):
    """Add a new asset."""
    form = None

    if request.method == 'GET':
        form = AssetForm(user=request.user)
    elif request.method == 'POST':
        form = AssetForm(request.POST, user=request.user)

        if not Asset.is_savable():
            messages.error(request, 'MAX_ASSETS reached. Contact Support team ;)')
            return render(request, 'add-asset.html', {'form': form})

        if form.is_valid():
            asset_args = {
                'value': encoding.unicode_escape(form.cleaned_data['value']),
                'name': form.cleaned_data['name'],
                'type': form.cleaned_data['type'],
                'criticity': form.cleaned_data['criticity'],
                'exposure': form.cleaned_data['exposure'],
                'description': form.cleaned_data['description'],
                'owner': request.user,
            }
            asset = Asset(**asset_args)
            asset.save()

            # Add categories (M2M)
            if len(form.cleaned_data['categories']) == 0:
                asset.categories.add(AssetCategory.objects.get(id=1))
            else:
                for cat in form.cleaned_data['categories']:
                    asset.categories.add(cat)

            # Add teams (M2M)
            if 'teams' in form.cleaned_data.keys():
                for team in form.cleaned_data['teams']:
                    asset.teams.add(team)

            # Save categories and teams
            asset.save()

            if asset.type in ['ip-range', 'ip-subnet']:
                # Create an asset group dynamically
                assetgroup_args = {
                    'name': "{} assets".format(asset.name),
                    'criticity': asset.criticity,
                    'description': "Asset dynamically created. Imported desc: {}".format(asset.description),
                    'owner': request.user
                }
                asset_group = AssetGroup(**assetgroup_args)
                asset_group.save()

                # Add the asset to the new group
                asset_group.assets.add(asset)

                # Add the teams to the new group
                for team in form.cleaned_data['teams']:
                    asset_group.teams.add(team)
                asset_group.save()

                # Caculate the risk grade
                asset_group.calc_risk_grade()
                asset_group.save()

            messages.success(request, 'New asset created')
            return redirect('detail_asset_view', asset_id=asset.id)

    return render(request, 'add-asset.html', {'form': form})