datatracker/ietf/nomcom/fields.py
Emilio Jiménez 5e3e4bfd2e Add a test to check encrypted feedback
Ver #913
 - Legacy-Id: 5157
2012-12-27 11:57:36 +00:00

35 lines
1 KiB
Python

import os
import tempfile
from django.conf import settings
from django.db import models
from ietf.utils.pipe import pipe
class EncryptedException(Exception):
pass
class EncryptedTextField(models.TextField):
def pre_save(self, instance, add):
if add:
comments = getattr(instance, 'comments')
position = getattr(instance, 'position')
cert_file = position.nomcom.public_key.path
comments_file = tempfile.NamedTemporaryFile(delete=False)
comments_file.write(comments)
comments_file.close()
code, out, error = pipe("%s smime -encrypt -in %s %s" % (settings.OPENSSL_COMMAND,
comments_file.name,
cert_file))
os.unlink(comments_file.name)
if not error:
instance.comments = out
return out
else:
raise EncryptedException(error)
else:
return instance.comments