<commit_before>
from django.contrib import admin
from django import forms

from email_extras.settings import USE_GNUPG, GNUPG_HOME


if USE_GNUPG:
	from gnupg import GPG
	from email_extras.models import Key, Address
	from email_extras.utils import addresses_for_key

	class KeyAdmin(admin.ModelAdmin):
	
		def save_model(self, request, obj, form, change):
			"""
			import the key and parse the addresses from it and save them
			and omit the super save_model call so as to never save the key instance
			"""
			gpg = GPG(gnupghome=GNUPG_HOME)
			result = gpg.import_keys(obj.key)
			if result.count == 0:
				raise forms.ValidationError("Invalid Key")
			else:
				addresses = []
				for key in result.results:
					addresses.extend(addresses_for_key(gpg, key["fingerprint"]))
				obj.addresses = ",".join(addresses)
				for address in addresses:
					Address.objects.get_or_create(address=address)

	admin.site.register(Key, KeyAdmin)
	admin.site.register(Address)


<commit_msg>FIX: Fixing a bug in admins.py that passes the key['fingerprint'] to addresses_for_key, instead of the key directly.<commit_after>
from django.contrib import admin
from django import forms

from email_extras.settings import USE_GNUPG, GNUPG_HOME


if USE_GNUPG:
	from gnupg import GPG
	from email_extras.models import Key, Address
	from email_extras.utils import addresses_for_key

	class KeyAdmin(admin.ModelAdmin):
	
		def save_model(self, request, obj, form, change):
			"""
			import the key and parse the addresses from it and save them
			and omit the super save_model call so as to never save the key instance
			"""
			gpg = GPG(gnupghome=GNUPG_HOME)
			result = gpg.import_keys(obj.key)
			if result.count == 0:
				raise forms.ValidationError("Invalid Key")
			else:
				addresses = []
				for key in result.results:
					addresses.extend(addresses_for_key(gpg, key))
				obj.addresses = ",".join(addresses)
				for address in addresses:
					Address.objects.get_or_create(address=address)

	admin.site.register(Key, KeyAdmin)
	admin.site.register(Address)



{'fraction_non_alphanumeric': 0.08113207547169811, 'fraction_numerical': 0.0009433962264150943, 'mean_word_length': 3.5417558886509637, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3474825', 'n_tokens_mistral': 789, 'n_tokens_neox': 646, 'n_words': 198}