How to pass value from field to a wizard in Odoo 13? - view

Im using two models for two form views.
I have this field below with model 1
name = fields.Many2one('hr.employee', string="USERNAME", required=True)
And I want to pass its value to the wizard when I click a button. Here's the code:
def create_field(self):
return {
'name': self.name,
'view_mode': 'form',
'res_model': 'dieu.chinh',
'view_id': False,
'res_id': wiz.id,
'res_id' : self.id,
'context': {'current_id': self.id},
'target': 'current',
'type': 'ir.actions.act_window',
}
The button in XML file:
button type="object" string="SUBMIT" name= "create_field" class="oe_highlight"/>
After clicking the button, it can open the expected form view with model 2, but still not show the value which was selected in the previous form.
So... How to pass value from field to a wizard in Odoo 13?
Please help!
Thank you!

Try this:
def create_field(self):
form_view = self.env.ref("your_wizard_form_view_external_id")
return{
'name': 'Wizard Name',
'views': [
(form_view.id, 'form'),
],
'res_model': 'dieu.chinh',
'target': 'new',
'type': 'ir.actions.act_window',
'context': {
'default_wizard_field_name': self.name.id, # for passing Many2One field context value in Wizard form view
},
}
'default_wizard_field_name': self.name.name # for passing Char field context value in Wizard form view

Related

In Sanity how to only use validation if boolean is set to true?

In a current document group I'm trying to find a way to only allow validation when the boolean is set to true:
{
title: 'Has the movie been released?',
name: 'released',
type: 'boolean',
initialValue: true
}
fooBar.ts:
export default {
title: 'Foo',
name: 'foo',
group: 'general',
type: 'object',
fields: [
{
title: 'Alpha',
name: 'alphaBool',
type: 'boolean',
description: 'Random text',
initialValue: false
},
{
title: 'Alpha Location',
name: 'alphaLoc',
type: 'string',
hidden: ({ parent }) => parent?.alphaBool !== true,
validation: Rule => Rule.required()
}
]
};
but this current implemented approach throws a required error even though it might be set to false. I've tried to see if I could pass down either parent or document from validation so I could attempt to get the value of alphaBool but they both show undefined:
validation: (rule, parent, document) => console.log(rule, parent, document)
but I'm allowed to see parent and document objects in hidden.
Research
Conditional fields
Field Groups
Conditional validation of string
Boolean
Now you see them, now you don’t. Introducing Conditional Fields.
Validation
Optional validation for hidden fields
In Sanity 2.35.0 how can I run validation only if the boolean value is set to true?
If I correctly understand your question, you can use custom validations for this.
Example:
{
title: 'Alpha',
name: 'alphaBool',
type: 'boolean',
description: 'Random text',
initialValue: false
},
{
title: 'Alpha Location',
name: 'alphaLoc',
type: 'string',
hidden: ({ parent }) => parent?.alphaBool !== true,
validation: (Rule) => Rule.custom((value, { document: { alphaBool } }) => {
return alphaBool && !value ? "Field required" : true
})
}
With Rule.custom, you can access the value of the field and the context object of the document. In this example, if alphaBool is set to true and the current field's value is an empty string, then it will throw a Field required validation message. If alphaBool is false, it will not validate anything.

How to generate a schema for a custom pagination in django rfw with drf-spectacular?

I am struggling to properly generate the schema for my custom pagination in django rest framework.
I am using drf-spectacular for the schema generation. My custom pagination includes a total-pages field which does not come with djangos PageNumberPagination.
The response is correctly serialized and returned and includes the total-pages, but the schema in my swagger docs does not include the field.
Here is my code:
pagination.py
from rest_framework import pagination
from rest_framework.response import Response
class CustomPagination(pagination.PageNumberPagination):
page_size = 10
page_size_query_param = 'page_size'
max_page_size = 100
page_query_param = 'p'
def get_paginated_response(self, data):
return Response({
'page_size': self.page_size,
'total_objects': self.page.paginator.count,
'total_pages': self.page.paginator.num_pages,
'current_page_number': self.page.number,
'next': self.get_next_link(),
'previous': self.get_previous_link(),
'results': data,
})
Here is my view:
views.py
#extend_schema_view(
get=extend_schema(
parameters=[OpenApiParameter("q", OpenApiTypes.STR, OpenApiParameter.QUERY),],
request=TestStandardSearchSerializer,
responses=TestStandardSerializer
)
)
class TestStandardSearchView(ListAPIView):
serializer_class = TestStandardSearchSerializer
queryset = TestStandard.objects.all()
pagination_class = CustomPagination
def get(self, request, *args, **kwargs):
query = self.request.query_params.get('q')
queryset = SearchQuerySet().all().filter(content=query).order_by('acronym')
page = self.paginate_queryset(queryset)
serializer = self.get_serializer(page, many=True)
return self.get_paginated_response(serializer.data)
def get_serializer_class(self):
if self.request.method == 'GET':
return TestStandardSearchSerializer
The response schema from my swagger doc is the following:
PaginatedTestStandardList
{
count integer example: 123
next string($uri) nullable: true example: http://api.example.org/accounts/?p=4
previous string($uri) nullable: true example: http://api.example.org/accounts/?p=2
results [TestStandard{...}]
}
The standard django pagination is correctly ínserted in the schema, but not my custom pagination response.
What I expected/wanted is to have my customized pagination response correctly integrated with the total-pages field on the same level as 'count', 'next' and 'previous'.
What I tried...
I have a working solution with drf_yasg using the PaginatorInspector providing a custom schema. But this is not available in drf-spectacular.
I also used inline_serializer with a custom response in #extend_schema_view such as:
responses={
200: inline_serializer(
name='PaginatedTestStandardSearchResponse',
fields={
'total-pages': serializers.IntegerField(),
'results': TestStandardSearchSerializer()
},
This resulted in a schema where total-pages is nested within results.
I am using:
drf-spectacular 0.21.2
Django 3.2.12
django-rest-swagger 2.2.0
djangorestframework 3.12.4
Any help is appreciated. I just recently started with django rfw and openapi schema generation. Sorry if I had missed something obvious here.
You need to overwrite the method get_paginated_response_schema in your CustomPagination. For the reference about how to compose it, you can see it on file pagination.py inside rest_framework package.
If you want to know how does that works, you could find it inside drf-spectacular package on file openapi.py, method _get_response_for_code. I hope that solve your problem.
I ended up with overwriting get_paginated_response().
This finally resolved my issue. Now the correct pagination parameters are shown in the swagger documentation.
This is my custom paginator:
from rest_framework import pagination
from rest_framework.response import Response
class CustomPagination(pagination.PageNumberPagination):
page_size = 10
page_size_query_param = 'page_size'
max_page_size = 100
page_query_param = 'p'
def get_paginated_response(self, data):
print(data)
print()
return Response({
'count': self.page.paginator.count,
'next': self.get_next_link(),
'previous': self.get_previous_link(),
'page_size': self.page_size,
'total_objects': self.page.paginator.count,
'total_pages': self.page.paginator.num_pages,
'current_page_number': self.page.number,
'results': data,
})
def get_paginated_response_schema(self, schema):
return {
'type': 'object',
'properties': {
'count': {
'type': 'integer',
'example': 123,
},
'next': {
'type': 'string',
'nullable': True,
'format': 'uri',
'example': 'http://api.example.org/accounts/?
{page_query_param}=4'.format(
page_query_param=self.page_query_param)
},
'previous': {
'type': 'string',
'nullable': True,
'format': 'uri',
'example': 'http://api.example.org/accounts/?
{page_query_param}=2'.format(
page_query_param=self.page_query_param)
},
'page_size' : {
'type': 'integer',
'example': 123,
},
'total_pages': {
'type': 'integer',
'example': 123,
},
'current_page_number': {
'type': 'integer',
'example': 123,
},
'results': schema,
},
}

autocomplete search key specify

By default autocomplete in ace editor picks up value for search and value for substituting in the text field. Can we specify a name for search and value for substitution? Any help is appreciated.
eg for completor
var completer = {
getCompletions: function(editor, session, pos, prefix, callback) {
var f = [
{'value': '2', 'name': 'two'},
{'value': '3', 'name': 'three'},
{'value': 'four', 'name': '4'}
];
callback(null, f);
}
};
In this example, the only value is used for the search. I want to show customers a verbose text during popup but when he/she hits select value should be substituted.

Programmatically changing a bound check box in Kendo Grid not holding its new value

I am in need of assistance in an attempt to programmatically check/uncheck a checkbox in a kendo grid.
I have part of my Grids datasource for this relevant field as...
receivereport: {
editable: true,
nullable: false,
required: false,
type: 'boolean',
validation: {
required: false
}
},
And the grids configuration is...
$("#contactGrid").kendoGrid({
dataSource: contactGridDS,
navigatable: true,
dataBound: mapContactTypes,
editable: true,
edit: function (input) {
},
toolbar: ["save", "cancel"],
pageable: false,
columns: [
{ field: 'email', title: 'Email', hidden: false, attributes: { "class": 'contactCell_email' } },
{ field: 'receivereport', title: 'Receive Reports?', hidden: false, attributes: { "class": 'contactCell_receivereport' }, template: '<input type="checkbox" #= receivereport ? "checked=checked" : "" # value="" disabled="disabled" ></input>' }
],
//filterable: true,
sortable: {
mode: 'single',
allowUnsort: false
}
});
For brevity sake, I cut some of the other code out that's not relevant, such as other fields not involved here.
So, I have an email method that has a regex in it that works, and what I want to do is, if on focus, or focus out of the email field, if that email is invalid, make the receive report field false, but it's not registering dirty editing, and I even tried forcing it by appending some CSS rules and classes, which makes it "look" dirty, but when I change the value of the checkbox, on save, it goes back to what it was.
The data is bound to the receivereport data. So I think I read on the forums here that I need to do something like datasource.set("receivereport", false); And maybe a sync? The syncinc fires but it doesn't help and I must be calling the set incorrectly because the console says its firing on an unidentified object.
Here's the real kicker, I know how to access that check box and render it as false, but it flips right back to what it was bound to! It's not holding. Unless I click into that cell and do a click on the check box, it doesn't hold...
...unless I can simulate a fake click event on the target, being the checkbox...
I looked at the example here, Disable/Enable the checkbox in kendo grid based on column Value, but it seems a bit different and not what I need.
Bottom line - if the checkbox is true/checked, and a user goes back into the email field and renders it invalid, I want to automatically uncheck that checkbox, as well as make that checkbox disabled, until the user makes the email valid again. This also implies that a null email, means the checkbox must be false and disabled.
Anyways, any help would be immensely appreciated. Thanks.
This can be done using the "change" event of the dataSource:
dataSource = new kendo.data.DataSource({
change: function(e) {
if (e.action == "itemchange" && e.field == "email") {
//you can check the email value as well:
//var model = e.items[0];
e.items[0].set("receivereport", false)
}
},
Here is the full example:
Grid: change field value depending on another field

ExtJS4 dataView - Select node id

I have an ExtJS 4 dataView and i would like to catch the id of a selected node.
It is the first time that i'm using the dataView, then, there are some troubles.
The store is loaded correctly and i see the datas into the view very well. The problem which i'm having, concern the "classic" actions of update and delete, particularly getting the id of a selected item.
For example into a grid i click, then select a record and through a button's pressing i open a window (or other actions) with a loaded form (by sending in AJAX to the store, the id of the selected row) and i update the datas.
I'm not still able to do it with the ExtJS 4 dataView.
Below my dataView:
dataView_player = Ext.create('Ext.Panel', {
id: 'images-view',
frame: true,
collapsible: false,
autoWidth: true,
title: 'Giocatori (0 items selected)',
items: [ Ext.create('Ext.view.View', {
id:'players-view',
store: store_player,
multiSelect: true,
height: 310,
trackOver: true,
overItemCls: 'x-item-over',
itemSelector: 'div.thumb-wrap',
emptyText: 'Nessun giocatore visualizzato',
tpl: [
'<tpl for=".">',
'<div class="thumb-wrap" id="{id}-{name}">',
'<div class="thumb">',
'<img src="/img/players/{picture}" title="{name} {surname}" alt="{name} {surname}" style="">',
'</div>',
'<span class="" style="height:30px;">{general_description}{name} {surname}</span>',
'</div>',
'</tpl>',
'<div class="x-clear"></div>'
],
plugins: [
Ext.create('Ext.ux.DataView.DragSelector', {}),
Ext.create('Ext.ux.DataView.LabelEditor', {dataIndex: 'name'})
],
prepareData: function(data) {
Ext.apply(data, {
name: data.name,
surname: data.surname,
general_description: Ext.util.Format.ellipsis(data.general_description, 15)
});
return data;
},
listeners: {
'selectionchange': function(record, item, index, e) {
var node = this.getNode(record); //this.getNode(record);
console.log(node.get('id'));
}
}
}) ],
dockedItems: [{
xtype: 'toolbar',
items: [{
iconCls: 'delete',
text: 'Cancella Selezionati',
scale: 'medium',
tooltip: 'Per <b>cancellare</b> i giocatori selezionati',
tooltipType: 'qtip',
id: 'delete-player',
disabled: true,
handler: delete_news
}, '-', {
iconCls: 'edit',
text: 'Aggiorna Selezionata',
scale: 'medium',
tooltip: 'Per <b>aggiornare</b> un giocatore selezionato',
tooltipType: 'qtip',
disabled: false,
id: 'update-player',
handler: function(nodes) {
var l = nodes.get('id');
console.log(l);
}
}
}
]
}]
});
Of course, this is a wrong example (because the listeners don't work) but it's just to make an idea.
There are two main things what i would like to do:
1) Catch the id (and other store's fields) of the selected item on the action "selectionchange". Obviously, now it doesn't work because of this: node.get('id'). Of course it's a wrong syntax but make up the idea of my will.
2) Catch the id of the selected item on the handler event of the "update-player" button. As above, the issue is the nodes.get('id'). Further trouble, is how to pass the selected item's features. in handler: function(nodes) { the nodes variable does not assume any value and i don't know how to pass the params from the dataview to the handler function.
I hope that somebody will able to help me.
According to the docs the selectionchange event provides the selection model as well as the array of selected records, so you are probably assuming the wrong parameters in your listener.
Without doing further testing, I think it should be something like this:
listeners: {
'selectionchange': function(selModel, selection, eOpts) {
var node = selection[0];
console.log(node.get('id'));
}
}
Note that you're using multiSelect: true, so it could be more than one record in the selection array.
Answer for second part of the question:
In button handler, you need to get selection model of the view and from it get information about selected records:
handler: function(nodes) {
// find view component
var view = dataView_player.down('dataview');
// get all selected records
var records = view.getSelectionModel().getSelection();
// process selected records
for(var i = 0; i < records.length; i++) {
console.log(records[i].getId());
}
}

Resources