async def post(self, *args, **kwargs): body = self.request.body body_decoded = json.loads(body) try: post_data = Customer(raw_data=body_decoded) except (ValueError, ValidationError, AssertionError, DataError) as e: self.set_status(400) self.write({'error': str(e)}) return cnpj = post_data.cnpj exists_customer = await Customer.objects().find(cnpj=cnpj) if exists_customer: self.set_status(400) self.write({'error': 'Customer already exists'}) return data = post_data.to_primitive() result = await Customer.objects().create(data) response_data = data response_data.update({"_id": str(result.inserted_id)}) self.set_status(201) self.write(response_data) return
def test_verify_correct_category_name_person(self): pk_person = 1 customer = Customer.objects.get(pk=pk_person) person = Person.objects.get(pk=pk_person) customer_with_subclass = Customer.get_child(pk_person, customer.type()) self.assertEqual(customer.type(), Customer.PERSON) self.assertEqual(customer_with_subclass.cpf, person.cpf)
def sign_up(request): if request.method == 'POST': form = SignUpForm(request.POST) if form.is_valid(): form.save() username = form.cleaned_data.get('username') user = User.objects.get(username=username) group = Group.objects.get(name="Customer") user.groups.add(group) customer = Customer(user=user, address=form.cleaned_data.get('address'), phone=form.cleaned_data.get('phone')) customer.save() return redirect('dashboard') else: form = SignUpForm() return render(request, 'registration/signup.html', {'form': form})
def test_verify_correct_category_name_company(self): pk_company = 2 customer = Customer.objects.get(pk=pk_company) company = Company.objects.get(pk=pk_company) customer_with_subclass = Customer.get_child(pk_company, customer.type()) self.assertEqual(customer.type(), Customer.COMPANY) self.assertEqual(customer_with_subclass.cnpj, company.cnpj) self.assertEqual(customer_with_subclass.doing_business_as, company.doing_business_as)
def save(cls, data, instance=None): try: customer = instance if instance else Customer() for key, value in data.items(): setattr(customer, key, value) customer.save() return customer except Exception as ex: # todo: handle log raise ex
def search_customer(request): """ 指定されたkeywordをname or emailに含む顧客を全て返す。keywordが指定されなければ、空のリスト([])を返す :param request: :return: """ keyword = request.GET.get('keyword') customers = [ customer.to_dict() for customer in Customer.search(keyword=keyword) ] return JsonResponse(customers, safe=False)
def run(): customer_count = Customer.objects.all().count() to_be_created = 100000 - customer_count customers = [ Customer( name=f"customer_{i}", email=f"customer_{i}@gmail.com", ) for i in range(to_be_created) ] print("Created customers: ", len(Customer.objects.bulk_create(customers)))
def save(cls, data, instance=None): try: customer_name = data.get('customer_name', '') ticket = instance if instance else Ticket() with transaction.atomic(): for key, value in data.items(): setattr(ticket, key, value) if customer_name: if ticket.customer_id: customer = CustomerService.get(ticket.customer_id) else: customer = Customer() customer.name = customer_name customer.save() ticket.customer_id = customer.id ticket.save() return ticket except Exception as ex: # todo: handle log raise ex
def post(self, request, *args, **kwargs): serializer = self.get_serializer(data=request.data) serializer.is_valid(raise_exception=True) customer = Customer() customer.name = serializer.data['name'] customer.email = serializer.data['email'] customer.tax_id = serializer.data['tax_id'] customer.save() headers = self.get_success_headers(serializer.data) return Response(serializer.data, status=status.HTTP_201_CREATED, headers=headers)
last_name=fake.last_name(), email=fake.free_email(), phone_number=fake.phone_number()) operator = Operator(info=employee, park=park, car=None) car = Car(car_id=i, model="BMW i3", amount_of_places=4, color=fake.color_name(), engineer=engineer, park=park, car_plate=2) customer = Customer(username=fake.user_name(), first_name=fake.first_name(), last_name=fake.last_name(), email=fake.free_email(), country=fake.country(), zipcode=fake.postcode(), city=fake.city(), phone_number=fake.phone_number(), operator=operator, car=car) p = datetime.now() p = random_date(d1, d2) make_aware(p) charge = Charge(charge_id=i, car=car, charging_station=charging_station, time=random_date(d1, d2), customer=customer) charge.save() for i in range(4, 110):
def send_email_to_all_task(): Customer.send_email_to_all()