class BaseAlbum(models.Model): class Meta(object): abstract = True ordering = ('order', 'created', 'name') permissions = ( ('moderate_albums', 'View, update and delete any album'), ) user = models.ForeignKey(get_user_model_name(), verbose_name=_('User'), null=True, blank=True, related_name='albums') name = models.CharField(_('Name'), max_length=100, blank=False, null=False) brief = models.CharField(_('Brief'), max_length=255, blank=True, default='', help_text=_('Short description')) created = models.DateTimeField(_('Created'), auto_now_add=True) updated = models.DateTimeField(_('Updated'), auto_now=True) is_public = models.BooleanField(_('Is public'), default=True) head = models.ForeignKey(get_model_string('Image'), verbose_name=_('Head'), related_name='head_of', null=True, blank=True, on_delete=models.SET_NULL) order = models.IntegerField(_('Order'), default=0) def get_head(self): if self.head: return self.head else: if self.images.all().count()>0: self.head = self.images.all()[0] self.save() return self.head else: return None @permalink def get_absolute_url(self): return 'imagestore:album', (), {'album_id': self.id} def __str__(self): return self.name def admin_thumbnail(self): img = self.get_head() if img: try: return '<img src="%s">' % get_thumbnail(img.image, '100x100', crop='center').url except IOError: logger.exception('IOError for album %s', img.image) return 'IOError' return _('Empty album') admin_thumbnail.short_description = _('Head') admin_thumbnail.allow_tags = True
class BaseImage(models.Model): class Meta(object): abstract = True ordering = ('order', 'id') permissions = (('moderate_images', 'View, update and delete any image'), ) title = models.CharField(_('Title'), max_length=255, blank=True, null=True) description = models.TextField(_('Description'), blank=True, null=True) tags = TagField(_('Tags'), blank=True) order = models.IntegerField(_('Order'), default=0) image = ImageField(verbose_name=_('File'), max_length=255, upload_to=FilePathGenerator(to=UPLOAD_TO)) user = models.ForeignKey(get_user_model_name(), verbose_name=_('User'), null=True, blank=True, related_name='images') created = models.DateTimeField(_('Created'), auto_now_add=True, null=True) updated = models.DateTimeField(_('Updated'), auto_now=True, null=True) album = models.ForeignKey(swapper.get_model_name('imagestore', 'Album'), verbose_name=_('Album'), null=True, blank=True, related_name='images') @permalink def get_absolute_url(self): return 'imagestore:image', (), {'pk': self.id} def __str__(self): return '%s' % self.id def admin_thumbnail(self): try: return '<img src="%s">' % get_thumbnail( self.image, '100x100', crop='center').url except IOError: logger.exception('IOError for image %s', self.image) return 'IOError' except ThumbnailError as ex: return 'ThumbnailError, %s' % ex.message admin_thumbnail.short_description = _('Thumbnail') admin_thumbnail.allow_tags = True
def setup_imagestore_permissions(instance, created, **kwargs): if not created: return try: Album = swapper.load_model('imagestore', 'Album') Image = swapper.load_model('imagestore', 'Image') perms = [] for model_class in [Album, Image]: for perm_name in ['add', 'change', 'delete']: app_label, model_name = model_class._meta.app_label, model_class.__name__.lower( ) perm = Permission.objects.get_by_natural_key( '%s_%s' % (perm_name, model_name), app_label, model_name) perms.append(perm) instance.user_permissions.add(*perms) except ObjectDoesNotExist: # Permissions are not yet installed or content does not created yet # probaly this is first pass if SELF_MANAGE: if django.VERSION[:2] >= (1, 7): post_save.connect(setup_imagestore_permissions, get_user_model_name()) else: post_save.connect(setup_imagestore_permissions, get_user_model())
try: Album = swapper.load_model('imagestore', 'Album') Image = swapper.load_model('imagestore', 'Image') image_applabel, image_classname = Image._meta.app_label, Image.__name__.lower() album_applabel, album_classname = Album._meta.app_label, Album.__name__.lower() image_ctype = ContentType.objects.get(app_label=image_applabel, model=image_classname) album_ctype = ContentType.objects.get(app_label=album_applabel, model=album_classname) add_image_permission = Permission.objects.get(codename='add_%s' % image_classname, content_type=image_ctype) add_album_permission = Permission.objects.get(codename='add_%s' % album_classname, content_type=album_ctype) change_image_permission = Permission.objects.get(codename='change_%s' % image_classname, content_type=image_ctype) change_album_permission = Permission.objects.get(codename='change_%s' % album_classname, content_type=album_ctype) delete_image_permission = Permission.objects.get(codename='delete_%s' % image_classname, content_type=image_ctype) delete_album_permission = Permission.objects.get(codename='delete_%s' % album_classname, content_type=album_ctype) instance.user_permissions.add(add_image_permission, add_album_permission,) instance.user_permissions.add(change_image_permission, change_album_permission,) instance.user_permissions.add(delete_image_permission, delete_album_permission,) except ObjectDoesNotExist: # Permissions are not yet installed or content does not created yet # probaly this is first pass if SELF_MANAGE: if django.VERSION[:2] >= (1, 7): post_save.connect(setup_imagestore_permissions, get_user_model_name()) else: post_save.connect(setup_imagestore_permissions, get_user_model())