Add or Update object from a form GAE python - google-app-engine

I try to create a class which will add data from a form to a db.model, but my problem is that when I will try to add an existing book, I want to update the copies and not to add a new object. I really try to find an answer, I read books for programming in GAE, but I find nothing for my problem.
Thank you in advance and I will really appreciate if someone answer with a sample code and not with just a command ( eg try get_by_id()).
Here is a part of my source
class Book(db.Model):
book_id = db.IntegerProperty()
title = db.StringProperty()
author = db.StringProperty()
copies = db.IntegerProperty()
isbn = db.IntegerProperty()
copyright_date = db.StringProperty()
category = db.StringProperty()
comments = db.StringProperty()
class Add(webapp.RequestHandler): #Form to input data
def get(self):
self.response.out.write("""
<html>
<body>
<form action="/sign" method="post">
<table border="0">
<tr>
<td>ISBN:</td> <td><input type="text" name="isbn"</td>
</tr>
<tr>
<td>Title:</td> <td><input type="text" name="title"</td>
</tr>
<tr>
<td>Author:</td> <td><input type="text" name="author"</td>
</tr>
<tr>
<td>Copies:</td> <td><input type="text" name="copies"</td>
</tr>
<tr>
<td>Copyright Date:</td> <td><input type="text" name="copyright_date"</td>
</tr>
<tr>
<td><div>Category:</td> <td><select>
<option name="category" value="adventure">Adventure</option>
<option name="category" value="comedy">Comedy</option>
<option name="category" value="dramatic">Dramatic</option>
<option name="category" value="mystery">Mystery</option>
<option name="category" value="science_fiction">Science Fiction</option></select></td>
</tr>
<tr><td>Comments:</td></tr></table>
<div><textarea name="comments" rows="5" cols="40" value="Add your comments here"></textarea></div>
<div><input type="submit" value="Add Book">
<input type="reset" value="Reset"></div>
</form>
</body>
</html>""")
class Guestbook(webapp.RequestHandler):
def post(self):
book = Book()
book.isbn = int(self.request.get('isbn'))
book.title = self.request.get('title')
book.author = self.request.get('author')
book.category = self.request.get('category')
book.copies = int(self.request.get('copies'))
book.copyright_date = self.request.get('copyright_date')
book.put()
class Stock(webapp.RequestHandler):
def get(self): #Retrieve all data from DataBase (Here is the StockHouse)
Book = db.GqlQuery("SELECT * FROM Book ")
for book in Book:
print book.book_id, book.isbn, book.title, book.author, book.category, book.copyright_date, book.copies, book.comments

in order to update an existing entry you'll need a different handler that will take the post arguments as well as the id or key of the entity to update. Usually this is in the url, e.g:
POST /updateentity/id
and then in the handler:
def get(self, id):
existing = Model.get_by_id(long(id))
model.field = self.request.get("field")
model.put()

You can use get_or_insert Model method. Also see this question.

Related

Use AngularJS filter of a column with filter option

i have a table with ng-repeat where it has a colunm with filter as follows:
<tbody>
<tr ng-repeat="objin listOfObj |filter: searchObj">
{{ (listAllElmt | filter:{idElt: obj.id})[0].nameElt || ''}}
</tr>
</tbody>
the search part is done with the following instruction:
<label >Elmt label</label>
<input type="text" ng-model="searchObj.id">
Here the user must write in the input the id in order that filter:searchObj return a result, however, it must write the name which is a logic way and not putting the id.
How can i fix this issue?
I used another variable where i created it in html as follows:
<label >Elmt label</label>
<input type="text" ng-model="searchObj.id">
<tbody>
<tr ng-repeat="obj in listOfObj |filter: searchObj">
{{objInterm = obj;
objInterm.id = (listAllElmt | filter:{idElt: obj.id})[0].nameElt || '';
;''}}
{{ objInterm.id}}
</tr>
</tbody>

How to see both masked and unmasked values with AngularUI ui-mask and ng-repeat?

I'm using AngularUI's ui-mask directive to mask phone numbers. We'd like to save both the masked and unmasked value to the database.
I see the docs have the option to use the model-view-value="true" to store the $viewValue as the model giving us the masked value.
However since we'd like both I'd prefer a way to leave this false and access the $viewValue on my own. I see in the demo however this is for a single input. It looks like it is bound to the name of the inputs.
My issue is this is a ng-repeat and so the names vary. I got it working for a single item (you can see in the first table it works).
The last <td> is where I'm trying to show the $viewValue. I'm also trying to pass it in to the setPhoneValue function but it is undefined there also.
Edit: Created a fiddle: https://jsfiddle.net/n35u7ncz/
<form name="phone_form" id="crm_phonegrid" ng-app="crm_phonegrid" ng-controller="phoneCtrl">
<table cellpadding="2">
<tr>
<td><input type="text" name="new_phone" placeholder="Number" ng-model="addNumber.phoneNumber" onfocus="onFocusUpdateMask()" ui-mask="{{mask}}" ui-mask-placeholder ui-mask-placeholder-char="_" /></td>
<td><select name="phoneTypeSelect" ng-model="addNumber.phoneType" ng-options="option.name for option in phoneTypeOptions track by option.Value"></select></td>
<td><input type="button" ng-click="addNumber()" value="Add" /></td>
<td>{{phone_form.new_phone.$viewValue}}</td>
<td>{{addNumber.phoneNumber}}</td>
</tr>
</table>
<table cellpadding="2">
<thead>
<tr>
<th>Phone Link</th><th>Phone Number</th><th>Phone Type</th><th>Primary Flag</th><th>Mask</th><th>View</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="number in numbers">
<td><a style="color:#1160B7;text-underline-position:below" href="" ng-click="openPhoneRecord(number)">{{ number.crm_phonenumber }}</a></td>
<td><input type="text" id="crm_phonegrid_number" name="phone_number_input" model-view-value="true" ng-model="number.crm_phonenumber" ng-change="setPhoneValue(number)" ui-mask="{{number.crm_crm_country_crm_phonenumber.crm_PhoneMask}}" ui-mask-placeholder ui-mask-placeholder-char="_" /></td>
<td><select name="phoneTypeSelect" ng-model="number.crm_phonetypecode" ng-change="setValue(number)" ng-options="option.name for option in phoneTypeOptions track by option.Value"></select></td>
<td><input type="radio" name="primary" id="primaryRadio" ng-checked="number.crm_isprimary" ng-model="number.crm_isprimary" ng-change="setValue(number)" ng-value="number.crm_isprimary" ng-click="setFlag(number)" /></td>
<td>{{number.crm_crm_country_crm_phonenumber.crm_PhoneMask}}</td>
<td>View: {{phone_form.phone_number_input.$viewValue}}</td>
</tr>
</tbody>
</table>
</form>
We got this working. The trick was to use the $index to generate a unique Id for the element and then pass that to the controller.
I've updated the fiddle here: https://jsfiddle.net/n35u7ncz/3/
The general idea is that the input field needs to be tagged and have an ng-change:
<input type="text" id="phone_input_id" name="phone_input_name_{{$index}}" ng-model="number.crm_phonenumber" ng-change="setValue(number, $index)" ui-mask="{{number.crm_crm_country_crm_phonenumber.crm_PhoneMask}}" ui-mask-placeholder ui-mask-placeholder-char="_" />
And then:
$scope.setValue = function(item, index)
{
item.crm_phonenumbermasked = $scope.phone_form["phone_input_name_" + index].$viewValue;
}

Angular JS implement Search

I have a form with multiple fields. How do I implement search using angularjs? For example I have the following page:
<pre>
<input name="title">
<input name="isbn">
<input name="author">
<br/>
<input type="submit">
<!-- list the book information -->
Title Author ISBN
</pre>
I want the bottom list to display based on user input on the search fields from the server. Note:The user can input on mutiple fields. How do I accomplish the search when the user clicks the submit button?
Do I need to write one submit method. If so how can I get the values and do the query depending on the field is left blank or not?
Thanks
If your using angularJS you don't need a submit it will do it asynchronously. Just add an ng-model to your input and filter the results based on that. Something like this:
<input name="title" ng-model="title">
<input name="isbn" ng-model="isbn">
<input name="author" ng-model="author>
<div filter: title | filter:isbn | filter:author >
Title Author ISBN
</div>
Something like this
<form name='fred' novalidate='novalidate' ng-submit='submitFunc()'>
<input name='username' required='required' pattern='a-z'>
<input type='email'>
<input type='submit' ng-disabled='fred.$invalid'>
</form>
In your submitFunc you would do an $http request to get your data and filter it and then store the results in an array and do an ng-repeat in the view below the form to iterate over your results
A basic implementation should look like this:
the code:
app.controller('SampleController', ['$scope', function($scope) {
var search = function() {
yourAjaxMethod({author: $scope.author, isbn: $scope.isbn, title: $scope.title}).then(function(list) {
$scope.list = list;
});
}
$scope.$watch('author', search);
$scope.$watch('isbn', search);
$scope.$watch('title', search);
// the init loading
search();
}])
The markup:
<pre>
<input name="title">
<input name="isbn">
<input name="author">
<br/>
<!-- list the book information -->
<table>
<tr>
<th>Title</th>
<th>Author</th>
<th>ISBN</th>
</tr>
<tr ng-repeat="book in list">
<td ng-bind="book.title"></td>
<td ng-bind="book.author"></td>
<td ng-bind="book.isbn"></td>
</tr>
</table>

AngularJs, accessing dynamic value in model

How can i access the dynamic value generated of "name" property in model. Following is the code:
<tbody ng-repeat="que in backgroundInfo.questions">
<tr>
<td>{{que.id}} {{que.text}}</td>
<td>
<input type="radio" name="{{que.id}}" value="Yes" ng-model="backgroundInfo.questions.answer"/>Yes
<input type="radio" name="queid" value="No" ng-model="backgroundInfo.questions.answer"/>No
<textarea name="comment{{que.id}}" ng-model="backgroundInfo.questions.comment[que.id]" rows="2"
cols="45"/>
</td>
</tr>
</tbody>
You are so close
name="{{comment[que.id]}}"
Is this already solved? Because I think if want to have a name like name="comment[1]", the code should be like:
<textarea name="comment[{{que.id}}]" ...
you can remove the ng-model cause I think, it will now work.
Furthermore, you can access the value of your textarea using
angular.element(<name>)
in your controller.

Retrieve some Array data from a Submit form in cakePHP

I am learning cakePHP 1.26.
I got a HTML Select tag with some options like this:
<form method="post" action="/testing">
<table border="1">
<tr>
<td>
<select name="data[Test][number]">
<option name="editquote" value="[29,1]">One</option>
<option name="editquote" value="[24,2]">Two</option>
</select>
</td>
<tr>
<td>
<input type="submit" value="Send" class="mybutton">
</td>
</tr>
</table>
</form>
I selected option One and submitted the form.
Here is the result from the cakePHP built-in function, Debug()
Array
(
[Test] => Array
(
[number] => [29,1]
)
)
I tried using the following code to get the two numbers from the data (i.e. 29 and 1 in this example) but failed to do it
$myData=$this->data;
$myData['Test']['number']; // [29, 1]
What should I do to get the two numbers separately?
You could try this with PHP explode.
$numbers = explode(',', trim($myData['Test']['number'], '[]'));
$numbers[0]; //29
$numbers[1]; //1

Resources