def write(request): """View to use as the guestbook writing page. Presents a form to write an entry into the guestbook. Upon submitting, the form values are validated and saved to the database, representing a digital signature. If form validation fails, the form is shown again along with an error message. A Django HTTP Request object must be passed. An HTTP Response is returned. """ guestbook_error = None if request.method == 'POST': try: new_entry = Entry( ip=request.META['REMOTE_ADDR'], namefirst=request.POST['namefirst'], namelast=request.POST['namelast'], comment=request.POST['comment'] ) new_entry.full_clean() new_entry.save(force_insert=True) except ValidationError as e: guestbook_error = '<br /><br />'.join(e.messages) else: return redirect(reverse('guestbook:index')) context = {'guestbook_error': guestbook_error} return render(request, 'guestbook/write.html', context)
def submit(request): ''' Create a new entry, save it and index it. Then render the home page ''' new_entry = Entry() new_entry.name = request.POST['name'] new_entry.email = request.POST['email'] new_entry.text = request.POST['text'] new_entry.save() api = ApiClient(API_URL) index = api.get_index(INDEX_NAME) variables = {0:0} index.add_document(new_entry.id, {'text': new_entry.name + ' ' + new_entry.email + ' ' + new_entry.text}, variables=variables) return redirect('guestbook.views.list')
def create_entry_examples(): api = ApiClient(API_URL) index = api.get_index(INDEX_NAME) doc_vars = {0:0} entry = Entry() entry.name = 'leandro' entry.email = '*****@*****.**' entry.text = 'Hello world!' entry.save() index.add_document(entry.id, {'text': entry.name + ' ' + entry.email + ' ' + entry.text}, variables= doc_vars) entry = Entry() entry.name = 'juan' entry.email = '*****@*****.**' entry.text = 'Just passed to say hello' entry.save() index.add_document(entry.id, {'text': entry.name + ' ' + entry.email + ' ' + entry.text}, variables=doc_vars) entry = Entry() entry.name = 'leandro' entry.email = '*****@*****.**' entry.text = 'hey, it\'s me again. HeLLo!!!' entry.save() index.add_document(entry.id, {'text': entry.name + ' ' + entry.email + ' ' + entry.text}, variables=doc_vars)