# -*- coding: utf-8 -*-
import os.path
from django.db import models
from django.utils.translation import ugettext_lazy as _
from django.db.models.signals import pre_delete
from django.dispatch.dispatcher import receiver
from django.utils.encoding import python_2_unicode_compatible
from django.utils.six import text_type
from django.core.urlresolvers import reverse
from waliki.models import Page
from waliki.settings import WALIKI_UPLOAD_TO


@python_2_unicode_compatible
class Attachment(models.Model):
    page = models.ForeignKey(Page, related_name='attachments')
    file = models.FileField(upload_to=WALIKI_UPLOAD_TO)

    class Meta:
        verbose_name = _("Attachment")
        verbose_name_plural = _("Attachments")

    def __str__(self):
    	return os.path.basename(self.file.name)

    def get_absolute_url(self):
        return reverse('waliki_attachment_file', args=(self.page.slug, self.id, text_type(self)))


@receiver(pre_delete, sender=Attachment)
def attachment_delete(sender, instance, **kwargs):
    # Pass false so FileField doesn't save the model.
    instance.file.delete(False)
@@ -359,16 +359,75 @@
 reverse%0A
+from django.core.exceptions import SuspiciousFileOperation%0A
 from wal
@@ -1127,16 +1127,29 @@
  model.%0A
+    try:%0A    
     inst
@@ -1171,8 +1171,57 @@
 e(False)
+%0A    except SuspiciousFileOperation:%0A        pass

allow pages deletion