1
0
mirror of https://github.com/r4sas/py-i2phosts synced 2025-01-25 22:14:16 +00:00

52 lines
1.5 KiB
Python
Raw Normal View History

2010-10-06 19:02:08 +00:00
from django.shortcuts import render_to_response
from django.http import HttpResponseRedirect
from django import forms
2010-10-06 19:02:08 +00:00
from django.forms import ModelForm
from django.template import RequestContext
from web.postkey.models import i2phost
from web.lib.validation import validate_hostname
from web.lib.validation import validate_b64hash
2010-10-06 19:02:08 +00:00
class AddForm(ModelForm):
2010-10-06 19:02:08 +00:00
"""
This is our class for host-add form. It's based on django's ModelForm
and uses our model "i2phost" (see postkey/models.py)
"""
2010-10-06 19:02:08 +00:00
class Meta:
model = i2phost
fields = ('name', 'b64hash', 'description')
widgets = {
'name': forms.TextInput(attrs={'size': '67'}),
'b64hash': forms.Textarea(attrs={'rows': '1', 'cols': '100'}),
'description': forms.Textarea(attrs={'rows': '2', 'cols': '72'})
}
def clean_name(self):
"""Validate hostname"""
data = self.cleaned_data['name']
data = validate_hostname(data)
return data
def clean_b64hash(self):
"""Validate base64 hash"""
data = self.cleaned_data['b64hash']
data = validate_b64hash(data)
return data
2010-10-06 19:02:08 +00:00
2010-10-06 19:02:08 +00:00
def addkey(request):
2010-10-06 19:02:08 +00:00
if request.method == 'POST':
form = AddForm(request.POST)
if form.is_valid():
2010-10-06 19:02:08 +00:00
form.save()
request.session['hostname'] = form.cleaned_data['name']
return HttpResponseRedirect('success')
2010-10-06 19:02:08 +00:00
else:
form = AddForm()
return render_to_response('postkey.html', {
'form': form,
}, context_instance=RequestContext(request))
2010-10-06 19:02:08 +00:00
def success(request):
return render_to_response('success_submission.html', {
'hostname': request.session['hostname'],
})