Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def save(self, *args, **kwargs):
update_fields=None
clean_name = self.name.split('.')
self.content.name = '{}_{}.{}'.format(self.record.identifier.replace(':','_'),clean_name[0],self.format.lower())
if self.pk is not None:
update_fields=["default","content","checksum"]
orig = Style.objects.get(pk=self.pk)
if orig.content:
if orig.checksum != self._calculate_checksum(self.content):
if os.path.isfile(orig.content.path):
os.remove(orig.content.path)
else:
update_fields = ["default"]
else:
if os.path.isfile(orig.content.path):
os.remove(orig.content.path)
if update_fields:
kwargs["update_fields"] = update_fields
super(Style, self).save(*args, **kwargs)
from django.contrib import admin
from . import models
class CollaboratorInline(admin.StackedInline):
model = models.Collaborator
extra = 1
class StyleInline(admin.StackedInline):
model = models.Style
extra = 1
@admin.register(models.Style)
class StyleAdmin(admin.ModelAdmin):
list_display = ('name','record','format',)
def get_readonly_fields(self, request, obj=None):
if obj:
return self.readonly_fields + ('name',)
return self.readonly_fields
@admin.register(models.Record)
class RecordAdmin(admin.ModelAdmin):
list_display = ("id", "identifier", "title",)
inlines = [StyleInline,]
readonly_fields = ('publication_date','modified',)
def get_readonly_fields(self, request, obj=None):
@receiver(pre_save, sender=Style)
def set_checksum (sender, instance, **kwargs):
checksum = md5.new()
checksum.update(instance.content.read())
instance.checksum = base64.b64encode(checksum.digest())
if self.pk is not None:
update_fields=["default","content","checksum"]
orig = Style.objects.get(pk=self.pk)
if orig.content:
if orig.checksum != self._calculate_checksum(self.content):
if os.path.isfile(orig.content.path):
os.remove(orig.content.path)
else:
update_fields = ["default"]
else:
if os.path.isfile(orig.content.path):
os.remove(orig.content.path)
if update_fields:
kwargs["update_fields"] = update_fields
super(Style, self).save(*args, **kwargs)
def default_style(self,format):
try:
return self.styles.get(format=format,default=True)
except Style.DoesNotExist:
return None