def purchase(self, request, **kwargs): self.method_check(request, allowed=['post']) data = json.loads(request.body) try: address = data.get('address') date = data.get('date') time = data.get('time') except KeyError: return self.create_response(request, {}, HttpBadRequest) user = request.user profile = UserProfile.objects.get(user=user) if user.is_authenticated(): orders_in_cart = Order.objects.filter( created_by=user, is_in_cart=True) context = { 'products': [], 'total_price': 0, 'user': { 'name': '%s %s' % (user.first_name, user.last_name), 'phone': profile.phone_number, 'address': address, 'date': date, 'time': time } } for order in orders_in_cart: product = { 'id': order.product.id, 'name': order.product.name, 'count': order.count, 'price': order.count * (order.product.price - order.product.discount) } context['products'].append(product) context['total_price'] += product['price'] subject = '[Mickey] Новый Заказ' EmailService.send_email('order.email.html', subject, context, settings.SEND_TO) orders_in_cart.update(is_in_cart=False) return self.create_response(request, {}) else: return self.create_response(request, {}, HttpUnauthorized)
def reset_password(self, request, **kwargs): self.method_check(request, allowed=['post']) data = json.loads(request.body) try: email = data.get('email') except KeyError: return self.create_response(request, {}, HttpBadRequest) try: user = User.objects.get(username=email) except ObjectDoesNotExist: return self.create_response(request, {}, HttpBadRequest) new_password = User.objects.make_random_password() user.set_password(new_password) user.save() context = { 'name': '%s %s' % (user.first_name, user.last_name), 'password': new_password } subject = '[mickey.by] Сброс пароля.' EmailService.send_email('reset_password.email.html', subject, context, [user.email, ]) return self.create_response(request, {})