1 year ago
#273688
Rajat Jog
Custom setting model does not apply until server restarted in django
I'm trying custom setting model to change some settings from admin.
class Setting(models.Model):
is_duel_cart_allowed = models.BooleanField(default=True)
free_cancellation_duration = models.DurationField(default="0:0:0")
return_order_duration = models.DurationField(default="0:0:0")
is_return_charges_deducted = models.BooleanField(_("Return charges applied when order is returned"),default=True)
is_shipping_charges_deducted_on_return = models.BooleanField(_("Deduct shipping charges when order is returned"),default=True)
is_product_cancellation_charge_deducted = models.BooleanField(_("Cancellation Charges deducted when product is cancelled"), default=True)
is_shipping_charges_deducted_on_cancel = models.BooleanField(_("Deduct shipping charges when order is cancelled"), default=True)
This is my model and when I turn ON/OFF any of these boolean fields, settings will remain unchanged until server is restarted.
How to tackle this problem?
Here is my view function code where I'm checking for these models changes
from core.models import Setting
setting = Setting.objects.first()
class ReturnOrderViewset(viewsets.ModelViewSet):
serializer_class = OrderSerializer
def get_queryset(self):
queryset = Order.objects.prefetch_related('items', 'items__product', 'items__user').filter(status_id=12)
return queryset
def create(self, request, *args, **kwargs):
order = request.POST.get('order_id')
return_reason = request.POST.get('return_reason')
current_time = timezone.now()
return_order_duration = setting.return_order_duration
try:
order = Order.objects.get(id=int(order))
return_reason = ReturnReason.objects.get(id=int(return_reason))
except Order.DoesNotExist:
return Response({'message': 'Order not found'}, status=status.HTTP_404_NOT_FOUND)
except ReturnReason.DoesNotExist:
return Response({'message': 'Order not found'}, status=status.HTTP_404_NOT_FOUND)
exp_time = order.created + return_order_duration
print(order.created, exp_time)
if order.status_id == 8:
print("status is complete ======")
if exp_time > current_time:
print("exp time is great than current time")
if all(item.product.is_return_allowed for item in order.items.all()):
print("All products are return allowed =====")
deductions = 0.0
if setting.is_return_charges_deducted:
deductions += float(return_reason.return_charges)
if setting.is_shipping_charges_deducted_on_return:
deductions += float(order.shipping_charges)
returning_amount = float(order.total_cost) - float(deductions)
order.status_id = 12
order.return_reason=return_reason
order.save()
subject = "Order Return Email"
message = render_to_string('order_return.html', {'order': order})
recipient_list = [order.user.email,]
send_html_email(subject, message, recipient_list)
return Response({
'message': 'Your order is returned',
'deductions': deductions,
'returning_amount': round(returning_amount, 2)},
status=status.HTTP_200_OK
)
else:
print("some products return not allowed =====")
return Response({'message': '[ERR RNA] Your order cannot be returned, please contact customer support'}, status=status.HTTP_200_OK)
else:
print("more than return time =====")
return Response({'message': '[ERR TMEXD] Your order cannot be returned, please contact customer support'}, status=status.HTTP_200_OK)
else:
print('Order status is not complete')
return Response({'message': '[ERR SNCOM] Your order cannot be returned, please contact customer support'}, status=status.HTTP_200_OK)
Even after changing setting from admin the calculations are done on previous settings. And once I restart server then its fine. How to tackle this in production?
Thanks In Advance
django
django-models
django-settings
django-3.0
0 Answers
Your Answer