I'm working with Qweb and Odoo 8, I've created my report in RH module, the problem is how can i print this report if condition
in hr_contract I've added a one2many fields
_columns = {
'contract_job_ids': fields.one2many(
'hr.contract.job',
'contract_id',
'Jobs',
),
I want print this report just if the len(object.contract_job_ids) >= 2
If you want to run specific code before your report prints (or not) you can create an Abstract model which defines a render_html function so that your function will run when printing the report rather than the generic odoo function. This is referenced in the documentation
HERE
Take a look at this example.
from openerp import models, fields, api, exceptions
class YourReport(models.AbstractModel):
_name = 'report.your_addon.report_template_id'
#api.multi
def render_html(self, data=None):
report_obj = self.env['report']
report = report_obj._get_report_from_name('your_addon.report_template_id')
docs = self.env['your_addon.your_model'].browse(self._ids)
for doc in docs:
if not len(doc.object.contract_job_ids) >= 2:
raise exceptions.ValidationError("You cant run this report\nYou need more contracts!")
docargs = {
'doc_model': report.model,
'docs': docs,
}
return report_obj.render('your_addon.report_template_id', docargs)
Related
I am using django_filter package for custom filtering in my django rest framework API,
Below is the given code
import django_filters
from src.core.models.rough_management import DocumentDetails
class DocumentDetailsFilter(django_filters.FilterSet):
my_date = django_filters.DateFromToRangeFilter()
class Meta:
model = DocumentDetails
fields = ['my_date']
Here i am getting "Enter a valid date" as an exception message when i enter invalid date range so my question is how to pass custom exception message to "No records found"?
Django filters use native django form fields to validate data.
DateFromToRangeFilter use django.forms.fields.DateField.
(Django DateField code)
You need to implement your own custom filter with little changes.
Not that from my perspective it's bad practice because that code interferes DateField error which should return default error as it is "Enter a valid date" and it's good information from api if you enter invalid date value like text "abcd".
Anyway for now i don't see better place to implement that and that is simpler than interference in FilterSet class.
You can also look on original code here to see how it's designed.
class CustomDateField(django.forms.fields.DateField):
default_error_messages = {
"invalid": "No records found",
}
class CustomDateRangeField(RangeField):
widget = DateRangeWidget
def __init__(self, *args, **kwargs):
fields = (CustomDateField(), CustomDateField()) # Important change here only
super().__init__(fields, *args, **kwargs)
def compress(self, data_list):
# Compress method stay the same as it is in original code
if data_list:
start_date, stop_date = data_list
if start_date:
start_date = handle_timezone(
datetime.combine(start_date, time.min), False
)
if stop_date:
stop_date = handle_timezone(
datetime.combine(stop_date, time.max), False
)
return slice(start_date, stop_date)
return None
class CustomDateFromToRangeFilter(RangeFilter):
field_class = CustomDateRangeField
I am trying to create a table that lists car information.
Here is my report data = [{carId:1,carName:"Honda",modelName:"Pilot",year="2020"},{carId:1,carName:"Honda",modelName:"Accord",year="2020"},{carId:1,carName:"Honda",modelName:"CRV",year="2020"}];
My report columns look like =
const [columns, setColumns] = React.useState([{ dataField: 'carName',text: 'Car'},{dataField: 'year',text: 'Year'}]);
How do I conditionally, using BootstrapTable, print the Car Name vs Car Model in the same column?
Thank you
What I did was to add formatter in the column and return cell (which is carName) when the condition is met, else return row.modelName
I have two tables in the analysis. I am using the script below to be able to filter table A and pass those filter selections to the matching filter in table B. Table A and B are visualized in a bar chart. I am triggering the code when the value of a document property changes, following instructions here.
I am running into two problems.
1) After the script runs, clicking Reset All Filters results in only table A being displayed in the visualization. Clicking Reset All Filters again fixes the issue.
2)When I add a second filter (commented out in the code below), making a selection in the Type_A or or Type_B filter wipes out the type B data from the visualization. I think the problem is in how IncludeAllValues is being handled, but I don't know how to fix it. Any help will be appreciated.
from Spotfire.Dxp.Application.Filters import *
from Spotfire.Dxp.Application.Visuals import VisualContent
from System import Guid
#Get the active page and filterPanel
page = Application.Document.ActivePageReference
filterPanel = page.FilterPanel
theFilterA = filterPanel.TableGroups[0].GetFilter("Type_A")
lbFilterA = theFilterA.FilterReference.As[ListBoxFilter]()
theFilter2A = filterPanel.TableGroups[1].GetFilter("Type_A")
lb2FilterA = theFilter2A.FilterReference.As[ListBoxFilter]()
lb2FilterA.IncludeAllValues = False
lb2FilterA.SetSelection(lbFilterA.SelectedValues)
#########################Type_B###########################
# theFilterB = filterPanel.TableGroups[0].GetFilter("Type_B")
# lbFilterB = theFilterB.FilterReference.As[ListBoxFilter]()
# theFilter2B = filterPanel.TableGroups[1].GetFilter("Type_B")
# lb2FilterB = theFilter2B.FilterReference.As[ListBoxFilter]()
# lb2FilterB.IncludeAllValues = False
# lb2FilterB.SetSelection(lbFilterB.SelectedValues)
I am using gdal-ruby to parse ESRI ShapeFiles like in this demo. I want to iterate through all features in order to push the field values into a database. However, I cannot find out how to retrieve the name of each field which I need to match the database column. By now I can only work with the field index of the field such as:
dataset = Gdal::Ogr.open(filename)
number_of_layers = dataset.get_layer_count
number_of_layers.times do |layer_index|
layer = dataset.get_layer(layer_index)
layer.get_feature_count.times do |feature_index|
feature = layer.get_feature(feature_index)
feature.get_field_count.times do |field_index|
field_value = feature.get_field(field_index)
# How can I find out the name of the field?
puts "Value = #{field_value} for unknown field name"
end
end
end
I checked the available methods with irb and looked into the API documentation. It seems as if I am searching for the wrong terms.
Looking at the OGR API itself, I think you need to go via feature.GetDefnRef, to get the feature definition, then .GetFieldDefn for the relevant field, and finally .GetNameRef...?
...
feature.get_field_count.times do |field_index|
defn_ref = feature.get_defn_ref
field_defn = defn_ref.get_field_defn(field_index)
field_name = field_defn.get_name
field_value = feature.get_field(field_index)
puts "Value = #{field_value} for field named #{field_name}"
end
...
ds = ogr.Open(filename, 1)
layer = ds.GetLayer()
for i in range(len(layer.schema)):
print(layer.schema[i].name)
Is there a way of returning items from a database in django-nonrel, using 'order_by' on a foreignkey?
Full details are as follows:
#Models.py
class Post(models.Model):
article = models.TextField(help_text='Paste or type HTML in here')
pub_date = models.DateField()
....
class TagItems(models.Model):
title = models.CharField(max_length=200)
....
class TagRel(models.Model):
the_post = models.ForeignKey('Post')
the_tag = models.ForeignKey('Tag')
TagRel defines a ManytoMany relationship between Post and TagItems classes.
I am wanting to get a list of articles for each tag.
#Desire output
My tag
-my first post
-my second post
My second tag
- my other post
- another post
All is good so far, as I use the following to filter the data:
def tagged_posts():
tag_items = TagItems.objects.all()
li =[]
for item in tag_items:
tag_rel_item = TagRel.objects.filter(the_tag__pk = item.pk)
li.append(tag_rel_item)
return {'list_of_objects': li}
I am using db-indexer to define the filter part of the query in db-indexes.py. All this works fine but I want to order my posts by publication dates.
Django docs tell me to use:
TagRel.objects.filter(the_tag__pk = item.pk).order_by('the_tag__pub_date')
But the order_by('the_tag__pub_date') part does not appear to be supported by django-nonrel.
The following also works in normal Django:
TagRel.objects.filter(the_tag__pk = item.pk).order_by('the_post')
This works because the Posts are already sorted by date in the model.
But this also does not appear to work in django-nonrel.
So my question is how do I return my posts ordered by date (latest>oldest)?
Thanks in advance
I'm taking a guess at this - you're using a ManyToManyField. I believe that's implemented using a ListProperty on App Engine's datastore.
See the section in the datastore documentation labeled "Properties With Multiple Values Can Have Surprising Behaviors":
http://code.google.com/appengine/docs/python/datastore/queries.html
That's most likely why your results appear unsorted. ManyToMany relations aren't supported natively in GAE. You'd probably have to sort them yourself after you get the results back.