def add(data, db_session, username): logger.info(LogMsg.START, username) schema_validate(data,ITEM_ADD_SCHEMA_PATH) logger.debug(LogMsg.SCHEMA_CHECKED) book_id = data.get('book_id') person_id = data.get('person_id') if person_id is None: logger.error(LogMsg.DATA_MISSING, 'person_id') if is_book_in_library(person_id, book_id, db_session): logger.error(LogMsg.ALREADY_IS_IN_LIBRARY, {'book_id': book_id}) raise Http_error(409, Message.ALREADY_EXISTS) book = get_book(book_id, db_session) if book is None: logger.error(LogMsg.NOT_FOUND, {'book_id': book_id}) raise Http_error(404, Message.NOT_FOUND) if book.type.name in ONLINE_BOOK_TYPES : if data.get('count') > 1: logger.error(LogMsg.BOOK_ONLINE_TYPE_COUNT_LIMITATION) raise Http_error(400, Message.ONLINE_BOOK_COUNT_LIMITATION) content_id = book_has_content(book_id,'Original',db_session) if not content_id: logger.error(LogMsg.CONTENT_NOT_FOUND,{'book_id':book_id}) raise Http_error(404,Message.BOOK_HAS_NO_CONTENT) if not is_generated(content_id): logger.error(LogMsg.CONTENT_NOT_GENERATED, {'content_id': content_id}) raise Http_error(404, Message.BOOK_NOT_GENERATED) model_instance = OrderItem() populate_basic_data(model_instance, username) logger.debug(LogMsg.POPULATING_BASIC_DATA) model_instance.order_id = data.get('order_id') model_instance.book_id = book_id model_instance.count = data.get('count') # TODO discount not imposed yet model_instance.discount = data.get('discount', 0.0) model_instance.description = data.get('description') model_instance.price_detail = data.get('price_detail') model_instance.unit_price = get_book_price_internal(model_instance.book_id, db_session) logger.debug(LogMsg.ORDER_ITEM_UNIT_PRICE, model_instance.unit_price) model_instance.net_price = calc_net_price(model_instance.unit_price, model_instance.count, model_instance.discount) logger.debug(LogMsg.ORDER_ITEM_NET_PRICE, model_instance.net_price) db_session.add(model_instance) logger.info(LogMsg.END) return model_instance
def add(data, db_session, username): logger.info(LogMsg.START, username) check_schema(['book_id', 'count', 'order_id', 'person_id'], data.keys()) logger.debug(LogMsg.SCHEMA_CHECKED) book_id = data.get('book_id') person_id = data.get('person_id') if person_id is None: logger.error(LogMsg.DATA_MISSING, 'person_id') if is_book_in_library(person_id, book_id, db_session): logger.error(LogMsg.ALREADY_IS_IN_LIBRARY, {'book_id': book_id}) raise Http_error(409, Message.ALREADY_EXISTS) book = get_book(book_id, db_session) if book is None: logger.error(LogMsg.NOT_FOUND, {'book_id': book_id}) raise Http_error(404, Message.NOT_FOUND) if book.type.name in ONLINE_BOOK_TYPES and data.get('count') > 1: logger.error(LogMsg.BOOK_ONLINE_TYPE_COUNT_LIMITATION) raise Http_error(400, Message.ONLINE_BOOK_COUNT_LIMITATION) model_instance = OrderItem() populate_basic_data(model_instance, username) logger.debug(LogMsg.POPULATING_BASIC_DATA) model_instance.order_id = data.get('order_id') model_instance.book_id = book_id model_instance.count = data.get('count') # TODO discount not imposed yet model_instance.discount = data.get('discount', 0.0) model_instance.description = data.get('description') model_instance.price_detail = data.get('price_detail') model_instance.unit_price = get_book_price_internal( model_instance.book_id, db_session) logger.debug(LogMsg.ORDER_ITEM_UNIT_PRICE, model_instance.unit_price) model_instance.net_price = calc_net_price(model_instance.unit_price, model_instance.count, model_instance.discount) logger.debug(LogMsg.ORDER_ITEM_NET_PRICE, model_instance.net_price) db_session.add(model_instance) logger.info(LogMsg.END) return model_instance
def form_valid(self, form): self.object = form.save() # Fetch all products basket = self.request.session.get('basket', []) products_in_basket = [item.get('product') for item in basket] products = Product.objects.filter(pk__in=products_in_basket, active=True, shop__active=True).order_by('shop') products_dict = {product.pk: product for product in products} # Total cost & valid items list per shop shop_items_and_cost = dict.fromkeys( {product.shop for product in products}) for key in shop_items_and_cost: shop_items_and_cost[key] = { 'total_cost': Decimal(0.00), 'order_items': [], 'item_count': 0 } # Create orderItems for item in basket: product = products_dict[item.get('product')] count = item.get('count') # If product is not found, skip product if product is None: continue # If count is 0 or below, skip item if count < 1: continue order_item = OrderItem() order_item.product = product order_item.order = self.object order_item.count = count # Save the offer/on sale price if any, else use normal price order_item.price = product.offer_price if product.offer_price else product.price order_item.save() shop_items_and_cost[product.shop]['item_count'] += count shop_items_and_cost[product.shop]['total_cost'] += Decimal( order_item.subtotal()) shop_items_and_cost[product.shop]['order_items'].append(order_item) context = { 'order': self.object, 'shop_items_and_cost': shop_items_and_cost } html_message = render_to_string('emails/order_confirmation.html', context) txt_message = render_to_string('emails/order_confirmation.txt', context) subject = gettext('Order confirmation') self.object.status = Order.ORDERED email = EmailMultiAlternatives(subject, txt_message) email.from_email = settings.DEFAULT_FROM_EMAIL email.to = [self.object.email] email.attach_alternative(html_message, "text/html") email.content_subtype = 'html' email.mixed_subtype = 'related' with open('base/static/base/img/fb_logo.png', mode='rb') as f: image = MIMEImage(f.read()) image.add_header('Content-ID', "<Foodbee_logo_long.png>") email.attach(image) email.send() # Clear session self.request.session.flush() return HttpResponseRedirect(self.get_success_url())