def validate(self, attrs):
# this is defined as a pre_save because cant be checked in clean() as
# there is not yet instance.message at this step.
same_named = attrs['message'].attachments.filter(
file__endswith='/{}'.format(attrs['file'].name))
if same_named.exists():
raise ValidationError((
'A file with the same name already exists '
'for this message : {}').format(same_named.first()))
# Now check whether we would fit within the total allowed
# attachment size for the message
total_size = 0
for attachment in attrs['message'].attachments.all():
total_size += attachment.size
total_size += attrs['file'].size
if total_size > settings.CAMPAIGNS['ATTACHMENT_TOTAL_MAX_SIZE']:
raise ValidationError(_(
'Can not accept this file. Total attachment size for this '
'message ({} bytes) would exceed the maximum allowed size of '
'{} bytes'.format(
total_size, settings.CAMPAIGNS[
'ATTACHMENT_TOTAL_MAX_SIZE'])))
return attrs
评论列表
文章目录