def create(self, validated_data): """ Create company and additional info :param validated_data: :return: """ validated_data.update({'lead': True}) customer = Customer(**validated_data) customer.save() return customer
def new(self, request): try: q = request.query_params q_d = {k: v[0] if len(v) == 1 else v for k, v in q.lists()} customer = Customer(**q_d) customer.save() return Response(data=CustomerSerializer(customer).data) except: print(traceback.format_exc()) return Response(data={"is": "false"})
def save(self, validated_user_data): user = Customer( first_name=validated_user_data['first_name'], last_name=validated_user_data['last_name'], street_address=validated_user_data['street_address'], city=validated_user_data['city'], state_province=validated_user_data['state_province'], email_address=validated_user_data['email_address'], ) user.save() return user
class ModelTestCase(TestCase): """This class defines the test suite for the bucketlist model.""" def setUp(self): """Define the test client and other test variables.""" self.customer_name = "Write world class code" self.customer = Customer(name=self.customer_name) def test_model_can_create_a_bucketlist(self): """Test the bucketlist model can create a bucketlist.""" old_count = Customer.objects.count() self.customer.save() new_count = Customer.objects.count() self.assertNotEqual(old_count, new_count)
def replace_user(c1_json): session.delete(Customer).filter( Customer.email == c1_json["email"]).first() c1 = Customer(name=c1_json["name"], address=c1_json["address"], email=c1_json["email"]) session.add(c1) return {"message": "User succesfully replaced."}
def post(): c1_json = request.get_json() if session.query(Customer).filter( Customer.email == c1_json["email"]).first() == None: c1 = Customer(name=c1_json["name"], address=c1_json["address"], email=c1_json["email"]) session.add(c1) session.commit() return (jsonify({"message": "Successfully loaded."}), 200) else: return (jsonify({"message": "This email already exists."}), 404)
def delete(): c1_json = request.get_json() try: if Customer.check_user_existance_and_apply(c1_json, session): session.delete( session.query(Customer).filter( Customer.email == c1_json["email"]).first()) session.commit() return (jsonify({"Message": "User deleted."}), 200) else: return (jsonify({"Message": "User could not be found."}), 404) except: return (jsonify({"Message": "The database could not be accesed."}), 500)
def get(): c1_json = request.get_json() try: if Customer.check_user_existance_and_apply(c1_json, session): a_user = session.query(Customer).filter( Customer.email == c1_json["email"]).first() return (jsonify({ "name": a_user.name, "address": a_user.address, "email": a_user.email }), 200) else: return (jsonify({"Message": "User could not be found."}), 404) except: return (jsonify({"Message": "The database could not be accesed."}), 500)
async def random_businesses(request): async def fin(): async with in_transaction() as connection: for business in businesses_db: await business.delete(using_db=connection) businesses_db = [] businesses = [] request.addfinalizer(fin) async with in_transaction() as connection: for _ in range(request.param): business_data = business_data() business = Customer(**asdict(business_data)) await business.save(using_db=connection) businesses_db.append(business) businesses.append(OutputBusinessSchema.from_orm(business).dict()) yield businesses_db, businesses
async def random_customers(request): async def fin(): async with in_transaction() as connection: for customer in customers_db: await customer.delete(using_db=connection) customers_db = [] customers = [] request.addfinalizer(fin) async with in_transaction() as connection: for _ in range(request.param): customer_data = customer_data() customer = Customer(**asdict(customer_data)) await customer.save(using_db=connection) customers_db.append(customer) customers.append(OutputCustomerSchema.from_orm(customer).dict()) yield customers_db, customers
def save_objects(data): """ Сохранение данных в бд """ login = data[0] name = data[1] total = int(data[2]) quantity = int(data[3]) spent_money = total * quantity date = data[4].replace('\n', '') # Сохранение Customer try: customer = Customer.objects.get(login=login) except Customer.DoesNotExist: Customer(login=login, spent_money=spent_money).save() customer = Customer.objects.get(login=login) # Сохранение Item try: item = Item.objects.get(name=name) except Item.DoesNotExist: Item(name=name, total=total).save() item = Item.objects.get(name=name) # Сохранение Даты покупки Customer-ом try: instance = PurchaseDate.objects.create(date=date) instance.customer.add(customer) except RuntimeWarning: pass # Сохранение ItemAndCustomer try: item_and_customer = ItemAndCustomer.objects.get(customer=customer, item=item) item_and_customer.quantity += int(quantity) item_and_customer.save() except ItemAndCustomer.DoesNotExist: ItemAndCustomer(quantity=quantity, customer=customer, item=item).save()
def register_user(): if not request.is_json: return jsonify({"msg": "Missing JSON in request"}), 400 if not request.json.get("username"): return jsonify({"msg": "JSON missing username"}), 400 username = request.json.get("username") user = User.query.filter_by(username=username).first() if user: return jsonify({"msg": "Username already taken"}), 400 request.json["active"] = True user_schema = UserSchema() user = user_schema.load(request.json) user.role = ROLE_CUSTOMER save_to_db(db, user) user_claims_ = {"id": user.id, "role": ROLE_CUSTOMER} access_token = create_access_token( identity=user.id, user_claims=user_claims_) refresh_token = create_refresh_token( identity=user.id, user_claims=user_claims_) # create a customer, only for simple auth purposes customer = Customer( name=username, email=user.email, user_id=user.id) save_to_db(db, customer) resp = { "user": user_schema.dump(user), "customer_id": customer.id, "access_token": access_token, "refresh_token": refresh_token} return jsonify(resp), 201
def check_customer(user_id): return Customer.confirm(user_id)
def new_task(): post_json = request.get_json() # Kunde anlegen oder über ID selektieren customer = None cus_id = None if hasattr(post_json, "cusID"): cus_id = post_json["cusID"] else: customer = Customer( cus_title = post_json["gender"], cus_first_name = post_json["firstName"], cus_last_name = post_json["lastName"], cus_email = post_json["email"], cus_phone_no = post_json["prefixNumber"] + " " + post_json["phone"], cus_street = post_json["street"], cus_house_number = post_json["houseNumber"], cus_post_code = post_json["postCode"] ) db.session.add(customer) # pylint: disable=maybe-no-member db.session.commit() # pylint: disable=maybe-no-member cus_id = customer.cus_id # Gerät anlegen oder über ID selektieren device = None dev_id = None if hasattr(post_json, "devID"): dev_id = post_json["devID"] else: device = Device( dev_name = post_json["devName"], dev_model = post_json["devModel"], dev_category = post_json["categoryName"], dev_electronic_mechanical_type = post_json["electricalMechanical"], dev_mnf_name = post_json["manufacturerName"], ) db.session.add(device) # pylint: disable=maybe-no-member db.session.commit() # pylint: disable=maybe-no-member dev_id = device.dev_id new_task = Task( tsk_fault_description = post_json["faultDescription"], tsk_creation_date = datetime.now(), tsk_cus_id = cus_id, tsk_dev_id = dev_id, tsk_state = "new", tsk_next_step = "not_set", ) db.session.add(new_task) # pylint: disable=maybe-no-member db.session.commit() # pylint: disable=maybe-no-member tk = generate_token("customer", new_task.tsk_id) # QR-Code generieren image_file = generate_qrcode_label("customer", new_task.tsk_id, tk) # Files anlegen files = post_json["files"] for filename in files: new_file = Image(img_filename=filename, img_tsk_id=new_task.tsk_id) db.session.add(new_file) # pylint: disable=maybe-no-member db.session.commit() # pylint: disable=maybe-no-member # Token als neuer Token in der Session speichern. session['NEW_TOKEN'] = tk return {'tsk_id':new_task.tsk_id, 'tsk_token': tk}
def setUp(self): """Define the test client and other test variables.""" self.customer_name = "Write world class code" self.customer = Customer(name=self.customer_name)