node_save() failing when creating node programatically - drupal-7

I am trying to create a node programatically like so,
$newNode = (object) NULL;
$newNode->type = 'job';
$newNode->title = $data['JobTitle'];
$newNode->uid = $user->uid;
$newNode->created = strtotime("now");
$newNode->changed = strtotime("now");
$newNode->status = 1;
$newNode->comment = 0;
$newNode->promote = 0;
$newNode->moderate = 0;
$newNode->sticky = 0;
$newNode->tid = 0;
$newNode->summary['und'][0]['value'] = $data['JobSummary'];
$newNode->body['und'][0]['value'] = $data['JobDescription'];
$newNode->field_employment_type['und'] = strtolower($data['JobType']);
$newNode->field_job_reference['und'][0]['value'] = $data['JobReference'];
$newNode->field_salary['und'][0]['value'] = "";
$newNode->field_salary_from['und'][0]['value'] = $data['SalaryFrom'];
$newNode->field_salary_to['und'][0]['value'] = $data['SalaryTo'];
$newNode->field_salary_override['und'][0]['value'] = $data['Salary'];
$newNode->field_application_email['und'][0]['value'] = $data['ApplicationEmail'];
$newNode->field_job_category['und'][2] = 2;
$newNode->field_job_category['und'][4] = 4;
//die(print_r($newNode));
// save node
node_save($newNode);
Here I have potentially 4 taxonomies to pick from (their id indicated in brackets) Creative (2), Technical (3), Marketing (4), Client Services (6).
On node_save I am getting the following error,
500 Internal Server Error : An error occurred (23000):
SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'tid'
cannot be null
How do I overcome this this I would have thought setting the field_job_category to the id of the taxonomy would be enough?

Delete $newNode->tid = 0; and prepare object :
$newNode = new stdClass();
$newNode->type = 'job';
node_object_prepare($node);
$newNode->title = $data['JobTitle'];
$newNode->uid = $user->uid;
$newNode->created = strtotime("now");
$newNode->changed = strtotime("now");
$newNode->status = 1;
$newNode->comment = 0;
$newNode->promote = 0;
$newNode->moderate = 0;
$newNode->sticky = 0;
$newNode->summary['und'][0]['value'] = $data['JobSummary'];
$newNode->body['und'][0]['value'] = $data['JobDescription'];
$newNode->field_employment_type['und'] = strtolower($data['JobType']);
$newNode->field_job_reference['und'][0]['value'] = $data['JobReference'];
$newNode->field_salary['und'][0]['value'] = "";
$newNode->field_salary_from['und'][0]['value'] = $data['SalaryFrom'];
$newNode->field_salary_to['und'][0]['value'] = $data['SalaryTo'];
$newNode->field_salary_override['und'][0]['value'] = $data['Salary'];
$newNode->field_application_email['und'][0]['value'] = $data['ApplicationEmail'];
$newNode->field_job_category['und'][]['tid'] = 2; // right syntax
$newNode->field_job_category['und'][]['tid']= 4; // right syntax
//die(print_r($newNode));
// save node
node_save($newNode);

If you want to have only one term id associated:
$newNode->field_category[LANGUAGE_NONE][0]['tid'] = <actual term id>
If you have multiple term ids to associate:
Iterate over your array of term ids, and increment index. Something like:
$newNode->field_category[LANGUAGE_NONE][0]['tid'] = <actual term id>
$newNode->field_category[LANGUAGE_NONE][1]['tid'] = <actual term id>
$newNode->field_category[LANGUAGE_NONE][2]['tid'] = <actual term id>
Note the index values above.
And, I usually set following attributes set for a new node:

Related

Input file for a Fortran program, containing "&PROBIN"

I have the following code (it's an input file for a Fortran code from some link):
&PROBIN
model_file = "model_file"
drdxfac = 5
max_levs = 1
n_cellx = 106
n_celly = 106
n_cellz = 106
max_grid_size = 32
anelastic_cutoff = 1.e3
base_cutoff_density = 1.e3
sponge_center_density = 3.d6
sponge_start_factor = 3.333d0
sponge_kappa = 10.0d0
max_mg_bottom_nlevels = 3
mg_bottom_solver = 4
hg_bottom_solver = 4
spherical_in = 1
dm_in = 3
do_sponge = .true.
prob_hi_x = 2.e10
prob_hi_y = 2.e10
prob_hi_z = 2.e10
max_step = 100
init_iter = 1
stop_time = 30000.
plot_int = 10
plot_deltat = 10.0d0
chk_int = 100
cflfac = 0.7d0
init_shrink = 0.1d0
max_dt_growth = 1.1d0
use_soundspeed_firstdt = T
use_divu_firstdt = T
bcx_lo = 12
bcx_hi = 12
bcy_lo = 12
bcy_hi = 12
bcz_lo = 12
bcz_hi = 12
verbose = 1
mg_verbose = 1
cg_verbose = 1
do_initial_projection = T
init_divu_iter = 3
drive_initial_convection = T
stop_initial_convection = 20
do_burning = F
velpert_amplitude = 1.d6
velpert_radius = 2.d7
velpert_scale = 1.d7
velpert_steep = 1.d5
enthalpy_pred_type = 1
evolve_base_state = F
dpdt_factor = 0.0d0
use_tfromp = T
single_prec_plotfiles = T
use_eos_coulomb = T
plot_trac = F
/
My question is: what is &PROBIN? where can I find more information on it?
Such an input file would be typically read using namelist formatting.
Details can be found using this term. One example of use is given in this answer to a question about input.
In summary, the &PROBIN says that following (up to a terminating /) is a set of pairs for variables and values. These correspond to the namelist probin. In the source file we would find a namelist statement:
namelist /probin/ list, of, variables
with corresponding input statement
read(unit, NML=probin)
where the unit unit is connected to that input file.
Of course, it's entirely possible that the file is an input file processed in the "usual" way. In this case &PROBIN has no special significance. The &PROBIN is necessary to support namelist formatting, but not unique to it.

Finding the nearest neighbour between coordinates in two separate lists

I have two galaxy catalogs and want to pair up each galaxy in catalog 1 with the closest galaxy from catalog 2. I have already written a script that does this for only one input catalog (not very efficiently but it works for what I need), which is posted below.
When I updated the code I've written for two catalogs, catalog 2 doesn't iterate further down the list than j = 3. It's getting stuck there for some reason.
Code for one catalog input:
def nnpairs(x):
"""
find nearest neighbour of each galaxy and pairs them up
"""
nearest_neighbours = []
shortest_distances = []
displacements = []
m_diffs = []
galaxies1 = []
galaxies2 = []
for i in range (len(x)):
nearest_neighbour = []
shortest_distance = []
displacement = []
m_diff = []
galaxy1 = []
galaxy2 = []
for j in range(len(x)):
if j == 0 and i!=j:
shortest_distance = separation(x[i], x[j])
if (separation(x[i], x[j])) < (shortest_distance) and i!=j:
shortest_distance = separation(x[i], x[j])
nearest_neighbour = CATAID[i], CATAID[j]
displacement = displacement_along_loa(x[i],x[j])
dRA1, dRA2, dDEC1, dDEC2 = displacement
galaxy1 = (CATAID[i], shortest_distance, dRA1, dDEC1)
galaxy2 = (CATAID[j], shortest_distance, dRA2, dDEC2)
m_diff = mass_diff(x[i],x[j])
nearest_neighbours.append(nearest_neighbour)
shortest_distances.append(shortest_distance)
displacements.append(displacement)
m_diffs.append(m_diff)
galaxies1.append(galaxy1)
galaxies2.append(galaxy2)
data = list(zip(nearest_neighbours))
return data
Code for two catalogs:
def nnpairs_2(catalog1, catalog2):
"""
find nearest neighbour of each galaxy and pairs them up
"""
nearest_neighbours = []
shortest_distances = []
displacements = []
m_diffs = []
for i in range (len(catalog1)):
nearest_neighbour = []
shortest_distance = []
displacement = []
m_diff = []
j = 0
while j < len(catalog2):
if j == 0 and i!=0:
shortest_distance = separation(catalog1[i], catalog2[j])
if (separation(catalog1[i], catalog2[j])) < (shortest_distance) and i!=j:
shortest_distance = separation(catalog1[i], catalog2[j])
nearest_neighbour = (i,j)
displacement = displacement_along_loa(catalog1[i],catalog2[j])
m_diff = mass_diff(catalog1[i],catalog2[j])
j = j+1
nearest_neighbours.append(nearest_neighbour)
shortest_distances.append(shortest_distance)
displacements.append(displacement)
m_diffs.append(m_diff)
data = list(zip(nearest_neighbours))
return data
The idea is that the code find the separation between every galaxy and creates the nearest neighbour pair for the smallest separation, but for the second code it only matches up all galaxies in catalog 1 with the third entry in catalog 2.
I understand there is quite a lot of information here but any help would be much appreciated, thanks in advance!

Biopython for Loop IndexError

I get "IndexError: list is out of range" when I input this code. Also, the retmax is set at 614 because that's the total number of results when I make the request. Is there a way to make the retmode equal to the number of results using a variable that changes depending on the search results?
#!/usr/bin/env python
from Bio import Entrez
Entrez.email = "something#gmail.com"
handle1 = Entrez.esearch(db = "nucleotide", term = "dengue full genome", retmax = 614)
record = Entrez.read(handle1)
IdNums = [int(i) for i in record['IdList']]
while i >= 0 and i <= len(IdNums):
handle2 = Entrez.esearch(db = "nucleotide", id = IdNums[i], type = "gb", retmode = "text")
record = Entrez.read(handle2)
print(record)
i += 1
Rather than using a while loop, you can use a for loop...
from Bio import Entrez
Entrez.email = 'youremailaddress'
handle1 = Entrez.esearch(db = 'nucleotide', term = 'dengue full genome', retmax = 614)
record = Entrez.read(handle1)
IdNums = [int(i) for i in record['IdList']]
for i in IdNums:
print(i)
handle2 = Entrez.esearch(db = 'nucleotide', term = 'dengue full genome', id = i, rettype = 'gb', retmode = 'text')
record = Entrez.read(handle2)
print(record)
I ran it on my computer and it seems to work. The for loop solved the out of bounds, and adding the term to handle2 solved the calling error.

Save not working using mssql

I'm trying the following code but not working in mssql and not producing any error message in CakePHP, using version 2.6:
$product_id = $this->request->data['products']['product_id'];
$this->request->data['ProductTbl']['id'] = strtotime(date('Y-m-d H:i:s'));
$this->request->data['ProductTbl']['retailerId'] = $this->request->data['products']['retailer_id'];
$this->request->data['ProductTbl']['productCode'] = $product_id;
$this->request->data['ProductTbl']['productType'] = $this->request->data['products']['product_type'];
//$this->request->data['ProductTbl']['mmGroupId'] = $this->request->data['products']['merchandise_id'];
$this->request->data['ProductTbl']['name'] = $this->request->data['products']['product_name'];
$this->request->data['ProductTbl']['description'] = $this->request->data['products']['product_desp'];
$this->request->data['ProductTbl']['isLayawayable'] = $this->request->data['products']['product_layway'];
$this->request->data['ProductTbl']['isTaxExemptible'] = $this->request->data['products']['product_tax'];
$this->request->data['ProductTbl']['productURL'] = $this->request->data['products']['product_url'];
$this->request->data['ProductTbl']['status'] = 2;
$this->request->data['ProductTbl']['lastModified'] = date('Y-m-d H:i:s');
$this->request->data['ProductTbl']['isDeleted'] = 0;
if(!empty($this->request->data['pjosn'])){
$pjson_arr = $this->request->data['pjosn'];
$this->request->data['ProductTbl']['propertiesJson'] = json_encode($pjson_arr);
}
$this->ProductTbl->save($this->request->data['ProductTbl'],false);
$last_id = $this->ProductTbl->getLastInsertID();
Thanks.
why are u using second argument 'false' in save function ???
i thinks possible issue should be second argument. I am using cakePHP 2.7.5 and according to me your save query will be ::
$res = $this->ProductTbl->save($this->request->data);
if($res){
// do whatever you wants
}
Or the second issue may be in following code :
if(!empty($this->request->data['pjosn'])){
$pjson_arr = $this->request->data['pjosn'];
$this->request->data['ProductTbl'['propertiesJson']=json_encode($pjson_arr);
}
in json_encode your data will be in joson format ( like : {"key1":value1,"key2":value2,"key3":value3,"key4":value4,"key5":value5} ) which will not be accepted by cakePHP insert query.

Is it better to change the db schema?

I'm building a web app with django. I use postgresql for the db. The app code is getting really messy(my begginer skills being a big factor) and slow, even when I run the app locally.
This is an excerpt of my models.py file:
REPEATS_CHOICES = (
(NEVER, 'Never'),
(DAILY, 'Daily'),
(WEEKLY, 'Weekly'),
(MONTHLY, 'Monthly'),
...some more...
)
class Transaction(models.Model):
name = models.CharField(max_length=30)
type = models.IntegerField(max_length=1, choices=TYPE_CHOICES) # 0 = 'Income' , 1 = 'Expense'
amount = models.DecimalField(max_digits=12, decimal_places=2)
date = models.DateField(default=date.today)
frequency = models.IntegerField(max_length=2, choices=REPEATS_CHOICES)
ends = models.DateField(blank=True, null=True)
active = models.BooleanField(default=True)
category = models.ForeignKey(Category, related_name='transactions', blank=True, null=True)
account = models.ForeignKey(Account, related_name='transactions')
The problem is with date, frequency and ends. With this info I can know all the dates in which transactions occurs and use it to fill a cashflow table. Doing things this way involves creating a lot of structures(dictionaries, lists and tuples) and iterating them a lot. Maybe there is a very simple way of solving this with the actual schema, but I couldn't realize how.
I think that the app would be easier to code if, at the creation of a transaction, I could save all the dates in the db. I don't know if it's possible or if it's a good idea.
I'm reading a book about google app engine and the datastore's multivalued properties. What do you think about this for solving my problem?.
Edit: I didn't know about the PickleField. I'm now reading about it, maybe I could use it to store all the transaction's datetime objects.
Edit2: This is an excerpt of my cashflow2 view(sorry for the horrible code):
def cashflow2(request, account_name="Initial"):
if account_name == "Initial":
uri = "/cashflow/new_account"
return HttpResponseRedirect(uri)
month_info = {}
cat_info = {}
m_y_list = [] # [(month,year),]
trans = []
min, max = [] , []
account = Account.objects.get(name=account_name, user=request.user)
categories = account.categories.all()
for year in range(2006,2017):
for month in range(1,13):
month_info[(month, year)] = [0, 0, 0]
for cat in categories:
cat_info[(cat, month, year)] = 0
previous_months = 1 # previous months from actual
next_months = 5
dates_list = month_year_list(previous_month, next_months) # Returns [(month,year)] from the requested range
m_y_list = [(date.month, date.year) for date in month_year_list(1,5)]
min, max = dates_list[0], dates_list[-1]
INCOME = 0
EXPENSE = 1
ONHAND = 2
transacs_in_dates = []
txs = account.transactions.order_by('date')
for tx in txs:
monthyear = ()
monthyear = (tx.date.month, tx.date.year)
if tx.frequency == 0:
if tx.type == 0:
month_info[monthyear][INCOME] += tx.amount
if tx.category:
cat_info[(tx.category, monthyear[0], monthyear[1])] += tx.amount
else:
month_info[monthyear][EXPENSE] += tx.amount
if tx.category:
cat_info[(tx.category, monthyear[0], monthyear[1])] += tx.amount
if monthyear in lista_m_a:
if tx not in transacs_in_dates:
transacs_in_dates.append(tx)
elif tx.frequency == 4: # frequency = 'Monthly'
months_dif = relativedelta.relativedelta(tx.ends, tx.date).months
if tx.ends.day < tx.date.day:
months_dif += 1
years_dif = relativedelta.relativedelta(tx.ends, tx.date).years
dif = months_dif + (years_dif*12)
dates_range = dif + 1
for i in range(dates_range):
dt = tx.date+relativedelta.relativedelta(months=+i)
if (dt.month, dt.year) in m_y_list:
if tx not in transacs_in_dates:
transacs_in_dates.append(tx)
if tx.type == 0:
month_info[(fch.month,fch.year)][INCOME] += tx.amount
if tx.category:
cat_info[(tx.category, fch.month, fch.year)] += tx.amount
else:
month_info[(fch.month,fch.year)][EXPENSE] += tx.amount
if tx.category:
cat_info[(tx.category, fch.month, fch.year)] += tx.amount
import operator
thelist = []
thelist = sorted((my + tuple(v) for my, v in month_info.iteritems()),
key = operator.itemgetter(1, 0))
thelistlist = []
for atuple in thelist:
thelistlist.append(list(atuple))
for i in range(len(thelistlist)):
if i != 0:
thelistlist[i][4] = thelistlist[i-1][2] - thelistlist[i-1][3] + thelistlist[i-1][4]
list = []
for el in thelistlist:
if (el[0],el[1]) in lista_m_a:
list.append(el)
transactions = account.transactions.all()
cats_in_dates_income = []
cats_in_dates_expense = []
for t in transacs_in_dates:
if t.category and t.type == 0:
if t.category not in cats_in_dates_income:
cats_in_dates_income.append(t.category)
elif t.category and t.type == 1:
if t.category not in cats_in_dates_expense:
cats_in_dates_expense.append(t.category)
cat_infos = []
for k, v in cat_info.items():
cat_infos.append((k[0], k[1], k[2], v))
Depends on how relevant App Engine is here. P.S. If you'd like to store pickled objects as well as JSON objects in the Google Datastore, check out these two code snippets:
http://kovshenin.com/archives/app-engine-json-objects-google-datastore/
http://kovshenin.com/archives/app-engine-python-objects-in-the-google-datastore/
Also note that the Google Datastore is a non-relational database, so you might have other trouble refactoring your code to switch to that.
Cheers and good luck!

Resources