def project_init(row): c = Project.query.filter_by(name=row['Categorie']).first() customer = Customer() customer.id = row['Id'] customer.category = c customer.display_as = row['Raison Sociale'] customer.firstname = row['Nom'] customer.lastname = row['Prénoms'] customer.adresse = row['Adresse'] customer.telephone = row['Téléphone'] customer.email = row['Email'] return customer
def customer_create(): params = request.json required_fields = ['fullname', 'phone'] for rf in required_fields: if not params.has_key(rf): raise BadRequest("Need `%s` field" % rf, jsonify({'errCode': 'ERROR_FIELD_REQUIRED'})) if not len(params['fullname']): raise BadRequest("Fullname not null", jsonify({'errCode': 'ERROR_FULLNAME_IS_NULL'})) phone = params['phone'] email = params['email'] note = params.get('note', "") if not re.match(PHONE_PATTERN, phone): raise BadRequest("Phone is not true format", response=jsonify({'errCode': 'ERROR_PHONE_FORMAT'})) if not re.match(EMAIL_PATTERN, email): raise BadRequest("Email is not true format", jsonify({'errCode': 'ERROR_EMAIL_FORMAT'})) if len(note) > 1000: raise BadRequest("Note is too long", jsonify({'errCode': 'ERROR_NOTE_TOO_LONG'})) conds = (Customer.phone == params['phone']) if params.get('email'): conds |= (Customer.email == params['email']) customer_obj = Customer.query.filter(conds).first() if customer_obj: raise SecurityError("Customer %s is existed!" % params['fullname'], jsonify({'errCode': 'ERROR_CUSTOMER_IS_EXISTED'})) #TODO verify email and phone customer_obj = Customer() customer_obj.fullname = params['fullname'] customer_obj.email = email customer_obj.phone = phone customer_obj.note = note customer_obj.birthday = params.get('birthday') customer_obj.created_at = datetime.datetime.utcnow() customer_obj.updated_at = datetime.datetime.utcnow() db.session.add(customer_obj) db.session.commit() customer_obj = Customer.query.filter(Customer.phone == phone).first() return jsonify(customer_obj.to_dict())
def customers_save_process(request): if request.session.is_empty(): return redirect('/login/') user = User.objects.get(username=request.session.get('username')) name = request.GET['name'] owner_first_name = request.GET['owner_first_name'] owner_last_name = request.GET['owner_last_name'] address = request.GET['address'] landline = request.GET['landline'] email = request.GET['email'] mobile = request.GET['mobile'] bank = request.GET['bank'] bank_number = request.GET['bank_number'] customer = Customer() customer.name = name customer.owner_first_name = owner_first_name customer.owner_last_name = owner_last_name customer.address = address customer.landline = landline customer.email = email customer.mobile = mobile customer.bank = bank customer.bank_number = bank_number try: customer.save() user.branch.customer.add(customer) sweetify.sweetalert(request, icon='success', title='Added Customer Successfully', text='{} successfully added'.format(customer.name), persistent='Dismiss') except: sweetify.sweetalert(request, icon='error', title='Something went wrong', persistent='Dismiss') return redirect('/customer/')
def post(self): """ Add customer """ data = request.json customer = Customer(last_name=data['last_name'], first_name=data['first_name']) if data.get('email', None) is not None: if len(Customer.search().query( 'match', email=data['email']).execute()) != 0: abort(400, error='Email already exist.') customer.email = data['email'] if data.get('phone_number', None) is not None: if len(Customer.search().query( 'match', phone_number=data['phone_number']).execute()) != 0: abort(400, error='Phone number already exist.') customer.phone_number = data['phone_number'] if data.get('bluetooth_mac_address', None) is not None: if len(Customer.search().query( 'match', bluetooth_mac_address=data['bluetooth_mac_address']). execute()) != 0: abort(400, error='Bluetooth mac address already exist.') customer.bluetooth_mac_address = data['bluetooth_mac_address'] customer.save() return customer.to_dict(include_id=True), 201