Skip to content

Commit

Permalink
Glossary: Use get_or_create for creating a glossary
Browse files Browse the repository at this point in the history
Otherwise this is prone to race conditions.

Fixes WEBLATE-4T4
  • Loading branch information
nijel committed Sep 23, 2020
1 parent c86ebf5 commit 7f44d50
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions weblate/glossary/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -313,23 +313,23 @@ def check_perm(self, user, perm):
@disable_for_loaddata
def create_glossary(sender, instance, created, **kwargs):
"""Creates glossary on project creation."""
project = instance.project
glossaries = {
glossary.source_language_id: glossary
for glossary in instance.project.glossary_set.iterator()
for glossary in project.glossary_set.iterator()
}

# Does the glossary for source language exist?
if instance.source_language_id in glossaries:
return

if glossaries:
name = "{}: {}".format(instance.project, instance.source_language.name)
name = "{}: {}".format(project, instance.source_language.name)
else:
name = instance.project
name = project.name

Glossary.objects.create(
project.glossary_set.get_or_create(
name=name,
color="silver",
project=instance.project,
source_language=instance.source_language,
)

0 comments on commit 7f44d50

Please sign in to comment.