Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
CIRCLE
/
django-sshkey
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
4f21d7a4
authored
Oct 03, 2014
by
Paul Kilgo
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
factor out BaseKey from UserKey
parent
414e19cd
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
25 deletions
+33
-25
django_sshkey/models.py
+33
-25
No files found.
django_sshkey/models.py
View file @
4f21d7a4
...
...
@@ -39,8 +39,7 @@ except ImportError:
from
django_sshkey.util
import
PublicKeyParseError
,
pubkey_parse
from
django_sshkey
import
settings
class
UserKey
(
models
.
Model
):
user
=
models
.
ForeignKey
(
User
,
db_index
=
True
)
class
BaseKey
(
models
.
Model
):
name
=
models
.
CharField
(
max_length
=
50
,
blank
=
True
)
key
=
models
.
TextField
(
max_length
=
2000
)
fingerprint
=
models
.
CharField
(
max_length
=
47
,
blank
=
True
,
db_index
=
True
)
...
...
@@ -49,13 +48,10 @@ class UserKey(models.Model):
last_used
=
models
.
DateTimeField
(
null
=
True
)
class
Meta
:
db_table
=
'sshkey_userkey'
unique_together
=
[
(
'user'
,
'name'
),
]
abstract
=
True
def
__unicode__
(
self
):
return
unicode
(
self
.
user
)
+
u': '
+
self
.
name
return
self
.
name
def
clean_fields
(
self
,
exclude
=
None
):
if
not
exclude
or
'key'
not
in
exclude
:
...
...
@@ -78,6 +74,36 @@ class UserKey(models.Model):
raise
ValidationError
(
'Name or key comment required'
)
self
.
name
=
pubkey
.
comment
def
export
(
self
,
format
=
'RFC4716'
):
pubkey
=
pubkey_parse
(
self
.
key
)
f
=
format
.
upper
()
if
f
==
'RFC4716'
:
return
pubkey
.
format_rfc4716
()
if
f
==
'PEM'
:
return
pubkey
.
format_pem
()
raise
ValueError
(
"Invalid format"
)
def
save
(
self
,
*
args
,
**
kwargs
):
if
kwargs
.
pop
(
'update_last_modified'
,
True
):
self
.
last_modified
=
now
()
super
(
BaseKey
,
self
)
.
save
(
*
args
,
**
kwargs
)
def
touch
(
self
):
self
.
last_used
=
now
()
self
.
save
(
update_last_modified
=
False
)
class
UserKey
(
BaseKey
):
user
=
models
.
ForeignKey
(
User
,
db_index
=
True
)
class
Meta
:
db_table
=
'sshkey_userkey'
unique_together
=
[
(
'user'
,
'name'
),
]
def
__unicode__
(
self
):
return
unicode
(
self
.
user
)
+
u': '
+
self
.
name
def
validate_unique
(
self
,
exclude
=
None
):
if
self
.
pk
is
None
:
objects
=
type
(
self
)
.
objects
...
...
@@ -98,24 +124,6 @@ class UserKey(models.Model):
except
type
(
self
)
.
DoesNotExist
:
pass
def
export
(
self
,
format
=
'RFC4716'
):
pubkey
=
pubkey_parse
(
self
.
key
)
f
=
format
.
upper
()
if
f
==
'RFC4716'
:
return
pubkey
.
format_rfc4716
()
if
f
==
'PEM'
:
return
pubkey
.
format_pem
()
raise
ValueError
(
"Invalid format"
)
def
save
(
self
,
*
args
,
**
kwargs
):
if
kwargs
.
pop
(
'update_last_modified'
,
True
):
self
.
last_modified
=
now
()
super
(
UserKey
,
self
)
.
save
(
*
args
,
**
kwargs
)
def
touch
(
self
):
self
.
last_used
=
now
()
self
.
save
(
update_last_modified
=
False
)
@receiver
(
pre_save
,
sender
=
UserKey
)
def
send_email_add_key
(
sender
,
instance
,
**
kwargs
):
if
not
settings
.
SSHKEY_EMAIL_ADD_KEY
or
instance
.
pk
:
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment