当前位置: 首页>>代码示例>>Python>>正文


Python Book.pk方法代码示例

本文整理汇总了Python中books.models.Book.pk方法的典型用法代码示例。如果您正苦于以下问题:Python Book.pk方法的具体用法?Python Book.pk怎么用?Python Book.pk使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在books.models.Book的用法示例。


在下文中一共展示了Book.pk方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: process_books_summary

# 需要导入模块: from books.models import Book [as 别名]
# 或者: from books.models.Book import pk [as 别名]
    def process_books_summary(self, session, user, book_list):
        seller_book_list = []
        amounts = {}

        for book in book_list:
            amount = book['amount']
            del book['amount']

            dbbook = Book(owner=user, accepted=False, sold=False)
            if 'pk' in book:
                dbbook.book_type_id = book['pk']
                seller_book_list.append(book['pk'])
                amounts[book['pk']] = amount
            else:
                book['isbn'] = re.sub(r'[^\dX]+', '', book['isbn'].upper())
                book['price'] = Decimal(book['price'])
                if book['publication_year'] == "":
                    book['publication_year'] = 1970

                book_type = BookType(**book)
                book_type.save()
                dbbook.book_type = book_type

                seller_book_list.append(book_type.pk)
                amounts[book_type.pk] = amount

            dbbook_list = []
            for i in range(0, amount):
                dbbook.pk = None
                dbbook_list.append(dbbook)

            Book.objects.bulk_create(dbbook_list)

        session['seller_books'] = (seller_book_list, amounts)
        return True, None
开发者ID:m4tx,项目名称:egielda,代码行数:37,代码来源:views.py

示例2: accept_books

# 需要导入模块: from books.models import Book [as 别名]
# 或者: from books.models.Book import pk [as 别名]
def accept_books(request, user_pk):
    user = get_object_or_404(AppUser, pk=user_pk)
    books = Book.objects.filter(owner=user, accepted=False).select_related('book_type')

    d = dict()
    for book in books:
        d[book.book_type] = d.setdefault(book.book_type, 0) + 1

    book_type_list = []
    correct_book_list = []
    for book_type, amount in d.items():
        book_type.amount = amount
        book_type_list.append(book_type)

    if not books:
        raise Http404("There's no books of that user.")

    if request.method == 'POST':
        with transaction.atomic():
            books_count = 0

            books.update(accepted=True, accept_date=timezone.now())
            for book_type in book_type_list:
                if not book_type.visible:
                    book_type.price = Decimal(request.POST.get('price-' + str(book_type.pk), 1))
                    book_type.visible = True
                    book_type.save()

                new_amount = int(request.POST.get('amount-' + str(book_type.pk), -1))
                if new_amount < 0:
                    return HttpResponseBadRequest()

                if new_amount < book_type.amount:
                    books_list = Book.objects.filter(owner=user, book_type=book_type)
                    books_to_keep = books_list[:new_amount]
                    books_list.exclude(pk__in=books_to_keep).delete()

                elif new_amount > book_type.amount:
                    amount_difference = new_amount - book_type.amount
                    book = Book(book_type=book_type, owner=user)

                    book_list = []
                    for i in range(0, amount_difference):
                        book.pk = None
                        book.accepted = True
                        book.accept_date = timezone.now()
                        book_list.append(book)

                    Book.objects.bulk_create(book_list)

                if new_amount > 0:
                    correct_book_list.append(book_type)

                book_type.amount = new_amount
                books_count += new_amount

        # Seller id shown to the user
        seller_id = timezone.now().strftime("%Y%m%d") + "-" + str(user.pk) + "-" + str(books_count)
        return render(request, 'sellers/success.html',
                      {'seller': user, 'seller_ID': seller_id, 'given_book_list': correct_book_list})
    else:
        hide_actions = True
        for book_type in book_type_list:
            if not book_type.visible:
                hide_actions = False
                break
        return render(request, 'sellers/accept.html',
                      {'user_name': user.user_name(), 'book_list': book_type_list,
                       'hide_actions': hide_actions, 'student_pk': user_pk,
                       'currency': getattr(settings, 'CURRENCY', 'USD')})
开发者ID:m4tx,项目名称:egielda,代码行数:72,代码来源:views.py


注:本文中的books.models.Book.pk方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。