forms.py 2.95 KB
Newer Older
1 2
# Copyright (c) 2014, Clemson University
# All rights reserved.
Scott Duckworth committed
3
#
4 5
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
Scott Duckworth committed
6
#
7 8
# * Redistributions of source code must retain the above copyright notice, this
#   list of conditions and the following disclaimer.
Scott Duckworth committed
9
#
10 11 12
# * Redistributions in binary form must reproduce the above copyright notice,
#   this list of conditions and the following disclaimer in the documentation
#   and/or other materials provided with the distribution.
Scott Duckworth committed
13
#
14 15 16 17 18 19 20 21 22 23 24 25 26 27
# * Neither the name of the {organization} nor the names of its
#   contributors may be used to endorse or promote products derived from
#   this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Scott Duckworth committed
28

29
from django import forms
30
from django.core.exceptions import ValidationError
Paul Kilgo committed
31 32
from django_sshkey.models import Key, UserKey
from django_sshkey.util import pubkey_parse
33

Paul Kilgo committed
34
class ApplicationKeyForm(forms.ModelForm):
35 36 37 38 39 40 41 42
  key = forms.CharField(
    max_length=2000, required=True,
    widget=forms.Textarea(attrs={
      'cols': 72,
      'rows': 15,
      'placeholder': "Paste in the contents of your public key file here",
    })
  )
Paul Kilgo committed
43

44 45 46 47 48
  def clean_key(self):
    key = self.cleaned_data['key']
    key = Key(key=key)
    key.full_clean()
    return key
Paul Kilgo committed
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69

  def save(self, commit=True):
    instance = super(ApplicationKeyForm, self).save(commit=False)
    if commit:
      basekey = self.cleaned_data['key']
      basekey.save()
      instance.basekey = basekey
      instance.save()
    return instance

class NamedKeyForm(ApplicationKeyForm):
  def clean(self):
    cleaned_data = super(NamedKeyForm, self).clean()
    if 'key' in cleaned_data and not cleaned_data.get('name'):
      pubkey = pubkey_parse(cleaned_data['key'].key)
      if not pubkey.comment:
        raise ValidationError('Name or key comment required')
      cleaned_data['name'] = pubkey.comment
    return cleaned_data

class UserKeyForm(NamedKeyForm):
70

71
  class Meta:
Paul Kilgo committed
72
    model = UserKey
73
    fields = ['name', 'key']
Paul Kilgo committed
74
    exclude = ['basekey']
75
    widgets = {
Paul Kilgo committed
76 77 78 79
      'name': forms.TextInput(attrs={
        'size': 50,
        'placeholder': "username@hostname, or leave blank to use key comment",
      })
80
    }