Skip to content

Commit

Permalink
feat: code can save multiple new name-email contrinutors at the same …
Browse files Browse the repository at this point in the history
…time
  • Loading branch information
lc-hd committed Nov 14, 2024
1 parent 2c7afba commit 04b30f7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
6 changes: 6 additions & 0 deletions communities/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -973,11 +973,17 @@ def edit_project(request, pk, project_uuid):
community_id=pk
)

# save additional contributors that are already in the hub
instances = formset.save(commit=False)
for instance in instances:
instance.project = data
instance.save()

save_new_contributors_by_name_and_email(
post_query_dict=request.POST,
project=data
)

# Add selected contributors to the ProjectContributors object
add_to_contributors(request, community, data)
return redirect('community-project-actions', community.id, project.unique_id)
Expand Down
4 changes: 2 additions & 2 deletions localcontexts/static/javascript/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ document.addEventListener('DOMContentLoaded', (event) => {
}

function initializeEditProject() {
let index = document.querySelectorAll('.additional_contributors_group').length
let index = 0

const addContributorBtn = document.getElementById('add-contributor-by-name-email-btn')
addContributorBtn.addEventListener('click', selectContributorsByNameAndEmail)
Expand All @@ -1143,7 +1143,7 @@ document.addEventListener('DOMContentLoaded', (event) => {

function selectContributorsByNameAndEmail() {
// get current name and email
const prefixIdentifier = `additional_contributors-${index}`
const prefixIdentifier = `new_additional_contributors-${index}`

if(!currentContributorName.reportValidity() || !currentContributorEmail.reportValidity()) {
// if the name or email is not valid, return
Expand Down
28 changes: 27 additions & 1 deletion projects/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.http import QueryDict

from researchers.models import Researcher
from institutions.models import Institution
from communities.models import Community
from projects.models import ProjectActivity, ProjectContributors
from projects.models import ProjectActivity, ProjectContributors, ProjectPerson, Project
from helpers.emails import send_contributor_email
from notifications.utils import send_simple_action_notification
from helpers.models import ProjectStatus
Expand Down Expand Up @@ -199,3 +201,27 @@ def can_download_project(request, project_creator_instance):
if not project_creator_instance.community.is_approved:
can_download = False
return can_download


def save_new_contributors_by_name_and_email(post_query_dict: QueryDict, project: Project):
"""
Saves all of new additional_contributors by name and email
Args:
post_query_dict: request POST query dict
project: project associated with the ProjectPerson
"""

i = 0
while True:
name = post_query_dict.get(f'new_additional_contributors-{i}-name')
email = post_query_dict.get(f'new_additional_contributors-{i}-email')
if name is None or email is None:
break

ProjectPerson(
name=name,
email=email,
project=project
).save()
i += 1

0 comments on commit 04b30f7

Please sign in to comment.