forked from adamlaska/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfields.py
More file actions
35 lines (29 loc) · 1.17 KB
/
fields.py
File metadata and controls
35 lines (29 loc) · 1.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# Copyright The IETF Trust 2012-2020, All Rights Reserved
# -*- coding: utf-8 -*-
from django.conf import settings
from django.db import models
from django.utils.encoding import smart_str
from ietf.utils.pipe import pipe
from ietf.utils.log import log
class EncryptedException(Exception):
pass
class EncryptedTextField(models.TextField):
def pre_save(self, instance, add):
if add:
comments = smart_str(getattr(instance, 'comments'))
nomcom = getattr(instance, 'nomcom')
try:
cert_file = nomcom.public_key.path
except ValueError as e:
raise ValueError("Trying to read the NomCom public key: " + str(e))
command = "%s smime -encrypt -in /dev/stdin %s" % (settings.OPENSSL_COMMAND, cert_file)
code, out, error = pipe(command, comments.encode('utf-8'))
if code != 0:
log("openssl error: %s:\n Error %s: %s" %(command, code, error))
if not error:
instance.comments = out
return out
else:
raise EncryptedException(error)
else:
return instance.comments