Update random row in DB using Flask-Sqlalchemy - sqlite

I've been trying to update a randomly selected row in my Sqlite database using Flask and the Flask-Sqlalchemy. I have just a few rows in the database with columns called "word", "yes", and "no", where word is a string and yes and no are integers. There are two buttons on the "vote" view, yes and no. When a button is pressed, the appropriate code executes, should increment the yes or no column, and the view is updated with a new random word from the Word table.
#app.route("/vote", methods=["GET", "POST"])
def vote():
#Get random row from database
query = db.session.query(Word)
rowCount = int(query.count())
row = query.offset(int(rowCount*random.random())).first()
#POST
# If "yes" button is pressed, increment yes column in database
if request.method == "POST":
if request.form.get("yes"):
row.yes += 1
db.session.commit()
return render_template("vote.html", row=row)
# otherwise increment no column
elif request.form.get("no"):
row.no += 1
db.session.commit()
return redirect(url_for("vote"))
#GET
# on get request, render vote.html
return render_template("vote.html", row=row)
This code is working, but the yes and no columns are only updated when the view comes back around to the random word the next time. If I close the browser right after clicking a button, the database is not incremented. I think this has something to do with db.session.commit(), or something about the session. It seems like:
row.yes += 1
is saved in the session object, but only committed when that database row is queried the next time. This code DID work when I replaced the query at the top of the method with:
row = Word.query.get(4)
which returns the row with id of 4. With this query, the yes or no column are updated immediately.
Any thoughts?
Thanks

Thanks all. I figured out the problem. The database incrementing was actually working fine, but I wasn't incrementing the correct rows. The problem was that I generated a random row from the database on each call of the vote() method, which meant that I got a random value for the GET request, and a different random value for the POST request, and ended up incrementing that different random value in the POST request.
I separated the logic out into two methods for the "/vote" route, getWord() and vote(), and created a randRow() method for the row generation. I needed to store the random row that gets generated when getWord() is called, so I used session variables so I could access the random row from the vote() method. It's a bit verbose, but seems to work.
Anyone have a better idea about how to achieve this?
#app.route('/vote', methods=["GET"])
def getWord():
wordObj = randRow()
session['word'] = wordObj.word
session['yesVotes'] = wordObj.yes
session['noVotes'] = wordObj.no
return render_template("vote.html", word=session['word'], yesVotes=session['yesVotes'], noVotes=session['noVotes'])
#app.route('/vote', methods=["POST"])
def vote():
# store session 'word' in word variable
# look up word in database and store object in wordObj
word = session['word']
wordObj = Word.query.filter_by(word=word).first()
# check button press on vote view, increment yes or no column
# depending on which button was pressed
if request.form.get("yes"):
wordObj.yes = wordObj.yes + 1
elif request.form.get("no"):
wordObj.no = wordObj.no + 1
db.session.commit()
return redirect(url_for("getWord"))
###### HELPERS ######
# returns a random row from the database
def randRow():
rowId = Word.query.order_by(func.random()).first().id
row = Word.query.get(rowId)
return row

I think you need to add the update into the session before the commit, using code like this:
[...]
row.yes += 1
db.session.add(row)
db.session.commit()
[...]
That's the pattern that I use for a basic update in Flask-SQLAlchemy.

Related

Updating fields from changes in scroll levels

I have a page that has 3 levels. Levels 0 & 1 are from the same record. Level 2 is from a second record.
When a change is made to level 1, I would like to apply that change to the same field in Level 2's record.
Basically, this deals with EFF_STATUS in peoplesoft. If an effective row gets added to the record, and the EFF_STATUS is changed to Active or Inactive, I'd like to update the EFF_STATUS in my second record to match.
Here is the code I'm trying to execute and it is giving me an error of.. "Invalid row number 2 for class Rowset method GetRow. (2,263) K_OFFNSV_REC_EX.EFF_STATUS.SaveEdit PCPC:267 Statement:8 "
If %Component = Component.K_OFFNSV_CMP Then
Local Rowset &LEVEL0, &Level1, &Level2;
Local Row &L1Row, &L2Row;
Local number &I, &J;
&LEVEL0 = GetLevel0();
&Level1 = &LEVEL0(1).GetRowset(Scroll.K_OFFNSV_REC);
&I = CurrentRowNumber();
&L1Row = &Level1(&I);
If &L1Row.IsNew Then
&L1Row.K_OFFNSV_REC.LASTUPDDTTM.Value = %Date;
&L1Row.K_OFFNSV_REC.OPRID.Value = %UserId;
End-If;
&Level2 = &L1Row.GetRowset(Scroll.K_OFFNSV_REC_EX);
For &J = 1 To &Level2.ActiveRowCount
&L2Row = &Level2(&J);
&L2Row.K_OFFNSV_REC_EX.EFFDT.Value = %Date;
&L2Row.K_OFFNSV_REC_EX.EFF_STATUS.Value = &L1Row.K_OFFNSV_REC.EFF_STATUS.Value;
End-For;
End-If;
A suggestion, change/set values on SavePreChange. SaveEdit should be use for validations only.
With that being said:
Your currentrownumber returns the current row, so probably it is returning the row #2 on the level #2.
You need CurrentRowNumber(1) to get the #1 level.
Also, why are you setting the EFFDT yourself on the save? Look at other peoplesoft pages, you will see it is populated on the add by PS itself.

Database row len() can be print, but this value can’t be shown in tkinter entry

I can”t understood, why I can print the value of rows, but not populate this to a tkinter entry.
My code:
cursor.execute(‘SELECT * FROM contacts;’)
print(‘row in table contacts:’,len(cursor.fetchall())) # prints 104
self.no_count.set(len(cursor.fetchall())) # populate 0
Any hint?
You should store the fetched data inside a variable and then access it through the variable. This is because a cursor is like a python generator, and once you use cursor.fetchall() the results will no longer contain the result again. So go for something like:
cursor.execute('SELECT * FROM contacts;')
data = cursor.fetchall() # Store in variable
print(f'row in table contacts: {len(data)}') # Used f strings instead of comma(can be ignored)
self.no_count.set(len(data))
Or you could also go for the inefficient way of repeating your query each time, like:
cursor.execute(‘SELECT * FROM contacts;’)
print(f‘row in table contacts: {len(cursor.fetchall())}')
cursor.execute(‘SELECT * FROM contacts;’) # Repeat the query
self.no_count.set(len(cursor.fetchall())) # Fetch again

javascript for Array how many and min

I am trying to create a form in Acrobat. I want it to do some calculations. I got almost all of them done aside from 2.
I have an array of cells DF1 to DF78 so I need a calculation script that will give me the minimum value in that array not counting the blank ones.
In the same array of cells DF1 to DF78 I need a calculation script to find how many fields in that array have value and bring me up the number.
I already tried using the min option on the acrobat DC and selecting the fields. Ii want to look at DF1 to DF78. However, it always shows 0 because it's counting the empty fields as well.
I tried looking online, but all the scripts that they show are very confusing. I can't find where to put the array in there.
I wish I had a script to put it in here... sorry.
I have fields DF1 to DF78 so a total of 78 fields, and I need to find the minimum value in that array not including the fields that are blank.
Another script for the same fields DF1 to DF78 needs to count how many of the fields actually have data ex: DF1, DF2, DF3 had data on it and the rest are empty so it should display the number 3 because 3 of the 78 fields have data in them.
I hope somebody can help me with this.
This should work... Add it to the calculate action of a new hidden field you want the numbers to show up. Fix the names on the last two lines first.
valueArray = [];
for (var i = 1; i <= 78 ; i++) {
//Get the fieldvalue by assembling the name with the prefix and the number increment
var fieldVal = this.getField("DF"+i).value;
//Acrobat field values are never null. The value of a blank field is an empty string
if (fieldVal != "") {
//Add non-empty field values to an Array.
valueArray.push(fieldValue);
}
}
// Get the minimum value in the array.
var minValue = Math.min.apply(null, valueArray);
// Get the number of non-blank fields.
var nonBlankFields = valueArray.length;
this.getField("RESULT FOR YOUR 1st QUESTION FIELD NAME HERE").value = minValue;
this.getField("RESULT FOR YOUR 2nd QUESTION FIELD NAME HERE").value = nonBlankFields;

How to create add and subtract buttons to track inventory

I've been searching for similar solutions out there but am coming up short so far. Here is what I want to accomplish:
I need to come up with a basic solution to sync inventory quantities at the end of each day. We take physical counts of inventory sold throughout the day but need something to log these changes and share between users. I would like to utilize two buttons (click one to subtract amount of items sold at the end of the day and click one button to add newly received inventory).
This is how my sheet is set up:
Col A: Product Tag
Col B: Product sku
Col C: Amount Sold Today
Col D: Total Inventory Quantity
Col E: Add New Inventory
Column D will be pre-populated with initial inventory counts. At the end of each day, I would like to go down my product list and fill in the amount of each item sold that day in Column C. Once Column C is fully populated, I would like to click the "subtract" button and have Column C subtracted from Column D.
On the other side, once we receive new stock of an item I would like to enter these counts into Column E. Once this column is fully populated, I would like to click the "Add" button and have Column E added to Column D. Ideally once the add or subtract function has been completed, columns C or E will be cleared and ready for the next days entry.
I already have designed my buttons, I just need help coming up with the scripts to accomplish this.
You can use Google Apps Script for this.
If you are unfamiliar, in your particular spreadsheet, go to Tools → Script Editor and then select the Blank Project option.
Then you can write functions like this to achieve what you want!
function subtractSold() {
var sheet = SpreadsheetApp.getActiveSheet();
var c1 = sheet.getRange("C2");
var c2 = sheet.getRange("D2");
while (!c1.isBlank() && !c2.isBlank()){
c2.setValue(c2.getValue() - c1.getValue());
c1.clear();
c1 = c1.offset(1, 0);
c2 = c2.offset(1, 0);
}
}
Basically what the function does is:
Get a reference to the active spreadsheet
Get references to the cells C2 and D2, for the first row of data.
Use a while loop to repeated go through the rows. Terminate when either cell is empty.
In the loop, we get the appropriate values, subtract and set the value back into the cell. Then we clear the cell in column C. We then move both cell references down by one row (the offset method returns a reference to the original cell, but offset by row, column).
Then assign the script to the button image by entering the name of the function (subtractSold in this case) in the "Assign script" option for the button.
I have made an example sheet here (go to File → Make a Copy to try the scripts and see the code): https://docs.google.com/spreadsheets/d/1qIJdTvG0d7ttWAUEov23HY5aLhq5wgv9Tdzk531yhfU/edit?usp=sharing
A bit faster
If you try the sheet above you can see it processes one row at a time, which might get pretty slow when you have a lot of rows. It is probably faster to process the entire column in bulk, but it may be a bit more complicated to understand:
function subtractSoldBulk() {
var sheet = SpreadsheetApp.getActiveSheet();
var maxRows = sheet.getMaxRows();
var soldRange = sheet.getRange(2, 3, maxRows); // row, column, number of rows
var totalRange = sheet.getRange(2, 4, maxRows);
var soldValues = soldRange.getValues();
var totalValues = totalRange.getValues();
for (var row in soldValues) {
var soldCellData = soldValues[row][0];
var totalCellData = totalValues[row][0];
if (soldCellData != "" && totalCellData != "") {
totalValues[row][0] = totalCellData - soldCellData;
soldValues[row][0] = "";
}
}
soldRange.setValues(soldValues);
totalRange.setValues(totalValues);
}
The difference here is that instead of getting one cell, we get one range of cells. The getValues() method then gives us a 2D array of the data in that range. We do the calculations on the two arrays, update the data in the arrays, and then set the values of the ranges based on the array data.
You can find documentation for the methods used above from Google's documentation: https://developers.google.com/apps-script/reference/spreadsheet/sheet

Cant get ExtededDataGrid in Flex to filter with ComboBox on multiple columns

LATEST UPDATE: Issue answered here. Some one else at stackoverflow had a similar issue and it was resolved. Solution provided for convenience. This is the line of code I was missing:
comboHeaderColumn.useLabelFunctionForFilterCompare = true;
that line is followed by these:
comboHeaderColumn.filterComboBoxBuildFromGrid = true;
comboHeaderColumn.labelFunction = formatState;
where formatState is a local method that formats the data for the combobox.
UPDATE: I've now got the combobox's loading with the correct data, but when I select a value nothing happens. The combo boxes load only data that is in the column, and when you select a value in the combobox, it's supposed to filter the rows on that value. It doesn't.
Thanks for looking. I'm having trouble getting multiple filters to work in Flex in Flash Builder 4 using the ExtendedDataGrid and ComboBox's. Here is an image of part of the grid:
The User Name and City filter properly if you type text into the box's above the column header and the Request Date lets you select date ranges if you click on the Custom bar, but the Request Reason and State ComboBoxes do not list anything. I've created them using comboHeaderColumn.filterComboBoxBuildFromGrid = true; but all it does is put "[object Object]" as the only other selection under All.
I've used this article but it will only allow you to use a single filter for the entire grid.
My finished grid will have about 20 columns and from 20,000 to 450,000 rows of data so the filters are really important and I'll need more than one.
The code is very straight forward and loops through all the returned data and if the column is identified as a filter column it does this:
comboHeaderColumn.filterComboBoxDataProvider = codeValuePairs;
comboHeaderColumn.filterComboBoxLabelField = "Value";
comboHeaderColumn.filterControl = "ComboBox";
comboHeaderColumn.filterOperation = FilterExpression.FILTER_OPERATION_TYPE_EQUALS;
comboHeaderColumn.headerText = ac.Header;
comboHeaderColumn.dataField = ac.Name;
if( ac.Header == "State" || ac.Header == "Request Reason" )
{
comboHeaderColumn.filterComboBoxBuildFromGrid = true;
}
ProfileDataColumns.push(comboHeaderColumn);
This creates 2 entries in the combo box: All and [object Object]
What am I missing??? Anyway, after half a day searching I decided to reach out.
Any suggestions or direction to an article would be very much appreciated.
Thanks.

Resources