def save(self): data = self.cleaned_data filename = generate_filename(data['file'].name) handler = FileHandlerDocument() handler.load(filename, data['file']) document = DocumentPlayerRepertoryItem(name=data['file'].name, document=filename, player_repertory_item_id=data['player_repertory_item']) if handler.is_image(): handler.save_thumbnails('PNG') document.type = DocumentType.image document.save() return document
class DocumentPlayerRepertoryItem(models.Model): name = models.CharField(max_length=64) player_repertory_item = models.ForeignKey(PlayerRepertoryItem, related_name='documents') document = models.CharField(max_length=255) notes = models.TextField(null=True, blank=True) type = models.SmallIntegerField(default=DocumentType.other) class Meta: unique_together = ('name', 'player_repertory_item') def __unicode__(self): return self.name def __init__(self, *args, **kwargs): super(DocumentPlayerRepertoryItem, self).__init__(*args, **kwargs) self.file_handler = FileHandlerDocument() if self.document: self.file_handler.load(self.document) @property def is_image(self): return self.type == DocumentType.image @property def url(self): return self.file_handler.single_url() @property def icon_url(self): if self.is_image: return self.file_handler.url('icon') else: return "/media/img/document_icon_16.png" @classmethod def pre_delete(cls, instance, **kwargs): instance.file_handler.delete()