def make_order(self, request): cart = get_cart(request) self.handle_user(request) self.handle_amount(request) # self.total_price = cart.total_price total_price = cart.get_price(price_type='total_price') # total_price = Decimal.from_float(total_price) # print("total_price:", total_price) # print("total_price:", type(total_price)) self.total_price = total_price self.ordered = True self.save() cart.order = self cart.ordered = True cart.save() from sw_utils.helpers import get_admin_url from django.contrib.sites.models import Site site = Site.objects.get_current().domain order_admin_url = site + get_admin_url(self) cart_items = CartItem.objects.filter(cart=cart) context = { 'cart_items': cart_items, 'order_admin_url': order_admin_url, } box_send_mail( subject=_(f'Отримано замовлення товарів # {self.id}'), template='sw_order/mail.html', email_config=OrderRecipientEmail, model=self, fail_silently=True, # fail_silently= False, context=context, )
def create_comment(request): # TODO: фукнція не дороблена. config = GlobalConfig.get_solo() query = request.POST or request.GET post_id = query.get('post_id') text = query.get('text') comment = PostComment.objects.create( text=text ) if post_id: comment.post = Post.objects.get(id=post_id) comment.save() if config.auto_comment_approval: comment.is_active = True comment.save() data = config.get_data('comment') box_send_mail( subject=data['subject'], recipients_list=data['emails'], ) box_send_mail( subject = f'Отримано коментар до блогу', template = 'sw_blog/mail.html', email_config = BlogRecipientEmail, model = comment, ) response = { 'status':'OK', 'is_active':comment.is_active, } return JsonResponse(response)
def sw_contact(request): # query = request.body.decode('utf-8') # query = json.loads(query) query = request.POST or request.GET if not query: query = request.data name = query.get('name', '---') email = query.get('email', '---') phone = query.get('phone', '---') message = query.get('message', '---') url = request.META.get('HTTP_REFERER') model = Contact.objects.create(name=name, email=email, phone=phone, message=message, url=url) box_send_mail( subject=_('Отримано контактну форму'), template='sw_contact_form/mail.html', email_config=ContactRecipientEmail, model=model, fail_silently=True, # fail_silently= False, ) return JsonResponse({ 'status': 'OK', })
def item_info(request): ''' заявка на інформацію про товар ''' query = request.GET or request.POST item_id = query.get('product_id') if not item_id: item_id = query.get('item_id') name = query.get('name', '---') phone = query.get('phone', '---') email = query.get('email', '---') message = query.get('message', '---') item_request = ItemRequest.objects.create( name=name, phone=phone, email=email, message=message, ) if item_id: item = Item.objects.get(id=item_id) item_request.item = item item_request.save() attributes = json.loads(query.get('attributes')) attrs = [] for attribute in attributes: item_attribute_id = attribute['item_attribute_id'] item_attribute = ItemAttribute.objects.get( id=item_attribute_id).attribute item_attribute_values = [] if 'item_attribute_value_id' in attribute: item_attribute_value_ids = [ attribute['item_attribute_value_id'], ] elif 'item_attribute_value_ids' in attribute: item_attribute_value_ids = attribute['item_attribute_value_ids'] for item_attribute_value_id in item_attribute_value_ids: item_attribute_value = ItemAttributeValue.objects.get( id=item_attribute_value_id).value item_attribute_values.append(item_attribute_value) attrs.append({ "item_attribute": item_attribute, "item_attribute_values": item_attribute_values, }) context = { "attrs": attrs, } box_send_mail( subject=('Було отримано заявку на інформацію про товар'), template='sw_order/item_info_mail.html', email_config=OrderRecipientEmail, model=item_request, fail_silently=False, context=context, ) return JsonResponse({ 'status': 'OK', })
def make_orderold(self, request): cart = get_cart(request) self.handle_user(request) self.handle_amount(request) self.total_price = cart.total_price self.ordered = True self.save() cart.order = self cart.ordered = True cart.save() cart_items = CartItem.objects.filter(cart=cart) context = {'cart_items': cart_items} box_send_mail( subject=_(f'Отримано замовлення товарів # {self.id}'), template='sw_order/mail.html', email_config=OrderRecipientEmail, model=self, fail_silently=False, context=context, )
def create_review(request): query = request.POST data = query.get('data') if data: query = json.loads(data) print(query) item_id = query['item_id'] rating = query['product_rating'] text = query.get('text', '---') name = query.get('name', '---') phone = query.get('phone', '---') email = query.get('email', '---') item = Item.objects.get(id=item_id) review = ItemReview.objects.create( item=item, text=text, phone=phone, email=email, name=name, rating=rating, ) # if GlobalConfig.get_solo().auto_review_approval: # review.is_active = True # review.save() json_review = ItemReviewSerializer(review).data response = { "review": json_review, "reviews_count": item.reviews.all().count(), "current_star": rating, "rounded_stars": item.rounded_stars, "stars": item.stars, "is_active": review.is_active, "status": "OK" } box_send_mail( subject=(f"Отримано відгук до товару {item.title}"), template='mail/item_review_mail.html', email_config=CatalogRecipientEmail, model=review, ) return JsonResponse(response)