prevent duplicate imports
This commit is contained in:
parent
fe1ddf1237
commit
906da25301
@ -10,7 +10,7 @@ from django.http import HttpResponseRedirect, HttpResponse
|
||||
from django.urls import reverse
|
||||
from django.utils.formats import date_format
|
||||
from django.utils.translation import gettext as _
|
||||
from cookbook.models import Keyword
|
||||
from cookbook.models import Keyword, Recipe
|
||||
|
||||
|
||||
class Integration:
|
||||
@ -81,6 +81,7 @@ class Integration:
|
||||
:param files: List of in memory files
|
||||
:return: HttpResponseRedirect to the recipe search showing all imported recipes
|
||||
"""
|
||||
ignored_recipes = []
|
||||
try:
|
||||
self.files = files
|
||||
for f in files:
|
||||
@ -90,15 +91,32 @@ class Integration:
|
||||
if self.import_file_name_filter(z):
|
||||
recipe = self.get_recipe_from_file(BytesIO(import_zip.read(z.filename)))
|
||||
recipe.keywords.add(self.keyword)
|
||||
if duplicate := self.is_duplicate(recipe):
|
||||
ignored_recipes.append(duplicate)
|
||||
import_zip.close()
|
||||
else:
|
||||
recipe = self.get_recipe_from_file(f.file)
|
||||
recipe.keywords.add(self.keyword)
|
||||
if duplicate := self.is_duplicate(recipe):
|
||||
ignored_recipes.append(duplicate)
|
||||
except BadZipFile:
|
||||
messages.add_message(self.request, messages.ERROR, _('Importer expected a .zip file. Did you choose the correct importer type for your data ?'))
|
||||
|
||||
if len(ignored_recipes) > 0:
|
||||
messages.add_message(self.request, messages.WARNING, _('The following recipes were ignored because they already existed:') + ' ' + ', '.join(ignored_recipes))
|
||||
return HttpResponseRedirect(reverse('view_search') + '?keywords=' + str(self.keyword.pk))
|
||||
|
||||
def is_duplicate(self, recipe):
|
||||
"""
|
||||
Checks if a recipe is already present, if so deletes it
|
||||
:param recipe: Recipe object
|
||||
"""
|
||||
if Recipe.objects.filter(space=self.request.space, name=recipe.name).exists():
|
||||
recipe.delete()
|
||||
return recipe.name
|
||||
else:
|
||||
return None
|
||||
|
||||
@staticmethod
|
||||
def import_recipe_image(recipe, image_file):
|
||||
"""
|
||||
|
Loading…
Reference in New Issue
Block a user