def edit_a_book(request, book_requested): if request.method == 'POST': formA = BookForm(request.POST, prefix='formA') formA.is_multipart() formB = AuthorForm(request.POST, prefix='formB') # Book Title Regex used before saving to DB -------------------------- if formA.is_valid(): title = formA.cleaned_data['title'] m = re.match('(\w+\s)*(\w+)', title) # continue normal view as before ------------------------------------- if all([formA.is_valid(), formB.is_valid()]) and m is not None and len(str(m.group())) == len(str(title)): orig_book = uncode_display_url(book_requested) book = get_object_or_404(Book, title=orig_book) author = Author.objects.get(book__title=orig_book) book.title = formA.cleaned_data['title'] author.first_name = formB.cleaned_data['first_name'] author.last_name = formB.cleaned_data['last_name'] book.description = formA.cleaned_data['description'] author.save() book.save() return HttpResponseRedirect(reverse('books:profile')) else: orig_book = uncode_display_url(book_requested) book = get_object_or_404(Book, title=orig_book) author = Author.objects.get(book__title=orig_book) formA = BookForm(prefix='formA', initial={'title': book.title, 'description': book.description}) formB = AuthorForm(prefix='formB', initial={'first_name': author.first_name, 'last_name': author.last_name}) return render(request, 'books/edit_a_book.html', {'formA': formA, 'formB': formB})
def add(request): """View to add a book, we will check if the seller is active first""" try: seller = Seller.objects.get(user=request.user.id) except Seller.DoesNotExist: seller = None if seller and seller.account_status == 'AC': can_add_book = True seller_id = seller.id else: can_add_book = False seller_id = None if request.method == 'POST': # If the form has been submitted... form = BookForm(request.POST, request.FILES) # A form bound to the POST data if form.is_valid(): # All validation rules pass # Process the data in form.cleaned_data # ... form.save() return redirect('/accounts/books/') # Redirect after POST else: form = BookForm(initial={'seller': seller_id}) # An unbound form return render_to_response('books/book_form.html', { 'form': form, 'can_add_book': can_add_book, 'seller': seller }, context_instance=RequestContext(request))
def book_view(request): if request.method == "GET": form = BookForm() else: form = BookForm(request.POST) """ WARING: this line will technically work, but is horribly sub-optimal. djangoperformance.com will make you aware of that sub-optimality by telling you how many queries are being generated for each view, and what those queries are """ # books = Book.objects.all() """ this is a simple solution to reduce the number of queries issued to the database. this should not be considered a recipe for optimizing all applications, and is only used here to illustrate expected use case of djangoperformance.com (you may, for example, want to use a custom model manager if you're doing something complicated in your application) """ books = Book.objects.select_related("publisher").all().prefetch_related("authors") if not form.is_valid() or request.method == "GET": return render_to_response( "books.html", {"form": form, "object_list": books}, context_instance=RequestContext(request) ) else: newitem = form.save() messages.success(request, "Book %s was added" % newitem) return HttpResponseRedirect(request.build_absolute_uri())
def create_book(request): if request.POST: form = BookForm(request.POST, request.FILES) if form.is_valid(): form.save() return HttpResponseRedirect('/books/') else: form = BookForm() args = {} args.update(csrf(request)) args['form'] = form # right box args['menu'] = Menu.objects.all() args['pop_books'] = Book.objects.all().order_by('likes').reverse()[:10] args['new_books'] = Book.objects.all().order_by('date').reverse()[:10] args['cats'] = Category.objects.all() args['tags'] = Tag.objects.all() args['current_item'] = 'книги' # user args['username'] = auth.get_user(request).username # poll args['question_web'] = Question.objects.get(text=u"Как вам наш сайт?") args['choices'] = Choice.objects.filter(question_id=args['question_web'].id) return render_to_response('book/create_book.html', args)
def add(request): """View to add a book, we will check if the seller is active first""" try: seller = Seller.objects.get(user=request.user.id) except Seller.DoesNotExist: seller = None if seller and seller.account_status == 'AC': can_add_book = True seller_id = seller.id else: can_add_book = False seller_id = None if request.method == 'POST': # If the form has been submitted... form = BookForm(request.POST, request.FILES) # A form bound to the POST data if form.is_valid(): # All validation rules pass # Process the data in form.cleaned_data # ... form.save() return redirect('/accounts/books/') # Redirect after POST else: form = BookForm(initial={'seller': seller_id}) # An unbound form return render_to_response('books/book_form.html', { 'form': form, 'can_add_book': can_add_book, 'seller': seller },context_instance=RequestContext(request))
def addbook(request,id=1): if request.POST: form = BookForm(request.POST) if form.is_valid(): book=form.save(commit=False) form.save() return redirect('/books/all',)
def check_books(request): form = BookForm(request.POST, request.FILES) # A form bound to the POST data if not form.is_valid(): # All validation rules pass return render(request, "index.html", {"form": form}) # Process the data in form.cleaned_data -- but what about the location data? book_file = form.cleaned_data['book_file'] google_url = form.cleaned_data['google_url'] lib_location = form.cleaned_data['lib_location'] if book_file: booklist = process_book_file(book_file) # returns list of title and author elif google_url: print google_url booklist = read_spreadsheet(google_url) # print "%r"%lib_location gr_rating = 0 checked_in_books = [] for book_title, author in booklist: library_base_url = create_library_url(book_title, lib_location) details = get_details(book_title, author, lib_location, library_base_url) if details: checked_in_books.append(details) print "processed!" # print booklist sorry_msg = """ Sorry, no amount of fairy dust can check in any of these books for you. Check back again in a few days. """ return render( request,'booklist.html', { 'booklist': checked_in_books } )
def addbook(request, id=1): if request.POST: form = BookForm(request.POST) if form.is_valid(): book = form.save(commit=False) form.save() return redirect('/books/all', )
def check_books(request): form = BookForm(request.POST, request.FILES) # A form bound to the POST data if not form.is_valid(): # All validation rules pass return render(request, "index.html", {"form": form}) # Process the data in form.cleaned_data -- but what about the location data? book_file = form.cleaned_data['book_file'] google_url = form.cleaned_data['google_url'] lib_location = form.cleaned_data['lib_location'] if book_file: booklist = process_book_file( book_file) # returns list of title and author elif google_url: print google_url booklist = read_spreadsheet(google_url) # print "%r"%lib_location gr_rating = 0 checked_in_books = [] for book_title, author in booklist: library_base_url = create_library_url(book_title, lib_location) details = get_details(book_title, author, lib_location, library_base_url) if details: checked_in_books.append(details) print "processed!" # print booklist sorry_msg = """ Sorry, no amount of fairy dust can check in any of these books for you. Check back again in a few days. """ return render(request, 'booklist.html', {'booklist': checked_in_books})
def post(self, request): form = BookForm(request.POST) if form.is_valid(): # 验证表单数据 print(form.cleaned_data) # 获取验证后的表单数据 return HttpResponse("OK") else: return render(request, 'book.html', {'form': form})
def nominations(request, poll_id): poll = Poll.objects.get(id=poll_id) books = Book.objects.filter(poll=poll_id) disabled = '' votes = {} for book in books: if book.user == request.user: disabled = 'disabled="disabled"' if request.method == "POST": form = BookForm(request.POST) if form.is_valid(): title = form.cleaned_data['title'] author = form.cleaned_data['author'] description = form.cleaned_data['description'] book = Book(title=title, author=author, description=description, poll=poll, user=request.user) book.save() return redirect('/polls/nominations/' + str(poll_id)) else: form = BookForm() context = RequestContext(request, { 'poll': poll, 'books': books, 'form': form, 'disabled': disabled, }) return render(request, 'polls/nominations.html', context)
def book_view(request): if request.method == 'GET': form = BookForm() else: form = BookForm(request.POST) """ WARING: this line will technically work, but is horribly sub-optimal. djangoperformance.com will make you aware of that sub-optimality by telling you how many queries are being generated for each view, and what those queries are """ # books = Book.objects.all() """ this is a simple solution to reduce the number of queries issued to the database. this should not be considered a recipe for optimizing all applications, and is only used here to illustrate expected use case of djangoperformance.com (you may, for example, want to use a custom model manager if you're doing something complicated in your application) """ books = Book.objects.select_related('publisher').all().prefetch_related('authors') if not form.is_valid() or request.method == 'GET': return render_to_response('books.html', {'form': form, 'object_list': books}, context_instance=RequestContext(request)) else: newitem = form.save() messages.success(request, "Book %s was added" % newitem) return HttpResponseRedirect(request.build_absolute_uri())
def edit_a_book(request, book_requested): if request.method == 'POST': formA = BookForm(request.POST, prefix='formA') formA.is_multipart() formB = AuthorForm(request.POST, prefix='formB') # Book Title Regex used before saving to DB -------------------------- if formA.is_valid(): title = formA.cleaned_data['title'] m = re.match('(\w+\s)*(\w+)', title) # continue normal view as before ------------------------------------- if all([formA.is_valid(), formB.is_valid() ]) and m is not None and len(str(m.group())) == len( str(title)): orig_book = uncode_display_url(book_requested) book = get_object_or_404(Book, title=orig_book) author = Author.objects.get(book__title=orig_book) book.title = formA.cleaned_data['title'] author.first_name = formB.cleaned_data['first_name'] author.last_name = formB.cleaned_data['last_name'] book.description = formA.cleaned_data['description'] author.save() book.save() return HttpResponseRedirect(reverse('books:profile')) else: orig_book = uncode_display_url(book_requested) book = get_object_or_404(Book, title=orig_book) author = Author.objects.get(book__title=orig_book) formA = BookForm(prefix='formA', initial={ 'title': book.title, 'description': book.description }) formB = AuthorForm(prefix='formB', initial={ 'first_name': author.first_name, 'last_name': author.last_name }) return render(request, 'books/edit_a_book.html', { 'formA': formA, 'formB': formB })
def add_book(request): if request.method == 'POST': form = BookForm(request.POST, request.FILES) if form.is_valid(): form.save() return HttpResponseRedirect('/books/all/') else: form = BookForm() args = {} args.update(csrf(request)) args['form'] = form return render(request, 'books/add_book.html', args)
def addbook(request): #form_args = {} if request.POST: book_form = BookForm(request.POST) if book_form.is_valid(): book = book_form.save(commit=False) book.user = request.user book.save() book_form.save_m2m() return HttpResponseRedirect("/stats/") else: book_form = BookForm(initial={'user': request.user}) return render_to_response('add_book.html', {'BookForm': book_form}, context_instance=RequestContext(request))
def edit(request, id): """Edit a book, using the passed id""" book = Book.objects.get(pk=id) if request.method == 'POST': # If the form has been submitted... form = BookForm(request.POST, request.FILES, instance=book) # A form bound to the POST data if form.is_valid(): # All validation rules pass # Process the data in form.cleaned_data form.save() return redirect('/accounts/books/') # Redirect after POST else: form = BookForm(instance=book) # Lets bound the form return render_to_response('books/book_form.html', { 'form': form, 'can_add_book': True },context_instance=RequestContext(request))
def addbook(request): args={} args.update(csrf(request)) args['form']=BookForm() args['username'] = auth.get_user(request).username if request.POST: form=BookForm(request.POST, request.FILES) if form.is_valid(): book = form.save(commit=False) book.book_from = request.user book.book_date = timezone.now() book.save() return redirect('/books/get/%s/' %book.id) else: form=BookForm() return render_to_response('AddBook.html', args)
def addbook(request): args = {} args.update(csrf(request)) args['form'] = BookForm() args['username'] = auth.get_user(request).username if request.POST: form = BookForm(request.POST, request.FILES) if form.is_valid(): book = form.save(commit=False) book.book_from = request.user book.book_date = timezone.now() book.save() return redirect('/books/get/%s/' % book.id) else: form = BookForm() return render_to_response('AddBook.html', args)
def search(request): """Search view.""" if request.GET: form = BookForm(request.GET) if form.is_valid(): name = form.cleaned_data.get('name_field') books = Book.objects.filter(name__icontains=name) context = { 'books_list': books } return render(request, 'shop/results.html', context) form = BookForm() context = { 'form': form } return render(request, 'shop/search.html', context)
def dodajKnjigu05(request): if request.method == 'POST': form = BookForm(request.POST) if form.is_valid(): knjiga = Knjiga.objects.create() knjiga.naziv = form.cleaned_data['naziv'] knjiga.autor = form.cleaned_data['autor'] knjiga.save() return vj05(request) else: form = BookForm() context = {'form' : form} return render(request, 'bookForm.html', context)
def book(request): logger.info("Showing the booking form") if request.method == 'POST': # If the form has been submitted... form = BookForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass # Process the data in form.cleaned_data study_type = form.cleaned_data['study_type'] start_time = datetime.datetime.strptime(form.cleaned_data.get("start_time"), "%H:%M:%S") end_time = datetime.datetime.strptime(form.cleaned_data.get("end_time"), "%H:%M:%S") start_datetime = datetime.datetime.combine(form.cleaned_data.get('start_date'), start_time.time()) end_datetime = datetime.datetime.combine(form.cleaned_data.get('end_date'), end_time.time()) gmt = pytz.timezone('Europe/London') start_datetime = gmt.localize(start_datetime) end_datetime = gmt.localize(end_datetime) request.session['start_datetime'] = start_datetime request.session['end_datetime'] = end_datetime try: pods = Pod.objects.filter(study_types__id__exact=study_type).exclude( booking__start_datetime__gt=start_datetime - datetime.timedelta(hours=1), booking__end_datetime__lt=end_datetime - datetime.timedelta(hours=1) ).prefetch_related('configset_set') for pod in pods: pod.my_configs = pod.configset_set.filter(user=request.user.username) pod.my_config_count = pod.my_configs.count() pods = sorted(pods, key=operator.attrgetter('my_config_count'), reverse=True) return render_to_response("book/pod_available.html", {'pods': pods}, context_instance=RequestContext(request)) except Pod.DoesNotExist: return HttpResponse("Ouch, no pods available at that time") else: form = BookForm() return render_to_response("book/book.html", {'form': form}, context_instance=RequestContext(request))
def edit(request, id): """Edit a book, using the passed id""" book = Book.objects.get(pk=id) if request.method == 'POST': # If the form has been submitted... form = BookForm(request.POST, request.FILES, instance=book) # A form bound to the POST data if form.is_valid(): # All validation rules pass # Process the data in form.cleaned_data form.save() return redirect('/accounts/books/') # Redirect after POST else: form = BookForm(instance=book) # Lets bound the form return render_to_response('books/book_form.html', { 'form': form, 'can_add_book': True }, context_instance=RequestContext(request))
def prominiKnjigu06(request, id): if request.method == 'POST': form = BookForm(request.POST) if form.is_valid(): knjiga = Knjiga.objects.get(pk=id) knjiga.naziv = form.cleaned_data['naziv'] knjiga.autor = form.cleaned_data['autor'] knjiga.save() return vj06(request) else: knjiga = Knjiga.objects.get(pk=id) form = BookForm(initial={'naziv':knjiga.naziv, 'autor':knjiga.autor}) context = {'form':form} return render(request, 'bookForm.html', context)
def index(request): # Code to lookup book availability if request.method == 'POST': # If the form has been submitted... print "in post" form = BookForm(request.POST) # A form bound to the POST data if form.is_valid(): # All validation rules pass # Process the data in form.cleaned_data print "in is_valid" book_title = form.cleaned_data['book_title'] print book_title # TODO: Add code htere to lookup if the book is checked in # TODO: is the bookchecked in? is_checked_in = True return render(request, 'booklist.html', { 'book_title': book_title, 'is_checked_in': is_checked_in } ) else: form = BookForm() # An unbound form return render(request, 'index.html', { 'form': form, })
def respond(request): saved = False people = 0 sessions = 0 message = '' if request.method == 'POST': form = BookForm(request.POST) if form.is_valid(): user_sender = User.objects.get(pk=request.POST['id_sender']) user_receiver = User.objects.get(pk=request.POST['id_receiver']) sent = True people = form.cleaned_data['people'] sessions = form.cleaned_data['sessions'] message = form.cleaned_data['message'] bm = Message() bm.id_sender = user_sender bm.id_receiver = user_receiver bm.message = message bm.people = people bm.sessions = sessions bm.save() mail_data = {} mail_data['subject'] = ' %s %s is Interested in you.' % (user_sender.first_name, user_sender.last_name) mail_data['message'] = ''' %s %s, <br> %s %s is Interested in talking to you. To answer check your inbox.<br> <h3>People: %s</h3> <br> <h3>Sessions: %s</h3> <br> <h3>Details: </h3><br> %s ''' % (user_receiver.first_name, user_receiver.last_name, user_sender.first_name, user_sender.last_name, people, sessions, message) mail_data['from'] = '*****@*****.**' mail_data['sender'] = user_sender.email mail_data['receiver'] = user_receiver.email sent = utils.send_mail(mail_data) saved = True return HttpResponseRedirect('/search/') else: return HttpResponseRedirect('/')