Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/multiple targets in org bulk upload via csv #1109

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions web/targetApp/templates/target/add.html
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@
<div class="alert alert-primary border-0 mb-4" role="alert">
Your txt file must have list of domains separated by a new line.
<br><br>
By default all domains imported from txt will have no description. If you choose to import multiple domains with description, csv import is recommended.
By default all domains imported from txt will have no description and no organization. If you choose to import multiple domains with description and/or organization, csv import is recommended.
</div>
<form method="post" enctype="multipart/form-data">
<div class="mb-3">
Expand All @@ -182,7 +182,7 @@ <h6 id="selectedTextFileName" class="text-primary"></h6>
<div class="row">
<div class="col-12">
<div class="alert alert-warning border-0 mb-4" role="alert">
Your csv file must be in the format of <strong>domain, description</strong> separated by a new line.
Your csv file must be in the format of <strong>domain, description, organization</strong> separated by a new line.
</div>
<form method="post" enctype="multipart/form-data">
<div class="mb-3">
Expand Down
17 changes: 15 additions & 2 deletions web/targetApp/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -191,18 +191,31 @@ def add_target(request, slug):
io_string = io.StringIO(csv_content)
for column in csv.reader(io_string, delimiter=','):
domain = column[0]
description = None if len(column) == 1 else column[1]
description = None if len(column) <= 1 else column[1]
organization = None if len(column) <= 2 else column[2]
domain_query = Domain.objects.filter(name=domain)
if not domain_query.exists():
if not validators.domain(domain):
messages.add_message(request, messages.ERROR, f'Domain {domain} is not a valid domain name. Skipping.')
continue
Domain.objects.create(
domain_obj = Domain.objects.create(
name=domain,
project=project,
description=description,
insert_date=timezone.now())
added_target_count += 1

# Optionally add domain to organization
if organization:
organization_query = Organization.objects.filter(name=organization)
if organization_query.exists():
organization = organization_query[0]
else:
organization = Organization.objects.create(
name=organization,
project=project,
insert_date=timezone.now())
organization.domains.add(domain_obj)

except Exception as e:
logger.exception(e)
Expand Down