def add_blog_form(request, pID='0'): BlogForm = forms.form_for_model(Blog, fields=('title', 'text')) bf = BlogForm() message = 'Unknown Request' p = Person.objects.get(id=pID) if request.method == 'GET': message = 'Add Blog for %s ' % p.name if request.method == 'POST': if request.POST['submit'] == 'Add': bf = BlogForm(request.POST.copy()) SaveForm = forms.form_for_model(Blog) postDict = request.POST.copy() postDict['date'] = datetime.now() save_bf = SaveForm(postDict) if save_bf.is_valid(): try: bObj = save_bf.save() p.blogs.add(bObj) p.save() message = 'Blog added to %s.' % p.name except: message = 'Database Error.' else: message = 'Invalid data in Form.' return render(request, 'People/add_blog_form.html', { 'bForm': bForm, 'message': message })
def create_form(self, request, queryset, form_class): """ Render form for creation of new collection entry. """ ResourceForm = forms.form_for_model(queryset.model, form=form_class) if request.POST: form = ResourceForm(request.POST) else: form = ResourceForm() template_name = '%s/%s_form.html' % (self.template_dir, queryset.model._meta.module_name) return render_to_response(template_name, {'form':form})
def create(self, request): """ Creates a resource with attributes given by POST, then redirects to the resource URI. """ # Create form filled with POST data ResourceForm = forms.form_for_model(self.queryset.model, form=self.form_class) data = self.receiver.get_post_data(request) form = ResourceForm(data) # If the data contains no errors, save the model, # return a "201 Created" response with the model's # URI in the location header and a representation # of the model in the response body. if form.is_valid(): new_model = form.save() model_entry = self.entry_class(self, new_model) response = model_entry.read(request) response.status_code = 201 response['Location'] = model_entry.get_url() return response # Otherwise return a 400 Bad Request error. raise InvalidModelData(form.errors)
def config(request, action): #from django.utils import simplejson from person.models import UserProfile, ImapServer, SmtpServer if action == "newconfig" or action == "newIMAPform": # we already know they don't have anything in the database, just show them a blank form #UserForm = forms.form_for_model(UserProfile) ImapForm = forms.form_for_model(ImapServer) #uform = UserForm() iform = ImapForm(initial={'address': 'localhost', 'port': '143'}) srvtype = "IMAP" elif action == "newSMTPform": SmtpForm = forms.form_for_model(SmtpServer) sform = SmtpForm(initial={'address': 'localhost', 'port': '25'}) srvtype = "SMTP" elif action == "addnew": if request.POST.get('newconfig') == "true": for srv in request.user.get_profile().imap_servers.all(): request.user.get_profile().imap_servers.remove(srv) # we are adding some new configuration ImapForm = forms.form_for_model(ImapServer) iform = ImapForm(request.POST) i = request.user.get_profile().imap_servers.create( address=request.POST.get('address'), port=request.POST.get('port'), username=request.POST.get('username'), passwd=request.POST.get('passwd')) i.save() # if they didn't have a "real" imap server setup before delete the fake one #if request.user.get_profile().imap_servers.all()[0].username == None: #o = request.user.get_profile().imap_servers.remove(0) #o = request.user.get_profile().imap_servers.remove(request.user.get_profile().imap_servers.all()[0]) return HttpResponseRedirect('/mail/') elif action == "addnewsmtp": SmtpForm = forms.form_for_model(SmtpServer) sform = SmtpForm(request.POST) s = request.user.get_profile().smtp_servers.create( address=request.POST.get('address'), port=request.POST.get('port'), username=request.POST.get('username'), passwd=request.POST.get('passwd')) s.save() return HttpResponse(simplejson.dumps({'status': 'OK'})) elif action == "edit": saction = request.GET.get('saction') srvtype = request.GET.get('srvtype') whichsrv = int(request.GET.get('which')) if saction == "REMOVE": srv = request.user.get_profile().imap_servers.remove( request.user.get_profile().imap_servers.all()[whichsrv]) #s = request.user.get_profile().imap_servers.get() FIXME: not finished here, but I need to go out and drink return HttpResponse(simplejson.dumps({'status': 'OK'})) else: # default action / index imapsrvs = request.user.get_profile().imap_servers.all() # the code below uses newforms, but these forms are so short it turned out working better to just hand code them #iforms = [] #for i in imapsrvs: #IForm = forms.form_for_instance(i, formfield_callback=form_callback) ##IForm.base_fields['passwd'].widget = forms.PasswordInput() #f = IForm() #iforms.append(f) return render_to_response('mail/config/' + action + '.html', locals())
def main(request): # initialize variables to sent to template message = '' submit_action = 'Add' edit_id = '' # generate default form ContactForm = forms.form_for_model(Contact) f = ContactForm() # handle edit and delete events if request.method == 'GET': if request.has_key('edit_id'): # replace default form with form based on row to edit contact = Contact.objects.get(pk=request.GET['edit_id']) ContactForm = forms.form_for_instance(contact) f = ContactForm() submit_action = 'Update' edit_id = request.GET['edit_id'] message = 'Editing contact ID ' + request.GET['edit_id'] if request.has_key('delete_id'): Contact.objects.get(pk=request.GET['delete_id']).delete() message = 'Contact deleted.' # handle add and update events if request.method == 'POST': if request.POST['submit_action'] == 'Add': # attempt to do add add_f = ContactForm(request.POST) if add_f.is_valid(): add_f.save() message = 'Contact added.' else: # validation failed: show submitted values in form f = add_f if request.POST['submit_action'] == 'Update': # attempt to do update contact = Contact.objects.get(pk=request.POST['edit_id']) ContactForm = forms.form_for_instance(contact) update_f = ContactForm(request.POST.copy()) if update_f.is_valid(): update_f.save() message = 'Contact updated.' else: # validation failed: prepare form for new update attempt submit_action = 'Update' edit_id = request.POST['edit_id'] f = update_f # get existing contacts contact_list = Contact.objects.all() # return rendered HTML page return render_to_response( 'contacts.html', { 'request': request, 'message': message, 'contact_list': contact_list, 'form': f.as_table(), 'submit_action': submit_action, 'edit_id': edit_id } )
from technex.app.models import UserProfile from django.forms import form_for_model RegisterForm = form_for_model(UserProfile)
def config(request, action): #from django.utils import simplejson from person.models import UserProfile, ImapServer, SmtpServer if action == "newconfig" or action == "newIMAPform": # we already know they don't have anything in the database, just show them a blank form #UserForm = forms.form_for_model(UserProfile) ImapForm = forms.form_for_model(ImapServer) #uform = UserForm() iform = ImapForm(initial={'address':'localhost','port':'143'}) srvtype = "IMAP" elif action == "newSMTPform": SmtpForm = forms.form_for_model(SmtpServer) sform = SmtpForm(initial={'address':'localhost','port':'25'}) srvtype = "SMTP" elif action == "addnew": if request.POST.get('newconfig') == "true": for srv in request.user.get_profile().imap_servers.all(): request.user.get_profile().imap_servers.remove(srv) # we are adding some new configuration ImapForm = forms.form_for_model(ImapServer) iform = ImapForm(request.POST) i = request.user.get_profile().imap_servers.create(address = request.POST.get('address'), port = request.POST.get('port'), username = request.POST.get('username'), passwd = request.POST.get('passwd')) i.save() # if they didn't have a "real" imap server setup before delete the fake one #if request.user.get_profile().imap_servers.all()[0].username == None: #o = request.user.get_profile().imap_servers.remove(0) #o = request.user.get_profile().imap_servers.remove(request.user.get_profile().imap_servers.all()[0]) return HttpResponseRedirect('/mail/') elif action == "addnewsmtp": SmtpForm = forms.form_for_model(SmtpServer) sform = SmtpForm(request.POST) s = request.user.get_profile().smtp_servers.create(address = request.POST.get('address'), port = request.POST.get('port'), username = request.POST.get('username'), passwd = request.POST.get('passwd')) s.save() return HttpResponse(simplejson.dumps({'status':'OK'})) elif action == "edit": saction = request.GET.get('saction') srvtype = request.GET.get('srvtype') whichsrv = int(request.GET.get('which')) if saction == "REMOVE": srv = request.user.get_profile().imap_servers.remove(request.user.get_profile().imap_servers.all()[whichsrv]) #s = request.user.get_profile().imap_servers.get() FIXME: not finished here, but I need to go out and drink return HttpResponse(simplejson.dumps({'status':'OK'})) else: # default action / index imapsrvs = request.user.get_profile().imap_servers.all() # the code below uses newforms, but these forms are so short it turned out working better to just hand code them #iforms = [] #for i in imapsrvs: #IForm = forms.form_for_instance(i, formfield_callback=form_callback) ##IForm.base_fields['passwd'].widget = forms.PasswordInput() #f = IForm() #iforms.append(f) return render_to_response('mail/config/'+action+'.html', locals())