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!
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'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 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
def registrar(request):
form = RegForm(request.POST or None)
context = {
"form":form
}
if request.method == 'POST':
form = RegForm(request.POST)
if form.is_valid():
b = form.cleaned_data['folioboleto']
n = form.cleaned_data['nombre']
aP = form.cleaned_data['apellidoPaterno']
aM = form.cleaned_data['apellidoMaterno']
fecha = form.cleaned_data['fechaDeNacimiento']
g = form.cleaned_data['genero']
e = form.cleaned_data['email']
tel = form.cleaned_data['telefono']
di = form.cleaned_data['direccion']
c = form.cleaned_data['ciudad']
est = form.cleaned_data['estado']
actP = form.cleaned_data['actividadPrincipal']
cSE = form.cleaned_data['comoSupoEvento']
aF = form.cleaned_data['aceptoFotos']
obj = Cliente.objects.create(Nombre=n,ApellidoPaterno=aP,ApellidoMaterno=aM,FechaDeNacimiento=fecha,Genero=g,Email=e,Telefono=tel,Direccion=di,Ciudad=c,Estado=est,ActividadPrincipal=actP,ComoSupoEvento=cSE,AceptoFotos=aF)
bole = Boleto.objects.get(Folio=b)
if bole.Folio == b:
bole.Estatus = '2'
bole.Cliente_id = obj.id
bole.save(update_fields=['Estatus'])
The object bole save method isn't saving the changes to the database.
I believe your problem is with bole.save(update_fields=['Estatus']), which would only update the field named Estatus, which doesn't seem to be being set. Try just using a bare save instead:
bole.save()
Good luck!
I have some database queries in Django 1.11 which takes about 40 seconds and I want to improve the speed, but my ideas are going out.
I want to create a Chart with 6 graphs with the data without having to wait 40 seconds.
This is The Model:
from django.db import models
class MinutenReserveLastYear(models.Model):
datum = models.DateField()
produkt = models.CharField(max_length=100)
grenz_lp = models.FloatField()
mittl_lp = models.FloatField()
class Meta:
indexes = [
models.Index(fields=['datum', 'produkt']),
]
I make an Ajax request to get the Data:
var neg_last_year = $.ajax({
method: "POST",
url: "/prognose/minutenreserve/data/",
data: { "produkt": "NEG", "zeitraum": "last_year" },
dataType: "json",
context: this
});
The view function to get the Data from database:
from .models import MinutenReserveLastYear
def minutenreserve_data(request):
if request.method == "POST":
produkt_group = request.POST.get("produkt")
zeitraum = request.POST.get("zeitraum")
datum_list = {}
grenz_lp = {}
mittl_lp = {}
if zeitraum == "last_year":
if produkt_group == "NEG":
produkt_list = ["NEG_00_04", "NEG_04_08", "NEG_08_12", "NEG_12_16", "NEG_16_20", "NEG_20_24"]
dataset = MinutenReserveLastYear.objects
last_year = datetime.date.fromtimestamp(datetime.datetime.timestamp(
datetime.datetime.now() - datetime.timedelta(weeks=52))).year
days = (datetime.date(last_year, 12, 31) - datetime.date(last_year, 1, 1)).days
for j in range(6):
grenz_lp[j] = []
mittl_lp[j] = []
datum_list[j] = []
produkt = produkt_list[j]
for i in range(days+1):
datum = datetime.date(last_year, 1, 1) + datetime.timedelta(days=i)
datum_list[j] += [datum.strftime('%d.%m.%Y')]
grenz_lp[j] += [dataset.filter(datum=datum, produkt=produkt).values_list()[0][3]]
mittl_lp[j] += [dataset.filter(datum=datum, produkt=produkt).values_list()[0][2]]
data = {'datum_list': datum_list, 'grenz_lp': grenz_lp, 'mittl_lp': mittl_lp}
return JsonResponse(data)
And when the Ajax request is done:
$.when(neg_last_year).done(function(data) {
var datum_list = data.datum_list[0];
var grenzlp_00_04 = data.grenz_lp[0];
var grenzlp_04_08 = data.grenz_lp[1];
var grenzlp_08_12 = data.grenz_lp[2];
var grenzlp_12_16 = data.grenz_lp[3];
var grenzlp_16_20 = data.grenz_lp[4];
var grenzlp_20_24 = data.grenz_lp[5];
var neg_last_year = document.getElementById('NEG_LAST_YEAR').getContext('2d');
var chart_neg_last_year = new Chart(neg_last_year, {
type: 'line',
data: {
labels: datum_list,
datasets: [datasets]
},
options: options
})
});
I suspect that the problem is somewhere in the many database queries through the loops. How can I speed up the code?
How can I reduce the number of database queries?
Edit:
I tried to improve the speed by using
dataset = MinutenReserveLastYear.objects.prefetch_related("grenz_lp", "mittl_lp")
but that did not help.
The main problem is that you are running queries in the loop. What you can do is to get all objects in one query and then filter the objects using if-else in the loops.
Solution:
dataset = MinutenReserveLastYear.objects.all()
for j in range(6):
grenz_lp[j] = []
mittl_lp[j] = []
datum_list[j] = []
produkt = produkt_list[j]
for i in range(days+1):
datum = datetime.date(last_year, 1, 1) + datetime.timedelta(days=i)
for data in dataset:
if data.datum == datum and data.produkt == produkt:
grenz_lp[j].append(data.grenz_lp)
mittl_lp[j].append(data.mittl_lp)