def test_order_serializer(self): """Expect failed to make nested order/order-item""" customer_context = { 'request': MockRequest(self.admin) # sarit is a customer } data = {'menus': [1, 1, 2, 3]} serializer = OrderSerializer(data=data, context=customer_context) is_valid = serializer.is_valid() msg = 'Invalid pk "1" - object does not exist.' assert False is is_valid assert msg == str(serializer.errors.get('menus')[0])
def update(self, request, *args, **kwargs): try: data = request.data new_data = "; ".join("{0} = {1}".format(key, value) for key, value in data.items()) licence = Licence.objects.get(pk=kwargs['id']) serzLice = LicenceSerializer(licence) old_data = "; ".join("{0} = {1}".format(key, value) for key, value in serzLice.data.items() if key != "id") audit = AuditTrail.objects.create(user=request.user.username, rowId=licence.id, action="Update", table_name="Licence", new_data=new_data, old_data=old_data) flag = 0 if licence.issueDate == licence.OrderID.orderStartDate: flag = 1 order = OrderCreateUpdateSerializer(licence.OrderID, data=request.data) if order.is_valid(): obj1 = order.save() request.data['OrderID'] = obj1.id if flag == 1: licence = LicenceCreateSerializer(licence, data=request.data) else: licence = LicenceUpdateSerializer(licence, data=request.data) if licence.is_valid(): obj2 = licence.save() audit.save() return Response({ "order": OrderSerializer(obj1).data, "licence": LicenceDetailSerializer(obj2).data }) else: print(licence.errors) print(order.errors) return Response({"error": "Failed to create"}, status=400) except Exception as error: print(error) logging("ERROR : LicenceApp->LicenceUpdate : {0}.\n".format(error)) return Response({"Error": "Wrong Request"}, status=500)
def update(self, request, *args, **kwargs): try: data = request.data new_data = "; ".join("{0} = {1}".format(key, value) for key, value in data.items()) licence = Licence.objects.get(pk=kwargs['id']) serzLice = LicenceSerializer(licence) old_data = "; ".join("{0} = {1}".format(key, value) for key, value in serzLice.data.items() if key != "id") audit = AuditTrail.objects.create(user=request.user.username, rowId=licence.id, action="GraceRenew", table_name="Licence", new_data=new_data, old_data=old_data) order = OrderGraceUpdateSerializer(licence.OrderID, data=request.data) if order.is_valid(): obj1 = order.save() order = licence.OrderID order.graceCount += 1 # if order.is_valid(): obj1 = order.save() # mutable = request.POST._mutable # request.POST._mutable = True # request.data['OrderID'] = obj1.id # request.POST._mutable = mutable licence = Licence.objects.get(pk=kwargs['id']) licence = LicenceGraceTrailUpdateSerializer(licence, data=request.data) if licence.is_valid(): obj2 = licence.save() print(obj2) audit.save() purpose = "graceNotify" users = User.objects.filter( profile__cid=obj2.MachineID.Customer) if users.exists(): print("yes users are presentr") for user in users: if user.is_active == True: print(user) mail(to_address=user.email, username=user.username, purpose=purpose, link=None, hciId=obj2.MachineID.MachineID, app=obj2.App.ApplicationName, grace=(obj2.graceExpiryDate - obj2.graceIssueDate).days + 1, start=obj2.graceIssueDate.strftime( '%d-%m-%Y'), end=obj2.graceExpiryDate.strftime( '%d-%m-%Y'), companyName=user.profile.cid.Name) return Response({ "order": OrderSerializer(order).data, "licence": LicenceDetailSerializer(obj2).data }) print(licence.errors) return Response({"Error": "Wrong Input"}, status=400) except Exception as error: print(error) logging( "ERROR : LicenceApp->LicenceGraceRenew : {0}.\n".format(error)) return Response({"Error": "Wrong Request"}, status=500)
def create(self, request, *args, **kwargs): try: data = request.data new_data = "; ".join("{0} = {1}".format(key, value) for key, value in data.items()) macid = request.data['MachineID'] appid = request.data['App'] objs = Licence.objs.filter(MachineID=macid, App=appid) existLic = None if objs.exists(): existLic = objs.first() print(existLic) if not existLic.isArchived: return Response( { "message": "Already a Licence is present for the given HCI and Application" }, status=400) order = OrderCreateUpdateSerializer(data=request.data) if order.is_valid(): obj1 = order.save() # mod_data = request.data.copy() request.data['OrderID'] = obj1.id if existLic and existLic.isArchived: request.data['isArchived'] = False data = LicenceCreateArchiveSerializer(existLic, data=request.data) else: data = LicenceCreateSerializer(data=request.data) if data.is_valid(): obj2 = data.save() audit = AuditTrail.objects.create( user=request.user.username, rowId=obj2.id, action="Create", table_name="Licence", new_data=new_data) audit.save() purpose = "" users = User.objects.filter( profile__cid=obj2.MachineID.Customer) if users.exists(): print("yes users are presentr") for user in users: if user.is_active == True: print(user) if request.data['period'] == 77: purpose = "licenceNotify" mail(to_address=user.email, username=user.username, purpose=purpose, link=None, hciId=obj2.MachineID.MachineID, app=obj2.App.ApplicationName, grace="-", start="-", end="-", companyName=user.profile.cid.Name) elif 'trial' in request.data and request.data[ 'trial']: purpose = "trialNotify" mail(to_address=user.email, username=user.username, purpose=purpose, link=None, hciId=obj2.MachineID.MachineID, app=obj2.App.ApplicationName, grace=(obj2.graceExpiryDate - obj2.graceIssueDate).days + 1, start=obj2.graceIssueDate.strftime( '%d-%m-%Y'), end=obj2.graceExpiryDate.strftime( '%d-%m-%Y'), companyName=user.profile.cid.Name) elif 'trial' not in request.data or ( 'trial' in request.data and not request.data['trial']): purpose = "licenceNotify" mail(to_address=user.email, username=user.username, purpose=purpose, link=None, hciId=obj2.MachineID.MachineID, app=obj2.App.ApplicationName, grace=(obj2.graceExpiryDate - obj2.graceIssueDate).days + 1, start=obj2.OrderID.orderStartDate. strftime('%d-%m-%Y'), end=obj2.expiryDate.strftime( '%d-%m-%Y'), companyName=user.profile.cid.Name) return Response({ "order": OrderSerializer(obj1).data, "licence": LicenceDetailSerializer(obj2).data }) print(data.errors) print(order.errors) return Response({"error": "Failed to create"}, status=400) except Exception as error: print(error) logging( "ERROR : LicenceApp->LicenceCreation : {0}.\n".format(error)) return Response({"Error": "Wrong Request"}, status=500)
def get_period(self, obj): orderObj = obj.OrderID obj = OrderSerializer(orderObj) return obj.data['period']
def get_startDate(self, obj): orderObj = obj.OrderID obj = OrderSerializer(orderObj) return obj.data['orderStartDate']