def add_def(request, term_uid): try: Terms.objects.get(uid=term_uid) except ObjectDoesNotExist: return HttpResponse(u'词条不存在 !') sform = SearchForm2() t = Terms.objects.get(uid=term_uid) if request.method == 'POST': form = DefinitionForm(request.POST, request.FILES) if form.is_valid(): d = Definitions( Terms_id=t.id, definition=form.cleaned_data['definition'], homepage=form.cleaned_data['homepage'], author_email=form.cleaned_data['author_email'], author=form.cleaned_data['author'], uid=definition_uid(), docfile=form.cleaned_data['docfile'] ) d.save() if is_online: send_d.delay(t.term, d.definition, d.author, d.homepage, d.author_email) return HttpResponseRedirect('/thanks.html') else: form = DefinitionForm() return render_to_response('add_def.html', {'form': form, 'sform': sform, 'term': t, 'is_online': is_online}, context_instance=RequestContext(request))
def add_search_term(request, search_term): sform = SearchForm2() desc = None if request.method == 'POST': form = TermForm(request.POST, request.FILES) if form.is_valid(): raw_term = form.cleaned_data['term'] raw_term = ' '.join(raw_term.split()) raw_term_pinyin = term_pinyin(raw_term) clean_term_pinyin = ' '.join(raw_term_pinyin.split()) t = Terms(term=raw_term, term_pinyin=clean_term_pinyin, uid=term_uid()) try: t.save() except IntegrityError: return HttpResponse(u'词条已经存在!') request.session['new_add_term'] = t.term request.session['new_add_term_uid'] = t.uid import pdb pdb.set_trace() d = Definitions(Terms_id=t.id, definition=definition_compact, homepage=form.cleaned_data['homepage'], author_email=form.cleaned_data['author_email'], author=form.cleaned_data['author'], uid=definition_uid(), docfile=form.cleaned_data['docfile']) d.save() request.session['contributor'] = True request.session['has_thanked'] = False request.session['author'] = form.cleaned_data['author'] request.session['author_email'] = form.cleaned_data['author_email'] request.session['homepage'] = form.cleaned_data['homepage'] request.session.set_expiry(60 * 30) if is_online: send_t.delay( t.term, d.definition, d.author, d.homepage, d.author_email) return HttpResponseRedirect('/thanks.html') else: form = TermForm( initial={'term': search_term, 'author': request.session.get('author', None), 'author_email': request.session.get('author_email', None), 'homepage': request.session.get('homepage', None)}) desc = search_term c = {'form': form, 'sform': sform} c.update(csrf(request)) return render_to_response( 'add_term.html', {'form': form, 'sform': sform, 'desc': desc, 'is_online': is_online}, context_instance=RequestContext(request))
def add_search_term(request, search_term): sform = SearchForm2() desc = None if request.method == 'POST': form = TermForm(request.POST, request.FILES) if form.is_valid(): raw_term = form.cleaned_data['term'] raw_term = ' '.join(raw_term.split()) raw_term_pinyin = term_pinyin(unicode(raw_term)) clean_term_pinyin = ' '.join(raw_term_pinyin.split()) t = Terms( term=raw_term, term_pinyin=clean_term_pinyin, uid=term_uid() ) try: t.save() except IntegrityError: return HttpResponse(u'词条已经存在!') d = Definitions( Terms_id=t.id, # definition=form.cleaned_data['definition'], definition=definition_compact, homepage=form.cleaned_data['homepage'], author_email=form.cleaned_data['author_email'], author=form.cleaned_data['author'], uid=definition_uid(), docfile=form.cleaned_data['docfile'] ) d.save() send_t.delay(t.term, d.definition, d.author, d.homepage, d.author_email) return HttpResponseRedirect('/thanks.html') else: form = TermForm(initial={'term': search_term}) desc = search_term c = {'form': form, 'sform': sform} c.update(csrf(request)) return render_to_response('add_term.html', {'form': form, 'sform': sform, 'desc': desc, 'is_online': is_online}, context_instance=RequestContext(request))