I have inherit product.product to get available products so:
'qty_stock': fields.related('rented_product_id', 'qty_available', type='float', relation='product.product', string="En stosck", readonly=True),
'qty_1': fields.related('rented_product_id', 'incoming_qty', type='integer', relation='product.product', string="Résrver", readonly=True),
'qty_2': fields.related('rented_product_id', 'outgoing_qty', type='integer', relation='product.product', string="Sortant", readonly=True),
'qty_state': fields.selection([('libre','Libre'),('louee','Louée'),('reserver','Réservée')], 'Status', readonly=True
I want to display in field 'qty_state':
if 'qty_1' > 0 and 'qty_2 < 0 : display 'reserver'
if 'qty_1' > 0 and 'qty_2 = 0 : display 'louee'
if 'qty_1' = 0 and 'qty_2 = 0 : display 'libre'
help please
You must use a fields.function instead of a fields.selection, like this :
def _get_qty_state(self, cr, uid, ids, field_name, args, context=None):
if context is None:
context = {}
res = {}
for product in self.browse(cr, uid, ids, context=context):
# Put 'libre' as default value
res[product.id] = 'libre'
if product.qty_1 > 0 and product.qty_2 < 0:
res[product.id] = 'reserver'
elif product.qty_1 > 0 and product.qty_2 == 0:
res[product.id] = 'louee'
return res
And in '_columns':
'qty_state': fields.function(
_get_qty_state,
method=True,
type='selection',
selection=[('libre','Libre'),('louee','Louée'),('reserver','Réservée')],
string='Status',
readonly=True,
store=False,
),
OR
You can keep the fields.selection and feel it by overriding the 'default_get' method.
Related
In this code:
t = {
num = '',
}
t[0].num = '0'
t[1].num = '1'
t[2].num = '2'
Is there a way for me to delete t[0], then shift all of the table's values down, so that afterword it looks like this:
t[0].num = '1'
t[1].num = '2'
Example with imaginary functions:
t = {
num = '',
}
t[0].num = '0'
t[1].num = '1'
t[2].num = '2'
for i=0,tableLength(t) do
print(t[i])
end
--Output: 012
remove(t[0])
for i=0,tableLength(t) do
print(t[i])
end
--Output: 12
t = {
num = '',
}
t[0].num = '0'
t[1].num = '1'
t[2].num = '2'
This code will cause errors for indexing t[0], a nil value.
t only has one field and that is t.num
You need to do something like this:
t = {}
for i = 0, 2 do
t[i] = {num = tostring(i)}
end
if you want to create the desired demo table.
As there are many useful functions in Lua that assume 1-based indexing you I'd recommend starting at index 1.
local t = {1,2,3,4,5}
Option 1:
table.remove(t, 1)
Option 2:
t = {table.unpack(t, 2, #t)}
Option 3:
t = table.move(t, 2, #t, 1, t)
t[#t] = nil
Option 4:
for i = 1, #t-1 do
t[i] = t[i+1]
end
t[#t] = nil
There are more options. I won't list them all. Some do it in place, some result in new table objects.
As stated in this answer, by creating a new table using the result of table.unpack:
t = {table.unpack(t, 1, #t)}
I am working on this exercise in pil4.
Exercise 15.5:
The approach of avoiding constructors when saving tables with cycles is too radical. It is
possible to save the table in a more pleasant format using constructors for the simple case, and to use
assignments later only to fix sharing and loops. Reimplement the function save (Figure 15.3, “Saving
tables with cycles”) using this approach. Add to it all the goodies that you have implemented in the previous
exercises (indentation, record syntax, and list syntax).
I have tried this with the code below, but it seems not to work on the nested table with a string key.
local function basicSerialize(o)
-- number or string
return string.format("%q",o)
end
local function save(name,value,saved,indentation,isArray)
indentation = indentation or 0
saved = saved or {}
local t = type(value)
local space = string.rep(" ",indentation + 2)
local space2 = string.rep(" ",indentation + 4)
if not isArray then io.write(name," = ") end
if t == "number" or t == "string" or t == "boolean" or t == "nil" then
io.write(basicSerialize(value),"\n")
elseif t == "table" then
if saved[value] then
io.write(saved[value],"\n")
else
if #value > 0 then
if indentation > 0 then io.write(space) end
io.write("{\n")
end
local indexes = {}
for i = 1,#value do
if type(value[i]) ~= "table" then
io.write(space2)
io.write(basicSerialize(value[i]))
else
local fname = string.format("%s[%s]",name,i)
save(fname,value[i],saved,indentation + 2,true)
end
io.write(",\n")
indexes[i] = true
end
if #value > 0 then
if indentation > 0 then io.write(space) end
io.write("}\n")
else
io.write("{}\n")
end
saved[value] = name
for k,v in pairs(value) do
if not indexes[k] then
k = basicSerialize(k)
local fname = string.format("%s[%s]",name,k)
save(fname,v,saved,indentation + 2)
io.write("\n")
end
end
end
else
error("cannot save a " .. t)
end
end
local a = { 1,2,3, {"one","Two"} ,5, {4,b = 4,5,6} ,a = "ddd"}
local b = { k = a[4]}
local t = {}
save("a",a,t)
save("b",b,t)
print()
And I got the wrong ouput.
a = {
1,
2,
3,
{
"one",
"Two",
}
,
5,
{
4,
5,
6,
}
a[6]["b"] = 4
,
}
a["a"] = "ddd"
b = {}
b["k"] = a[4]
How could I make the text ' a[6]["b"] = 4 ' jump out of the table constructor?
I have a table in a table structure like this:
[1] = {
[1] = {
category = "WORD",
cursor = <filtered>,
ptype = "STATEMENT",
ttype = "SERVICE",
value = "service",
<metatable> = <filtered>
},
[2] = {
category = "VARIABLE",
cursor = <filtered>,
ptype = "STATEMENT",
ttype = "IDENTIFIER",
value = "TestService",
<metatable> = <filtered>
},
[3] = {
ttype = "BRACE_BLOCK",
value = {
[1] = { ...
...
[2] = {
[1] = {
category = "WORD",
cursor = <filtered>,
ptype = "STATEMENT",
ttype = "SERVICE",
value = "service",
<metatable> = <filtered>
},
[2] = {
category = "VARIABLE",
cursor = <filtered>,
ptype = "STATEMENT",
ttype = "IDENTIFIER",
value = "HelloWorld",
<metatable> = <filtered>
},
I programmed a simply loop which looks for the first table with the ttype, filtered that information out and would like to assign the rest of the tokens until the next Service starts to corresponding service. My idea looks like that:
local found_service = 0
if found_service == 0 then
for k1, v1 in pairs (parse_tree) do
for i=1,#v1 do
if v1[i].ttype == "SERVICE" then
--Store wanted data in an object
found_service = 1
end
if (found_service == 1 and v1[i].ttype ~= "SERVICE") then
-- ->assign the rest to last found service
end
if found_service == 1 and v1[i].ttype == "SERVICE" then
-- ->Found the next service -->starting over
found_service = 0
end
end
end
end
The problem is that I stuck at index i, and v1[i] is a "SERVICE", so he enters directly the last if-clause, too. How do I end one loop-iteration (after the first if-clause). Or ist there a much better way to do this?
Thanks in advise.
Theo
I'm not sure if I understand your general idea, but here is the answer of how to skip loop body on first "SERVICE" capture event.
local found_service = 0
if found_service == 0 then
for k1, v1 in pairs (parse_tree) do
for i=1,#v1 do
if (found_service == 0 and v1[i].ttype == "SERVICE") then
--Store wanted data in an object
found_service = 1
else
if (found_service == 1 and v1[i].ttype ~= "SERVICE") then
-- ->assign the rest to last found service
end
if found_service == 1 and v1[i].ttype == "SERVICE" then
-- ->Found the next service -->starting over
found_service = 0
end
end
end
end
end
But I'm still don't get it what should be done on current record not "SERVICE" and found_service == 0. By the way, in my answer after found_service become 0 in third if, the first if could be true again.
If your idea is to build some kind of vector like:
SERVICE_1 (other ttype tables until next SERVICE)
SERVICE_2 (other ttype tables until next SERVICE)
...
In that case code could be:
local found_service = 0
if found_service == 0 then
for k1, v1 in pairs (parse_tree) do
for i=1,#v1 do
if (found_service == 0 and v1[i].ttype == "SERVICE") then
--Store wanted data in an object
found_service = 1
current_service = v1[i]
else
if (found_service == 1 and v1[i].ttype ~= "SERVICE") then
-- ->assign the rest to last found service
end
if found_service == 1 and v1[i].ttype == "SERVICE" then
-- ->Found the next service -->starting over
current_service = v1[i]
end
end
end
end
end
I want to sort a hash by the following conditions:
An object that has hash[:sold] as false comes before an object that has hash[:sold] as true.
If both objects have hash[:sold] as false enter code here, then compute two variables distance_a, distance_b for the two hashes. I want object a to come before object b if distance_a < distance_b.
Code:
curr_hash.sort do |a,b|
status_a = a[1][:sold]
status_b = b[1][:sold]
if status_a == false && status_b == false
a_first_loc = [a[1][:loc_id], current_loc_id].min
a_second_loc = [a[1][:loc_id], current_loc_id].max
b_first_loc = [b[1][:loc_id], current_loc_id].min
b_second_loc = [b[1][:loc_id], current_loc_id].max
distance_a = LocationDistance.find_by(city_id: current_city.id, loc_a: a_first_loc, loc_b: a_second_loc).distance
distance_b = LocationDistance.find_by(city_id: current_city.id, loc_a: b_first_loc, loc_b: b_second_loc).distance
distance_a <=> distance_b
else return status_a == false ? 1 : 0
end
end
Borrowing #Maxim's distance function and using sort_by means the distance is only calculated once for each item.
Extracting distance as a function also has the advantage that it can be tested independently
def distance(hash)
LocationDistance.find_by(loc_a: hash[1][:loc_id], current_loc_id].min,
loc_b: [hash[1][:loc_id], current_loc_id].max,
city_id: current_city.id).distance
end
a.sort_by(|x| [true ? 1 : 0, distance(x)])
This has to do the job nicely for you:
def distance(hash)
LocationDistance.find_by(loc_a: hash[1][:loc_id], current_loc_id].min,
loc_b: [hash[1][:loc_id], current_loc_id].max,
city_id: current_city.id).distance
end
a.sort{|a,b| a[:sold] == b[:sold] ? ((distance(a) < distance(b)) ? -1 : 1) : (a[:sold] ? 1 : -1)}
I've been using tags in my projects. I was browsing the custom tags on grails.org to find some new tags for my library.
http://www.grails.org/Contribute+a+Tag
I was wondering if people in the StackOverflow community have a favorite custom tag that they would like to share.
I find the DecimalFormat class (and Grails's formatNumber tag by extension) a bit opaque for certain use cases, and I still haven't found a reasonable way to do some pretty basic formatting with it without some ugly pre-processing to generate an appropriate format string. I threw together a simple number formatting tag several months ago which essentially constructs a format string and does some minimal processing to the number itself.
It's not as generic or elegant as I'd like (it's all we needed at the time - it's super basic, but it still keeps some ugly processing out of GSPs), but it should be easy to read, and it's obvious where it could be trivially improved (i.e. making the scaling iterative instead of naive if-elseif slop, allowing the user to pass in custom scaling markers, allowing for a custom number validator as a parameter, etc.).
// Formats a number to 3 significant digits, appending appropriate scale marker
// (k, m, b, t, etc.). Defining var allows you to use a string representation
// of the formatted number anywhere you need it within the tag body, and
// provides the scale as well (in case highlighting or other special formatting
// based upon scale is desired).
def formatNumberScaled = {attrs, body -> // number, prefix, suffix, invalid, var
Double number
String numberString
String scale
try {
number = attrs.'number'.toDouble()
} catch (Exception e) {
number = Double.NaN
}
if (number.isNaN() || number.isInfinite()) {
numberString = scale = attrs.'invalid' ?: "N/A"
} else {
Boolean negative = number < 0d
number = negative ? -number : number
if (number < 1000d) {
scale = ''
} else if (number < 1000000d) {
scale = 'k'
number /= 1000d
} else if (number < 1000000000d) {
scale = 'm'
number /= 1000000d
} else if (number < 1000000000000d) {
scale = 'b'
number /= 1000000000d
} else if (number < 1000000000000000d) {
scale = 't'
number /= 1000000000000d
}
String format
if (number < 10d) {
format = '#.00'
} else if (number < 100d) {
format = '##.0'
} else {
format = '###'
}
format = "'${attrs.'prefix' ?: ''}'${format}'${scale} ${attrs.'suffix' ?: ''}'"
numberString = g.formatNumber('number': negative ? -number : number, 'format': format)
}
// Now, either print the number or output the tag body with
// the appropriate variables set
if (attrs.'var') {
out << body((attrs.'var'): numberString, 'scale': scale)
} else {
out << numberString
}
}
I have a "fmt:relDate" tag that gives you Twitter-like relative dates "3 days ago", "less than 30 seconds ago", etc., with the real time as a tooltip.
The current implementation is basically a gigantic chain of if/then statements with the boundaries that I like. A binary-search based algorithm would be better (in the sense of "more efficient"), and the current implementation has my personal preferences encoded into it, so I'm reluctant to share the tag.
I have a remote paginate tab, that helps me paginate results via ajax. Its an improvement over the default tab and takes in custom arguments.
Here's the code:
class CustomRemotePaginateTagLib {
static namespace = 'myTagLib'
/** * Creates next/previous links to support pagination for the current controller * * <g:paginate total="$ { Account.count() } " /> */
def remotePaginate = {attrs ->
def writer = out
if (attrs.total == null) throwTagError("Tag [remotePaginate] is missing required attribute [total]")
if (attrs.update == null) throwTagError("Tag [remotePaginate] is missing required attribute [update]")
def locale = RequestContextUtils.getLocale(request)
def total = attrs.total.toInteger()
def update = attrs.update
def action = (attrs.action ? attrs.action : (params.action ? params.action : "list"))
def controller = (attrs.controller ? attrs.controller : params.controller)
def offset = params.offset?.toInteger()
def max = params.max?.toInteger()
def maxsteps = (attrs.maxsteps ? attrs.maxsteps.toInteger() : 10)
if (!offset) offset = (attrs.offset ? attrs.offset.toInteger() : 0)
if (!max) max = (attrs.max ? attrs.max.toInteger() : 10)
def linkParams = [offset: offset - max, max: max]
if (params.sort) linkParams.sort = params.sort
if (params.order) linkParams.order = params.order
if (attrs.params) linkParams.putAll(attrs.params)
linkParams['action'] = action
linkParams['controller'] = controller
def linkTagAttrs = [url: "#"]
if (attrs.controller) { linkTagAttrs.controller = attrs.controller }
if (attrs.id != null) { linkTagAttrs.id = attrs.id }
// determine paging variables
def steps = maxsteps > 0
int currentstep = (offset / max) + 1
int firststep = 1
int laststep = Math.round(Math.ceil(total / max))
// display previous link when not on firststep
if (currentstep > firststep) {
linkTagAttrs.class = 'prevLink'
def prevOffset = linkParams.offset
def params = attrs.params ?: []
params.'offset' = prevOffset
linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params)
writer << link(linkTagAttrs.clone()) {
(attrs.prev ? attrs.prev : g.message(code: 'default.paginate.prev', default: 'Previous'))
}
}
// display steps when steps are enabled and laststep is not firststep
if (steps && laststep > firststep) {
linkTagAttrs.class = 'step'
// determine begin and endstep paging variables
int beginstep = currentstep - Math.round(maxsteps / 2) + (maxsteps % 2)
int endstep = currentstep + Math.round(maxsteps / 2) - 1
if (beginstep < firststep) {
beginstep = firststep
endstep = maxsteps
}
if (endstep > laststep) {
beginstep = laststep - maxsteps + 1
if (beginstep < firststep) {
beginstep = firststep
}
endstep = laststep
}
// display firststep link when beginstep is not firststep
if (beginstep > firststep) {
linkParams.offset = 0
def params = attrs.params ?: []
params['offset'] = linkParams.offset
linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params)
writer << link(linkTagAttrs.clone()) { firststep.toString() }
writer << '<span class="step">..</span>'
}
// display paginate steps
(beginstep..endstep).each {i ->
if (currentstep == i) {
writer << "<span class=\"currentStep\">${i}</span>"
} else {
linkParams.offset = (i - 1) * max
def params = attrs.params ?: []
params['offset'] = linkParams.offset
linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params)
writer << link(linkTagAttrs.clone()) { i.toString() }
}
}
// display laststep link when endstep is not laststep
if (endstep < laststep) {
writer << '<span class="step">..</span>'
linkParams.offset = (laststep - 1) * max
def params = attrs.params ?: []
params['offset'] = linkParams.offset
linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params)
writer << link(linkTagAttrs.clone()) { laststep.toString() }
}
}
// display next link when not on laststep
if (currentstep < laststep) {
linkTagAttrs.class = 'nextLink'
linkParams.offset = offset + max
def params = attrs.params ?: []
params['offset'] = linkParams.offset
linkTagAttrs.onclick = g.remoteFunction(update: update, action: linkParams.action, controller: linkParams.controller, params: params)
writer << link(linkTagAttrs.clone()) {
(attrs.next ? attrs.next : g.message(code: 'default.paginate.next', default: 'Next'))
}
}
}