'File attachments are stored at the DB level and synced here
with the document\'s HTML content.
We find them by regex-searching over the HTML for URLs that match the
file URL patterns.'

def populate_attachments(self):

    mt_files = DEKI_FILE_URL.findall(self.html)
    kuma_files = KUMA_FILE_URL.findall(self.html)
    params = None
    if mt_files:
        params = models.Q(mindtouch_attachment_id__in=mt_files)
        if kuma_files:
            params = (params | models.Q(id__in=kuma_files))
    if (kuma_files and (not params)):
        params = models.Q(id__in=kuma_files)
    Attachment = apps.get_model('attachments', 'Attachment')
    if params:
        found_attachments = Attachment.objects.filter(params)
    else:
        found_attachments = Attachment.objects.none()
    self.attached_files.filter(is_original=False).delete()
    self.attached_files.all().update(is_linked=False)
    '\n                                three    options    of    state:\n\n                                -    linked    in    the    document,    but    not    originally    uploaded\n                                -    linked    in    the    document    and    originally    uploaded\n                                -    not    linked    in    the    document,    but    originally    uploaded\n                                '
    populated = []
    for attachment in found_attachments.only('pk', 'current_revision').iterator():
        revision = attachment.current_revision
        (relation, created) = self.files.through.objects.update_or_create(file_id=attachment.pk, document_id=self.pk, defaults={'attached_by': revision.creator, 'name': revision.filename, 'is_linked': True})
        populated.append((relation, created))
    return populated
