I was wondering how to Create/Read/Update/Delete records and their standard/custom fields, using python SOAP with NS WSDL
Here's a script that gives an example on how to login, and then to CRUD the records in NetSuite.
from zeep import Client
WSDL_URL = 'https://webservices.netsuite.com/wsdl/v2017_2_0/netsuite.wsdl' #WSDL we're using (don't change)
NS_EMAIL= 'NSemail#email.com' #NS-Email
NS_PASSWORD = 'NSPassword' #NS-Password
NS_ROLE = 'NSroleId' #NS-Role Id
NS_ACCOUNT = 'NSaccountId' #NS-Account Id
NS_APPID = 'NS268FD3-8553-4464-AEEB-FB6BE2EE616E' #NS App-ID
def make_app_info(client): #make app info, used in login_client
AppInfo = client.get_type('ns4:ApplicationInfo')
app_info = AppInfo(applicationId = NS_APPID)
return app_info
def make_passport(client): #make passport, used in login_client
RecordRef = client.get_type('ns0:RecordRef')
Passport = client.get_type('ns0:Passport')
role = RecordRef(internalId=NS_ROLE)
return Passport(email=NS_EMAIL,
password=NS_PASSWORD,
account=NS_ACCOUNT,
role=role)
def login_client(): #login
client = Client(WSDL_URL)
login = client.service.login(passport=make_passport(client), _soapheaders={'applicationInfo': make_app_info(client)}, )
return client
def add_customer(): #add a customer, example
client = login_client()
RecordRef = client.get_type('ns0:RecordRef') #set RecordRef as a RecordReference
StringCustomFieldRef = client.get_type('ns0:StringCustomFieldRef') #custom string field ref
CustomFieldList = client.get_type('ns0:CustomFieldList') #translator for custom field list
#customField acctName
acctName = StringCustomFieldRef() #this custom field is a string
acctName.internalID = '1569'
acctName.scriptId = 'custentity_sf_account_name'
acctName.value = 'Test data'
#customField acctId
acctID= StringCustomFieldRef()
acctID.internalId= '1596'
acctID.scriptId= 'custentity_account_id'
acctID.value = 'More Test data'
Customer = client.get_type('ns13:Customer') #make customer of type Customer
customer = Customer( #set customer
companyName='TEST',
entityId='TEST (US LLC)',
subsidiary = RecordRef(internalId='5', type='subsidiary'), #subsidiary is a RecordRef field, so you need to use RecordRef
customFieldList = CustomFieldList([acctID,acctName]) #set custom Fields
)
response = client.service.add(customer) #make the call
print(response)
def get_customer(id): #fetch customer
client = login_client()
Record = client.get_type('ns0:RecordRef') #set Record = to RecordReference
record = Record(internalId=id, type='customer') #change type for different type searches
response = client.service.get(record)
r = response.body.readResponse
#print(r) #detailed view
if r.status.isSuccess:
print(r.record) #r.record.companyName #simple view
def update_customer(): #update a customer
client = login_client()
Customer = client.get_type('ns13:Customer') #set Customer = to NS customer type
customer = Customer(
internalId = '451348', #must specify internalId
companyName='Testing 123'
)
response = client.service.update(customer) #upsert can also be used
print(response)
def delete_customer(id): #delete
client = login_client()
Record = client.get_type('ns0:RecordRef') #set Record to RecordReference
record = Record(internalId=id, type='customer')
response = client.service.delete(record) #run command
print(response)
def add_order(): #add an order
client = login_client()
RecordRef = client.get_type('ns0:RecordRef')
SelectCustomFieldRef = client.get_type('ns0:SelectCustomFieldRef')
StringCustomFieldRef = client.get_type('ns0:StringCustomFieldRef')
CustomFieldList = client.get_type('ns0:CustomFieldList')
SalesOrderItemList = client.get_type('ns19:SalesOrderItemList')
Order = client.get_type('ns19:SalesOrder')
SalesOrderItem = client.get_type('ns19:SalesOrderItem')
billtier = SelectCustomFieldRef() #using SelectCustomFieldRef since custom field is a select
billtier.internalId = '308' #custField InternalId
billtier.scriptId = 'custbody_bill_to_tier' #custField scriptId
billtier.value ={
'internalId': '2', #option ID
'externalId': None,
'typeId': '12' #list Id
}
item1 = SalesOrderItem() #making a single lineitem
item1.quantity = '1'
item1.item = RecordRef(internalId='5816')
item1.amount = 999
order = Order(
entity = RecordRef(internalId='451656', type='customer'),
itemList= SalesOrderItemList([item1]), #Add the line item
customFieldList = CustomFieldList([billtier]) #add the custom fields
)
response = client.service.add(order) #add the order
print(response)
Some Notes:
In cases where it says: RecordRef = client.get_type('ns0:RecordRef')
the ns0 refers to the namespace .xsd file that is in the import section on the wsdl.
A list of all NetSuite objects and their fields can be found here: http://www.netsuite.com/help/helpcenter/en_US/srbrowser/Browser2016_1/schema/other/salesorderitemlist.html?mode=package
Details about field references to use when handling different types of custom fields:
https://system.na1.netsuite.com/app/help/helpcenter.nl?fid=section_n3458179.html
Related
I am pulling data from a json file on the web, and updating it in my django database. I want to keep track of users that are associated with each team, but as soon as a user loads the page once they are added to the model. How do I avoid this?
class Team(models.Model):
name = models.CharField(max_length=120)
abbreviation = models.CharField(max_length=3)
id = models.IntegerField(primary_key=True)
link = models.CharField(max_length=120)
wins = models.IntegerField(default=0)
losses = models.IntegerField(default=0)
ties = models.IntegerField(default=0)
points = models.IntegerField(default=0)
users = models.ManyToManyField(User)
def getTeams():
import requests
baseUrl = "https://statsapi.web.nhl.com/"
# INITALIZING THE DATA IN THE DATA DICTIONARY
r = requests.get(baseUrl + '/api/v1/teams')
originalData = r.json()
# i dont need the copyright, only care about the teams
originalData = originalData["teams"]
for team in originalData:
id = team["id"]
try:
databaseTeam = Team.objects.get(id = id)
except Exception:
Team.objects.create(id = id)
databaseTeam = Team.objects.get(id = id)
databaseTeam.name = team["name"]
databaseTeam.abbreviation = team["abbreviation"]
databaseTeam.link = team["link"]
databaseTeam.save()
print("done")
#login_required
def myTeamView(request):
t1 = Thread(target=getTeams)
t1.start()
return(render(request, "teams/myTeams.html", {}))
The user is stored on user variable inside the request, so first we need to pass it to getTeams method. Then we use the method add of Manytomany fields to append an record to it, in this case the user.
def getTeams(request):
import requests
baseUrl = "https://statsapi.web.nhl.com/"
# INITALIZING THE DATA IN THE DATA DICTIONARY
r = requests.get(baseUrl + '/api/v1/teams')
originalData = r.json()
# i dont need the copyright, only care about the teams
originalData = originalData["teams"]
for team in originalData:
id = team["id"]
try:
databaseTeam = Team.objects.get(id = id)
except Exception:
Team.objects.create(id = id)
databaseTeam = Team.objects.get(id = id)
databaseTeam.name = team["name"]
databaseTeam.abbreviation = team["abbreviation"]
databaseTeam.link = team["link"]
databaseTeam.save()
databaseTeam.users.add(request.user) # after save!!
print("done")
#login_required
def myTeamView(request):
t1 = Thread(target=getTeams, args=(request, ))
t1.start()
return(render(request, "teams/myTeams.html", {}))
I have the following code I am trying to test:
def customer_add_order(request):
"""
params:
access_token
restaurant_id
address
order_details(json format), example:
[{"meal_id": 1, "quantity":2}, {"meal_id": 2, "quantity":3}]
stripe_token
return:
{"status": "success"}
"""
if request.method == "POST":
#Get token
access_token = AccessToken.objects.get(token = request.POST.get("access_token"),
expires__gt = timezone.now())
#Get profile
customer = access_token.user.customer
# Check if customer has a order that is not delivered
if Order.objects.filter(customer = customer).exclude(status = Order.DELIVERED):
return JsonResponse({"status": "fail", "error": "Your Last Order must be completed"})
# Check Address
if not request.POST("address"):
return JsonResponse({"status": "failed", "error": "Address is required."})
# Ger Order Details
order_details = json.load(request.POST["order_details"])
order_total = 0
for meal in order_details:
order_total += Meal.objects.get(id = meal["meal_id"]).price * meal[quantity]
if len(order_details)>0:
# Step 1 - Create an Order
order = Order.objects.create(
customer = customer,
restaurant_id = request.POST["restaurant_id"],
total = order_total,
status = Order.PENDING,
address = request.POST["address"]
)
# Step 2 - Create Order details
for meal in order_details:
OrderDetails.objects.create(
order = order,
meal_id = meal["meal_id"],
quantity = meal["quantity"],
sub_total = Meal.objects.get(id = meal["meal_id"]).price * meal["quantity"]
)
return JsonResponse({"status": "success"})
I enter the params in Postman, and use an access token that shows valid in django, and it hasn't expired. I am using the rest framework and the function is for API.
I use the following function for creating Access Token:
def create_user_by_type(backend, user, response, *args, **kwargs):
request = backend.strategy.request_data()
if backend.name == 'facebook':
avatar = 'https://graph.facebook.com/%s/picture?type=large' % response['id']
if request.get("user_type") == "driver" and not Driver.objects.filter(user_id=user.id):
Driver.objects.create(user_id=user.id, avatar = avatar)
elif not Customer.objects.filter(user_id=user.id):
Customer.objects.create(user_id=user.id, avatar = avatar)
Also, the line if request.get("user_type") == "driver" and not Driver.objects.filter(user_id=user.id): previously had an error because I had it in an outdated format request["user_type"]. Not sure if this is correct syntax for what I want to do, but I can create Access Tokens through Postman.
The issue was I was using the params in Postman to enter the Access Token and other information, instead it should in in 'body' with form-data selected.
I'm creating my very first web app with Flask. It must connect to MySQL (table called "Tasks") with SQLAlchemy and after that output all tasks from this table. All "tasks" have fields "StartDevelop" and "FinishDevelop" (DateTime fields). So my task is to output two buttons next to every name of "task". They must work with MySQL and send current time.
How can I connect two buttons to every row and connect all of them to python's script?
Example:
task1(string), AcceptTask1(button), CompleteTask1(button)
task2(string), AcceptTask2(button), CompleteTask2(button)
task3(string), AcceptTask3(button), CompleteTask3(button)
task4(string), AcceptTask4(button), CompleteTask4(button)
...
where number of tasks is accepted from datebase.
Code I made
I made dictionary, where key is task.Id and value is form(with string fields and two buttons). I also using field.descriptions to output/not output them.
#app.route('/database', methods=['GET','POST'])
#login_required
def database():
page = request.args.get('page', 1, type=int)
tasks = Tasks.query.filter(current_user.Id == Tasks.Developer or current_user.Id == Tasks.Reviewer).paginate(page, app.config['TASKS_PER_PAGE'], False)
forms = dict()
for task in tasks.items:
forms[task.Id] = DataBaseForm()
forms[task.Id].set_values(task,current_user)
class DataBaseForm(FlaskForm):
eord = StringField()
task = StringField()
assign_date = DateTimeField()
type = StringField()
accept_date = DateTimeField()
accepted = SubmitField(label = 'Submit', description = 'Submit')
comment = StringField()
completed = SubmitField(label = 'Submit', description = None)
def set_values(self, task, user):
self.eord.description = task.course.Name
self.task.description = task.Name
self.assign_date.description = task.Created
if (user.Id == task.Developer):
if(task.EndDevelop):
self.accept_date.description = task.EndDevelop
self.accepted.description = ""
else:
self.accepted.description = 'Submit'
self.comment.description = 'Comment'
if (user.Id == task.Reviewer):
if(task.EndReview):
self.accept_date.description = task.EndReview
self.accepted.description = ""
else:
self.accepted.description = 'Submit'
self.comment.description = 'Comment'
if(task.EndReview and task.EndDevelop):
self.completed.description = 'Complete'
I'm trying to practice some django basics by implementing my own sortable table in Django and I've run into a couple of snags.
Here's the code that I'm working with in my view:
def __table_view_helper__(request):
if not request.session.__contains__('filters'):
filters = {'filterA':'',
'filterB':'',
'filterC':''}
request.session['filters'] = filters
if not request.session.__contains__('headers'):
headers = {'sortA':'asc',
'sortB':'des',
'sortC':'asc',
'sortD':'asc'}
request.session['headers'] = headers
def table_view(request):
__table_view_helper__(request)
records = Record.objects.all()
nums = list(set(records.values_list('fieldA','fieldA')))
nums.sort()
if request.method == 'POST':
filter = FilterForm(nums,request.POST)
if filter.is_valid():
fieldA = filter.cleaned_data['fieldA']
fieldB = filter.cleaned_data['fieldB']
fieldC = filter.cleaned_data['fieldC']
filters = request.session['filters']
filters['fieldA'] = fieldA
filters['fieldB'] = fieldB
filters['fieldC'] = fieldC
request.session['filters'] = filters
else:
filter = FilterForm(nums)
filters = request.session['filters']
filter.fields['fieldA'].initial = filters['fieldA']
filter.fields['fieldB'].initial = filters['fieldB']
filter.fields['fieldC'].initial = filters['fieldC']
if filters['fieldA']:
records = records.filter(fieldA=filters['fieldA'])
if filters['fieldB']:
records = records.filter(fieldB__in=filters['fieldB'])
if filters['fieldC']:
records = records.filter(fieldC=filters['fieldC'])
sort = request.GET.get('sort')
if sort is not None:
headers = request.session['headers']
if headers[sort] == "des":
records = records.order_by(sort).reverse()
headers[sort] = "asc"
else:
records = records.order_by(sort)
headers[sort] = "des"
request.session['headers'] = headers
return render_to_response("secure/table.html",{'user':request.user,'profile':request.user.get_profile(),'records':records,'fform':filter})
I changed a lot of my code to use sessions now. It works fine. Is this a good way to do this you think?
To set the initial values from the view you have to do:
filter.fields['fieldA'].initial = filters['filterA']
To keep user related data persistent through different requests you shouldn't use globals, but sessions!
I need to update a record in the datastore, but instead of updated record I get always a new record.
My model:
class PageModel(db.Model):
title = db.StringProperty()
content = db.TextProperty()
reference = db.SelfReferenceProperty()
user = db.UserProperty(auto_current_user = True)
created = db.DateTimeProperty(auto_now_add = True)
modified = db.DateTimeProperty(auto_now = True)
type = db.StringProperty()
template = db.StringProperty()
position = db.IntegerProperty()
hidden = db.BooleanProperty()
historical = db.BooleanProperty()
My handler:
class EditHandler(webapp.RequestHandler):
def post(self):
if self.request.path[6:] == '':
page_id = 8
else:
page_id = int(self.request.path[6:]) # path: /edit/35
#id = self.request.get('id')
CP = PageModel.get_by_id(int(page_id))
key = CP.key()
title = self.request.get('title')
content = self.request.get('content')
type = self.request.get('type')
hidden = self.request.get('hidden')
#position = self.request.get('type')
reference = PageModel.get_by_id(int(self.request.get('reference')))
template = self.request.get('template')
if ".htm" not in template:
template = "index.htm"
#if title == '' or content == '':
#doRender(self,'create_page.htm',{'error' : 'Please fill in all fields'} )
#return
#edited_page = PageModel(key=CP.key, title=title, content=content, type=type, reference=reference, template=template)
edited_page = PageModel()
edited_page.key = CP.key()
edited_page.title = title
edited_page.content = content
edited_page.type = type
edited_page.reference = reference
edited_page.template = template
edited_page.put()
There should be something wrong with edited_page.key = CP.key(), or what?!
Why are you created a new PageModel everytime? instead edit the one you got by id i.e. CP ? e.g.
edited_page = CP
edited_page.title = title
edited_page.content = content
edited_page.type = type
edited_page.reference = reference
edited_page.template = template
edited_page.put()