Skip to content

Commit

Permalink
Upgrade 2 to 3
Browse files Browse the repository at this point in the history
  • Loading branch information
mugwort-rc committed Feb 28, 2017
1 parent bdf754e commit 7572e78
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 62 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: python

python:
- "2.7"
- "3.5"

install: "pip install -r requirements.txt"

Expand Down
2 changes: 1 addition & 1 deletion pgpdb/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '0.1.0'
__version__ = '0.2.0'
4 changes: 2 additions & 2 deletions pgpdb/admin.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django.contrib import admin

import forms
import models
from . import forms
from . import models

class PGPKeyModelAdmin(admin.ModelAdmin):
list_display = ('uid', 'user', 'user_ids', 'key_id', 'is_revoked', 'crc24', 'file',)
Expand Down
2 changes: 1 addition & 1 deletion pgpdb/forms.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from django import forms
from django.utils.translation import ugettext_lazy as _

import models
from . import models

class PGPUserIDModelForm(forms.ModelForm):
class Meta:
Expand Down
32 changes: 11 additions & 21 deletions pgpdb/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,13 @@
)
from pgpdump.utils import crc24, PgpdumpException

from django_extensions.db.fields import UUIDField

import utils
from . import utils

def register_file(path, data):
return default_storage.save(path, ContentFile(data))

def unregister_file(fp):
path = os.path.dirname(fp.path)
default_storage.delete(fp)
if len(os.listdir(path)) != 0:
return
try:
os.rmdir(path)
except OSError:
pass

def read_file(fp):
return default_storage.open(fp).read()
Expand Down Expand Up @@ -67,8 +58,8 @@ def post_save(self, sender, instance, created, **kwargs):
pgp = pgpdump.BinaryData(data)
update_file(instance.file, pgp.data)
crc = crc24(pgp.data)
crc_bin = ''.join([chr((crc >> i) & 0xff) for i in [16, 8, 0]])
instance.crc24 = base64.b64encode(crc_bin)
crc_bin = crc.to_bytes(3, "big")
instance.crc24 = base64.b64encode(crc_bin).decode("utf-8")
instance.save()

# parse packets
Expand Down Expand Up @@ -109,7 +100,7 @@ def post_save(self, sender, instance, created, **kwargs):
keyid=keyid
)
elif isinstance(packet, UserIDPacket):
userid = packet.data
userid = packet.data.decode("utf-8")
last_userid = PGPUserIDModel.objects.create(
index=index,
key=instance,
Expand Down Expand Up @@ -152,7 +143,7 @@ def _pgp_key_model_upload_to(instance, filename):
return os.path.join(settings.MEDIA_ROOT, path)

class PGPKeyModel(models.Model):
uid = UUIDField()
uid = models.UUIDField()
user = models.ForeignKey(User, blank=True, null=True)
file = models.FileField(upload_to=_pgp_key_model_upload_to,
storage=default_storage)
Expand Down Expand Up @@ -257,10 +248,10 @@ class PGPPublicKeyModel(PGPPacketModel):
keyid = models.CharField(max_length=16)

def algorithm_str(self):
return unicode(self.PKA_MAP[self.algorithm])
return str(self.PKA_MAP[self.algorithm])

def simple_algorithm_str(self):
return unicode(self.SIMPLE_PKA_MAP[self.algorithm])
return str(self.SIMPLE_PKA_MAP[self.algorithm])

def is_public_key(self):
return True
Expand Down Expand Up @@ -399,17 +390,16 @@ class PGPSignatureModel(PGPPacketModel):
keyid = models.CharField(max_length=16)

def type_str(self):
return unicode(self.SIG_MAP[self.type])
return str(self.SIG_MAP[self.type])

def pka_str(self):
return unicode(self.PKA_MAP[self.pka])
return str(self.PKA_MAP[self.pka])

def simple_pka_str(self):
return unicode(self.SIMPLE_PKA_MAP[self.pka])
return str(self.SIMPLE_PKA_MAP[self.pka])

def hash_str(self):
return unicode(self.HASH_MAP[self.hash])
return str(self.HASH_MAP[self.hash])

def is_signature(self):
return True

33 changes: 16 additions & 17 deletions pgpdb/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
from django.test.client import Client, RequestFactory
from django.utils.translation import ugettext_lazy as _

import models
from . import models

import pgpdb

Expand Down Expand Up @@ -455,54 +455,54 @@ def test_algorithm_str(self):
algorithm_str = first.algorithm_str()
PKA_MAP = models.PGPPublicKeyModel.PKA_MAP
rsa_enc_sign = PKA_MAP[models.PGPPublicKeyModel.RSA_ENC_SIGN]
self.assertEqual(algorithm_str, unicode(rsa_enc_sign))
self.assertEqual(algorithm_str, str(rsa_enc_sign))

def test_simple_algorithm_str(self):
first = self.ALICE.public_keys.first()
simple_str = first.simple_algorithm_str()
SIMPLE_PKA_MAP = models.PGPPublicKeyModel.SIMPLE_PKA_MAP
simple_rsa = SIMPLE_PKA_MAP[models.PGPPublicKeyModel.RSA_ENC_SIGN]
self.assertEqual(simple_str, unicode(simple_rsa))
self.assertEqual(simple_str, str(simple_rsa))

def test_type_str(self):
first = self.ALICE.signatures.first()
type_str = first.type_str()
SIG_MAP = models.PGPSignatureModel.SIG_MAP
key_positive = SIG_MAP[models.PGPSignatureModel.KEY_POSITIVE]
self.assertEqual(type_str, unicode(key_positive))
self.assertEqual(type_str, str(key_positive))

def test_pka_str(self):
first = self.ALICE.signatures.first()
pka_str = first.pka_str()
PKA_MAP = models.PGPSignatureModel.PKA_MAP
rsa_enc_sign = PKA_MAP[models.PGPSignatureModel.RSA_ENC_SIGN]
self.assertEqual(pka_str, unicode(rsa_enc_sign))
self.assertEqual(pka_str, str(rsa_enc_sign))

def test_simple_pka_str(self):
first = self.ALICE.signatures.first()
simple_pka_str = first.simple_pka_str()
SIMPLE_PKA_MAP = models.PGPSignatureModel.SIMPLE_PKA_MAP
simple_rsa = SIMPLE_PKA_MAP[models.PGPSignatureModel.RSA_ENC_SIGN]
self.assertEqual(simple_pka_str, unicode(simple_rsa))
self.assertEqual(simple_pka_str, str(simple_rsa))

def test_hash_str(self):
first = self.ALICE.signatures.first()
hash_str = first.hash_str()
HASH_MAP = models.PGPSignatureModel.HASH_MAP
sha1 = HASH_MAP[models.PGPSignatureModel.SHA1]
self.assertEqual(hash_str, unicode(sha1))
self.assertEqual(hash_str, str(sha1))

class PGPDBViewTest(TestCase):
def setUp(self):
self.CLIENT = Client()

def test_index(self):
uri = reverse('pgpdb.views.index')
uri = reverse('index')
resp = self.CLIENT.get(uri)
self.assertTemplateUsed(resp, 'pgpdb/index.html')

def test_add(self):
uri = reverse('pgpdb.views.add')
uri = reverse('add')

data = {
'keytext': GPG_ALICE_KEY,
Expand All @@ -522,7 +522,7 @@ def test_add(self):
self.assertTemplateUsed(resp, 'pgpdb/add_method_not_allowed.html')

def test_add_multi(self):
uri = reverse('pgpdb.views.add')
uri = reverse('add')

self.assertEqual(models.PGPKeyModel.objects.all().count(), 0)

Expand All @@ -539,7 +539,7 @@ def test_add_multi(self):
self.assertEqual(last.ascii_armor(), PGPDB_BOB_KEY)

def test_lookup(self):
uri = reverse('pgpdb.views.lookup')
uri = reverse('lookup')

data = {
'op': 'index',
Expand Down Expand Up @@ -572,7 +572,7 @@ def test_lookup(self):
self.assertTemplateUsed(resp, 'pgpdb/lookup_get.html')

def test_lookup_mr(self):
uri = reverse('pgpdb.views.lookup')
uri = reverse('lookup')

data = {
'op': 'index',
Expand All @@ -591,7 +591,7 @@ def test_lookup_mr(self):
}
resp = self.CLIENT.get(uri, data=data)
self.assertEqual(resp['Content-Type'], 'text/plain')
self.assertEqual(resp.content, MACHINE_READABLE_INDEX1)
self.assertEqual(resp.content.decode("ascii"), MACHINE_READABLE_INDEX1)

data = {
'op': 'get',
Expand All @@ -600,7 +600,7 @@ def test_lookup_mr(self):
}
resp = self.CLIENT.get(uri, data=data)
self.assertEqual(resp['Content-Type'], 'application/pgp-keys')
self.assertEqual(resp.content, PGPDB_ALICE_KEY)
self.assertEqual(resp.content.decode("ascii"), PGPDB_ALICE_KEY)

models.PGPKeyModel.objects.save_to_storage(None, GPG_BOB_KEY)

Expand All @@ -611,7 +611,7 @@ def test_lookup_mr(self):
}
resp = self.CLIENT.get(uri, data=data)
self.assertEqual(resp['Content-Type'], 'text/plain')
self.assertEqual(resp.content, MACHINE_READABLE_INDEX2)
self.assertEqual(resp.content.decode("ascii"), MACHINE_READABLE_INDEX2)

data = {
'op': 'get',
Expand All @@ -620,5 +620,4 @@ def test_lookup_mr(self):
}
resp = self.CLIENT.get(uri, data=data)
self.assertEqual(resp['Content-Type'], 'application/pgp-keys')
self.assertEqual(resp.content, PGPDB_MULTI_KEY)

self.assertEqual(resp.content.decode("ascii"), PGPDB_MULTI_KEY)
14 changes: 8 additions & 6 deletions pgpdb/urls.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from django.conf.urls import patterns, include, url
from django.conf.urls import include, url

urlpatterns = patterns('',
url(r'^$', 'pgpdb.views.index', name='pgpkeyserver'),
url(r'^add$', 'pgpdb.views.add'),
url(r'^lookup$', 'pgpdb.views.lookup'),
)
from . import views

urlpatterns = [
url(r"^$", views.index, name="index"),
url(r"^add$", views.add, name="add"),
url(r"^lookup$", views.lookup, name="lookup"),
]
14 changes: 7 additions & 7 deletions pgpdb/utils.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import base64
import calendar
import datetime
import urllib
import urllib.parse

import pgpdb
from pgpdump.packet import (
Expand All @@ -22,9 +22,9 @@
def encode_ascii_armor(data, crc=None):
if crc is None:
crc = crc24(bytearray(data))
crc = ''.join([chr((crc >> i) & 0xff) for i in [16, 8, 0]])
crc = base64.b64encode(crc)
data = base64.b64encode(str(data))
crc = crc.to_bytes(3, "big")
crc = base64.b64encode(crc).decode("ascii")
data = base64.b64encode(data).decode("ascii")
data = '\n'.join(data[i:i+64] for i in range(0, len(data), 64))
return PGP_ARMOR_BASE.format(data, crc)

Expand All @@ -37,7 +37,7 @@ def parse_public_key_packets(pgp):
if ( tmp and
isinstance(packet, PublicKeyPacket) and
not isinstance(packet, PublicSubkeyPacket) ):
data = str(pgp.data)[start:next]
data = bytes(pgp.data)[start:next]
start = next
result.append((data, tmp))
tmp = []
Expand Down Expand Up @@ -111,7 +111,7 @@ def keys_ascii_armor(keys):
if keys.count() == 1:
return keys[0].ascii_armor()
else:
data = ''
data = b''
for key in keys:
data += key.read()
return encode_ascii_armor(data)
Expand Down Expand Up @@ -148,7 +148,7 @@ def build_machine_readable_indexes(keys):

# uid
for uid in key.userids.all():
escaped_uid = urllib.quote(uid.userid, SAFE_7BIT)
escaped_uid = urllib.parse.quote(uid.userid, SAFE_7BIT)
sig = uid.signatures.filter(keyid=keyid).first()
creation_unix = int(calendar.timegm(sig.creation_time.timetuple()))
creationdate = str(creation_unix)
Expand Down
6 changes: 3 additions & 3 deletions pgpdb/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
UserAttributePacket
)

import forms, models, utils
from . import forms, models, utils

def index(request):
c = {
Expand All @@ -34,8 +34,8 @@ def add(request):
keytext = form.cleaned_data['keytext']
# check keytext
try:
pgp = pgpdump.AsciiData(keytext)
except Exception:
pgp = pgpdump.AsciiData(keytext.encode("utf-8", "ignore"))
except:
raise __AddException
keys = utils.parse_public_key_packets(pgp)
keytexts = []
Expand Down
12 changes: 9 additions & 3 deletions run_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,21 @@ def main():
# get Django running tests.
try:
settings.configure(
TEMPLATE_DIRS=[
os.path.join(BASE_DIR, 'templates'),
TEMPLATES = [
{
'BACKEND': 'django.template.backends.django.DjangoTemplates',
'DIRS': [
os.path.join(BASE_DIR, 'templates'),
],
'APP_DIRS': True,
},
],
INSTALLED_APPS=[
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.admin',
'django.contrib.sessions',
'bootstrap3',
'bootstrapform',
'pgpdb',
],
# Django replaces this, but it still wants it. *shrugs*
Expand Down

0 comments on commit 7572e78

Please sign in to comment.