Skip to content
This repository has been archived by the owner on Aug 26, 2022. It is now read-only.

Commit

Permalink
Bug 1110801 - Don't allow creating multiple identical document/attac…
Browse files Browse the repository at this point in the history
…hment M2M relationships.
  • Loading branch information
jezdez committed Mar 3, 2016
1 parent 93db364 commit b14c0b4
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 7 deletions.
7 changes: 0 additions & 7 deletions kuma/attachments/models.py
Expand Up @@ -60,13 +60,6 @@ def attach(self, document, user, revision):
try:
document_attachment = (document.files.through.objects
.get(pk=self.pk))
except MultipleObjectsReturned:
# There may be multiple uploaded files referenced in the document
# content which could have created multiple of the intermediates
# TODO: what to do with the others?
document_attachment = (document.files.through.objects
.filter(pk=self.pk)
.first())
except document.files.through.DoesNotExist:
# no previous uploads found, create a new document-attachment
document.files.through.objects.create(file=self,
Expand Down
9 changes: 9 additions & 0 deletions kuma/wiki/models.py
Expand Up @@ -189,6 +189,15 @@ class Meta:
def __unicode__(self):
return u'"%s" for document "%s"' % (self.file, self.document)

def clean(self):
if self.pk and (self.document.files.through.objects.exclude(pk=self.pk)
.exists()):
raise ValidationError(
_("Attachment %(attachment_id)s can't be attached "
"multiple times to document %(document_id)s") %
{'attachment_id': self.pk, 'document_id': self.document.pk}
)


@register_live_index
class Document(NotificationsMixin, models.Model):
Expand Down

0 comments on commit b14c0b4

Please sign in to comment.