Django manytomany field holding Users autofilling - python

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", {}))

Related

How to make multiple buttons in Flask process them in separate way?

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'

Python - Create/Read/Update/Delete records in NetSuite

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

In Django 1.6.x how to get data of associated models in views?

After so much googling I did'nt found any relevant solution for getting the associated models data in views.
Things is I am requesting to the server via Ajax and want to fetch the folders and folder creator name(from User model). But it did'nt return me the first_name of the creator. Here's the code
models.py
class UserFolder(models.Model):
creator = models.ForeignKey(User)
name = models.CharField(blank=False, max_length=150)
is_shared = models.BooleanField(default=False)
created_date = models.DateTimeField(auto_now_add=True)
modified_date = models.DateTimeField(auto_now=True)
def __str__(self):
return self.name
views.py
def pagination(obj, request):
max = request.GET.get('max') if 'max' in request.GET else 1
paginator = Paginator(obj, max) # Show 25 contacts per page
page = request.GET.get('page')
try:
return paginator.page(page)
except PageNotAnInteger:
# If page is not an integer, deliver first page.
return paginator.page(1)
except EmptyPage:
return ""
def folder_list(request): #called via ajax
folders = UserFolder.objects.filter(creator=request.user.id).order_by('-modified_date')
folders = pagination(folders, request) #for pagination
folders = json.dumps({}) if not folders else serializers.serialize("json", folders)
return HttpResponse(folders)
I have tried .select_related('creator') too but it did'nt work.
In js I am fetching the data like this :
$.each(data, function(i, v) {
var t = fldrTpl, id = v.pk;
v = v.fields
t = t.replace(/\{(ID)\}/g, id);
t = t.replace(/\{(NAME)\}/g, v.name);
t = t.replace(/\{(C_SIZE)\}/g, (v.current_size == null?0:v.current_size));
t = t.replace(/\{(C_ID)\}/g, v.creator.first_name);
t = t.replace(/\{(C_DATE)\}/g, v.created_date);
$(".my-folder-table").append(t);
});
v.creator.first_name always return undefined.
Any help would be really appreciated.
Thanks
You can always create a dictionary of the data you want to use on your client side:
def folder_list(request): #called via ajax
folders_obj = UserFolder.objects.filter(creator=request.user).select_related('creator').order_by('-modified_date').all()
folders_dict = []
for folder in folders_obj:
d = {}
for k, v in folder.__dict__.items():
d[k] = str(v)
d["creator__first_name"] = folder.creator.first_name
folders_dict.append(d)
folders = pagination(folders_dict, request) #for pagination
folders = json.dumps({}) if not folders else serializers.serialize("json", folders)
return HttpResponse(folders)
And in your js:
t = t.replace(/\{(C_ID)\}/g, v.creator__first_name);

Django POST/GET exercise

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!

App Engine, Python: problem updating datastore record

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()

Categories

Resources