I want to use newman for running a collection but i should use iterationData for this because i have an Object where i have keys and values with js files. And by using these js files i get a function that returns JSON payload which i want to send. I noticed that i need to use file path fo using iterationData option. How i can use iteration for this?
const tests = { DARS36: TITLE5, DARS37: TITLE19, DARS39: TITLE20, DARS40: TITLE7, DARS41: TITLE8, DARS42: TITLE18,
DARS47: TITLE3, DARS224: TITLE21, DARS225: TITLE1, DARS226: TITLE6, DARS70: TITLE1, DARS227: TITLE1, DARS69: TITLE1,
DARS236: TITLE1, DARS88: TITLE1, DARS90: TITLE5, DARS91: TITLE1, DARS92: TITLE1, DARS94: TITLE1, DARS95: TITLE2,
DARS98: TITLE1, DARS410: TITLE1, DARS411: Company, DARS412: TITLE21, DARS413: TITLE19, DARS415: TITLE8, DARS416: TITLE7,
DARS417: Trust, DARS44: TransferPlate, DARS48: TITLE1, DARS49: TransferPlate, DARS50: TITLE1, DARS99: TransferPlate,
DARS71: TITLE1, DARS72: TITLE1, DARS73: TransferPlate, DARS81: TITLE1, DARS97: TITLE1, DARS93: TITLE1, DARS79: TransferPlate, DARS80: TransferPlate, DARS89: TITLE1, DARS233: TransferPlate}
newman.run({
collection: Collection.getSample(tests[key].sample),
iterationData: tests[key].sample
}).on('request', (error, data) => {
if (error) {
console.log(error);
return;
}
console.log(data.response.stream.toString());
});
Related
I have a shop in WooCommerce and I try to disable the check-out page for some countries (France and Greece). These countries should be forced to log-in as b2b customers in order to continue their check-out.
I use this code, but unfortunately the action applies to all countries and not just the two I need (France and Greece):
add_action( 'template_redirect', 'checkout_redirect_non_logged_to_login_access');
function checkout_redirect_non_logged_to_login_access() {
global $woocommerce;
$msg_states = array( 'BE', 'BG', 'DE', 'DK', 'EE', 'FI', 'IT', 'HR', 'LI', 'LT', 'MT', 'IE', 'IS', 'NL', 'NO', 'PT', 'RO', 'CH', 'SK', 'SI', 'CZ', 'HU', 'GB', 'AT' );
// Here the conditions (woocommerce checkout page and unlogged user)
if( is_checkout() && !is_user_logged_in() && !in_array( WC()->customer->get_billing_country, $msg_states ) ) {
// Redirecting to your custom login area
wp_redirect( get_permalink( get_option('woocommerce_myaccount_page_id') ) );
// always use exit after wp_redirect() function.
exit;
}
}
Is there any possibility to differentiate the kind of log-in, from a normal user to b2b client?
Can I translate this sql into querydsl form?
select count(ppe),v.name
from personal_progress_entity ppe left join user_detail_entity ude
on ppe.student_entity_id=ude.user_id
right join (values ('aaa'),('bbb'),('ccc'),('ddd')) as v(name)
on ude.people_category=v.name
group by v.name;
The PostgreSQL VALUES function is not supported by querydsl. However, you can get the same result using a UNION.
CREATE TABLE personal_progress_entity(student_entity_id INTEGER);
INSERT INTO personal_progress_entity VALUES (1);
CREATE TABLE user_detail_entity(user_id INTEGER, people_category VARCHAR);
INSERT INTO user_detail_entity VALUES (1, 'aaa');
INSERT INTO user_detail_entity VALUES (1, 'bbb');
SELECT COUNT(personal_progress_entity.student_entity_id),
name.value_alias
FROM personal_progress_entity personal_progress_entity
LEFT JOIN user_detail_entity user_detail_entity ON personal_progress_entity.student_entity_id = user_detail_entity.user_id
RIGHT JOIN ((SELECT 'aaa' AS value_alias)
UNION
(SELECT 'bbb' AS value_alias)
UNION
(SELECT 'ccc' AS value_alias)
UNION
(SELECT 'ddd' AS value_alias)) AS name
ON name.value_alias = user_detail_entity.people_category
GROUP BY name.value_alias;
Gives:
1 "aaa"
1 "bbb"
0 "ddd"
0 "ccc"
Here's my querydsl-sql implementation. I've added the private static <T> Union<T> union(PathBuilder<T> pathBuilder, T... values) method to reduce boilerplate.
public List<Tuple> stackoverflowAnswer() {
PathBuilder<String> valueAlias = new PathBuilder<>(String.class, "value_alias");
PathBuilder<String> name = new PathBuilder<>(String.class, "name");
return query().select(personalProgressEntity.studentEntityId.count(), name.get(valueAlias))
.from(personalProgressEntity)
.leftJoin(userDetailEntity).on(personalProgressEntity.studentEntityId.eq(userDetailEntity.userId))
.rightJoin(union(valueAlias, "aaa", "bbb", "ccc", "ddd"), name).on(name.get(valueAlias).eq(userDetailEntity.peopleCategory))
.groupBy(name.get(valueAlias))
.fetch();
}
private static <T> Union<T> union(PathBuilder<T> pathBuilder, T... values) {
return SQLExpressions.union(
Stream.of(values)
.map(p -> SQLExpressions.select(Expressions.constantAs(p, pathBuilder)))
.collect(Collectors.toList()));
}
I am currently writing a data.sql SQL-script for my spring-boot application. I want it to fill in default values for various tables (e.g. on initial start-up).
I have set spring.jpa.hibernate.ddl-auto=update in the application.properties to keep the contents of the database, but the data.sql is executed each time the service boots up.
Now I am looking for a way to insert rows if they do not exist without updating them if they do exist.
I am using H2Dialect (and a H2-database).
Initially I wanted to use some sort of "IF NOT EXISTS"-statement along with SELECT COUNT(*) FROM table, but it seems H2 does not support this directly.
So the next best thing to do is to use MERGE INTO, but this does not seem to work as expected and I do not understand why so.
This is what my script looks like, but sadly it does not work as expected:
MERGE INTO Languages AS T USING (SELECT * FROM Languages) AS S ON (T.ID = S.ID)
WHEN NOT MATCHED THEN
INSERT (LANGUAGE_CODE, NAME, I18N_NAME, LOCALIZE_UI, CAN_CHOOSE) VALUES
('en', 'English', 'USA', TRUE, TRUE),
('de', 'German', 'Deutschland', FALSE, FALSE);
I'd like to add these values either if they are absent (which is probably easier) or if there are no values present in the Languages-table (which is what I would prefer).
This is the (shortened) exception I receive when I start my spring-boot application:
Caused by: org.h2.jdbc.JdbcSQLException: Syntax Fehler in SQL Befehl "MERGE INTO LANGUAGES AS[*] T USING (SELECT * FROM LANGUAGES) AS S ON (T.ID = S.ID) WHEN NOT MATCHED THEN INSERT (LANGUAGE_CODE, NAME, I18N_NAME, LOCALIZE_UI, CAN_CHOOSE) VALUES ('en', 'English', 'USA', TRUE, TRUE), ('de', 'German', 'Deutschland', FALSE, FALSE) "; erwartet "., (, KEY, VALUES, (, WITH, SELECT, FROM"
Syntax error in SQL statement "MERGE INTO LANGUAGES AS[*] T USING (SELECT * FROM LANGUAGES) AS S ON (T.ID = S.ID) WHEN NOT MATCHED THEN INSERT (LANGUAGE_CODE, NAME, I18N_NAME, LOCALIZE_UI, CAN_CHOOSE) VALUES ('en', 'English', 'USA', TRUE, TRUE), ('de', 'German', 'Deutschland', FALSE, FALSE) "; expected "., (, KEY, VALUES, (, WITH, SELECT, FROM"; SQL statement:
MERGE INTO Languages AS T USING (SELECT * FROM Languages) AS S ON (T.ID = S.ID) WHEN NOT MATCHED THEN INSERT (LANGUAGE_CODE, NAME, I18N_NAME, LOCALIZE_UI, CAN_CHOOSE) VALUES ('en', 'English', 'USA', TRUE, TRUE), ('de', 'German', 'Deutschland', FALSE, FALSE) [42001-196]
at org.h2.message.DbException.getJdbcSQLException(DbException.java:345) ~[h2-1.4.196.jar:1.4.196]
at org.h2.message.DbException.getSyntaxError(DbException.java:205) ~[h2-1.4.196.jar:1.4.196]
at org.h2.command.Parser.getSyntaxError(Parser.java:541) ~[h2-1.4.196.jar:1.4.196]
at org.h2.command.Parser.parseSelectSimple(Parser.java:2073) ~[h2-1.4.196.jar:1.4.196]
at org.h2.command.Parser.parseSelectSub(Parser.java:1940) ~[h2-1.4.196.jar:1.4.196]
at org.h2.command.Parser.parseSelectUnion(Parser.java:1755) ~[h2-1.4.196.jar:1.4.196]
at org.h2.command.Parser.parseSelect(Parser.java:1743) ~[h2-1.4.196.jar:1.4.196]
at org.h2.command.Parser.parseMerge(Parser.java:1053) ~[h2-1.4.196.jar:1.4.196]
at org.h2.command.Parser.parsePrepared(Parser.java:423) ~[h2-1.4.196.jar:1.4.196]
at org.h2.command.Parser.parse(Parser.java:321) ~[h2-1.4.196.jar:1.4.196]
at org.h2.command.Parser.parse(Parser.java:297) ~[h2-1.4.196.jar:1.4.196]
at org.h2.command.Parser.prepareCommand(Parser.java:258) ~[h2-1.4.196.jar:1.4.196]
at org.h2.engine.Session.prepareLocal(Session.java:578) ~[h2-1.4.196.jar:1.4.196]
at org.h2.engine.Session.prepareCommand(Session.java:519) ~[h2-1.4.196.jar:1.4.196]
at org.h2.jdbc.JdbcConnection.prepareCommand(JdbcConnection.java:1204) ~[h2-1.4.196.jar:1.4.196]
at org.h2.jdbc.JdbcStatement.executeInternal(JdbcStatement.java:176) ~[h2-1.4.196.jar:1.4.196]
at org.h2.jdbc.JdbcStatement.execute(JdbcStatement.java:164) ~[h2-1.4.196.jar:1.4.196]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_121]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_121]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_121]
at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_121]
at org.apache.tomcat.jdbc.pool.StatementFacade$StatementProxy.invoke(StatementFacade.java:114) ~[tomcat-jdbc-8.5.27.jar:na]
at com.sun.proxy.$Proxy97.execute(Unknown Source) ~[na:na]
at org.springframework.jdbc.datasource.init.ScriptUtils.executeSqlScript(ScriptUtils.java:470) ~[spring-jdbc-4.3.14.RELEASE.jar:4.3.14.RELEASE]
... 78 common frames omitted
The exception stacktrace is huge, but the "caused by"-parts basically say, that beans could not be created due to the deepest exception, the "caused by"-clause of which I posted above. If anyone needs more details, please let me know.
Edit: or is there a best practice for initializing data (such as default user or default anything)? Preferably one I can reuse if a database reset is performed.
Edit (25.04.2018): I figured something might have gone wrong with my Language-class, because the SQL for schema creation looks a little odd. Here it is:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
import com.particles.authservice.jwtservice.JSONHelper;
import com.particles.authservice.languageservice.converters.LanguageDeserializer;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* This class describes the entity for a language.
*/
#JsonDeserialize(using = LanguageDeserializer.class)
#Data
#NoArgsConstructor
#Entity(name = "Languages")
public class Language {
private static Language defaultLanguage;
#Id
#Column(nullable = false, unique = true)
private String languageCode;
#Column(nullable = false)
private String name;
#Column(nullable = false)
private String i18nName;
#Column(nullable = false)
private boolean localizeUi = false;
#Column(nullable = false)
private boolean canChoose = false;
/**
* This method sets the class attribute {#link Language#defaultLanguage}.
*
* #param defaultLanguage
* ({#link Language}) default language
*/
public static void setDefaultLanguage(final Language defaultLanguage) {
Language.defaultLanguage = defaultLanguage;
}
/**
* #return ({#link Language}) default language
*/
public static Language getDefaultLanguage() {
return Language.defaultLanguage;
}
}
Now I am looking for a way to insert rows if they do not exist without updating them if they do exist.
You could use INSERT INTO ... SELECT ...:
INSERT INTO Languages(LANGUAGE_CODE, NAME, I18N_NAME, LOCALIZE_UI, CAN_CHOOSE)
SELECT LANGUAGE_CODE, NAME, I18N_NAME, LOCALIZE_UI, CAN_CHOOSE
FROM (SELECT 'en' AS Language_code, 'English' AS name,
'USA' AS I18N_NAME, TRUE AS LOCALIZE_UI, TRUE AS CAN_CHOOSE
UNION ALL
SELECT 'de', 'German', 'Deutschland', FALSE, FALSE
) sub
WHERE NOT EXISTS (SELECT 1
FROM Languages l
WHERE sub.Language_code = l.Language_code);
Assuming that LANGUAGE_CODE is UNIQUE, otherwise you need to explicitly specify ID column.
Alternatively using MINUS/EXCEPT:
INSERT INTO Languages(LANGUAGE_CODE, NAME, I18N_NAME, LOCALIZE_UI, CAN_CHOOSE)
SELECT LANGUAGE_CODE, NAME, I18N_NAME, LOCALIZE_UI, CAN_CHOOSE
FROM (SELECT 'en' AS Language_code, 'English' AS name,
'USA' AS I18N_NAME, TRUE AS LOCALIZE_UI, TRUE AS CAN_CHOOSE
UNION ALL
SELECT 'de', 'German', 'Deutschland', FALSE, FALSE
) sub
EXCEPT
SELECT LANGUAGE_CODE, NAME, I18N_NAME, LOCALIZE_UI, CAN_CHOOSE
FROM Languages;
EDIT:
Is there for instance a way to avoid UNION?
I am not sure if H2 supports VALUES clause:
SELECT LANGUAGE_CODE, NAME, I18N_NAME, LOCALIZE_UI, CAN_CHOOSE
FROM (SELECT 'en' AS Language_code, 'English' AS name,
'USA' AS I18N_NAME, TRUE AS LOCALIZE_UI, TRUE AS CAN_CHOOSE
UNION ALL
SELECT 'de', 'German', 'Deutschland', FALSE, FALSE
) sub
<=>
SELECT *
FROM (VALUES('en', 'English', 'USA', TRUE, TRUE),
('de', 'German', 'Deutschland', FALSE, FALSE)
) sub(LANGUAGE_CODE, NAME, I18N_NAME, LOCALIZE_UI, CAN_CHOOSE)
SQLFiddle Demo
Based on your requirements, you should be able to use MERGE statement without using WHEN NOT MATCHED clause.
MERGE:
Updates existing rows, and insert rows that don't exist. If no key
column is specified, the primary key columns are used to find the row.
If more than one row per new row is affected, an exception is thrown.
MERGE INTO Languages
KEY(LANGUAGE_CODE, NAME, I18N_NAME, LOCALIZE_UI, CAN_CHOOSE) VALUES
('en', 'English', 'USA', TRUE, TRUE),
('de', 'German', 'Deutschland', FALSE, FALSE);
Let me know if this still doesn't work for you.
I have inherited from purchase.order.line. I have added a bunch of related fields. They are all related to fields in product.product via product_id in order_line.
What I want to achieve is that when a user selects or changes a product in the purchase order line form, the related fields should get refreshed/updated/populated with values of the selected product.
I have written the onchange method for this but I'm not sure how to invoke it from the inherited view? The product_id field was already there in the (parent) order_line view and it already has an onchange defined. How do I get the system to use my onchange as well?
I don't want to disturb the onchange_productid method that is already there in purchase.order.line. So either before my onchange or after it, it should continue with its standard functioning.
Thanks
EDIT: latest version (getting errors when any of the related many2one or selection fields has a value).
class purchase_order_line_custom(osv.osv):
_name = 'purchase.order.line'
_inherit = 'purchase.order.line'
def onchange_product_id(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id, partner_id, date_order=False, fiscal_position_id=False, date_planned=False, name=False, price_unit=False, context=None):
values = super(purchase_order_line_custom, self).onchange_product_id(cr, uid, ids, pricelist_id, product_id, qty, uom_id, partner_id, date_order, fiscal_position_id, date_planned,name, price_unit, context=context)
if product_id:
product = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
values['value'].update({
'qualified_name':product.qualified_name,
'product_type' : product.product_type or None,
'product_subtype' : product.product_subtype,
'count_id':product.count_id or None
})
return values
_columns={
'product_type': fields.related('product_id','product_type',type='selection', string='Product Type', selection=[('X','X'),('Y','Y'),('Z','Z')]),
'product_subtype': fields.related('product_id','product_subtype',type='char', size=64, string='Sub-Type'),
'qualified_name': fields.related('product_id','qualified_name',type='char', size=64, string='Qualified Name'),
'count_id': fields.related('product_id','count_id',type='many2one',relation='product.mycount',string='Count')
}
purchase_order_line_custom()
you need to overide the onchange function(you can use super() ) for the field 'product_id' and update the result.
for example
def onchange_product(self,cr,uid,ids,product_id,context=None):
values = super(<your_class_name>,self).onchange_product(cr, uid,ids,product_id,context=context)
# values will be a dictionary containing 'value' as a key.
# You need to add all the newly added related fields and other fields to the values['value'].
# if 'aaa' is the newly added field, then values['value'].update({'aaa':<value for aaa>})
# then return values
return values
modify you onchange to the following
def onchange_product_id(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id,
partner_id, date_order=False, fiscal_position_id=False, date_planned=False,
name=False, price_unit=False, context=None):
values = super(purchase_order_line_custom, self).onchange_product_id(cr, uid, ids, pricelist_id, product_id, qty, uom_id, partner_id, date_order, fiscal_position_id, date_planned,name, price_unit, context=context)
if product_id:
product = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
values['value'].update({
'product_type' : product.product_type,
'product_subtype' : product.product_subtype,
'qualified_name' : product.qualified_name,
'count_id' : product.count_id
})
return values
First let's understand why fields.related is used?
fields.related field that points to some data inside another field of the current record.
That means, fields which are related will be filled automatically when you will select the value of other field.
Example:
_columns = {
'product_id': fields.many2one('product.product', 'Product'),
'product_name': fields.related('product_id', 'name', type='char', string='Product Name'),
}
In this example product_name will be filled automatically when you will select the product_id. You don't have to write onchange for that.
You can find many examples for fields.related.
Followup question from openerp override onchange behavior without affecting base
15244031
The following code does get fired but I get server error 500 when any of the related fields has a value.
When I look at the log, it says JSON serialization issue.
TypeError: browse_record(product.mycount, 1) is not JSON serializable
Please suggest a solution.
class purchase_order_line_custom(osv.osv):
_name = 'purchase.order.line'
_inherit = 'purchase.order.line'
def onchange_product_id(self, cr, uid, ids, pricelist_id, product_id, qty, uom_id, partner_id, date_order=False, fiscal_position_id=False, date_planned=False, name=False, price_unit=False, context=None):
values = super(purchase_order_line_custom, self).onchange_product_id(cr, uid, ids, pricelist_id, product_id, qty, uom_id, partner_id, date_order, fiscal_position_id, date_planned,name, price_unit, context=context)
if product_id:
product = self.pool.get('product.product').browse(cr, uid, product_id, context=context)
values['value'].update({
'qualified_name':product.qualified_name,
'product_type' : product.product_type or None,
'product_subtype' : product.product_subtype,
'count_id':product.count_id or None
})
return values
_columns={
'product_type': fields.related('product_id','product_type',type='selection', string='Product Type', selection=[('X','X'),('Y','Y'),('Z','Z')]),
'product_subtype': fields.related('product_id','product_subtype',type='char', size=64, string='Sub-Type'),
'qualified_name': fields.related('product_id','qualified_name',type='char', size=64, string='Qualified Name'),
'count_id': fields.related('product_id','count_id',type='many2one',relation='product.mycount',string='Count')
}
purchase_order_line_custom()
in the line 'count_id':product.count_id or None, I think count_id is a many2one field in product.you are trying to pass object to the count_id field in product.order.line, you need to pass product.count_id.id to get the id.